feat(nx-plugin): create cli in packages folder (#18234)

This commit is contained in:
Emily Xiong 2023-07-21 10:20:04 -04:00 committed by GitHub
parent 717a8dd027
commit 1861c931d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 11 deletions

View File

@ -20,7 +20,7 @@ describe('create-nx-plugin', () => {
runCreatePlugin(pluginName, {
packageManager,
extraArgs: `--createPackageName='false'`,
extraArgs: `--createPackageName=false`,
});
checkFilesExist(
@ -62,14 +62,14 @@ describe('create-nx-plugin', () => {
runCLI(`build ${pluginName}`);
checkFilesExist(
`dist/${pluginName}/package.json`,
`dist/${pluginName}/generators.json`,
`e2e/tests/${pluginName}.spec.ts`
`dist/packages/${pluginName}/package.json`,
`dist/packages/${pluginName}/generators.json`,
`packages/${pluginName}-e2e/tests/${pluginName}.spec.ts`
);
runCLI(`build create-${pluginName}-package`);
checkFilesExist(`dist/create-${pluginName}-package/bin/index.js`);
checkFilesExist(`dist/packages/create-${pluginName}-package/bin/index.js`);
expect(() => runCLI(`e2e e2e`)).not.toThrow();
expect(() => runCLI(`e2e ${pluginName}-e2e`)).not.toThrow();
});
});

View File

@ -6,6 +6,7 @@ import {
formatFiles,
runTasksInSerial,
GeneratorCallback,
names,
} from '@nx/devkit';
import { Linter } from '@nx/linter';
import { PackageJson } from 'nx/src/utils/package-json';
@ -15,18 +16,30 @@ import createPackageGenerator from '../create-package/create-package';
export default async function (tree: Tree, options: PresetGeneratorSchema) {
const tasks: GeneratorCallback[] = [];
const pluginProjectName = names(
options.pluginName.includes('/')
? options.pluginName.split('/')[1]
: options.pluginName
).fileName;
options.createPackageName =
options.createPackageName === 'false' // for command line in e2e, it is passed as a string
? undefined
: options.createPackageName;
const pluginTask = await pluginGenerator(tree, {
compiler: 'tsc',
linter: Linter.EsLint,
name: options.pluginName.includes('/')
? options.pluginName.split('/')[1]
: options.pluginName,
name: pluginProjectName,
skipFormat: true,
unitTestRunner: 'jest',
importPath: options.pluginName,
rootProject: true,
e2eTestRunner: 'jest',
publishable: true,
// when creating a CLI package, the plugin will be in the packages folder
directory:
options.createPackageName && options.createPackageName !== 'false'
? 'packages'
: undefined,
rootProject: options.createPackageName ? false : true,
});
tasks.push(pluginTask);
@ -34,9 +47,11 @@ export default async function (tree: Tree, options: PresetGeneratorSchema) {
moveNxPluginToDevDeps(tree);
if (options.createPackageName) {
const e2eProject = `${options.pluginName}-e2e`;
const cliTask = await createPackageGenerator(tree, {
directory: 'packages',
name: options.createPackageName,
e2eProject: 'e2e',
e2eProject: e2eProject,
project: options.pluginName,
skipFormat: true,
unitTestRunner: 'jest',