redhot
Version:
TypeScript Monorepo Management
36 lines (28 loc) • 1.21 kB
JavaScript
const { join } = require('path')
const { exec, logSeparator, isInitialized } = require('../util')
exports.exec = function exec (program, config, workingDir) {
program
.command('exec [command...]')
.option('-o, --only <packageName>', 'Run a command in a single package')
.description('execute a command in all managed packages')
.action((command, options) => executeCommand(config, workingDir, command, options))
}
function executeCommand (config, workingDir, command, options) {
isInitialized(config)
const cmd = command.join(' ')
const packages = options.only
? config.packages.filter(p => p === options.only)
: config.packages
if (packages.length === 0) {
if (options.only) {
return console.log('Cannot find package ' + options.only)
}
return console.log('Cannot find any packages to test :(')
}
packages.forEach(function (packageName) {
const packageDir = join(workingDir, packageName)
exec(cmd, { silent: true, cwd: packageDir }, () => logSeparator(packageName) || console.log('\n ' + cmd))
.then(([out]) => console.log(' ' + out) || logSeparator())
.catch(([, err]) => console.log(' ' + err) || logSeparator())
})
}