Skip to content

Commit

Permalink
cleanup(core): address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Jan 16, 2024
1 parent 2d7591b commit df64d71
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
48 changes: 17 additions & 31 deletions packages/nx/src/command-line/add/add.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { exec } from 'child_process';
import { existsSync } from 'fs';
import * as ora from 'ora';
import { join, relative } from 'path';
import { isAngularPluginInstalled } from '../../adapter/angular-json';
import type { GeneratorsJsonEntry } from '../../config/misc-interfaces';
import { readNxJson } from '../../config/nx-json';
import { runNxAsync } from '../../utils/child-process';
import { writeJsonFile } from '../../utils/fileutils';
import { logger } from '../../utils/logger';
import { output } from '../../utils/output';
Expand All @@ -14,7 +14,7 @@ import {
} from '../../utils/package-manager';
import { handleErrors } from '../../utils/params';
import { getPluginCapabilities } from '../../utils/plugins';
import { workspaceRoot, workspaceRootInner } from '../../utils/workspace-root';
import { workspaceRoot } from '../../utils/workspace-root';
import type { AddOptions } from './command-object';

export function addHandler(args: AddOptions): Promise<void> {
Expand Down Expand Up @@ -54,7 +54,7 @@ async function installPackage(
if (error) {
spinner.fail();
output.addNewline();
logger.log(stdout);
logger.error(stdout);
output.error({
title: `Failed to install ${pkgName}. Please check the error above for more details.`,
});
Expand All @@ -71,35 +71,21 @@ async function installPackage(
nxJson.installation.plugins[pkgName] = version;
writeJsonFile('nx.json', nxJson);

const cwd = process.cwd();
const offsetFromRoot = relative(cwd, workspaceRootInner(cwd, null));

let nxExecutable: string;
if (process.platform === 'win32') {
nxExecutable = '.\\' + join(`${offsetFromRoot}`, 'nx.bat');
} else {
nxExecutable = './' + join(`${offsetFromRoot}`, 'nx');
try {
await runNxAsync('');
} catch (e) {
// revert adding the plugin to nx.json
nxJson.installation.plugins[pkgName] = undefined;
writeJsonFile('nx.json', nxJson);

spinner.fail();
output.addNewline();
logger.error(e.message);
output.error({
title: `Failed to install ${pkgName}. Please check the error above for more details.`,
});
process.exit(1);
}

await new Promise<void>((resolve) =>
exec(`${nxExecutable}`, (error, _stdout, stderr) => {
if (error) {
// revert adding the plugin to nx.json
nxJson.installation.plugins[pkgName] = undefined;
writeJsonFile('nx.json', nxJson);

spinner.fail();
output.addNewline();
logger.log(stderr);
output.error({
title: `Failed to install ${pkgName}. Please check the error above for more details.`,
});
process.exit(1);
}

return resolve();
})
);
}

spinner.succeed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function performInstallation(
fs.writeFileSync(
installationPath,
JSON.stringify({
...currentInstallation,
name: 'nx-installation',
devDependencies: {
nx: nxJson.installation.version,
...nxJson.installation.plugins,
Expand Down
50 changes: 49 additions & 1 deletion packages/nx/src/utils/child-process.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { execSync, ExecSyncOptions } from 'child_process';
import {
exec,
execSync,
type ExecOptions,
type ExecSyncOptions,
} from 'child_process';
import { existsSync } from 'fs';
import { join, relative } from 'path';
import { getPackageManagerCommand } from './package-manager';
Expand Down Expand Up @@ -26,3 +31,46 @@ export function runNxSync(
}
execSync(`${baseCmd} ${cmd}`, options);
}

export async function runNxAsync(
cmd: string,
options?: ExecOptions & { cwd?: string; silent?: boolean }
) {
let baseCmd: string;
if (existsSync(join(workspaceRoot, 'package.json'))) {
baseCmd = `${getPackageManagerCommand().exec} nx`;
} else {
options ??= {};
options.cwd ??= process.cwd();
const offsetFromRoot = relative(
options.cwd,
workspaceRootInner(options.cwd, null)
);
if (process.platform === 'win32') {
baseCmd = '.\\' + join(`${offsetFromRoot}`, 'nx.bat');
} else {
baseCmd = './' + join(`${offsetFromRoot}`, 'nx');
}
}
const silent = options?.silent ?? true;
if (options?.silent) {
delete options.silent;
}
await new Promise((resolve, reject) => {
const child = exec(
`${baseCmd} ${cmd}`,
options,
(error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(stdout);
}
}
);
if (!silent) {
child.stdout?.pipe(process.stdout);
child.stderr?.pipe(process.stderr);
}
});
}

0 comments on commit df64d71

Please sign in to comment.