From b3abd88257105c2483f022466bfdfadeba3a6dea Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Tue, 31 Oct 2023 15:59:11 -0400 Subject: [PATCH] fix(misc): nx init should add .nx/cache to gitignore --- .../init/implementation/add-nx-to-monorepo.ts | 2 ++ .../init/implementation/add-nx-to-nest.ts | 2 ++ .../init/implementation/add-nx-to-npm-repo.ts | 2 ++ .../nx/src/command-line/init/implementation/utils.ts | 12 ++++++++++++ packages/nx/src/command-line/init/init.ts | 1 - 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/nx/src/command-line/init/implementation/add-nx-to-monorepo.ts b/packages/nx/src/command-line/init/implementation/add-nx-to-monorepo.ts index b8653323780e3..6b74bf22a9aac 100644 --- a/packages/nx/src/command-line/init/implementation/add-nx-to-monorepo.ts +++ b/packages/nx/src/command-line/init/implementation/add-nx-to-monorepo.ts @@ -13,6 +13,7 @@ import { initCloud, printFinalMessage, runInstall, + updateGitIgnore, } from './utils'; type Options = Pick; @@ -88,6 +89,7 @@ export async function addNxToMonorepo(options: Options) { scriptOutputs ); + updateGitIgnore(repoRoot); addDepsToPackageJson(repoRoot); output.log({ title: '📦 Installing dependencies' }); diff --git a/packages/nx/src/command-line/init/implementation/add-nx-to-nest.ts b/packages/nx/src/command-line/init/implementation/add-nx-to-nest.ts index 041453c4c01ed..e618c47965f12 100644 --- a/packages/nx/src/command-line/init/implementation/add-nx-to-nest.ts +++ b/packages/nx/src/command-line/init/implementation/add-nx-to-nest.ts @@ -23,6 +23,7 @@ import { markRootPackageJsonAsNxProject, printFinalMessage, runInstall, + updateGitIgnore, } from './utils'; import { nxVersion } from '../../../utils/versions'; @@ -117,6 +118,7 @@ export async function addNxToNest(options: Options, packageJson: PackageJson) { const pmc = getPackageManagerCommand(); + updateGitIgnore(repoRoot); addDepsToPackageJson(repoRoot); addNestPluginToPackageJson(repoRoot); markRootPackageJsonAsNxProject( diff --git a/packages/nx/src/command-line/init/implementation/add-nx-to-npm-repo.ts b/packages/nx/src/command-line/init/implementation/add-nx-to-npm-repo.ts index a129d3070551c..09f7a6a085d8a 100644 --- a/packages/nx/src/command-line/init/implementation/add-nx-to-npm-repo.ts +++ b/packages/nx/src/command-line/init/implementation/add-nx-to-npm-repo.ts @@ -11,6 +11,7 @@ import { markRootPackageJsonAsNxProject, printFinalMessage, runInstall, + updateGitIgnore, } from './utils'; type Options = Pick; @@ -72,6 +73,7 @@ export async function addNxToNpmRepo(options: Options) { const pmc = getPackageManagerCommand(); + updateGitIgnore(repoRoot); addDepsToPackageJson(repoRoot); markRootPackageJsonAsNxProject( repoRoot, diff --git a/packages/nx/src/command-line/init/implementation/utils.ts b/packages/nx/src/command-line/init/implementation/utils.ts index 4502dfe30323e..5157212ab390b 100644 --- a/packages/nx/src/command-line/init/implementation/utils.ts +++ b/packages/nx/src/command-line/init/implementation/utils.ts @@ -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 { return await enquirer @@ -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() diff --git a/packages/nx/src/command-line/init/init.ts b/packages/nx/src/command-line/init/init.ts index 4dbbb3c3c8718..b14f2eed0125c 100644 --- a/packages/nx/src/command-line/init/init.ts +++ b/packages/nx/src/command-line/init/init.ts @@ -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';