diff --git a/packages/koot/libs/safeguard.js b/packages/koot/libs/safeguard.js deleted file mode 100644 index 6f1374787..000000000 --- a/packages/koot/libs/safeguard.js +++ /dev/null @@ -1,71 +0,0 @@ -/* eslint-disable no-console */ - -/** - * @module - * Safeguard mechanism for koot commands - */ - -const resolve = require('resolve'); -const log = require('./log'); -const __ = require('../utils/translate'); -const getCwd = require('../utils/get-cwd'); -const getAppType = require('../utils/get-app-type'); -const getAppTypeString = require('../utils/get-app-type-string'); -const envUpdateAppType = require('./env/update-app-type'); - -/** - * Safeguard mechanism for koot commands. If error occurs, throw Error - * @async - * @void - */ -const safeguard = async (kootConfig = {}) => { - // ======================================================================== - // - // Ensure App Type related env - // - // ======================================================================== - const appType = await getAppType(); - if (!appType) envUpdateAppType(getAppTypeString(kootConfig.type)); - if (!(await getAppType())) { - console.log(' '); - log('error', __('safeguard.INVALID_CONFIG', { key: 'type' })); - console.log(' '); - console.log(' '); - throw new Error('INVALID_CONFIG:type'); - } - - // ======================================================================== - // - // Node.js - // - // ======================================================================== - // TODO 判断 node 版本 - - // ======================================================================== - // - // Electron - // - // ======================================================================== - const isElectronApp = - kootConfig.target === 'electron' || - process.env.KOOT_BUILD_TARGET === 'electron'; - if (isElectronApp) { - await new Promise((r, reject) => { - resolve('koot-electron', { basedir: getCwd() }, (err, res) => { - if (err) reject(err); - else r(res); - }); - }).catch((err) => { - if (err.code === 'MODULE_NOT_FOUND') { - err.message = 'NEED_PACKAGE_KOOT_ELECTRON'; - console.log(' '); - log('error', __('safeguard.NEED_PACKAGE_KOOT_ELECTRON')); - console.log(' '); - console.log(' '); - } - throw err; - }); - } -}; - -module.exports = safeguard; diff --git a/packages/koot/libs/safeguard/app-type.js b/packages/koot/libs/safeguard/app-type.js new file mode 100644 index 000000000..230477708 --- /dev/null +++ b/packages/koot/libs/safeguard/app-type.js @@ -0,0 +1,26 @@ +/* eslint-disable no-console */ + +require('../../typedef'); + +const logError = require('./libs/log-error'); +const __ = require('../../utils/translate'); +const getAppType = require('../../utils/get-app-type'); +const getAppTypeString = require('../../utils/get-app-type-string'); +const envUpdateAppType = require('../env/update-app-type'); + +/** + * Safeguard: App Type + * - Ensure environment variables updated: `WEBPACK_BUILD_TYPE` `KOOT_PROJECT_TYPE` `KOOT_BUILD_TARGET` + * - If cannot get a valid app type, throw an error + * @async + * @param {AppConfig} appConfig + * @void + */ +module.exports = async (appConfig = {}) => { + const appType = await getAppType(); + if (!appType) envUpdateAppType(getAppTypeString(appConfig.type)); + if (!(await getAppType())) { + logError(__('safeguard.INVALID_CONFIG', { key: 'type' })); + throw new Error('INVALID_CONFIG:type'); + } +}; diff --git a/packages/koot/libs/safeguard/electron.js b/packages/koot/libs/safeguard/electron.js new file mode 100644 index 000000000..87ced08be --- /dev/null +++ b/packages/koot/libs/safeguard/electron.js @@ -0,0 +1,36 @@ +/* eslint-disable no-console */ + +require('../../typedef'); + +const resolve = require('resolve'); + +const logError = require('./libs/log-error'); +const __ = require('../../utils/translate'); +const getCwd = require('../../utils/get-cwd'); + +/** + * Safeguard: Electron + * - If module `koot-electron` not found, throw an error + * @async + * @param {AppConfig} appConfig + * @void + */ +module.exports = async (appConfig = {}) => { + const isElectronApp = + appConfig.target === 'electron' || + process.env.KOOT_BUILD_TARGET === 'electron'; + if (isElectronApp) { + await new Promise((r, reject) => { + resolve('koot-electron', { basedir: getCwd() }, (err, res) => { + if (err) reject(err); + else r(res); + }); + }).catch((err) => { + if (err.code === 'MODULE_NOT_FOUND') { + err.message = 'NEED_PACKAGE_KOOT_ELECTRON'; + logError(__('safeguard.NEED_PACKAGE_KOOT_ELECTRON')); + } + throw err; + }); + } +}; diff --git a/packages/koot/libs/safeguard/index.js b/packages/koot/libs/safeguard/index.js new file mode 100644 index 000000000..16f2216dc --- /dev/null +++ b/packages/koot/libs/safeguard/index.js @@ -0,0 +1,28 @@ +/* eslint-disable no-console */ + +/** + * @module + * Safeguard mechanism for koot commands + */ + +require('../../typedef'); + +const safeNodejs = require('./nodejs'); +const safeAppType = require('./app-type'); +const safeElectron = require('./electron'); + +// ============================================================================ + +/** + * Safeguard mechanism for koot commands. If error occurs, throw Error + * @async + * @param {AppConfig} appConfig + * @void + */ +const safeguard = async (appConfig = {}) => { + await safeNodejs(appConfig); + await safeAppType(appConfig); + await safeElectron(appConfig); +}; + +module.exports = safeguard; diff --git a/packages/koot/libs/safeguard/libs/log-error.js b/packages/koot/libs/safeguard/libs/log-error.js new file mode 100644 index 000000000..cc16f2136 --- /dev/null +++ b/packages/koot/libs/safeguard/libs/log-error.js @@ -0,0 +1,10 @@ +/* eslint-disable no-console */ + +const log = require('../../log'); + +module.exports = (msg) => { + console.log(' '); + log('error', msg); + console.log(' '); + console.log(' '); +}; diff --git a/packages/koot/libs/safeguard/nodejs.js b/packages/koot/libs/safeguard/nodejs.js new file mode 100644 index 000000000..2433a6718 --- /dev/null +++ b/packages/koot/libs/safeguard/nodejs.js @@ -0,0 +1,31 @@ +/* eslint-disable no-console */ + +require('../../typedef'); + +const semver = require('semver'); + +const logError = require('./libs/log-error'); +const __ = require('../../utils/translate'); + +const kootPackage = require('../../package.json'); + +/** + * Safeguard: Node.js + * - If _Node.js_ installed version dosenot meet requirement, throw an error + * @async + * @param {AppConfig} appConfig + * @void + */ +module.exports = async (appConfig = {}) => { + const { engines: { node: nodeRequired } = {} } = kootPackage; + const nodeLocal = semver.valid(semver.coerce(process.version)); + if (!semver.satisfies(nodeLocal, nodeRequired)) { + logError( + __('safeguard.NODE_LOCAL_VERSION_NOT_SATISFIED', { + require: nodeRequired, + local: nodeLocal, + }) + ); + throw new Error('NODE_LOCAL_VERSION_NOT_SATISFIED'); + } +}; diff --git a/packages/koot/locales/en.json b/packages/koot/locales/en.json index 10659f766..a44cc42a4 100644 --- a/packages/koot/locales/en.json +++ b/packages/koot/locales/en.json @@ -8,7 +8,8 @@ }, "safeguard": { "INVALID_CONFIG": "Invalid config: ${key}", - "NEED_PACKAGE_KOOT_ELECTRON": "Missing dependency: 'koot-electron'. Please install using 'npm install' or 'yarn add' command." + "NEED_PACKAGE_KOOT_ELECTRON": "Missing dependency: 'koot-electron'. Please install using 'npm install' or 'yarn add' command.", + "NODE_LOCAL_VERSION_NOT_SATISFIED": "Node.js on this system is not valid. Require: ${require} | Local: ${local}" }, "validateConfig": { "error": { diff --git a/packages/koot/locales/zh_cn.json b/packages/koot/locales/zh_cn.json index 13df3b2e6..2829466ca 100644 --- a/packages/koot/locales/zh_cn.json +++ b/packages/koot/locales/zh_cn.json @@ -8,7 +8,8 @@ }, "safeguard": { "INVALID_CONFIG": "无效的配置: ${key}", - "NEED_PACKAGE_KOOT_ELECTRON": "缺少依赖包: 'koot-electron'。请使用 'npm install' 或 'yarn add' 进行安装。" + "NEED_PACKAGE_KOOT_ELECTRON": "缺少依赖包: 'koot-electron'。请使用 'npm install' 或 'yarn add' 进行安装。", + "NODE_LOCAL_VERSION_NOT_SATISFIED": "系统 Node.js 版本不符合要求。要求: ${require} | 本地系统: ${local}" }, "validateConfig": { "error": {