fix(nx-plugin): create-nx-plugin should generate valid import path (#10107)

This commit is contained in:
Craigory Coppola 2022-05-03 10:42:23 -04:00 committed by GitHub
parent cfe21b2eb8
commit 62d0535acd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 124 additions and 30 deletions

View File

@ -178,6 +178,47 @@ export function runCreateWorkspace(
return create ? create.toString() : '';
}
export function runCreatePlugin(
name: string,
{
pluginName,
packageManager,
extraArgs,
useDetectedPm = false,
}: {
pluginName?: string;
packageManager?: 'npm' | 'yarn' | 'pnpm';
extraArgs?: string;
useDetectedPm?: boolean;
}
) {
projName = name;
const pm = getPackageManagerCommand({ packageManager });
let command = `${pm.runUninstalledPackage} create-nx-plugin ${name}`;
if (pluginName) {
command += ` --pluginName=${pluginName}`;
}
if (packageManager && !useDetectedPm) {
command += ` --package-manager=${packageManager}`;
}
if (extraArgs) {
command += ` ${extraArgs}`;
}
const create = execSync(command, {
cwd: e2eCwd,
stdio: [0, 1, 2],
env: process.env,
encoding: 'utf-8',
});
return create ? create.toString() : '';
}
export function packageInstall(
pkg: string,
projName?: string,

View File

@ -0,0 +1,30 @@
import {
checkFilesExist,
getSelectedPackageManager,
packageManagerLockFile,
runCLI,
uniq,
runCreatePlugin,
} from '@nrwl/e2e/utils';
describe('create-nx-plugin', () => {
const packageManager = getSelectedPackageManager() || 'pnpm';
it('should be able to create a plugin repo and run plugin e2e', () => {
const wsName = uniq('ws-plugin');
const pluginName = uniq('plugin');
runCreatePlugin(wsName, {
packageManager,
pluginName,
});
checkFilesExist(
'workspace.json',
'package.json',
packageManagerLockFile[packageManager],
`packages/${pluginName}/package.json`
);
expect(() => runCLI(`e2e ${pluginName}-e2e`)).not.toThrow();
});
});

View File

@ -98,7 +98,7 @@ function createNxPlugin(
packageManager,
parsedArgs: any
) {
const importPath = parsedArgs.importPath ?? `${workspaceName}/${pluginName}`;
const importPath = parsedArgs.importPath ?? `@${workspaceName}/${pluginName}`;
const command = `nx generate @nrwl/nx-plugin:plugin ${pluginName} --importPath=${importPath}`;
console.log(command);

View File

@ -0,0 +1,5 @@
export * from './src/generators/e2e-project/e2e';
export * from './src/generators/executor/executor';
export * from './src/generators/generator/generator';
export * from './src/generators/plugin/plugin';
export * from './src/generators/migration/migration';

View File

@ -5,36 +5,54 @@ import {
runNxCommandAsync,
uniq,
} from '@nrwl/nx-plugin/testing';
describe('<%= pluginName %> e2e', () => {
it('should create <%= pluginName %>', async () => {
const plugin = uniq('<%= pluginName %>');
ensureNxProject('<%= npmPackageName %>', '<%= pluginOutputPath %>');
await runNxCommandAsync(`generate <%=npmPackageName%>:<%= pluginName %> ${plugin}`);
// Setting up individual workspaces per
// test can cause e2e runs to take a long time.
// For this reason, we recommend each suite only
// consumes 1 workspace. The tests should each operate
// on a unique project in the workspace, such that they
// are not dependant on one another.
beforeAll(() => {
ensureNxProject('<%= npmPackageName %>', '<%= pluginOutputPath %>');
});
const result = await runNxCommandAsync(`build ${plugin}`);
expect(result.stdout).toContain('Executor ran');
}, 120000)
afterAll(() => {
// `nx reset` kills the daemon, and performs
// some work which can help clean up e2e leftovers
runNxCommandAsync('reset');
});
describe('--directory', () => {
it('should create src in the specified directory', async () => {
const plugin = uniq('<%= pluginName %>');
ensureNxProject('<%= npmPackageName %>', '<%= pluginOutputPath %>');
await runNxCommandAsync(
`generate <%=npmPackageName%>:<%= pluginName %> ${plugin} --directory subdir`
);
expect(() => checkFilesExist(`libs/subdir/${plugin}/src/index.ts`)).not.toThrow();
}, 120000);
});
it('should create <%= pluginName %>', async () => {
const project = uniq('<%= pluginName %>');
await runNxCommandAsync(
`generate <%=npmPackageName%>:<%= pluginName %> ${project}`
);
const result = await runNxCommandAsync(`build ${project}`);
expect(result.stdout).toContain('Executor ran');
}, 120000);
describe('--tags', () => {
it('should add tags to the project', async () => {
const plugin = uniq('<%= pluginName %>');
ensureNxProject('<%= npmPackageName %>', '<%= pluginOutputPath %>');
await runNxCommandAsync(
`generate <%=npmPackageName%>:<%= pluginName %> ${plugin} --tags e2etag,e2ePackage`
);
const project = readJson(`libs/${plugin}/project.json`);
expect(project.tags).toEqual(['e2etag', 'e2ePackage']);
}, 120000);
});
})
describe('--directory', () => {
it('should create src in the specified directory', async () => {
const project = uniq('<%= pluginName %>');
await runNxCommandAsync(
`generate <%=npmPackageName%>:<%= pluginName %> ${project} --directory subdir`
);
expect(() =>
checkFilesExist(`libs/subdir/${project}/src/index.ts`)
).not.toThrow();
}, 120000);
});
describe('--tags', () => {
it('should add tags to the project', async () => {
const projectName = uniq('<%= pluginName %>');
ensureNxProject('<%= npmPackageName %>', '<%= pluginOutputPath %>');
await runNxCommandAsync(
`generate <%=npmPackageName%>:<%= pluginName %> ${projectName} --tags e2etag,e2ePackage`
);
const project = readJson(`libs/${projectName}/project.json`);
expect(project.tags).toEqual(['e2etag', 'e2ePackage']);
}, 120000);
});
});