Skip to content

Commit

Permalink
feat(core): implement #274
Browse files Browse the repository at this point in the history
  • Loading branch information
Diablohu committed Mar 4, 2021
1 parent 486039d commit 0bbc83e
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 73 deletions.
71 changes: 0 additions & 71 deletions packages/koot/libs/safeguard.js

This file was deleted.

26 changes: 26 additions & 0 deletions packages/koot/libs/safeguard/app-type.js
Original file line number Diff line number Diff line change
@@ -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');
}
};
36 changes: 36 additions & 0 deletions packages/koot/libs/safeguard/electron.js
Original file line number Diff line number Diff line change
@@ -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;
});
}
};
28 changes: 28 additions & 0 deletions packages/koot/libs/safeguard/index.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions packages/koot/libs/safeguard/libs/log-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable no-console */

const log = require('../../log');

module.exports = (msg) => {
console.log(' ');
log('error', msg);
console.log(' ');
console.log(' ');
};
31 changes: 31 additions & 0 deletions packages/koot/libs/safeguard/nodejs.js
Original file line number Diff line number Diff line change
@@ -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');
}
};
3 changes: 2 additions & 1 deletion packages/koot/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion packages/koot/locales/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 0bbc83e

Please sign in to comment.