Skip to content

Commit

Permalink
fix(misc): align nx init package.json scripts handling when deselecti…
Browse files Browse the repository at this point in the history
…ng all plugins (#22490)
  • Loading branch information
leosvelperez authored Mar 26, 2024
1 parent a1b1351 commit bc4339c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import {
addDepsToPackageJson,
createNxJsonFile,
initCloud,
markPackageJsonAsNxProject,
printFinalMessage,
runInstall,
updateGitIgnore,
} from './utils';
import { connectExistingRepoToNxCloudPrompt } from '../../connect/connect-to-nx-cloud';

type Options = Pick<InitArgs, 'nxCloud' | 'interactive' | 'cacheable'>;
type Options = Pick<InitArgs, 'nxCloud' | 'interactive' | 'cacheable'> & {
legacy?: boolean;
};

export async function addNxToMonorepo(options: Options) {
const repoRoot = process.cwd();
Expand Down Expand Up @@ -91,6 +94,14 @@ export async function addNxToMonorepo(options: Options) {
cacheableOperations,
scriptOutputs
);
if (!options.legacy) {
packageJsonFiles.forEach((packageJsonPath) => {
markPackageJsonAsNxProject(
join(repoRoot, packageJsonPath),
cacheableOperations
);
});
}

updateGitIgnore(repoRoot);
addDepsToPackageJson(repoRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
addDepsToPackageJson,
createNxJsonFile,
initCloud,
markRootPackageJsonAsNxProject,
markRootPackageJsonAsNxProjectLegacy,
printFinalMessage,
runInstall,
updateGitIgnore,
Expand Down Expand Up @@ -121,7 +121,7 @@ export async function addNxToNest(options: Options, packageJson: PackageJson) {
updateGitIgnore(repoRoot);
addDepsToPackageJson(repoRoot);
addNestPluginToPackageJson(repoRoot);
markRootPackageJsonAsNxProject(repoRoot, cacheableOperations, pmc);
markRootPackageJsonAsNxProjectLegacy(repoRoot, cacheableOperations, pmc);

createProjectJson(repoRoot, packageJson, nestCLIConfiguration);
removeFile(repoRoot, 'nest-cli.json');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as enquirer from 'enquirer';
import { join } from 'path';
import { InitArgs } from '../init-v1';
import { readJsonFile } from '../../../utils/fileutils';
import { output } from '../../../utils/output';
Expand All @@ -7,14 +8,17 @@ import {
addDepsToPackageJson,
createNxJsonFile,
initCloud,
markRootPackageJsonAsNxProject,
markPackageJsonAsNxProject,
markRootPackageJsonAsNxProjectLegacy,
printFinalMessage,
runInstall,
updateGitIgnore,
} from './utils';
import { connectExistingRepoToNxCloudPrompt } from '../../connect/connect-to-nx-cloud';

type Options = Pick<InitArgs, 'nxCloud' | 'interactive' | 'cacheable'>;
type Options = Pick<InitArgs, 'nxCloud' | 'interactive' | 'cacheable'> & {
legacy?: boolean;
};

export async function addNxToNpmRepo(options: Options) {
const repoRoot = process.cwd();
Expand Down Expand Up @@ -78,7 +82,14 @@ export async function addNxToNpmRepo(options: Options) {

updateGitIgnore(repoRoot);
addDepsToPackageJson(repoRoot);
markRootPackageJsonAsNxProject(repoRoot, cacheableOperations, pmc);
if (options.legacy) {
markRootPackageJsonAsNxProjectLegacy(repoRoot, cacheableOperations, pmc);
} else {
markPackageJsonAsNxProject(
join(repoRoot, 'package.json'),
cacheableOperations
);
}

output.log({ title: '📦 Installing dependencies' });

Expand Down
20 changes: 19 additions & 1 deletion packages/nx/src/command-line/init/implementation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export function addVsCodeRecommendedExtensions(
}
}

export function markRootPackageJsonAsNxProject(
export function markRootPackageJsonAsNxProjectLegacy(
repoRoot: string,
cacheableScripts: string[],
pmc: PackageManagerCommands
Expand All @@ -194,6 +194,24 @@ export function markRootPackageJsonAsNxProject(
writeJsonFile(`package.json`, json);
}

export function markPackageJsonAsNxProject(
packageJsonPath: string,
cacheableScripts: string[]
) {
const json = readJsonFile<PackageJson>(packageJsonPath);
if (!json.scripts) {
return;
}

json.nx = { includedScripts: [] };
for (let script of cacheableScripts) {
if (json.scripts[script]) {
json.nx.includedScripts.push(script);
}
}
writeJsonFile(packageJsonPath, json);
}

export function printFinalMessage({
learnMoreLink,
bodyLines,
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/init/init-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export async function initHandler(options: InitArgs) {
} else if (isNestCLI(packageJson)) {
await addNxToNest(options, packageJson);
} else if (isMonorepo(packageJson)) {
await addNxToMonorepo(options);
await addNxToMonorepo({ ...options, legacy: true });
} else {
await addNxToNpmRepo(options);
await addNxToNpmRepo({ ...options, legacy: true });
}
} else {
const useDotNxFolder = await prompt<{ useDotNxFolder: string }>([
Expand Down

0 comments on commit bc4339c

Please sign in to comment.