Skip to content

Commit

Permalink
fix(core): fix no plugins found for nx init without packge.json
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Mar 21, 2024
1 parent c20e00c commit be634a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/nx/src/command-line/add/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function installPackage(pkgName: string, version: string): Promise<void> {
writeJsonFile('nx.json', nxJson);

try {
await runNxAsync('--help');
await runNxAsync('--help', { silent: true });
} catch (e) {
// revert adding the plugin to nx.json
nxJson.installation.plugins[pkgName] = undefined;
Expand Down
28 changes: 16 additions & 12 deletions packages/nx/src/command-line/init/init-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ export async function initHandler(options: InitArgs): Promise<void> {

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
// their targetDefaults correctly so their package scripts will work.
const packageJson: PackageJson = readJsonFile('package.json');
Expand All @@ -96,19 +96,17 @@ export async function initHandler(options: InitArgs): Promise<void> {
createNxJsonFile(repoRoot, [], [], {});
updateGitIgnore(repoRoot);

addDepsToPackageJson(repoRoot, detectPluginsResponse.plugins);
addDepsToPackageJson(repoRoot, plugins);

output.log({ title: '📦 Installing Nx' });

runInstall(repoRoot, pmc);

output.log({ title: '🔨 Configuring plugins' });
for (const plugin of detectPluginsResponse.plugins) {
for (const plugin of plugins) {
execSync(
`${pmc.exec} nx g ${plugin}:init --keepExistingVersions ${
detectPluginsResponse.updatePackageScripts
? '--updatePackageScripts'
: ''
updatePackageScripts ? '--updatePackageScripts' : ''
} --no-interactive`,
{
stdio: [0, 1, 2],
Expand All @@ -117,7 +115,7 @@ export async function initHandler(options: InitArgs): Promise<void> {
);
}

if (!detectPluginsResponse.updatePackageScripts) {
if (!updatePackageScripts) {
const rootPackageJsonPath = join(repoRoot, 'package.json');
const json = readJsonFile<PackageJson>(rootPackageJsonPath);
json.nx = { includedScripts: [] };
Expand Down Expand Up @@ -167,9 +165,10 @@ const npmPackageToPluginMap: Record<string, string> = {
'@remix-run/dev': '@nx/remix',
};

async function detectPlugins(): Promise<
undefined | { plugins: string[]; updatePackageScripts: boolean }
> {
async function detectPlugins(): Promise<{
plugins: string[];
updatePackageScripts: boolean;
}> {
let files = ['package.json'].concat(
globWithWorkspaceContext(process.cwd(), ['**/*/package.json'])
);
Expand Down Expand Up @@ -203,7 +202,12 @@ async function detectPlugins(): Promise<

const plugins = Array.from(detectedPlugins);

if (plugins.length === 0) return undefined;
if (plugins.length === 0) {
return {
plugins: [],
updatePackageScripts: false,
};
}

output.log({
title: `Recommended Plugins:`,
Expand Down

0 comments on commit be634a8

Please sign in to comment.