Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(misc): align nx init package.json scripts handling when deselecting all plugins #22490

Merged
merged 1 commit into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
@@ -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
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
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