Skip to content

Commit

Permalink
feat(core): ensure @nx/js plugin is installed for all JS workspaces (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored Aug 30, 2023
1 parent 99c44f9 commit c9ea9e0
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 43 deletions.
2 changes: 1 addition & 1 deletion e2e/nx-misc/src/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ describe('Workspace Tests', () => {

expect(error).toBeDefined();
expect(error.stdout.toString()).toContain(
`${lib1} is still depended on by the following projects`
`${lib1} is still a dependency of the following projects`
);
expect(error.stdout.toString()).toContain(lib2);

Expand Down
2 changes: 1 addition & 1 deletion e2e/utils/create-project-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function newProject({

if (!directoryExists(tmpBackupProjPath())) {
runCreateWorkspace(projScope, {
preset: 'empty',
preset: 'apps',
packageManager,
});

Expand Down
34 changes: 31 additions & 3 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ export const commandsObject: yargs.Argv<Arguments> = 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)
Expand Down Expand Up @@ -217,6 +224,28 @@ async function main(parsedArgs: yargs.Arguments<Arguments>) {
}
}

function normalizeAndWarnOnDeprecatedPreset(
deprecatedPresets: Partial<Record<Preset, Preset>>
): (argv: yargs.Arguments<Arguments>) => Promise<void> {
return async (args: yargs.Arguments<Arguments>): Promise<void> => {
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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -782,6 +809,7 @@ async function determinePackageBasedOrIntegratedOrStandalone(): Promise<

return workspaceType;
}

async function determineStandaloneOrMonorepo(): Promise<
'integrated' | 'standalone'
> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`new --preset should generate necessary npm dependencies for empty prese
{
"dependencies": {},
"devDependencies": {
"@nx/js": "0.0.1",
"@nx/workspace": "0.0.1",
"nx": "0.0.1",
},
Expand Down
10 changes: 2 additions & 8 deletions packages/workspace/src/generators/new/generate-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 } };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -80,19 +80,33 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
await generateWorkspaceFiles(tree, {
name: 'proj',
directory: 'proj',
preset: Preset.Empty,
preset: Preset.Apps,
defaultBase: 'main',
isCustomPreset: false,
});
const nxJson = readJson<NxJsonConfiguration>(tree, '/proj/nx.json');
expect(nxJson).toMatchInlineSnapshot(`
{
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"namedInputs": {
"default": [
"{projectRoot}/**/*",
"sharedGlobals",
],
"production": [
"default",
],
"sharedGlobals": [],
},
"targetDefaults": {
"build": {
"dependsOn": [
"^build",
],
"inputs": [
"production",
"^production",
],
},
},
"tasksRunnerOptions": {
Expand Down Expand Up @@ -168,7 +182,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
await generateWorkspaceFiles(tree, {
name: 'proj',
directory: 'proj',
preset: Preset.Empty,
preset: Preset.Apps,
defaultBase: 'main',
isCustomPreset: false,
});
Expand All @@ -184,7 +198,7 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
await generateWorkspaceFiles(tree, {
name: 'proj',
directory: 'proj',
preset: Preset.Empty,
preset: Preset.Apps,
defaultBase: 'main',
isCustomPreset: false,
});
Expand Down
18 changes: 5 additions & 13 deletions packages/workspace/src/generators/new/generate-workspace-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ||
Expand Down Expand Up @@ -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'],
Expand All @@ -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, {
Expand Down Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/src/generators/new/new.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 1 addition & 7 deletions packages/workspace/src/generators/new/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand All @@ -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.`);
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/src/generators/preset/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions packages/workspace/src/generators/utils/presets.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down

0 comments on commit c9ea9e0

Please sign in to comment.