Advertisement
AtomicOs

util.js

May 28th, 2021
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Log = require('./logging')
  2. const { spawnSync } = require('child_process')
  3.  
  4. const getNPMConfig = (path) => {
  5.   const key = path.join('_').replace('-', '_')
  6.   const npm_prefix = 'npm_config_'
  7.   const package_config_prefix = 'npm_package_config_'
  8.   const package_prefix = 'npm_package_'
  9.   return process.env[npm_prefix + key] ||
  10.     process.env[package_config_prefix + key] ||
  11.     process.env[package_prefix + key]
  12. }
  13.  
  14. const getProjectVersion = (projectName) => {
  15.   return getNPMConfig(['projects', projectName, 'tag']) || getNPMConfig(['projects', projectName, 'branch'])
  16. }
  17.  
  18. const run = (cmd, args = [], options = {}) => {
  19.   const { continueOnFail, ...cmdOptions } = options
  20.   Log.command(cmdOptions.cwd, cmd, args)
  21.   const prog = spawnSync(cmd, args, cmdOptions)
  22.   if (prog.status !== 0) {
  23.     if (!continueOnFail) {
  24.       console.log(prog.stdout && prog.stdout.toString())
  25.       console.error(prog.stderr && prog.stderr.toString())
  26.       process.exit(1)
  27.     }
  28.   }
  29.   return prog
  30. }
  31.  
  32. const runGit = (repoPath, gitArgs, continueOnFail = false) => {
  33.   let prog = run('git', gitArgs, { cwd: repoPath, continueOnFail })
  34.  
  35.   if (prog.status !== 0) {
  36.     return null
  37.   } else {
  38.     return prog.stdout.toString().trim()
  39.   }
  40. }
  41.  
  42. module.exports = {
  43.   getNPMConfig,
  44.   getProjectVersion,
  45.   run,
  46.   runGit
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement