feat(workspace): Enable run-commands preferLocal option by default (#6807)

This commit is contained in:
Linbudu 2021-09-16 01:08:39 +08:00 committed by GitHub
parent effbdc7fac
commit ebee6388cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 107 additions and 13 deletions

View File

@ -1 +1,2 @@
test:$6FrCaT/v0dwE:autocreated 2020-03-25T19:10:50.254Z test:$6FrCaT/v0dwE:autocreated 2020-03-25T19:10:50.254Z
dddd:ZpRSQxtFPtZog:autocreated 2021-08-21T07:57:21.687Z

View File

@ -77,8 +77,8 @@
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3", "@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
"@popperjs/core": "^2.9.2", "@popperjs/core": "^2.9.2",
"@reduxjs/toolkit": "1.5.0", "@reduxjs/toolkit": "1.5.0",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-babel": "^5.3.0", "@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-image": "^2.1.0", "@rollup/plugin-image": "^2.1.0",
"@rollup/plugin-json": "^4.1.0", "@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.4", "@rollup/plugin-node-resolve": "^13.0.4",
@ -267,6 +267,7 @@
"core-js": "^3.6.5", "core-js": "^3.6.5",
"glob": "7.1.4", "glob": "7.1.4",
"gray-matter": "^4.0.2", "gray-matter": "^4.0.2",
"npm-run-path": "^4.0.1",
"react": "17.0.2", "react": "17.0.2",
"react-copy-to-clipboard": "^5.0.3", "react-copy-to-clipboard": "^5.0.3",
"react-dom": "17.0.2", "react-dom": "17.0.2",

View File

@ -67,6 +67,7 @@
"glob": "7.1.4", "glob": "7.1.4",
"ignore": "^5.0.4", "ignore": "^5.0.4",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"npm-run-path": "^4.0.1",
"open": "^7.4.2", "open": "^7.4.2",
"rxjs": "^6.5.4", "rxjs": "^6.5.4",
"semver": "7.3.4", "semver": "7.3.4",

View File

@ -2,6 +2,8 @@ import { readFileSync, unlinkSync, writeFileSync } from 'fs';
import { relative } from 'path'; import { relative } from 'path';
import { dirSync, fileSync } from 'tmp'; import { dirSync, fileSync } from 'tmp';
import runCommands, { LARGE_BUFFER } from './run-commands.impl'; import runCommands, { LARGE_BUFFER } from './run-commands.impl';
import { env } from 'npm-run-path';
const { version } = require('package.json');
function normalize(p: string) { function normalize(p: string) {
return p.startsWith('/private') ? p.substring(8) : p; return p.startsWith('/private') ? p.substring(8) : p;
@ -61,7 +63,10 @@ describe('Command Runner Builder', () => {
expect(exec).toHaveBeenCalledWith(`echo --a=123 --b=456`, { expect(exec).toHaveBeenCalledWith(`echo --a=123 --b=456`, {
stdio: [0, 1, 2], stdio: [0, 1, 2],
cwd: undefined, cwd: undefined,
env: process.env, env: {
...process.env,
...env(),
},
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
}); });
}); });
@ -82,11 +87,17 @@ describe('Command Runner Builder', () => {
expect(exec).toHaveBeenCalledTimes(2); expect(exec).toHaveBeenCalledTimes(2);
expect(exec).toHaveBeenNthCalledWith(1, 'echo --a=123 --b=456', { expect(exec).toHaveBeenNthCalledWith(1, 'echo --a=123 --b=456', {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env }, env: {
...process.env,
...env(),
},
}); });
expect(exec).toHaveBeenNthCalledWith(2, 'echo foo --a=123 --b=456', { expect(exec).toHaveBeenNthCalledWith(2, 'echo foo --a=123 --b=456', {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env }, env: {
...process.env,
...env(),
},
}); });
}); });
@ -109,11 +120,17 @@ describe('Command Runner Builder', () => {
expect(exec).toHaveBeenCalledTimes(2); expect(exec).toHaveBeenCalledTimes(2);
expect(exec).toHaveBeenNthCalledWith(1, 'echo --a=123 --b=456', { expect(exec).toHaveBeenNthCalledWith(1, 'echo --a=123 --b=456', {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env }, env: {
...process.env,
...env(),
},
}); });
expect(exec).toHaveBeenNthCalledWith(2, 'echo foo --a=123 --b=456', { expect(exec).toHaveBeenNthCalledWith(2, 'echo foo --a=123 --b=456', {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env }, env: {
...process.env,
...env(),
},
}); });
}); });
@ -136,11 +153,17 @@ describe('Command Runner Builder', () => {
expect(exec).toHaveBeenCalledTimes(2); expect(exec).toHaveBeenCalledTimes(2);
expect(exec).toHaveBeenNthCalledWith(1, 'echo', { expect(exec).toHaveBeenNthCalledWith(1, 'echo', {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env }, env: {
...process.env,
...env(),
},
}); });
expect(exec).toHaveBeenNthCalledWith(2, 'echo foo', { expect(exec).toHaveBeenNthCalledWith(2, 'echo foo', {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env }, env: {
...process.env,
...env(),
},
}); });
}); });
@ -261,11 +284,17 @@ describe('Command Runner Builder', () => {
expect(exec).toHaveBeenCalledTimes(2); expect(exec).toHaveBeenCalledTimes(2);
expect(exec).toHaveBeenNthCalledWith(1, `echo 'Hello World'`, { expect(exec).toHaveBeenNthCalledWith(1, `echo 'Hello World'`, {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env }, env: {
...process.env,
...env(),
},
}); });
expect(exec).toHaveBeenNthCalledWith(2, `echo 'Hello Universe'`, { expect(exec).toHaveBeenNthCalledWith(2, `echo 'Hello Universe'`, {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env }, env: {
...process.env,
...env(),
},
}); });
}); });
@ -283,16 +312,73 @@ describe('Command Runner Builder', () => {
expect(exec).toHaveBeenCalledTimes(2); expect(exec).toHaveBeenCalledTimes(2);
expect(exec).toHaveBeenNthCalledWith(1, `echo 'Hello World'`, { expect(exec).toHaveBeenNthCalledWith(1, `echo 'Hello World'`, {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env, FORCE_COLOR: `true` }, env: { ...process.env, FORCE_COLOR: `true`, ...env() },
}); });
expect(exec).toHaveBeenNthCalledWith(2, `echo 'Hello Universe'`, { expect(exec).toHaveBeenNthCalledWith(2, `echo 'Hello Universe'`, {
maxBuffer: LARGE_BUFFER, maxBuffer: LARGE_BUFFER,
env: { ...process.env, FORCE_COLOR: `true` }, env: { ...process.env, FORCE_COLOR: `true`, ...env() },
}); });
}); });
}); });
describe('cwd', () => { describe('cwd', () => {
it('should use local installed package when cwd is specified', async () => {
const root = dirSync().name;
const childFolder = dirSync({ dir: root }).name;
const cwd = relative(root, childFolder);
const f = fileSync().name;
const result = await runCommands(
{
commands: [
{
command: `yarn init -y`,
},
{
command: `yarn add nx@12.0.0 @nrwl/workspace@12.0.0 @nrwl/cli@12.0.0 --save --registry=https://registry.yarnpkg.com/`,
},
{
command: `echo '{"name":"tmp","scripts":{"nx":"nx"}}' >> package.json`,
},
{
command: `echo '{}' >> nx.json`,
},
{
command: `echo '{}' >> workspace.json`,
},
{
command: `nx --version >> ${f}`,
},
],
parallel: false,
cwd,
},
{ root } as any
);
expect(result).toEqual(expect.objectContaining({ success: true }));
expect(normalize(readFile(f))).toBe('12.0.0');
});
it('should use workspace root package when cwd is not specified', async () => {
const root = dirSync().name;
const f = fileSync().name;
const result = await runCommands(
{
commands: [
{
command: `nx --version >> ${f}`,
},
],
parallel: true,
cwd: process.cwd(),
},
{ root } as any
);
expect(result).toEqual(expect.objectContaining({ success: true }));
expect(normalize(readFile(f))).toBe(version);
});
it('should run the task in the workspace root when no cwd is specified', async () => { it('should run the task in the workspace root when no cwd is specified', async () => {
const root = dirSync().name; const root = dirSync().name;
const f = fileSync().name; const f = fileSync().name;

View File

@ -2,6 +2,7 @@ import { ExecutorContext } from '@nrwl/devkit';
import { exec, execSync } from 'child_process'; import { exec, execSync } from 'child_process';
import * as path from 'path'; import * as path from 'path';
import * as yargsParser from 'yargs-parser'; import * as yargsParser from 'yargs-parser';
import { env as appendLocalEnv } from 'npm-run-path';
export const LARGE_BUFFER = 1024 * 1000000; export const LARGE_BUFFER = 1024 * 1000000;
@ -227,7 +228,11 @@ function calculateCwd(
} }
function processEnv(color: boolean) { function processEnv(color: boolean) {
const env = { ...process.env }; const env = {
...process.env,
...appendLocalEnv(),
};
if (color) { if (color) {
env.FORCE_COLOR = `${color}`; env.FORCE_COLOR = `${color}`;
} }