Skip to content

Commit

Permalink
exit with a status code of 1 if a task fails. (#24)
Browse files Browse the repository at this point in the history
- update target so we can use `new Error('', {cause: })`
- allow us to use "allSettled" so all tests finish
- added new `fails` test case... not verified
  • Loading branch information
fromkeith authored Oct 8, 2024
1 parent 8ef1ec1 commit 4711be3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
14 changes: 11 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import { Is } from './utils'
import { getGlobalTaskManager } from './task-manager'
import chalk from 'chalk'
import { initDefaultCli } from './default-cli'
import { spawn } from 'child_process'
import { spawn, ChildProcess } from 'child_process'

const { foyFiles, registers, defaultCli } = initDefaultCli()
async function main() {
const pkg = await fs.readJson('./package.json')
const isESM = pkg.type === 'module'
const deps = { ...pkg.dependencies, ...pkg.devDependencies }
const results: ChildProcess[] = [];
for (const foyFile of foyFiles) {
let executor = defaultCli.options.executor
if (!executor) {
Expand Down Expand Up @@ -57,18 +58,25 @@ async function main() {
NODE_OPTIONS += ' ' + inspect
}
})
spawn(executor, args, {
results.push(spawn(executor, args, {
stdio: 'inherit',
shell: true,
cwd: process.cwd(),
env:{
...process.env,
NODE_OPTIONS,
}
})
}));
}
for (const p of results) {
await new Promise<void>((resolve) => p.on('exit', () => resolve()))
if (p.exitCode !== 0 && p.exitCode !== null) {
process.exitCode = p.exitCode;
}
}
}

main().catch((err) => {
console.error(err)
process.exitCode = 1;
})
1 change: 1 addition & 0 deletions src/task-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export class TaskManager {
} catch (e) {
logger.error(e)
await this.runListner('onerror', t.namespaces, [e, t])
throw Error('Task Failed', {cause: e})
} finally {
await this.runListner('after', t.namespaces, [t])
if (loading) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/fixtures/Foyfile1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ task('pushpopd', async ctx => {
ctx.popd()
ctx.log(`popd 2 works`, ctx.cwd === resolve(pwd))
})

task('fails', async ctx => {
logger.info('Fail this task')
await ctx.exec('node -e "console.log(\\"start\\"); process.exit(1)"')
logger.info('This line not hit')
});
5 changes: 5 additions & 0 deletions src/test/fixtures/snaps/Foyfile1_ts_fails
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DependencyGraph for task [fails]:
- fails

[info] Fail this task
start
14 changes: 9 additions & 5 deletions src/test/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ function normal(s: string) {
s = s.replace(/.pnpm\/[^\/]+\/node_modules/,'')
return s
}
function test(cmd: string) {
function test(cmd: string, expectedExitCode: number = 0) {
let out = 'Not initialized'
let snap = ''
let exitCode = 0;
return {
name: cmd,
it() {
it(cmd, () => {
expect(normal(out)).toBe(normal(snap))
expect(exitCode).toEqual(expectedExitCode)
})
},
async init() {
let p = await exec(`ts-node ./src/cli.ts --config ${fixturesDir}/${cmd}`).catch(er => er)
out = normal(p.stdout + p.stderr)
let p = await exec(`ts-node ./src/cli.ts --config ${fixturesDir}/${cmd}`, {all: true}).catch(er => er);
exitCode = p.exitCode;
out = normal(p.all ?? '')
let snapFile = snapsDir + '/' + cmd.replace(/[^\w-]/g, '_')
if (UpdateSnap) {
// tslint:disable-next-line:no-floating-promises
Expand Down Expand Up @@ -62,9 +65,10 @@ describe('task', function () {
test(`Foyfile1.ts async:priority`),
test(`Foyfile1.ts resolveOptions -c 123`),
test(`Foyfile1.ts resolveOptions`),
test(`Foyfile1.ts fails`, 1),
test(`Foyfile1.ts pushpopd`),
test(`Foyfile2.ts start`),
test(`Foyfile2.ts error`),
test(`Foyfile2.ts error`, 1),
test(`Foyfile2.ts ns1:t1`),
test(`Foyfile2.ts ns1:error`),
test(`Foyfile2.ts ns1:ns2:t2`),
Expand All @@ -74,7 +78,7 @@ describe('task', function () {
if (UpdateSnap) {
await fs.rmrf(snapsDir)
}
await Promise.all(tests.map(t => t.init())).catch(logger.error)
await Promise.allSettled(tests.map(t => t.init())).catch(logger.error)
console.log('init')
}, 600 * 1000)
tests.forEach(t => t.it())
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es2015'. */
"moduleResolution": "node",
"lib": ["es2015"], /* Specify library files to be included in the compilation: */
"lib": ["es2022"], /* Specify library files to be included in the compilation: */
"allowJs": false, /* Allow javascript files to be compiled. */
"checkJs": false, /* Report errors in .js files. */
"jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
Expand Down

0 comments on commit 4711be3

Please sign in to comment.