|
| 1 | +/** |
| 2 | + Copyright (c) 2015, 2019, Oracle and/or its affiliates. |
| 3 | + The Universal Permissive License (UPL), Version 1.0 |
| 4 | +*/ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const CONSTANTS = require('../util/constants'); |
| 8 | +const fs = require('fs'); |
| 9 | +const path = require('path'); |
| 10 | + |
| 11 | +module.exports = { |
| 12 | + runAfterAppCreateHook() { |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + // Get hooks config |
| 15 | + const hooksConfig = _getHooksConfigObj(); |
| 16 | + // Get after_app_prepare hook's path |
| 17 | + const hookPath = hooksConfig.after_app_create; |
| 18 | + if (hookPath && fs.existsSync(path.resolve(hookPath))) { |
| 19 | + const hook = require(path.resolve(hookPath)); // eslint-disable-line |
| 20 | + // Execute hook |
| 21 | + hook() |
| 22 | + .then(() => resolve()) |
| 23 | + .catch(err => reject(err)); |
| 24 | + } else { |
| 25 | + console.warn('Hook \'after_app_create\' not defined.'); |
| 26 | + resolve(); |
| 27 | + } |
| 28 | + }); |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * ## _getHooksConfigObj |
| 34 | + * Reads the hooks.json file |
| 35 | + * |
| 36 | + * @private |
| 37 | + */ |
| 38 | +function _getHooksConfigObj() { |
| 39 | + const configFilePath = path.resolve(CONSTANTS.PATH_TO_HOOKS_CONFIG); |
| 40 | + if (fs.existsSync(configFilePath)) { |
| 41 | + const configFileContent = fs.readFileSync(configFilePath, 'utf8'); |
| 42 | + let configFileContentAsJson = {}; |
| 43 | + try { |
| 44 | + configFileContentAsJson = JSON.parse(configFileContent); |
| 45 | + // If came to here, then valid json |
| 46 | + } catch (e) { |
| 47 | + console.error(`\x1b[31mError: File '${configFilePath}' is not of type 'json'.\x1b[0m`); |
| 48 | + process.exit(1); |
| 49 | + } |
| 50 | + return configFileContentAsJson.hooks || {}; |
| 51 | + } |
| 52 | + return {}; |
| 53 | +} |
0 commit comments