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

Add checkHybrid job #90

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading