cleanup(repo): avoid referencing node_modules

This commit is contained in:
Tasos Bekos 2020-09-11 15:46:31 +03:00 committed by Victor Savkin
parent b11a50f9a0
commit 71c8e9c493
9 changed files with 65 additions and 90 deletions

View File

@ -12,32 +12,16 @@ export function initGlobal() {
if (workspace) { if (workspace) {
// Found a workspace root - hand off to the local copy of Nx // Found a workspace root - hand off to the local copy of Nx
const localNx = path.join( try {
workspace.dir, const localNx = require.resolve('@nrwl/cli/bin/nx.js', {
'node_modules', paths: [workspace.dir],
'@nrwl',
'cli',
'bin',
'nx.js'
);
if (fs.existsSync(localNx)) {
require(localNx);
} else {
if (fs.existsSync(path.join(workspace.dir, 'node_modules'))) {
output.error({
title: `Could not find Nx in this workspace.`,
bodyLines: [
`To convert an Angular workspace to Nx run: ${chalk.bold.white(
`ng add @nrwl/workspace`
)}`,
],
}); });
} else { require(localNx);
} catch (e) {
output.error({ output.error({
title: `Could not find a node_modules folder in this workspace.`, title: `Could not find @nrwl/cli module in this workspace.`,
bodyLines: [`Have you run ${chalk.bold.white(`npm/yarn install`)}?`], bodyLines: [`Have you run ${chalk.bold.white(`npm/yarn install`)}?`],
}); });
}
process.exit(1); process.exit(1);
} }
} else { } else {

View File

@ -18,15 +18,7 @@ export function initLocal(workspace: Workspace) {
if (supportedNxCommands.includes(process.argv[2])) { if (supportedNxCommands.includes(process.argv[2])) {
// required to make sure nrwl/workspace import works // required to make sure nrwl/workspace import works
if (workspace.type === 'nx') { if (workspace.type === 'nx') {
require(path.join( require('@nrwl/tao/src/compat/compat.js');
workspace.dir,
'node_modules',
'@nrwl',
'tao',
'src',
'compat',
'compat.js'
));
} }
require('@nrwl/workspace' + '/src/command-line/nx-commands').commandsObject require('@nrwl/workspace' + '/src/command-line/nx-commands').commandsObject
.argv; .argv;
@ -40,27 +32,23 @@ export function initLocal(workspace: Workspace) {
} }
function loadCli(workspace: Workspace) { function loadCli(workspace: Workspace) {
let cliPath: string;
if (workspace.type === 'nx') { if (workspace.type === 'nx') {
require(path.join( cliPath = '@nrwl/tao/index.js';
workspace.dir,
'node_modules',
'@nrwl',
'tao',
'index.js'
));
} else if (workspace.type === 'angular') { } else if (workspace.type === 'angular') {
require(path.join( cliPath = '@angular/cli/lib/init.js';
workspace.dir,
'node_modules',
'@angular',
'cli',
'lib',
'init.js'
));
} else { } else {
console.error(`Cannot recognize the workspace type.`); console.error(`Cannot recognize the workspace type.`);
process.exit(1); process.exit(1);
} }
try {
const cli = require.resolve(cliPath, { paths: [workspace.dir] });
require(cli);
} catch (e) {
console.error(`Could not find ${cliPath} module in this workspace.`, e);
process.exit(1);
}
} }
function runOneOptions( function runOneOptions(

View File

@ -1,4 +1,3 @@
import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import { findWorkspaceRoot } from './find-workspace-root'; import { findWorkspaceRoot } from './find-workspace-root';
@ -14,23 +13,21 @@ requireCli();
function requireCli() { function requireCli() {
process.env.NX_CLI_SET = 'true'; process.env.NX_CLI_SET = 'true';
let cliPath: string;
if (workspace.type === 'nx') { if (workspace.type === 'nx') {
require(path.join( cliPath = '@nrwl/tao/index.js';
workspace.dir,
'node_modules',
'@nrwl',
'tao',
'index.js'
));
} else { } else {
require(path.join( cliPath = '@angular/cli/lib/init.js';
workspace.dir, }
'node_modules',
'@angular', try {
'cli', const cli = require.resolve(cliPath, {
'lib', paths: [workspace.dir],
'init.js' });
)); require(cli);
} catch (e) {
console.error(`Could not find ${cliPath} module in this workspace.`, e);
process.exit(1);
} }
} }

View File

@ -2,6 +2,7 @@
// we can import from '@nrwl/workspace' because it will require typescript // we can import from '@nrwl/workspace' because it will require typescript
import { output } from '@nrwl/workspace/src/utils/output'; import { output } from '@nrwl/workspace/src/utils/output';
import { getPackageManagerExecuteCommand } from '@nrwl/workspace/src/utils/detect-package-manager';
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import { writeFileSync } from 'fs'; import { writeFileSync } from 'fs';
import * as inquirer from 'inquirer'; import * as inquirer from 'inquirer';
@ -442,6 +443,7 @@ function createApp(
: ` --interactive=false`; : ` --interactive=false`;
const defaultBaseArg = defaultBase ? ` --defaultBase="${defaultBase}"` : ``; const defaultBaseArg = defaultBase ? ` --defaultBase="${defaultBase}"` : ``;
const packageExec = getPackageManagerExecuteCommand(packageManager);
console.log( console.log(
`new ${name} ${args} --preset="${preset}"${appNameArg}${styleArg}${nxCloudArg}${interactiveArg}${defaultBaseArg} --collection=@nrwl/workspace` `new ${name} ${args} --preset="${preset}"${appNameArg}${styleArg}${nxCloudArg}${interactiveArg}${defaultBaseArg} --collection=@nrwl/workspace`
); );
@ -462,7 +464,7 @@ function createApp(
if (nxCloud) { if (nxCloud) {
output.addVerticalSeparator(); output.addVerticalSeparator();
execSync(`npx nx g @nrwl/nx-cloud:init --no-analytics`, { execSync(`${packageExec} nx g @nrwl/nx-cloud:init --no-analytics`, {
stdio: [0, 1, 2], stdio: [0, 1, 2],
cwd: path.join(process.cwd(), name), cwd: path.join(process.cwd(), name),
}); });

View File

@ -1,6 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import { platform } from 'os'; import { getPackageManagerExecuteCommand } from '../utils/detect-package-manager';
import * as yargs from 'yargs'; import * as yargs from 'yargs';
import { nxVersion } from '../utils/versions'; import { nxVersion } from '../utils/versions';
import { generateGraph } from './dep-graph'; import { generateGraph } from './dep-graph';
@ -179,10 +179,7 @@ export const commandsObject = yargs
`, `,
(yargs) => yargs, (yargs) => yargs,
() => { () => {
const executable = const executable = `${getPackageManagerExecuteCommand()} tao`;
platform() === 'win32'
? `.\\node_modules\\.bin\\tao`
: `./node_modules/.bin/tao`;
execSync(`${executable} migrate ${process.argv.slice(3).join(' ')}`, { execSync(`${executable} migrate ${process.argv.slice(3).join(' ')}`, {
stdio: ['inherit', 'inherit', 'inherit'], stdio: ['inherit', 'inherit', 'inherit'],
}); });

View File

@ -23,11 +23,13 @@ import * as fs from 'fs';
import { readFileSync, writeFileSync } from 'fs'; import { readFileSync, writeFileSync } from 'fs';
import { copySync, removeSync } from 'fs-extra'; import { copySync, removeSync } from 'fs-extra';
import * as inquirer from 'inquirer'; import * as inquirer from 'inquirer';
import { platform } from 'os';
import * as path from 'path'; import * as path from 'path';
import * as yargsParser from 'yargs-parser'; import * as yargsParser from 'yargs-parser';
import { appRootPath } from '../utils/app-root'; import { appRootPath } from '../utils/app-root';
import { detectPackageManager } from '../utils/detect-package-manager'; import {
detectPackageManager,
getPackageManagerExecuteCommand,
} from '../utils/detect-package-manager';
import { fileExists, readJsonFile, writeJsonFile } from '../utils/fileutils'; import { fileExists, readJsonFile, writeJsonFile } from '../utils/fileutils';
import { output } from '../utils/output'; import { output } from '../utils/output';
import { CompilerOptions } from 'typescript'; import { CompilerOptions } from 'typescript';
@ -89,10 +91,8 @@ function compileToolsDir(outDir: string) {
include: [path.join(schematicsDir(), '**/*.ts')], include: [path.join(schematicsDir(), '**/*.ts')],
}); });
const tsc = const packageExec = getPackageManagerExecuteCommand();
platform() === 'win32' const tsc = `${packageExec} tsc`;
? `.\\node_modules\\.bin\\tsc`
: `./node_modules/.bin/tsc`;
try { try {
execSync(`${tsc} -p ${tmpTsConfigPath}`, { execSync(`${tsc} -p ${tmpTsConfigPath}`, {
stdio: 'inherit', stdio: 'inherit',

View File

@ -25,7 +25,7 @@ import { nxVersion } from '../../utils/versions';
import * as path from 'path'; import * as path from 'path';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { spawn } from 'child_process'; import { spawn } from 'child_process';
import { platform } from 'os'; import { getPackageManagerExecuteCommand } from '../../utils/detect-package-manager';
// @ts-ignore // @ts-ignore
import yargsParser = require('yargs-parser'); import yargsParser = require('yargs-parser');
@ -75,10 +75,7 @@ function createPresetTaskExecutor(cli: string, opts: Schema) {
shell: true, shell: true,
cwd: path.join(process.cwd(), opts.directory), cwd: path.join(process.cwd(), opts.directory),
}; };
const executable = const executable = `${getPackageManagerExecuteCommand()} ${cliCommand}`;
platform() === 'win32'
? `.\\node_modules\\.bin\\${cliCommand}`
: `./node_modules/.bin/${cliCommand}`;
const args = [ const args = [
`g`, `g`,
`@nrwl/workspace:preset`, `@nrwl/workspace:preset`,

View File

@ -317,14 +317,10 @@ export class TaskOrchestrator {
} }
private getCommand() { private getCommand() {
return path.join( const cli = require.resolve(`@nrwl/cli/lib/run-cli.js`, {
this.workspaceRoot, paths: [this.workspaceRoot],
'node_modules', });
'@nrwl', return `${cli}`;
'cli',
'lib',
'run-cli.js'
);
} }
private getCommandArgs(task: Task) { private getCommandArgs(task: Task) {

View File

@ -18,3 +18,17 @@ export function detectPackageManager(): string {
: 'npm'; : 'npm';
} }
} }
export function getPackageManagerExecuteCommand(
packageManager = detectPackageManager()
) {
if (packageManager === 'yarn') {
return `yarn`;
}
if (packageManager === 'pnpm') {
return `pnpx`;
}
return `npx`;
}