Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use execa 6.x in scripts #19759

Merged
merged 12 commits into from
Nov 21, 2022
2 changes: 1 addition & 1 deletion scripts/build-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async function run() {
selection?.filter(Boolean).forEach(async (v) => {
const commmand = (await readJSON(resolve(v.location, 'package.json'))).scripts.prep;
const cwd = resolve(__dirname, '..', 'code', v.location);
const sub = require('execa').command(
const sub = await import('execa').execaCommand(
`${commmand}${watchMode ? ' --watch' : ''}${prodMode ? ' --optimized' : ''}`,
{
cwd,
Expand Down
2 changes: 1 addition & 1 deletion scripts/check-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async function run() {
selection?.filter(Boolean).forEach(async (v) => {
const commmand = (await readJSON(resolve(v.location, 'package.json'))).scripts.check;
const cwd = resolve(__dirname, '..', 'code', v.location);
const sub = require('execa').command(`${commmand}${watchMode ? ' --watch' : ''}`, {
const sub = await import('execa').execaCommand(`${commmand}${watchMode ? ' --watch' : ''}`, {
cwd,
buffer: false,
shell: true,
Expand Down
4 changes: 2 additions & 2 deletions scripts/combine-compodoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// then combine the results into one large documentation.json

import { join, resolve } from 'path';
import execa from 'execa';
import { execaCommand } from 'execa';
import { realpath, readFile, writeFile, lstat } from 'fs-extra';
import glob from 'glob';
import { directory } from 'tempy';
Expand Down Expand Up @@ -37,7 +37,7 @@ async function run(cwd: string) {
dirs.map(async (dir) => {
const outputDir = directory();
const resolvedDir = await realpath(dir);
await execa.command(
await execaCommand(
`yarn compodoc ${resolvedDir} -p ./tsconfig.json -e json -d ${outputDir}`,
{ cwd }
);
Expand Down
4 changes: 2 additions & 2 deletions scripts/next-repro-generators/generate-repros.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import { join, relative } from 'path';
import { command } from 'execa';
import { execaCommand } from 'execa';
import type { Options as ExecaOptions } from 'execa';
import pLimit from 'p-limit';
import prettyTime from 'pretty-hrtime';
Expand Down Expand Up @@ -85,7 +85,7 @@ export const runCommand = async (script: string, options: ExecaOptions) => {
console.log(`Running command: ${script}`);
}

return command(script, { stdout: shouldDebug ? 'inherit' : 'ignore', shell: true, ...options });
return execaCommand(script, { stdout: shouldDebug ? 'inherit' : 'ignore', shell: true, ...options });
};

const addDocumentation = async (
Expand Down
8 changes: 4 additions & 4 deletions scripts/next-repro-generators/publish.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import program from 'commander';
import { join } from 'path';
import { existsSync } from 'fs';
import { command } from 'execa';
import { execaCommand } from '../utils/exec';
import * as tempy from 'tempy';
import { copy, emptyDir, readdir, remove, stat, writeFile } from 'fs-extra';

Expand All @@ -27,8 +27,8 @@ const publish = async (options: PublishOptions & { tmpFolder: string }) => {
const templatesData = await getTemplatesData();

logger.log(`👯‍♂️ Cloning the repository ${remote} in branch ${gitBranch}`);
await command(`git clone ${remote} .`, { cwd: tmpFolder });
await command(`git checkout ${gitBranch}`, { cwd: tmpFolder });
await execaCommand(`git clone ${remote} .`, { cwd: tmpFolder });
await execaCommand(`git checkout ${gitBranch}`, { cwd: tmpFolder });

// otherwise old files will stick around and result inconsistent states
logger.log(`🗑 Delete existing template dirs from clone`);
Expand Down Expand Up @@ -67,7 +67,7 @@ const publish = async (options: PublishOptions & { tmpFolder: string }) => {
`);

if (push) {
await command(`git push --set-upstream origin ${gitBranch}`, {
await execaCommand(`git push --set-upstream origin ${gitBranch}`, {
cwd: tmpFolder,
});
const remoteRepoUrl = `${remote.replace('.git', '')}/tree/${gitBranch}`;
Expand Down
8 changes: 4 additions & 4 deletions scripts/next-repro-generators/utils/git.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { command } from 'execa';
import { execaCommand } from '../../utils/exec';
import { logger } from '../publish';

export async function commitAllToGit(cwd: string) {
try {
logger.log(`💪 Committing everything to the repository`);

await command('git add .', { cwd });
await execaCommand('git add .', { cwd });

const currentCommitSHA = await command('git rev-parse HEAD');
await command(
const currentCommitSHA = await execaCommand('git rev-parse HEAD');
await execaCommand(
`git commit -m "Update examples - ${new Date().toDateString()} - ${currentCommitSHA.stdout
.toString()
.slice(0, 12)}"`,
Expand Down
2 changes: 1 addition & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-react": "^7.31.10",
"eslint-plugin-storybook": "^0.6.6",
"execa": "^5.0.0",
"execa": "^6.1.0",
"express": "^4.17.1",
"find-up": "^5.0.0",
"fs-extra": "^9.0.1",
Expand Down
24 changes: 20 additions & 4 deletions scripts/utils/exec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-await-in-loop, no-restricted-syntax */
import execa, { ExecaChildProcess, Options } from 'execa';
import type { ExecaChildProcess, Options } from 'execa';
import chalk from 'chalk';

const logger = console;
Expand All @@ -12,11 +12,27 @@ type StepOptions = {
signal?: AbortSignal;
};

// Note this is to fool `ts-node` into not turning the `import()` into a `require()`.
// See: https://github.com/TypeStrong/ts-node/discussions/1290
const dynamicImport = new Function('specifier', 'return import(specifier)');
export const getExeca = async () => (await dynamicImport('execa')) as typeof import('execa');

// Reimplementation of `execaCommand` to use `getExeca`
export const execaCommand = async (
command: string,
options: Options = {}
): Promise<ExecaChildProcess<string>> => {
const { execaCommand } = await getExeca();
// We await here because execaCommand returns a promise, but that's not what the user expects
return await execaCommand(command, options);
};

export const exec = async (
command: string | string[],
options: Options = {},
{ startMessage, errorMessage, dryRun, debug, signal }: StepOptions = {}
): Promise<void> => {
const execa = await getExeca()
logger.info();
if (startMessage) logger.info(startMessage);

Expand All @@ -30,7 +46,7 @@ export const exec = async (
stdout: debug ? 'inherit' : 'pipe',
stderr: debug ? 'inherit' : 'pipe',
};
let currentChild: ExecaChildProcess;
let currentChild: ExecaChildProcess<string>;

// Newer versions of execa have explicit support for abort signals, but this works
if (signal) {
Expand All @@ -40,12 +56,12 @@ export const exec = async (
try {
if (typeof command === 'string') {
logger.debug(`> ${command}`);
currentChild = execa.command(command, { ...defaultOptions, ...options });
currentChild = execa.execaCommand(command, { ...defaultOptions, ...options });
await currentChild;
} else {
for (const subcommand of command) {
logger.debug(`> ${subcommand}`);
currentChild = execa.command(subcommand, { ...defaultOptions, ...options });
currentChild = execa.execaCommand(subcommand, { ...defaultOptions, ...options });
await currentChild;
}
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/utils/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import command from 'execa';
import { execaCommand } from './exec';
import memoize from 'memoizerific';
import { resolve } from 'path';

Expand All @@ -7,7 +7,7 @@ export type Workspace = { name: string; location: string };
const codeDir = resolve(__dirname, '../../code');

async function getWorkspaces() {
const { stdout } = await command('yarn workspaces list --json', {
const { stdout } = await execaCommand('yarn workspaces list --json', {
cwd: codeDir,
shell: true,
});
Expand Down
74 changes: 72 additions & 2 deletions scripts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3399,7 +3399,7 @@ __metadata:
eslint-plugin-import: ^2.26.0
eslint-plugin-react: ^7.31.10
eslint-plugin-storybook: ^0.6.6
execa: ^5.0.0
execa: ^6.1.0
express: ^4.17.1
find-up: ^5.0.0
fs-extra: ^9.0.1
Expand Down Expand Up @@ -8846,6 +8846,23 @@ __metadata:
languageName: node
linkType: hard

"execa@npm:^6.1.0":
version: 6.1.0
resolution: "execa@npm:6.1.0"
dependencies:
cross-spawn: ^7.0.3
get-stream: ^6.0.1
human-signals: ^3.0.1
is-stream: ^3.0.0
merge-stream: ^2.0.0
npm-run-path: ^5.1.0
onetime: ^6.0.0
signal-exit: ^3.0.7
strip-final-newline: ^3.0.0
checksum: 004ee32092af745766a1b0352fdba8701a4001bc3fe08e63101c04276d4c860bbe11bb8ab85f37acdff13d3da83d60e044041dcf24bd7e25e645a543828d9c41
languageName: node
linkType: hard

"exit@npm:^0.1.2":
version: 0.1.2
resolution: "exit@npm:0.1.2"
Expand Down Expand Up @@ -9747,7 +9764,7 @@ __metadata:
languageName: node
linkType: hard

"get-stream@npm:^6.0.0":
"get-stream@npm:^6.0.0, get-stream@npm:^6.0.1":
version: 6.0.1
resolution: "get-stream@npm:6.0.1"
checksum: 49825d57d3fd6964228e6200a58169464b8e8970489b3acdc24906c782fb7f01f9f56f8e6653c4a50713771d6658f7cfe051e5eb8c12e334138c9c918b296341
Expand Down Expand Up @@ -10539,6 +10556,13 @@ __metadata:
languageName: node
linkType: hard

"human-signals@npm:^3.0.1":
version: 3.0.1
resolution: "human-signals@npm:3.0.1"
checksum: 0bb27e72aea1666322f69ab9816e05df952ef2160346f2293f98f45d472edb1b62d0f1a596697b50d48d8f8222e6db3b9f9dc0b6bf6113866121001f0a8e48e9
languageName: node
linkType: hard

"humanize-ms@npm:^1.2.1":
version: 1.2.1
resolution: "humanize-ms@npm:1.2.1"
Expand Down Expand Up @@ -11191,6 +11215,13 @@ __metadata:
languageName: node
linkType: hard

"is-stream@npm:^3.0.0":
version: 3.0.0
resolution: "is-stream@npm:3.0.0"
checksum: eb2f7127af02ee9aa2a0237b730e47ac2de0d4e76a4a905a50a11557f2339df5765eaea4ceb8029f1efa978586abe776908720bfcb1900c20c6ec5145f6f29d8
languageName: node
linkType: hard

"is-string@npm:^1.0.5, is-string@npm:^1.0.7":
version: 1.0.7
resolution: "is-string@npm:1.0.7"
Expand Down Expand Up @@ -14111,6 +14142,13 @@ __metadata:
languageName: node
linkType: hard

"mimic-fn@npm:^4.0.0":
version: 4.0.0
resolution: "mimic-fn@npm:4.0.0"
checksum: de9cc32be9996fd941e512248338e43407f63f6d497abe8441fa33447d922e927de54d4cc3c1a3c6d652857acd770389d5a3823f311a744132760ce2be15ccbf
languageName: node
linkType: hard

"min-document@npm:^2.19.0":
version: 2.19.0
resolution: "min-document@npm:2.19.0"
Expand Down Expand Up @@ -14725,6 +14763,15 @@ __metadata:
languageName: node
linkType: hard

"npm-run-path@npm:^5.1.0":
version: 5.1.0
resolution: "npm-run-path@npm:5.1.0"
dependencies:
path-key: ^4.0.0
checksum: ff6d77514489f47fa1c3b1311d09cd4b6d09a874cc1866260f9dea12cbaabda0436ed7f8c2ee44d147bf99a3af29307c6f63b0f83d242b0b6b0ab25dff2629e3
languageName: node
linkType: hard

"npmlog@npm:^5.0.1":
version: 5.0.1
resolution: "npmlog@npm:5.0.1"
Expand Down Expand Up @@ -14997,6 +15044,15 @@ __metadata:
languageName: node
linkType: hard

"onetime@npm:^6.0.0":
version: 6.0.0
resolution: "onetime@npm:6.0.0"
dependencies:
mimic-fn: ^4.0.0
checksum: 4eef7c6abfef697dd4479345a4100c382d73c149d2d56170a54a07418c50816937ad09500e1ed1e79d235989d073a9bade8557122aee24f0576ecde0f392bb6c
languageName: node
linkType: hard

"open@npm:8.4.0, open@npm:^8.4.0":
version: 8.4.0
resolution: "open@npm:8.4.0"
Expand Down Expand Up @@ -15385,6 +15441,13 @@ __metadata:
languageName: node
linkType: hard

"path-key@npm:^4.0.0":
version: 4.0.0
resolution: "path-key@npm:4.0.0"
checksum: 794efeef32863a65ac312f3c0b0a99f921f3e827ff63afa5cb09a377e202c262b671f7b3832a4e64731003fa94af0263713962d317b9887bd1e0c48a342efba3
languageName: node
linkType: hard

"path-parse@npm:^1.0.7":
version: 1.0.7
resolution: "path-parse@npm:1.0.7"
Expand Down Expand Up @@ -18009,6 +18072,13 @@ __metadata:
languageName: node
linkType: hard

"strip-final-newline@npm:^3.0.0":
version: 3.0.0
resolution: "strip-final-newline@npm:3.0.0"
checksum: a771a17901427bac6293fd416db7577e2bc1c34a19d38351e9d5478c3c415f523f391003b42ed475f27e33a78233035df183525395f731d3bfb8cdcbd4da08ce
languageName: node
linkType: hard

"strip-indent@npm:^3.0.0":
version: 3.0.0
resolution: "strip-indent@npm:3.0.0"
Expand Down