-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): tweaks to nx init (nrwl#30002)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
- Loading branch information
1 parent
672318d
commit 8b1cd48
Showing
10 changed files
with
570 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/nx/src/command-line/init/implementation/add-nx-to-turborepo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { writeFileSync } from 'node:fs'; | ||
import { join } from 'node:path'; | ||
import { readJsonFile, writeJsonFile } from '../../../utils/fileutils'; | ||
import { output } from '../../../utils/output'; | ||
import { getPackageManagerCommand } from '../../../utils/package-manager'; | ||
import { InitArgs } from '../init-v1'; | ||
import { | ||
addDepsToPackageJson, | ||
createNxJsonFromTurboJson, | ||
runInstall, | ||
updateGitIgnore, | ||
} from './utils'; | ||
|
||
type Options = Pick<InitArgs, 'nxCloud' | 'interactive'>; | ||
|
||
export async function addNxToTurborepo(_options: Options) { | ||
const repoRoot = process.cwd(); | ||
|
||
output.log({ | ||
title: 'Initializing Nx based on your old Turborepo configuration', | ||
}); | ||
|
||
output.log({ | ||
title: '💡 Did you know?', | ||
bodyLines: [ | ||
'- Turborepo requires you to maintain all your common scripts like "build", "lint", "test" in all your packages, as well as their applicable cache inputs and outputs.', | ||
`- Nx is extensible and has plugins for the tools you use to infer all of this for you purely based on that tool's configuration file within your packages.`, | ||
'', | ||
' - E.g. the `@nx/vite` plugin will infer the "build" script based on the existence of a vite.config.js file.', | ||
' - Therefore with zero package level config, `nx build my-app` knows to run the `vite build` CLI directly, with all Nx cache inputs and outputs automatically inferred.', | ||
'', | ||
`NOTE: None of your existing package.json scripts will be modified as part of this initialization process, you can already use them as-is with Nx, but you can learn more about the benefits of Nx's inferred tasks at https://nx.dev/concepts/inferred-tasks`, | ||
], | ||
}); | ||
|
||
let nxJson = createNxJsonFromTurboJson(readJsonFile('turbo.json')); | ||
const nxJsonPath = join(repoRoot, 'nx.json'); | ||
|
||
// Turborepo workspaces usually have prettier installed, so try and match the formatting before writing the file | ||
try { | ||
const prettier = await import('prettier'); | ||
const config = await prettier.resolveConfig(repoRoot); | ||
writeFileSync( | ||
nxJsonPath, | ||
// @ts-ignore - Always await prettier.format, in modern versions it's async | ||
await prettier.format(JSON.stringify(nxJson, null, 2), { | ||
...(config ?? {}), | ||
parser: 'json', | ||
}) | ||
); | ||
} catch (err) { | ||
// Apply fallback JSON write | ||
writeJsonFile(nxJsonPath, nxJson); | ||
} | ||
|
||
const pmc = getPackageManagerCommand(); | ||
|
||
updateGitIgnore(repoRoot); | ||
addDepsToPackageJson(repoRoot); | ||
|
||
output.log({ title: '📦 Installing dependencies' }); | ||
|
||
runInstall(repoRoot, pmc); | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/nx/src/command-line/init/implementation/deduce-default-base.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { execSync } from 'node:child_process'; | ||
import { deduceDefaultBase as gitInitDefaultBase } from '../../../utils/default-base'; | ||
|
||
export function deduceDefaultBase() { | ||
try { | ||
execSync(`git rev-parse --verify main`, { | ||
stdio: ['ignore', 'ignore', 'ignore'], | ||
windowsHide: false, | ||
}); | ||
return 'main'; | ||
} catch { | ||
try { | ||
execSync(`git rev-parse --verify dev`, { | ||
stdio: ['ignore', 'ignore', 'ignore'], | ||
windowsHide: false, | ||
}); | ||
return 'dev'; | ||
} catch { | ||
try { | ||
execSync(`git rev-parse --verify develop`, { | ||
stdio: ['ignore', 'ignore', 'ignore'], | ||
windowsHide: false, | ||
}); | ||
return 'develop'; | ||
} catch { | ||
try { | ||
execSync(`git rev-parse --verify next`, { | ||
stdio: ['ignore', 'ignore', 'ignore'], | ||
windowsHide: false, | ||
}); | ||
return 'next'; | ||
} catch { | ||
try { | ||
execSync(`git rev-parse --verify master`, { | ||
stdio: ['ignore', 'ignore', 'ignore'], | ||
windowsHide: false, | ||
}); | ||
return 'master'; | ||
} catch { | ||
return gitInitDefaultBase(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.