From aaf96f14f9a1ede5b92af1d0ba7f58d9e59742c9 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 30 Aug 2023 13:07:43 -0400 Subject: [PATCH] feat(core): ensure @nx/js plugin is installed for all JS workspaces Deprecates NPM and Core presets and show user a warning. --- .../bin/create-nx-workspace.ts | 34 +++++++++++++++++-- .../preset/point-to-tutorial-and-course.ts | 2 -- .../src/generators/new/generate-preset.ts | 10 ++---- .../new/generate-workspace-files.spec.ts | 8 ++--- .../new/generate-workspace-files.ts | 18 +++------- .../workspace/src/generators/new/new.spec.ts | 2 +- packages/workspace/src/generators/new/new.ts | 8 +---- .../workspace/src/generators/preset/preset.ts | 2 +- .../workspace/src/generators/utils/presets.ts | 9 +++-- 9 files changed, 52 insertions(+), 41 deletions(-) diff --git a/packages/create-nx-workspace/bin/create-nx-workspace.ts b/packages/create-nx-workspace/bin/create-nx-workspace.ts index 5a30500a191ebf..025aca3f012061 100644 --- a/packages/create-nx-workspace/bin/create-nx-workspace.ts +++ b/packages/create-nx-workspace/bin/create-nx-workspace.ts @@ -171,7 +171,14 @@ export const commandsObject: yargs.Argv = yargs throw error; }); }, - [normalizeArgsMiddleware as yargs.MiddlewareFunction<{}>] + [ + normalizeArgsMiddleware, + normalizeAndWarnOnDeprecatedPreset({ + // TODO(v18): Remove Empty and Core presets + [Preset.Core]: Preset.NPM, + [Preset.Empty]: Preset.Apps, + }), + ] as yargs.MiddlewareFunction<{}>[] ) .help('help', chalk.dim`Show help`) .updateLocale(yargsDecorator) @@ -217,6 +224,28 @@ async function main(parsedArgs: yargs.Arguments) { } } +function normalizeAndWarnOnDeprecatedPreset( + deprecatedPresets: Partial> +): (argv: yargs.Arguments) => Promise { + return async (args: yargs.Arguments): Promise => { + if (!args.preset) return; + if (deprecatedPresets[args.preset]) { + args.preset = deprecatedPresets[args.preset] as Preset; + output.addVerticalSeparator(); + output.note({ + title: `The "${args.preset}" preset is deprecated.`, + bodyLines: [ + `The "${ + args.preset + }" preset will be removed in a future Nx release. Use the "${ + deprecatedPresets[args.preset] + }" preset instead.`, + ], + }); + } + }; +} + /** * This function is used to normalize the arguments passed to the command. * It would: @@ -336,8 +365,6 @@ async function determineStack( case Preset.NodeStandalone: case Preset.Express: return 'node'; - case Preset.Core: - case Preset.Empty: case Preset.Apps: case Preset.NPM: case Preset.TS: @@ -782,6 +809,7 @@ async function determinePackageBasedOrIntegratedOrStandalone(): Promise< return workspaceType; } + async function determineStandaloneOrMonorepo(): Promise< 'integrated' | 'standalone' > { diff --git a/packages/create-nx-workspace/src/utils/preset/point-to-tutorial-and-course.ts b/packages/create-nx-workspace/src/utils/preset/point-to-tutorial-and-course.ts index f5e39a7890368c..a5942728c34eff 100644 --- a/packages/create-nx-workspace/src/utils/preset/point-to-tutorial-and-course.ts +++ b/packages/create-nx-workspace/src/utils/preset/point-to-tutorial-and-course.ts @@ -4,10 +4,8 @@ import { Preset } from './preset'; export function pointToTutorialAndCourse(preset: Preset) { const title = `First time using Nx? Check out this interactive Nx tutorial.`; switch (preset) { - case Preset.Empty: case Preset.NPM: case Preset.Apps: - case Preset.Core: output.addVerticalSeparator(); output.note({ title, diff --git a/packages/workspace/src/generators/new/generate-preset.ts b/packages/workspace/src/generators/new/generate-preset.ts index 75e976f9d2eee6..578655c195376f 100644 --- a/packages/workspace/src/generators/new/generate-preset.ts +++ b/packages/workspace/src/generators/new/generate-preset.ts @@ -16,14 +16,6 @@ import * as yargsParser from 'yargs-parser'; import { spawn, SpawnOptions } from 'child_process'; export function addPresetDependencies(host: Tree, options: NormalizedSchema) { - if ( - options.preset === Preset.Apps || - options.preset === Preset.Core || - options.preset === Preset.Empty || - options.preset === Preset.NPM - ) { - return; - } const { dependencies, dev } = getPresetDependencies(options); return addDependenciesToPackageJson( host, @@ -98,6 +90,8 @@ function getPresetDependencies({ e2eTestRunner, }: NormalizedSchema) { switch (preset) { + case Preset.Apps: + case Preset.NPM: case Preset.TS: case Preset.TsStandalone: return { dependencies: {}, dev: { '@nx/js': nxVersion } }; diff --git a/packages/workspace/src/generators/new/generate-workspace-files.spec.ts b/packages/workspace/src/generators/new/generate-workspace-files.spec.ts index 33ce79fc8f982b..dadd540da8fc18 100644 --- a/packages/workspace/src/generators/new/generate-workspace-files.spec.ts +++ b/packages/workspace/src/generators/new/generate-workspace-files.spec.ts @@ -19,7 +19,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => { await generateWorkspaceFiles(tree, { name: 'proj', directory: 'proj', - preset: Preset.Empty, + preset: Preset.Apps, defaultBase: 'main', isCustomPreset: false, }); @@ -80,7 +80,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => { await generateWorkspaceFiles(tree, { name: 'proj', directory: 'proj', - preset: Preset.Empty, + preset: Preset.Apps, defaultBase: 'main', isCustomPreset: false, }); @@ -168,7 +168,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => { await generateWorkspaceFiles(tree, { name: 'proj', directory: 'proj', - preset: Preset.Empty, + preset: Preset.Apps, defaultBase: 'main', isCustomPreset: false, }); @@ -184,7 +184,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => { await generateWorkspaceFiles(tree, { name: 'proj', directory: 'proj', - preset: Preset.Empty, + preset: Preset.Apps, defaultBase: 'main', isCustomPreset: false, }); diff --git a/packages/workspace/src/generators/new/generate-workspace-files.ts b/packages/workspace/src/generators/new/generate-workspace-files.ts index 34dcc820a1ee94..28a2e28b2dc5cc 100644 --- a/packages/workspace/src/generators/new/generate-workspace-files.ts +++ b/packages/workspace/src/generators/new/generate-workspace-files.ts @@ -53,7 +53,7 @@ export async function generateWorkspaceFiles( function setPresetProperty(tree: Tree, options: NormalizedSchema) { updateJson(tree, join(options.directory, 'nx.json'), (json) => { - if (options.preset === Preset.Core || options.preset === Preset.NPM) { + if (options.preset === Preset.NPM) { addPropertyWithStableKeys(json, 'extends', 'nx/presets/npm.json'); delete json.implicitDependencies; delete json.targetDefaults; @@ -64,11 +64,7 @@ function setPresetProperty(tree: Tree, options: NormalizedSchema) { } function createAppsAndLibsFolders(tree: Tree, options: NormalizedSchema) { - if ( - options.preset === Preset.Core || - options.preset === Preset.TS || - options.preset === Preset.NPM - ) { + if (options.preset === Preset.TS || options.preset === Preset.NPM) { tree.write(join(options.directory, 'packages/.gitkeep'), ''); } else if ( options.preset === Preset.AngularStandalone || @@ -113,11 +109,7 @@ function createNxJson( if (defaultBase === 'main') { delete nxJson.affected; } - if ( - preset !== Preset.Core && - preset !== Preset.NPM && - preset !== Preset.Empty - ) { + if (preset !== Preset.NPM) { nxJson.namedInputs = { default: ['{projectRoot}/**/*', 'sharedGlobals'], production: ['default'], @@ -138,7 +130,7 @@ function createFiles(tree: Tree, options: NormalizedSchema) { options.preset === Preset.NextJsStandalone || options.preset === Preset.TsStandalone ? './files-root-app' - : options.preset === Preset.NPM || options.preset === Preset.Core + : options.preset === Preset.NPM ? './files-package-based-repo' : './files-integrated-repo'; generateFiles(tree, join(__dirname, filesDirName), options.directory, { @@ -233,7 +225,7 @@ function normalizeOptions(options: NormalizedSchema) { } function setUpWorkspacesInPackageJson(tree: Tree, options: NormalizedSchema) { - if (options.preset === Preset.NPM || options.preset === Preset.Core) { + if (options.preset === Preset.NPM) { if (options.packageManager === 'pnpm') { tree.write( join(options.directory, 'pnpm-workspace.yaml'), diff --git a/packages/workspace/src/generators/new/new.spec.ts b/packages/workspace/src/generators/new/new.spec.ts index 80933dac107c6b..8a1287f466b33f 100644 --- a/packages/workspace/src/generators/new/new.spec.ts +++ b/packages/workspace/src/generators/new/new.spec.ts @@ -53,7 +53,7 @@ describe('new', () => { name: 'my-workspace', directory: 'my-workspace', appName: 'app', - preset: Preset.Empty, + preset: Preset.Apps, }); expect(readJson(tree, 'my-workspace/package.json')).toMatchSnapshot(); diff --git a/packages/workspace/src/generators/new/new.ts b/packages/workspace/src/generators/new/new.ts index c1cad2e2361aa3..bc78ae98e32776 100644 --- a/packages/workspace/src/generators/new/new.ts +++ b/packages/workspace/src/generators/new/new.ts @@ -60,11 +60,7 @@ export async function newGenerator(host: Tree, opts: Schema) { } installPackagesTask(host, false, options.directory, options.packageManager); // TODO: move all of these into create-nx-workspace - if ( - options.preset !== Preset.NPM && - options.preset !== Preset.Core && - !options.isCustomPreset - ) { + if (options.preset !== Preset.NPM && !options.isCustomPreset) { await generatePreset(host, options); } }; @@ -76,9 +72,7 @@ function validateOptions(options: Schema, host: Tree) { if ( options.skipInstall && options.preset !== Preset.Apps && - options.preset !== Preset.Core && options.preset !== Preset.TS && - options.preset !== Preset.Empty && options.preset !== Preset.NPM ) { throw new Error(`Cannot select a preset when skipInstall is set to true.`); diff --git a/packages/workspace/src/generators/preset/preset.ts b/packages/workspace/src/generators/preset/preset.ts index 7ee77b7e0b6450..025873d2a09c5f 100644 --- a/packages/workspace/src/generators/preset/preset.ts +++ b/packages/workspace/src/generators/preset/preset.ts @@ -20,7 +20,7 @@ export async function presetGenerator(tree: Tree, options: Schema) { export default presetGenerator; async function createPreset(tree: Tree, options: Schema) { - if (options.preset === Preset.Empty || options.preset === Preset.Apps) { + if (options.preset === Preset.Apps) { return; } else if (options.preset === Preset.AngularMonorepo) { const { diff --git a/packages/workspace/src/generators/utils/presets.ts b/packages/workspace/src/generators/utils/presets.ts index 2c200d1114dbe6..258379c73e59c3 100644 --- a/packages/workspace/src/generators/utils/presets.ts +++ b/packages/workspace/src/generators/utils/presets.ts @@ -1,7 +1,12 @@ export enum Preset { Apps = 'apps', - Empty = 'empty', // same as apps, deprecated - Core = 'core', // same as npm, deprecated + // TODO(v18): Remove Empty and Core presets + /** @deprecated Use Apps instead + */ + Empty = 'empty', + /** @deprecated Use NPM instead + */ + Core = 'core', NPM = 'npm', TS = 'ts', WebComponents = 'web-components',