Skip to content
This repository has been archived by the owner on Nov 12, 2021. It is now read-only.

Commit

Permalink
fix: sync terminate + detached worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Oct 22, 2019
1 parent b3ca562 commit eaeb190
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 51 deletions.
44 changes: 44 additions & 0 deletions src/bin/child-mono-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env node

const { cac } = require('cac')
const { monorepoRun } = require('../')
const consola = require('consola')
const chalk = require('chalk')
const pkg = require('../../package.json')

const cli = cac()

cli.option('--patterns <patterns>', 'Folder glob patterns (by default will take yarn workspaces)')

cli.option('--stream [throttle]', 'Stream output directly instead of waiting for the end. You can also throttle (ms) the output when streaming is enabled.', {
default: false,
})

cli.help()
cli.version(pkg.version)

cli.command('<script>', 'Run a script in the monorepo packages')
.action(async (script, options) => {
if (options.stream && !isNaN(parseInt(options.stream))) {
options.stream = parseInt(options.stream)
}
if (options.patterns) {
if (options.patterns.startsWith('[')) {
options.patterns = JSON.parse(options.patterns)
} else {
options.patterns = options.patterns.split(',')
}
}
try {
const time = Date.now()
const { folders } = await monorepoRun(script, options.patterns, null, options.stream)
consola.success(`Completed ${script} (${Math.round((Date.now() - time) / 10) / 100}s) in:`)
consola.log(chalk.green(folders.join('\n')))
process.exit()
} catch (e) {
consola.error(e)
process.exit(1)
}
})

cli.parse()
50 changes: 11 additions & 39 deletions src/bin/mono-run.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,16 @@
#!/usr/bin/env node

const { cac } = require('cac')
const { monorepoRun } = require('../')
const consola = require('consola')
const chalk = require('chalk')
const pkg = require('../../package.json')
const cp = require('child_process')
const path = require('path')
const nodeCleanup = require('node-cleanup')
const { terminate } = require('../util/terminate')

const cli = cac()

cli.option('--patterns <patterns>', 'Folder glob patterns (by default will take yarn workspaces)')

cli.option('--stream [throttle]', 'Stream output directly instead of waiting for the end. You can also throttle (ms) the output when streaming is enabled.', {
default: false,
const child = cp.spawn(path.join(__dirname, 'child-mono-run.js'), process.argv.slice(2), {
stdio: 'inherit',
cwd: process.cwd(),
detached: true,
})

cli.help()
cli.version(pkg.version)

cli.command('<script>', 'Run a script in the monorepo packages')
.action(async (script, options) => {
if (options.stream && !isNaN(parseInt(options.stream))) {
options.stream = parseInt(options.stream)
}
if (options.patterns) {
if (options.patterns.startsWith('[')) {
options.patterns = JSON.parse(options.patterns)
} else {
options.patterns = options.patterns.split(',')
}
}
try {
const time = Date.now()
const { folders } = await monorepoRun(script, options.patterns, null, options.stream)
consola.success(`Completed ${script} (${Math.round((Date.now() - time) / 10) / 100}s) in:`)
consola.log(chalk.green(folders.join('\n')))
process.exit()
} catch (e) {
consola.error(e)
process.exit(1)
}
})

cli.parse()
nodeCleanup(() => {
terminate(child, process.cwd())
})
5 changes: 0 additions & 5 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const path = require('path')
const pty = require('node-pty')
const chalk = require('chalk')
const consola = require('consola')
const nodeCleanup = require('node-cleanup')
const { terminate } = require('./util/terminate')

/** @typedef {import('node-pty').IPty} IPty */
Expand Down Expand Up @@ -148,7 +147,3 @@ exports.killAll = () => {
}
children.clear()
}

nodeCleanup(() => {
exports.killAll()
})
11 changes: 4 additions & 7 deletions src/util/terminate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const util = require('util')
const cp = require('child_process')
const path = require('path')
const {
Expand All @@ -7,10 +6,7 @@ const {
isMacintosh,
} = require('./platform')

const execFile = util.promisify(cp.execFile)
const spawn = util.promisify(cp.spawn)

exports.terminate = async function (childProcess, cwd) {
exports.terminate = function (childProcess, cwd) {
if (isWindows) {
try {
let options = {
Expand All @@ -19,14 +15,15 @@ exports.terminate = async function (childProcess, cwd) {
if (cwd) {
options.cwd = cwd
}
await execFile('taskkill', ['/T', '/F', '/PID', childProcess.pid.toString()], options)
cp.execFileSync('taskkill', ['/T', '/F', '/PID', childProcess.pid.toString()], options)
} catch (err) {
return { success: false, error: err }
}
} else if (isLinux || isMacintosh) {
try {
let cmd = path.resolve(__dirname, './terminate.sh')
let result = await spawn(cmd, [childProcess.pid.toString()], {
let result = cp.spawnSync(cmd, [childProcess.pid.toString()], {
stdio: 'inherit',
cwd,
})
if (result.error) {
Expand Down

0 comments on commit eaeb190

Please sign in to comment.