Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. const { spawn } = require('child_process')
  2. const { existsSync, readdirSync, lstatSync, unlinkSync, rmdirSync } = require('fs')
  3.  
  4. const utils = {
  5. /**
  6. * Convert an options object into a valid arguments array for the child_process.spawn method
  7. * from:
  8. * var options = {
  9. * foo: 'hello',
  10. * baz: 'world'
  11. * }
  12. * to:
  13. * ['--foo=', 'hello', '--baz=','world']
  14. *
  15. * @param { Object } obj - object we need to convert
  16. * @param { Array } optionsPrefix - use a prefix for the new array created
  17. * @param { Boolean } hasEquals - set the options commands using the equal
  18. * @returns { Array } - options array
  19. */
  20. optionsToArray(obj, optionsPrefix, hasEquals) {
  21. optionsPrefix = optionsPrefix || '--'
  22. const ret = []
  23. Object.keys(obj).forEach((key) => {
  24. ret.push(optionsPrefix + key + (hasEquals ? '=' : ''))
  25. if (obj[key]) ret.push(obj[key])
  26. })
  27. return ret
  28. },
  29.  
  30. /**
  31. * Simple object extend function
  32. * @param { Object } desintation object - destination
  33. * @param { Object } source object - source
  34. * @returns { Object } - destination object
  35. */
  36. extend(destination, source) {
  37. Object.keys(source).forEach(key => {
  38. if (source.hasOwnProperty(key)) destination[key] = source[key]
  39. })
  40. return destination
  41. },
  42.  
  43. /**
  44. * Run system command
  45. * @param { String } command - command to execute
  46. * @param { Array } args - command arguments
  47. * @param { Object } envVariables - command environment variables
  48. * @returns { Promise } chainable promise object
  49. */
  50. exec(command, args, envVariables) {
  51. const { normalize } = require('path')
  52. const { platform } = require('os')
  53.  
  54. return new Promise((resolve, reject) => {
  55. if (platform() === 'win32' || platform() === 'win64') command += '.cmd'
  56.  
  57. utils.extend(process.env, envVariables)
  58. utils.print(`Executing: ${command} ${args.join(' ')} \n`, 'confirm')
  59.  
  60. const cmd = spawn(normalize(command), args, {
  61. stdio: 'inherit',
  62. cwd: process.cwd()
  63. })
  64.  
  65. cmd.on('exit', code => {
  66. if (code === 1)
  67. reject()
  68. else
  69. resolve()
  70. })
  71. })
  72. },
  73.  
  74. /**
  75. * Read all the files crawling starting from a certain folder path
  76. * @param { String } path directory path
  77. * @param { bool } mustDelete delete the files found
  78. * @returns { Array } files path list
  79. */
  80. listFiles(path, mustDelete) {
  81. utils.print(`Listing all the files in the folder: ${path}`, 'confirm')
  82. const files = []
  83. if (existsSync(path)) {
  84. const tmpFiles = readdirSync(path)
  85. tmpFiles.forEach((file) => {
  86. const curPath = path + '/' + file
  87. files.push(curPath)
  88. if (lstatSync(curPath).isDirectory()) { // recurse
  89. utils.listFiles(curPath, mustDelete)
  90. } else if (mustDelete) { // delete file
  91. unlinkSync(curPath)
  92. }
  93. })
  94. if (mustDelete) {
  95. rmdirSync(path)
  96. }
  97. }
  98.  
  99. return files
  100. },
  101.  
  102. /**
  103. * Delete synchronously any folder or file
  104. * @param { String } path - path to clean
  105. */
  106. clean(path) {
  107. const files = utils.listFiles(path, true)
  108. utils.print(`Deleting the following files: \n ${files.join('\n')}`, 'cool')
  109. },
  110.  
  111. /**
  112. * Log messages in the terminal using custom colors
  113. * @param { String } msg - message to output
  114. * @param { String } type - message type to handle the right color
  115. */
  116. print(msg, type) {
  117. let color
  118. switch (type) {
  119. case 'error':
  120. color = '\x1B[31m'
  121. break
  122. case 'warning':
  123. color = '\x1B[33m'
  124. break
  125. case 'confirm':
  126. color = '\x1B[32m'
  127. break
  128. case 'cool':
  129. color = '\x1B[36m'
  130. break
  131. default:
  132. color = ''
  133. }
  134. console.log(`${color} ${msg} \x1B[39m`)
  135. }
  136. }
  137.  
  138. module.exports = utils;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement