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

fix: error exit code. #37

Merged
merged 1 commit into from
Jan 23, 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
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"rules": {
"no-console": "error",
"n/no-process-exit": "off",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is funny: eslint/eslint#795

"n/shebang": [
"error",
{
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3.5.2
uses: actions/checkout@v4.1.1
- name: Setup Node
uses: actions/setup-node@v3.6.0
uses: actions/setup-node@v4.0.1
with:
node-version: '20.10.0'
node-version: '20.11.0'
- name: Install Dependencies
run: npm ci
- name: Save error log
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4.2.0
if: ${{ failure() }}
with:
name: npm-debug-log-${{ hashFiles('package-lock.json') }}
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3.5.2
uses: actions/checkout@v4.1.1
- name: Setup Node
uses: actions/setup-node@v3.6.0
uses: actions/setup-node@v4.0.1
with:
node-version: '20.10.0'
node-version: '20.11.0'
- name: Install Dependencies
run: npm ci
- name: Save error log
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4.2.0
if: ${{ failure() }}
with:
name: npm-debug-log-${{ hashFiles('package-lock.json') }}
Expand All @@ -30,7 +30,7 @@ jobs:
- name: Pack
run: npm pack
- name: Push to NPM registry
uses: JS-DevTools/npm-publish@v2.1.0
uses: JS-DevTools/npm-publish@v3.0.1
with:
token: ${{ secrets.NPM_AUTH_TOKEN }}
tag: ${{ contains(github.ref, '-') && 'next' || 'latest' }}
Expand Down
8 changes: 0 additions & 8 deletions babel.config.json

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@knighted/duel",
"version": "1.0.5",
"version": "1.0.6",
"description": "TypeScript dual packages.",
"type": "module",
"main": "dist",
Expand Down
40 changes: 26 additions & 14 deletions src/duel.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,26 @@
reject(new Error(`Failed to compile: ${err.message}`))
})

build.on('close', code => {
if (code === null) {
return reject(new Error('Failed to compile.'))
}

build.on('exit', code => {
if (code > 0) {
return reject(new Error('Compilation errors found.'))
return reject(new Error(code))
}

resolve(code)
})
})
}
const handleErrorAndExit = message => {
const exitCode = Number(message)

if (isNaN(exitCode)) {
logError(message)
process.exit(1)

Check warning on line 47 in src/duel.js

View check run for this annotation

Codecov / codecov/patch

src/duel.js#L46-L47

Added lines #L46 - L47 were not covered by tests
} else {
logError('Compilation errors found.')
process.exit(exitCode)
}
}
const duel = async args => {
const ctx = await init(args)

Expand Down Expand Up @@ -175,7 +182,7 @@
])
success = true
} catch ({ message }) {
logError(message)
handleErrorAndExit(message)

Check warning on line 185 in src/duel.js

View check run for this annotation

Codecov / codecov/patch

src/duel.js#L185

Added line #L185 was not covered by tests
}

if (success) {
Expand All @@ -202,7 +209,7 @@
await runPrimaryBuild()
success = true
} catch ({ message }) {
logError(message)
handleErrorAndExit(message)
}

if (success) {
Expand All @@ -213,6 +220,7 @@
)
const tsconfigDual = getOverrideTsConfig()
const pkgRename = 'package.json.bak'
let errorMsg = ''

/**
* Create a new package.json with updated `type` field.
Expand All @@ -236,14 +244,18 @@
await runBuild(dualConfigPath, absoluteDualOutDir)
} catch ({ message }) {
success = false
logError(message)
errorMsg = message

Check warning on line 247 in src/duel.js

View check run for this annotation

Codecov / codecov/patch

src/duel.js#L247

Added line #L247 was not covered by tests
} finally {
// Cleanup and restore
await rm(dualConfigPath, { force: true })
await rm(pkg.path, { force: true })
await rename(join(pkgDir, pkgRename), pkg.path)

if (errorMsg) {
handleErrorAndExit(errorMsg)
}

Check warning on line 256 in src/duel.js

View check run for this annotation

Codecov / codecov/patch

src/duel.js#L255-L256

Added lines #L255 - L256 were not covered by tests
}

// Cleanup and restore
await rm(dualConfigPath, { force: true })
await rm(pkg.path, { force: true })
await rename(join(pkgDir, pkgRename), pkg.path)

if (success) {
const filenames = await glob(`${absoluteDualOutDir}/**/*{.js,.d.ts}`, {
ignore: 'node_modules/**',
Expand Down
13 changes: 12 additions & 1 deletion test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,22 @@ describe('duel', () => {

it('reports compilation errors during a build', async t => {
const spy = t.mock.method(global.console, 'log')
const spyExit = t.mock.method(process, 'exit')

t.after(async () => {
await rmDist(errDist)
})
await duel(['-p', 'test/__fixtures__/compileErrors/tsconfig.json'])
spyExit.mock.mockImplementation(number => {
throw new Error(`Mocked process.exit: ${number}`)
})
await assert.rejects(
async () => {
await duel(['-p', 'test/__fixtures__/compileErrors/tsconfig.json'])
},
{ message: /Mocked process\.exit/ },
)

assert.ok(spyExit.mock.calls[0].arguments > 0)
assert.equal(spy.mock.calls[1].arguments[1], 'Compilation errors found.')
})

Expand Down
Loading