@abtnode/core 1.6.14 → 1.6.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,68 +1,23 @@
1
- const childProcess = require('child_process');
2
1
  const get = require('lodash/get');
3
2
  const camelCase = require('lodash/camelCase');
3
+ const runScript = require('@abtnode/util/lib/run-script');
4
4
 
5
5
  // eslint-disable-next-line global-require
6
6
  const logger = require('@abtnode/logger')(`${require('../../package.json').name}:blocklet:hooks`);
7
7
 
8
8
  const { getSafeEnv } = require('../util');
9
9
 
10
- const runAsync = ({ appDir, env, hook, progress = false }) => {
11
- const safeEnv = getSafeEnv(env);
12
- const child = childProcess.exec(hook, {
13
- cwd: appDir,
14
- env: safeEnv,
15
- stdio: 'inherit',
16
- });
17
-
18
- if (progress) {
19
- child.stdout.pipe(process.stdout);
20
- child.stderr.pipe(process.stderr);
21
- }
22
-
23
- return new Promise((resolve, reject) => {
24
- const errorMessages = [];
25
- let hasUnhandledRejection = false;
26
-
27
- child.stderr.on('data', (err) => {
28
- // Check if has unhandledRejection in childProcess
29
- // https://stackoverflow.com/questions/32784649/gracefully-handle-errors-in-child-processes-in-nodejs
30
- if (err.includes('UnhandledPromiseRejectionWarning')) {
31
- hasUnhandledRejection = true;
32
- }
33
- errorMessages.push(err);
34
- });
35
-
36
- child.on('exit', (code) => {
37
- if (errorMessages.length > 0) {
38
- if (code !== 0 || hasUnhandledRejection) {
39
- return reject(new Error(errorMessages.join('\r\n')));
40
- }
41
-
42
- if (!progress) {
43
- errorMessages.forEach((message) => process.stderr.write(message));
44
- }
45
- }
46
-
47
- return resolve();
48
- });
49
- });
50
- };
51
-
52
- /**
53
- * @param {*} args.did root blocklet did
54
- */
55
- const runUserHook = async (name, args) => {
56
- const { appDir, hooks, env, exitOnError = true, progress = false, notification, did } = args;
57
- const hook = get(hooks, `[${name}]`) || get(hooks, `[${camelCase(name)}]`);
10
+ const runUserHook = async (appId, hookName, args) => {
11
+ const { appDir, hooks, env, exitOnError = true, silent = false, notification, did } = args;
12
+ const hook = get(hooks, `[${hookName}]`) || get(hooks, `[${camelCase(hookName)}]`);
58
13
 
59
14
  try {
60
15
  if (!hook) {
61
16
  return;
62
17
  }
63
18
 
64
- logger.info(`run hook:${name}:`, { hook });
65
- await runAsync({ appDir, env, hook, progress });
19
+ logger.info(`run hook:${hookName}:`, { hook });
20
+ await runScript(hook, [appId, hookName].join(':'), { cwd: appDir, env: getSafeEnv(env), silent });
66
21
  } catch (error) {
67
22
  logger.error(`run ${hook} error:`, { error });
68
23
 
@@ -77,33 +32,31 @@ const runUserHook = async (name, args) => {
77
32
  }
78
33
 
79
34
  if (exitOnError) {
80
- throw new Error(`Run [${name}] failed: ${error.message}`);
35
+ throw new Error(`Run [${hookName}] failed: ${error.message}`);
81
36
  }
82
37
  }
83
38
  };
84
39
 
85
- const preDeploy = (...args) => runUserHook('pre-deploy', ...args);
86
- const preInstall = (...args) => runUserHook('pre-install', ...args);
87
- const postInstall = (...args) => runUserHook('post-install', ...args);
88
-
89
- const preStart = async (blocklet, option) => {
40
+ const preDeploy = (appId, ...args) => runUserHook(appId, 'pre-deploy', ...args);
41
+ const preInstall = (appId, ...args) => runUserHook(appId, 'pre-install', ...args);
42
+ const postInstall = (appId, ...args) => runUserHook(appId, 'post-install', ...args);
43
+ const preConfig = (appId, ...args) => runUserHook(appId, 'pre-config', ...args);
44
+ const preStart = async (blocklet, options) => {
90
45
  // check required environments
91
46
  let environments = get(blocklet, 'meta.environments', []);
92
47
  if (!Array.isArray(environments)) {
93
48
  environments = [environments];
94
49
  }
95
50
 
96
- const tmp = environments.filter((e) => e.required && option.env[e.name] === undefined).map((e) => e.name);
51
+ const tmp = environments.filter((e) => e.required && options.env[e.name] === undefined).map((e) => e.name);
97
52
  if (tmp.length > 0) {
98
53
  throw new Error(`Required environments is not set: ${tmp.join(',')}`);
99
54
  }
100
55
 
101
- return runUserHook('pre-start', option);
56
+ return runUserHook(blocklet.env.appId, 'pre-start', options);
102
57
  };
103
58
 
104
- const preUninstall = (...args) => runUserHook('pre-uninstall', ...args);
105
- const preStop = (...args) => runUserHook('pre-stop', ...args);
106
-
107
- const preConfig = (...args) => runUserHook('pre-config', ...args);
59
+ const preUninstall = (appId, ...args) => runUserHook(appId, 'pre-uninstall', ...args);
60
+ const preStop = (appId, ...args) => runUserHook(appId, 'pre-stop', ...args);
108
61
 
109
62
  module.exports = { preDeploy, preInstall, postInstall, preStart, preUninstall, preStop, preConfig };