shell.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const { spawn, spawnSync, exec, execFile } = require('child_process')
  2. function betterExec(cmd, args, options = {}, callback) {
  3. let childProcess
  4. if (options.live) {
  5. childProcess = spawn(cmd, args, {
  6. shell: args == null,
  7. stdio: 'inherit',
  8. ...options
  9. })
  10. childProcess.on('close', function(code) {
  11. callback(null, { success: code === 0 })
  12. })
  13. } else if (args == null) { // exec in a shell
  14. childProcess = exec(cmd, {
  15. encoding: 'utf8',
  16. ...options
  17. }, function(error, stdout, stderr) {
  18. callback(error, { stdout, stderr, success: !error })
  19. })
  20. } else {
  21. childProcess = execFile(cmd, args, {
  22. encoding: 'utf8',
  23. ...options
  24. }, function(error, stdout, stderr) {
  25. callback(error, { stdout, stderr, success: !error })
  26. })
  27. }
  28. return childProcess
  29. }
  30. function betterExecSync(cmd, args, options = {}) {
  31. let res = spawnSync(cmd, args, {
  32. encoding: 'utf8',
  33. shell: args == null,
  34. stdio: options.live ? 'inherit' : 'pipe',
  35. ...options
  36. })
  37. return { ...res, success: res.status === 0 }
  38. }
  39. betterExec.sync = betterExecSync
  40. exports.exec = betterExec
  41. /* parsing workspaces
  42. const PROJECT_ROOT = path.resolve(__dirname, '..')
  43. let res = exec('yarn', [ 'workspaces', '--json', 'info' ], { // order of args matters
  44. cwd: PROJECT_ROOT
  45. })
  46. if (!res.success) {
  47. console.error(res.stderr)
  48. process.exit(1)
  49. } else {
  50. let workspaces
  51. try {
  52. let wrapper = JSON.parse(res.stdout)
  53. workspaces = JSON.parse(wrapper.data)
  54. } catch(err) {
  55. console.error('Couldn\'t parse JSON')
  56. process.exit(1)
  57. }
  58. console.log(workspaces)
  59. }
  60. */