Skip to content

Commit

Permalink
chore(#1037): replace execa with nano-spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeraa committed Oct 4, 2024
1 parent faa4fcb commit 0775694
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 170 deletions.
6 changes: 3 additions & 3 deletions docs/.vitepress/loaders/cli.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve, join } from 'node:path';
import { resolve } from 'node:path';
import consola from 'consola';
import { execaCommand } from 'execa';
import spawn from 'nano-spawn';

const cliDir = resolve('packages/wxt/src/cli/commands');
const cliDirGlob = resolve(cliDir, '**');
Expand Down Expand Up @@ -37,7 +37,7 @@ export default {
};

async function getHelp(command: string): Promise<string> {
const res = await execaCommand(command + ' --help', {
const res = await spawn('pnpm', [command, '--help'], {
cwd: 'packages/wxt',
});
return res.stdout;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"changelogen": "^0.5.7",
"consola": "^3.2.3",
"dependency-graph": "^1.0.0",
"execa": "^9.4.0",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"hasha": "^6.0.0",
"lint-staged": "^15.2.10",
"nano-spawn": "^0.1.0",
"npm-run-all": "^4.1.5",
"prettier": "^3.3.3",
"simple-git-hooks": "^2.11.1",
Expand Down
10 changes: 6 additions & 4 deletions packages/wxt/e2e/tests/auto-imports.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';
import { TestProject } from '../utils';
import { execaCommand } from 'execa';
import spawn from 'nano-spawn';

describe('Auto Imports', () => {
describe('imports: { ... }', () => {
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('Auto Imports', () => {
await project.prepare({
imports: { eslintrc: { enabled: version } },
});
return await execaCommand('pnpm eslint entrypoints/background.js', {
return await spawn('pnpm', ['eslint', 'entrypoints/background.js'], {
cwd: project.root,
});
}
Expand All @@ -205,7 +205,8 @@ describe('Auto Imports', () => {
);

await expect(runEslint(project, 9)).rejects.toMatchObject({
message: expect.stringContaining(
exitCode: 1,
stdout: expect.stringContaining(
"'defineBackground' is not defined",
),
});
Expand Down Expand Up @@ -253,7 +254,8 @@ describe('Auto Imports', () => {
);

await expect(runEslint(project, 8)).rejects.toMatchObject({
message: expect.stringContaining(
exitCode: 1,
stdout: expect.stringContaining(
"'defineBackground' is not defined",
),
});
Expand Down
30 changes: 19 additions & 11 deletions packages/wxt/e2e/tests/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { describe, it, expect } from 'vitest';
import { TestProject, WXT_PACKAGE_DIR } from '../utils';
import { execaCommand } from 'execa';
import spawn from 'nano-spawn';
import glob from 'fast-glob';
import { mkdir, writeJson } from 'fs-extra';

describe('Init command', () => {
it('should download and create a template', async () => {
const project = new TestProject();

await execaCommand(`pnpm -s wxt init ${project.root} -t vue --pm npm`, {
env: { ...process.env, CI: 'true' },
stdio: 'ignore',
cwd: WXT_PACKAGE_DIR,
});
await spawn(
'pnpm',
['-s', 'wxt', 'init', project.root, '-t', 'vue', '--pm', 'npm'],
{
env: { ...process.env, CI: 'true' },
stdio: 'ignore',
cwd: WXT_PACKAGE_DIR,
},
);
const files = await glob('**/*', {
cwd: project.root,
onlyFiles: true,
Expand Down Expand Up @@ -51,11 +55,15 @@ describe('Init command', () => {
await writeJson(project.resolvePath('package.json'), {});

await expect(() =>
execaCommand(`pnpm -s wxt init ${project.root} -t vue --pm npm`, {
env: { ...process.env, CI: 'true' },
stdio: 'ignore',
cwd: WXT_PACKAGE_DIR,
}),
spawn(
'pnpm',
['-s', 'wxt', 'init', project.root, '-t', 'vue', '--pm', 'npm'],
{
env: { ...process.env, CI: 'true' },
stdio: 'ignore',
cwd: WXT_PACKAGE_DIR,
},
),
).rejects.toThrowError('Command failed with exit code 1:');
});
});
12 changes: 7 additions & 5 deletions packages/wxt/e2e/tests/zip.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import { TestProject } from '../utils';
import extract from 'extract-zip';
import { execaCommand } from 'execa';
import spawn from 'nano-spawn';
import { readFile, writeFile } from 'fs-extra';

process.env.WXT_PNPM_IGNORE_WORKSPACE = 'true';
Expand Down Expand Up @@ -41,13 +41,15 @@ describe('Zipping', () => {

// Build zipped extension
await expect(
execaCommand('pnpm i --ignore-workspace --frozen-lockfile false', {
spawn('pnpm', ['i', '--ignore-workspace', '--frozen-lockfile', 'false'], {
cwd: unzipDir,
}),
).resolves.toMatchObject({ exitCode: 0 });
).resolves.not.toHaveProperty('exitCode');
await expect(
execaCommand('pnpm wxt build -b firefox', { cwd: unzipDir }),
).resolves.toMatchObject({ exitCode: 0 });
spawn('pnpm', ['wxt', 'build', '-b', 'firefox'], {
cwd: unzipDir,
}),
).resolves.not.toHaveProperty('exitCode');

await expect(project.fileExists(unzipDir, '.output')).resolves.toBe(true);
expect(
Expand Down
4 changes: 2 additions & 2 deletions packages/wxt/e2e/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dirname, relative, resolve } from 'path';
import fs, { mkdir } from 'fs-extra';
import glob from 'fast-glob';
import { execaCommand } from 'execa';
import spawn from 'nano-spawn';
import {
InlineConfig,
UserConfig,
Expand Down Expand Up @@ -119,7 +119,7 @@ export class TestProject {
await fs.writeFile(filePath, content ?? '', 'utf-8');
}

await execaCommand('pnpm --ignore-workspace i --ignore-scripts', {
await spawn('pnpm', ['--ignore-workspace', 'i', '--ignore-scripts'], {
cwd: this.root,
});
await mkdir(resolve(this.root, 'public'), { recursive: true }).catch(
Expand Down
2 changes: 1 addition & 1 deletion packages/wxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
"dequal": "^2.0.3",
"dotenv": "^16.4.5",
"esbuild": "^0.24.0",
"execa": "^9.4.0",
"fast-glob": "^3.3.2",
"filesize": "^10.1.6",
"fs-extra": "^11.2.0",
Expand All @@ -111,6 +110,7 @@
"linkedom": "^0.18.5",
"magicast": "^0.3.5",
"minimatch": "^10.0.1",
"nano-spawn": "^0.1.0",
"natural-compare": "^1.4.0",
"normalize-path": "^3.0.0",
"nypm": "^0.3.12",
Expand Down
4 changes: 2 additions & 2 deletions packages/wxt/src/cli/cli-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export function createAliasedCommand(
const args = process.argv.slice(
process.argv.indexOf(aliasedCommand.name) + 1,
);
const { execa } = await import('execa');
await execa(bin, args, {
const { default: spawn } = await import('nano-spawn');
await spawn(bin, args, {

Check warning on line 91 in packages/wxt/src/cli/cli-utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/wxt/src/cli/cli-utils.ts#L90-L91

Added lines #L90 - L91 were not covered by tests
stdio: 'inherit',
});
} catch {
Expand Down
4 changes: 2 additions & 2 deletions packages/wxt/src/core/package-managers/__tests__/npm.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { beforeAll, describe, expect, it } from 'vitest';
import path from 'node:path';
import { npm } from '../npm';
import { execaCommand } from 'execa';
import spawn from 'nano-spawn';
import { exists } from 'fs-extra';

describe('NPM Package Management Utils', () => {
describe('listDependencies', () => {
const cwd = path.resolve(__dirname, 'fixtures/simple-npm-project');
beforeAll(async () => {
// NPM needs the modules installed for 'npm ls' to work
await execaCommand('npm i', { cwd });
await spawn('npm', ['i'], { cwd });
}, 60e3);

it('should list direct dependencies', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/wxt/src/core/package-managers/__tests__/pnpm.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeAll, describe, expect, it } from 'vitest';
import path from 'node:path';
import { pnpm } from '../pnpm';
import { execaCommand } from 'execa';
import spawn from 'nano-spawn';

process.env.WXT_PNPM_IGNORE_WORKSPACE = 'true';

Expand All @@ -10,7 +10,7 @@ describe('PNPM Package Management Utils', () => {
const cwd = path.resolve(__dirname, 'fixtures/simple-pnpm-project');
beforeAll(async () => {
// PNPM needs the modules installed, or 'pnpm ls' will return a blank list.
await execaCommand('pnpm i --ignore-workspace', { cwd });
await spawn('pnpm', ['i', '--ignore-workspace'], { cwd });
});

it('should list direct dependencies', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/wxt/src/core/package-managers/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const bun: WxtPackageManagerImpl = {
if (options?.all) {
args.push('--all');
}
const { execa } = await import('execa');
const res = await execa('bun', args, { cwd: options?.cwd });
const { default: spawn } = await import('nano-spawn');
const res = await spawn('bun', args, { cwd: options?.cwd });

Check warning on line 15 in packages/wxt/src/core/package-managers/bun.ts

View check run for this annotation

Codecov / codecov/patch

packages/wxt/src/core/package-managers/bun.ts#L14-L15

Added lines #L14 - L15 were not covered by tests
return dedupeDependencies(
res.stdout
.split('\n')
Expand Down
8 changes: 4 additions & 4 deletions packages/wxt/src/core/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const npm: WxtPackageManagerImpl = {
overridesKey: 'overrides',
async downloadDependency(id, downloadDir) {
await ensureDir(downloadDir);
const { execa } = await import('execa');
const res = await execa('npm', ['pack', id, '--json'], {
const { default: spawn } = await import('nano-spawn');
const res = await spawn('npm', ['pack', id, '--json'], {
cwd: downloadDir,
});
const packed: PackedDependency[] = JSON.parse(res.stdout);
Expand All @@ -19,8 +19,8 @@ export const npm: WxtPackageManagerImpl = {
if (options?.all) {
args.push('--depth', 'Infinity');
}
const { execa } = await import('execa');
const res = await execa('npm', args, { cwd: options?.cwd });
const { default: spawn } = await import('nano-spawn');
const res = await spawn('npm', args, { cwd: options?.cwd });
const project: NpmListProject = JSON.parse(res.stdout);

return flattenNpmListOutput([project]);
Expand Down
4 changes: 2 additions & 2 deletions packages/wxt/src/core/package-managers/pnpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const pnpm: WxtPackageManagerImpl = {
) {
args.push('--ignore-workspace');
}
const { execa } = await import('execa');
const res = await execa('pnpm', args, { cwd: options?.cwd });
const { default: spawn } = await import('nano-spawn');
const res = await spawn('pnpm', args, { cwd: options?.cwd });
const projects: NpmListProject[] = JSON.parse(res.stdout);

return flattenNpmListOutput(projects);
Expand Down
4 changes: 2 additions & 2 deletions packages/wxt/src/core/package-managers/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const yarn: WxtPackageManagerImpl = {
if (options?.all) {
args.push('--depth', 'Infinity');
}
const { execa } = await import('execa');
const res = await execa('yarn', args, { cwd: options?.cwd });
const { default: spawn } = await import('nano-spawn');
const res = await spawn('yarn', args, { cwd: options?.cwd });
const tree = res.stdout
.split('\n')
.map<JsonLine>((line) => JSON.parse(line))
Expand Down
Loading

0 comments on commit 0775694

Please sign in to comment.