From ac5ffb5e479c67b56649c508177eec0e049e49bc Mon Sep 17 00:00:00 2001 From: junderw Date: Thu, 19 Sep 2024 18:54:47 +0900 Subject: [PATCH] Add checkHybrid job --- .github/workflows/main_ci.yml | 10 +++++++++ package.json | 3 ++- test.cjs | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100755 test.cjs diff --git a/.github/workflows/main_ci.yml b/.github/workflows/main_ci.yml index 804d3a0..1ffaca5 100644 --- a/.github/workflows/main_ci.yml +++ b/.github/workflows/main_ci.yml @@ -37,6 +37,16 @@ jobs: registry-url: https://registry.npmjs.org/ - run: npm ci - run: npm run format:ci + check-hybrid: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v4 + with: + node-version: 18 + registry-url: https://registry.npmjs.org/ + - run: npm ci + - run: npm run checkHybrid gitdiff: runs-on: ubuntu-latest steps: diff --git a/package.json b/package.json index 1965b80..18e4d51 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ }, "scripts": { "build": "tsc -p ./tsconfig.json && tsc -p ./tsconfig.cjs.json", - "postbuild": "find src/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\; && chmod +x fixup.cjs && node fixup.cjs", + "checkHybrid": "chmod +x test.cjs && node test.cjs", "coverage-report": "npm run build && npm run nobuild:coverage-report", "coverage": "npm run build && npm run nobuild:coverage", "format": "npm run prettier -- --write", @@ -34,6 +34,7 @@ "nobuild:coverage-report": "c8 report --reporter=lcov", "nobuild:coverage": "c8 --check-coverage --branches 90 --functions 90 npm run nobuild:unit", "nobuild:unit": "tape test/*.js", + "postbuild": "find src/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\; && chmod +x fixup.cjs && node fixup.cjs", "prettier": "prettier 'ts-src/**/*.ts' --ignore-path ./.prettierignore", "test": "npm run build && npm run format:ci && npm run lint && npm run nobuild:coverage", "unit": "npm run build && npm run nobuild:unit" diff --git a/test.cjs b/test.cjs new file mode 100755 index 0000000..4be73a2 --- /dev/null +++ b/test.cjs @@ -0,0 +1,39 @@ +const fs = require('fs'); +const childProcess = require('child_process'); + +let mainFile = JSON.parse(fs.readFileSync('./package.json').toString()).module; +if (!mainFile.startsWith('.')) { + // ESM imports of files must start with ./ + mainFile = `./${mainFile}`; +} +const cjsFile = './tmp.cjs'; +const esmFile = './tmp.mjs'; +const cjs = `const x = require('.')\nconsole.log(x)`; +const esm = `import * as x from '${mainFile}';\nconsole.log(x)`; + +fs.writeFileSync(cjsFile, cjs); +fs.writeFileSync(esmFile, esm); + +const result1 = childProcess.spawnSync('node', [cjsFile], { + cwd: __dirname, +}); +const result2 = childProcess.spawnSync('node', [esmFile], { + cwd: __dirname, +}); + +fs.unlinkSync(cjsFile); +fs.unlinkSync(esmFile); + +const errors = []; +if (result1.status !== 0) { + errors.push(result1.stderr.toString()); +} +if (result2.status !== 0) { + errors.push(result2.stderr.toString()); +} +if (errors.length > 0) { + console.error( + `Failed to run this library under CJS or ESM. Errors:\n${errors.join('\n')}`, + ); + process.exit(1); +}