Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. 'use strict';
  2. const path = require('path');
  3. const childProcess = require('child_process');
  4. const crossSpawn = require('cross-spawn');
  5. const stripFinalNewline = require('strip-final-newline');
  6. const npmRunPath = require('npm-run-path');
  7. const onetime = require('onetime');
  8. const makeError = require('./lib/error');
  9. const normalizeStdio = require('./lib/stdio');
  10. const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler} = require('./lib/kill');
  11. const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream.js');
  12. const {mergePromise, getSpawnedPromise} = require('./lib/promise.js');
  13. const {joinCommand, parseCommand} = require('./lib/command.js');
  14. const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;
  15. const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) => {
  16. const env = extendEnv ? {...process.env, ...envOption} : envOption;
  17. if (preferLocal) {
  18. return npmRunPath.env({env, cwd: localDir, execPath});
  19. }
  20. return env;
  21. };
  22. const handleArguments = (file, args, options = {}) => {
  23. const parsed = crossSpawn._parse(file, args, options);
  24. file = parsed.command;
  25. args = parsed.args;
  26. options = parsed.options;
  27. options = {
  28. maxBuffer: DEFAULT_MAX_BUFFER,
  29. buffer: true,
  30. stripFinalNewline: true,
  31. extendEnv: true,
  32. preferLocal: false,
  33. localDir: options.cwd || process.cwd(),
  34. execPath: process.execPath,
  35. encoding: 'utf8',
  36. reject: true,
  37. cleanup: true,
  38. all: false,
  39. windowsHide: true,
  40. ...options
  41. };
  42. options.env = getEnv(options);
  43. options.stdio = normalizeStdio(options);
  44. if (process.platform === 'win32' && path.basename(file, '.exe') === 'cmd') {
  45. // #116
  46. args.unshift('/q');
  47. }
  48. return {file, args, options, parsed};
  49. };
  50. const handleOutput = (options, value, error) => {
  51. if (typeof value !== 'string' && !Buffer.isBuffer(value)) {
  52. // When `execa.sync()` errors, we normalize it to '' to mimic `execa()`
  53. return error === undefined ? undefined : '';
  54. }
  55. if (options.stripFinalNewline) {
  56. return stripFinalNewline(value);
  57. }
  58. return value;
  59. };
  60. const execa = (file, args, options) => {
  61. const parsed = handleArguments(file, args, options);
  62. const command = joinCommand(file, args);
  63. let spawned;
  64. try {
  65. spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);
  66. } catch (error) {
  67. // Ensure the returned error is always both a promise and a child process
  68. const dummySpawned = new childProcess.ChildProcess();
  69. const errorPromise = Promise.reject(makeError({
  70. error,
  71. stdout: '',
  72. stderr: '',
  73. all: '',
  74. command,
  75. parsed,
  76. timedOut: false,
  77. isCanceled: false,
  78. killed: false
  79. }));
  80. return mergePromise(dummySpawned, errorPromise);
  81. }
  82. const spawnedPromise = getSpawnedPromise(spawned);
  83. const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);
  84. const processDone = setExitHandler(spawned, parsed.options, timedPromise);
  85. const context = {isCanceled: false};
  86. spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));
  87. spawned.cancel = spawnedCancel.bind(null, spawned, context);
  88. const handlePromise = async () => {
  89. const [{error, exitCode, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, processDone);
  90. const stdout = handleOutput(parsed.options, stdoutResult);
  91. const stderr = handleOutput(parsed.options, stderrResult);
  92. const all = handleOutput(parsed.options, allResult);
  93. if (error || exitCode !== 0 || signal !== null) {
  94. const returnedError = makeError({
  95. error,
  96. exitCode,
  97. signal,
  98. stdout,
  99. stderr,
  100. all,
  101. command,
  102. parsed,
  103. timedOut,
  104. isCanceled: context.isCanceled,
  105. killed: spawned.killed
  106. });
  107. if (!parsed.options.reject) {
  108. return returnedError;
  109. }
  110. throw returnedError;
  111. }
  112. return {
  113. command,
  114. exitCode: 0,
  115. stdout,
  116. stderr,
  117. all,
  118. failed: false,
  119. timedOut: false,
  120. isCanceled: false,
  121. killed: false
  122. };
  123. };
  124. const handlePromiseOnce = onetime(handlePromise);
  125. crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);
  126. handleInput(spawned, parsed.options.input);
  127. spawned.all = makeAllStream(spawned, parsed.options);
  128. return mergePromise(spawned, handlePromiseOnce);
  129. };
  130. module.exports = execa;
  131. module.exports.sync = (file, args, options) => {
  132. const parsed = handleArguments(file, args, options);
  133. const command = joinCommand(file, args);
  134. validateInputSync(parsed.options);
  135. let result;
  136. try {
  137. result = childProcess.spawnSync(parsed.file, parsed.args, parsed.options);
  138. } catch (error) {
  139. throw makeError({
  140. error,
  141. stdout: '',
  142. stderr: '',
  143. all: '',
  144. command,
  145. parsed,
  146. timedOut: false,
  147. isCanceled: false,
  148. killed: false
  149. });
  150. }
  151. const stdout = handleOutput(parsed.options, result.stdout, result.error);
  152. const stderr = handleOutput(parsed.options, result.stderr, result.error);
  153. if (result.error || result.status !== 0 || result.signal !== null) {
  154. const error = makeError({
  155. stdout,
  156. stderr,
  157. error: result.error,
  158. signal: result.signal,
  159. exitCode: result.status,
  160. command,
  161. parsed,
  162. timedOut: result.error && result.error.code === 'ETIMEDOUT',
  163. isCanceled: false,
  164. killed: result.signal !== null
  165. });
  166. if (!parsed.options.reject) {
  167. return error;
  168. }
  169. throw error;
  170. }
  171. return {
  172. command,
  173. exitCode: 0,
  174. stdout,
  175. stderr,
  176. failed: false,
  177. timedOut: false,
  178. isCanceled: false,
  179. killed: false
  180. };
  181. };
  182. module.exports.command = (command, options) => {
  183. const [file, ...args] = parseCommand(command);
  184. return execa(file, args, options);
  185. };
  186. module.exports.commandSync = (command, options) => {
  187. const [file, ...args] = parseCommand(command);
  188. return execa.sync(file, args, options);
  189. };
  190. module.exports.node = (scriptPath, args, options = {}) => {
  191. if (args && !Array.isArray(args) && typeof args === 'object') {
  192. options = args;
  193. args = [];
  194. }
  195. const stdio = normalizeStdio.node(options);
  196. const defaultExecArgv = process.execArgv.filter(arg => !arg.startsWith('--inspect'));
  197. const {
  198. nodePath = process.execPath,
  199. nodeOptions = defaultExecArgv
  200. } = options;
  201. return execa(
  202. nodePath,
  203. [
  204. ...nodeOptions,
  205. scriptPath,
  206. ...(Array.isArray(args) ? args : [])
  207. ],
  208. {
  209. ...options,
  210. stdin: undefined,
  211. stdout: undefined,
  212. stderr: undefined,
  213. stdio,
  214. shell: false
  215. }
  216. );
  217. };