Skip to content

Commit

Permalink
fix(misc): nx init should add .nx/cache to gitignore (#19961)
Browse files Browse the repository at this point in the history
(cherry picked from commit 18f71d2)
  • Loading branch information
AgentEnder authored and FrozenPandaz committed Nov 1, 2023
1 parent 713bdf1 commit 5d32213
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
initCloud,
printFinalMessage,
runInstall,
updateGitIgnore,
} from './utils';

type Options = Pick<InitArgs, 'nxCloud' | 'interactive' | 'cacheable'>;
Expand Down Expand Up @@ -88,6 +89,7 @@ export async function addNxToMonorepo(options: Options) {
scriptOutputs
);

updateGitIgnore(repoRoot);
addDepsToPackageJson(repoRoot);

output.log({ title: '📦 Installing dependencies' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
markRootPackageJsonAsNxProject,
printFinalMessage,
runInstall,
updateGitIgnore,
} from './utils';
import { nxVersion } from '../../../utils/versions';

Expand Down Expand Up @@ -117,6 +118,7 @@ export async function addNxToNest(options: Options, packageJson: PackageJson) {

const pmc = getPackageManagerCommand();

updateGitIgnore(repoRoot);
addDepsToPackageJson(repoRoot);
addNestPluginToPackageJson(repoRoot);
markRootPackageJsonAsNxProject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
markRootPackageJsonAsNxProject,
printFinalMessage,
runInstall,
updateGitIgnore,
} from './utils';

type Options = Pick<InitArgs, 'nxCloud' | 'interactive' | 'cacheable'>;
Expand Down Expand Up @@ -72,6 +73,7 @@ export async function addNxToNpmRepo(options: Options) {

const pmc = getPackageManagerCommand();

updateGitIgnore(repoRoot);
addDepsToPackageJson(repoRoot);
markRootPackageJsonAsNxProject(
repoRoot,
Expand Down
12 changes: 12 additions & 0 deletions packages/nx/src/command-line/init/implementation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../../../utils/package-manager';
import { joinPathFragments } from '../../../utils/path';
import { nxVersion } from '../../../utils/versions';
import { readFileSync, writeFileSync } from 'fs';

export async function askAboutNxCloud(): Promise<boolean> {
return await enquirer
Expand Down Expand Up @@ -121,6 +122,17 @@ export function addDepsToPackageJson(repoRoot: string) {
writeJsonFile(path, json);
}

export function updateGitIgnore(root: string) {
const ignorePath = join(root, '.gitignore');
try {
let contents = readFileSync(ignorePath, 'utf-8');
if (!contents.includes('.nx/cache')) {
contents = [contents, '', '.nx/cache'].join('\n');
writeFileSync(ignorePath, contents, 'utf-8');
}
} catch {}
}

export function runInstall(
repoRoot: string,
pmc: PackageManagerCommands = getPackageManagerCommand()
Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/command-line/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { execSync } from 'child_process';
import { prompt } from 'enquirer';
import { existsSync } from 'fs';
import { prerelease } from 'semver';
import * as parser from 'yargs-parser';
import { addNxToMonorepo } from './implementation/add-nx-to-monorepo';
import { addNxToNest } from './implementation/add-nx-to-nest';
import { addNxToNpmRepo } from './implementation/add-nx-to-npm-repo';
Expand Down

0 comments on commit 5d32213

Please sign in to comment.