From 1992b15b9bf17c8f29b4d5367f79cd59bcba7981 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Mon, 17 Jan 2022 14:01:33 +0800 Subject: [PATCH] chore: use zx for all npm scripts --- package.json | 4 +-- scripts/prepublish.mjs | 3 ++- snapshot.js => scripts/snapshot.mjs | 35 +++++++----------------- scripts/test.mjs | 19 +++++++++++++ test.js | 42 ----------------------------- 5 files changed, 33 insertions(+), 70 deletions(-) rename snapshot.js => scripts/snapshot.mjs (63%) create mode 100644 scripts/test.mjs delete mode 100644 test.js diff --git a/package.json b/package.json index ce5170b8..898c917e 100644 --- a/package.json +++ b/package.json @@ -17,9 +17,9 @@ "prepare": "husky install", "format": "prettier --write .", "build": "esbuild --bundle index.js --format=cjs --platform=node --outfile=outfile.cjs", - "snapshot": "node snapshot.js", + "snapshot": "zx ./scripts/snapshot.mjs", "pretest": "run-s build snapshot", - "test": "node test.js", + "test": "zx ./scripts/test.mjs", "prepublishOnly": "zx ./scripts/prepublish.mjs" }, "repository": { diff --git a/scripts/prepublish.mjs b/scripts/prepublish.mjs index b472c7f8..e3a31dcc 100644 --- a/scripts/prepublish.mjs +++ b/scripts/prepublish.mjs @@ -6,7 +6,8 @@ await $`pnpm snapshot` let { version } = JSON.parse(await fs.readFile('./package.json')) -cd('./playground') +const playgroundDir = path.resolve(__dirname, '../playground/') +cd(playgroundDir) await $`git add -A .` try { diff --git a/snapshot.js b/scripts/snapshot.mjs similarity index 63% rename from snapshot.js rename to scripts/snapshot.mjs index 2a1dac74..18ad2b0a 100644 --- a/snapshot.js +++ b/scripts/snapshot.mjs @@ -1,29 +1,10 @@ -import { spawnSync } from 'child_process' -import path from 'path' +#!/usr/bin/env zx +import 'zx/globals' -const __dirname = path - .dirname(new URL(import.meta.url).pathname) - .substring(process.platform === 'win32' ? 1 : 0) +$.verbose = false -const bin = path.resolve(__dirname, './outfile.cjs') -const playgroundDir = path.resolve(__dirname, './playground/') - -function createProjectWithFeatureFlags(flags) { - const projectName = flags.join('-') - console.log(`Creating project ${projectName}`) - const { status } = spawnSync( - 'node', - [bin, projectName, ...flags.map((flag) => `--${flag}`), '--force'], - { - cwd: playgroundDir, - stdio: ['pipe', 'pipe', 'inherit'] - } - ) - - if (status !== 0) { - process.exit(status) - } -} +const bin = path.resolve(__dirname, '../outfile.cjs') +const playgroundDir = path.resolve(__dirname, '../playground/') const featureFlags = ['typescript', 'jsx', 'router', 'pinia', 'with-tests'] @@ -59,6 +40,10 @@ function fullCombination(arr) { const flagCombinations = fullCombination(featureFlags) flagCombinations.push(['default']) +cd(playgroundDir) for (const flags of flagCombinations) { - createProjectWithFeatureFlags(flags) + const projectName = flags.join('-') + console.log(`Creating project ${projectName}`) + + await $`node ${[bin, projectName, ...flags.map((flag) => `--${flag}`), '--force']}` } diff --git a/scripts/test.mjs b/scripts/test.mjs new file mode 100644 index 00000000..5d29eb1c --- /dev/null +++ b/scripts/test.mjs @@ -0,0 +1,19 @@ +#!/usr/bin/env zx +import 'zx/globals' + +const playgroundDir = path.resolve(__dirname, '../playground/') + +for (const projectName of fs.readdirSync(playgroundDir)) { + if (projectName.endsWith('with-tests')) { + cd(path.resolve(playgroundDir, projectName)) + + console.log(`Running unit tests in ${projectName}`) + await $`pnpm test:unit:ci` + + console.log(`Building ${projectName}`) + await $`pnpm build` + + console.log(`Running e2e tests in ${projectName}`) + await $`pnpm test:e2e:ci` + } +} diff --git a/test.js b/test.js deleted file mode 100644 index c80ebd39..00000000 --- a/test.js +++ /dev/null @@ -1,42 +0,0 @@ -import fs from 'fs' -import path from 'path' -import { fileURLToPath } from 'url' - -import { spawnSync } from 'child_process' - -const __dirname = path.dirname(fileURLToPath(import.meta.url)) -const playgroundDir = path.resolve(__dirname, './playground/') - -for (const projectName of fs.readdirSync(playgroundDir)) { - if (projectName.endsWith('with-tests')) { - console.log(`Running unit tests in ${projectName}`) - const unitTestResult = spawnSync('pnpm', ['test:unit:ci'], { - cwd: path.resolve(playgroundDir, projectName), - stdio: 'inherit', - shell: true - }) - if (unitTestResult.status !== 0) { - throw new Error(`Unit tests failed in ${projectName}`) - } - - console.log(`Building ${projectName}`) - const buildResult = spawnSync('pnpm', ['build'], { - cwd: path.resolve(playgroundDir, projectName), - stdio: 'inherit', - shell: true - }) - if (buildResult.status !== 0) { - throw new Error(`Build failed in ${projectName}`) - } - - console.log(`Running e2e tests in ${projectName}`) - const e2eTestResult = spawnSync('pnpm', ['test:e2e:ci'], { - cwd: path.resolve(playgroundDir, projectName), - stdio: 'inherit', - shell: true - }) - if (e2eTestResult.status !== 0) { - throw new Error(`E2E tests failed in ${projectName}`) - } - } -} -- 2.39.5