fix(core): fix no plugins found for nx init without packge.json (#22434)

This commit is contained in:
Emily Xiong 2024-03-22 12:52:36 -04:00 committed by GitHub
parent da343418aa
commit b7b70da6f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 14 deletions

View File

@ -63,7 +63,7 @@ async function installPackage(pkgName: string, version: string): Promise<void> {
writeJsonFile('nx.json', nxJson); writeJsonFile('nx.json', nxJson);
try { try {
await runNxAsync('--help'); await runNxAsync('--help', { silent: true });
} catch (e) { } catch (e) {
// revert adding the plugin to nx.json // revert adding the plugin to nx.json
nxJson.installation.plugins[pkgName] = undefined; nxJson.installation.plugins[pkgName] = undefined;

View File

@ -72,9 +72,9 @@ export async function initHandler(options: InitArgs): Promise<void> {
output.log({ title: '🧐 Checking dependencies' }); output.log({ title: '🧐 Checking dependencies' });
const detectPluginsResponse = await detectPlugins(); const { plugins, updatePackageScripts } = await detectPlugins();
if (!detectPluginsResponse?.plugins.length) { if (!plugins.length) {
// If no plugins are detected/chosen, guide users to setup // If no plugins are detected/chosen, guide users to setup
// their targetDefaults correctly so their package scripts will work. // their targetDefaults correctly so their package scripts will work.
const packageJson: PackageJson = readJsonFile('package.json'); const packageJson: PackageJson = readJsonFile('package.json');
@ -96,19 +96,17 @@ export async function initHandler(options: InitArgs): Promise<void> {
createNxJsonFile(repoRoot, [], [], {}); createNxJsonFile(repoRoot, [], [], {});
updateGitIgnore(repoRoot); updateGitIgnore(repoRoot);
addDepsToPackageJson(repoRoot, detectPluginsResponse.plugins); addDepsToPackageJson(repoRoot, plugins);
output.log({ title: '📦 Installing Nx' }); output.log({ title: '📦 Installing Nx' });
runInstall(repoRoot, pmc); runInstall(repoRoot, pmc);
output.log({ title: '🔨 Configuring plugins' }); output.log({ title: '🔨 Configuring plugins' });
for (const plugin of detectPluginsResponse.plugins) { for (const plugin of plugins) {
execSync( execSync(
`${pmc.exec} nx g ${plugin}:init --keepExistingVersions ${ `${pmc.exec} nx g ${plugin}:init --keepExistingVersions ${
detectPluginsResponse.updatePackageScripts updatePackageScripts ? '--updatePackageScripts' : ''
? '--updatePackageScripts'
: ''
} --no-interactive`, } --no-interactive`,
{ {
stdio: [0, 1, 2], stdio: [0, 1, 2],
@ -117,7 +115,7 @@ export async function initHandler(options: InitArgs): Promise<void> {
); );
} }
if (!detectPluginsResponse.updatePackageScripts) { if (!updatePackageScripts) {
const rootPackageJsonPath = join(repoRoot, 'package.json'); const rootPackageJsonPath = join(repoRoot, 'package.json');
const json = readJsonFile<PackageJson>(rootPackageJsonPath); const json = readJsonFile<PackageJson>(rootPackageJsonPath);
json.nx = { includedScripts: [] }; json.nx = { includedScripts: [] };
@ -167,9 +165,10 @@ const npmPackageToPluginMap: Record<string, string> = {
'@remix-run/dev': '@nx/remix', '@remix-run/dev': '@nx/remix',
}; };
async function detectPlugins(): Promise< async function detectPlugins(): Promise<{
undefined | { plugins: string[]; updatePackageScripts: boolean } plugins: string[];
> { updatePackageScripts: boolean;
}> {
let files = ['package.json'].concat( let files = ['package.json'].concat(
globWithWorkspaceContext(process.cwd(), ['**/*/package.json']) globWithWorkspaceContext(process.cwd(), ['**/*/package.json'])
); );
@ -203,7 +202,12 @@ async function detectPlugins(): Promise<
const plugins = Array.from(detectedPlugins); const plugins = Array.from(detectedPlugins);
if (plugins.length === 0) return undefined; if (plugins.length === 0) {
return {
plugins: [],
updatePackageScripts: false,
};
}
output.log({ output.log({
title: `Recommended Plugins:`, title: `Recommended Plugins:`,
@ -222,7 +226,11 @@ async function detectPlugins(): Promise<
}, },
]).then((r) => r.plugins); ]).then((r) => r.plugins);
if (pluginsToInstall?.length === 0) return undefined; if (pluginsToInstall?.length === 0)
return {
plugins: [],
updatePackageScripts: false,
};
const updatePackageScripts = const updatePackageScripts =
existsSync('package.json') && existsSync('package.json') &&