Skip to content

Commit

Permalink
Add checkHybrid job
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Sep 19, 2024
1 parent 0dadee9 commit ac5ffb5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .github/workflows/main_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
39 changes: 39 additions & 0 deletions test.cjs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit ac5ffb5

Please sign in to comment.