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.

329 lines
9.0 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
error: true,
runCommand: true,
isScriptKeyExist: true,
formatLog: true,
getNpmClient: true,
formatEnv: true,
parseScripts: true
};
exports.formatLog = formatLog;
exports.getNpmClient = getNpmClient;
exports.formatEnv = formatEnv;
exports.parseScripts = parseScripts;
exports.isScriptKeyExist = exports.runCommand = exports.error = void 0;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _fs() {
const data = require("fs");
_fs = function _fs() {
return data;
};
return data;
}
function _child_process() {
const data = require("child_process");
_child_process = function _child_process() {
return data;
};
return data;
}
function _umi() {
const data = require("umi");
_umi = function _umi() {
return data;
};
return data;
}
var _enums = require("../core/enums");
var _stats = require("./stats");
Object.keys(_stats).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _stats[key];
}
});
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const spawn = _umi().utils.spawn;
const error = (msg, name = 'TaskError') => {
const err = new Error(msg);
err.name = name;
throw err;
};
exports.error = error;
const runCommand = (script, options = {}, ipc = false) => {
options.env = _objectSpread(_objectSpread(_objectSpread({}, process.env), options.env), {}, {
FORCE_COLOR: '1'
});
options.cwd = options.cwd || process.cwd();
if (process.platform !== 'win32' || !ipc) {
options.stdio = ipc ? [null, null, null, 'ipc'] : 'pipe';
options.env = _objectSpread(_objectSpread(_objectSpread({}, process.env), options.env), {}, {
FORCE_COLOR: '1'
});
options.cwd = options.cwd || process.cwd();
const sh = 'sh';
const shFlag = '-c';
const proc = spawn(sh, [shFlag, script], options);
return proc;
}
options.stdio = 'pipe';
const binPath = require.resolve(script.indexOf('umi') > -1 ? 'umi/bin/umi' : '@alipay/bigfish/bin/bigfish', {
paths: [options.cwd]
});
const child = (0, _child_process().fork)(binPath, ['dev'], options);
return child;
};
exports.runCommand = runCommand;
const isScriptKeyExist = (pkgPath, key) => {
if (!(0, _fs().existsSync)(pkgPath)) {
return false;
}
let pkg = {};
try {
pkg = require(pkgPath);
} catch (_) {
console.log(_.stack);
return false;
}
return !!(pkg.scripts && pkg.scripts[key]);
};
exports.isScriptKeyExist = isScriptKeyExist;
function formatLog(log) {
return log;
}
function getNpmClient() {
try {
(0, _child_process().execSync)('tnpm --version', {
stdio: 'ignore'
});
return _enums.NpmClient.tnpm;
} catch (e) {}
try {
(0, _child_process().execSync)('cnpm --version', {
stdio: 'ignore'
});
return _enums.NpmClient.cnpm;
} catch (e) {}
try {
(0, _child_process().execSync)('npm --version', {
stdio: 'ignore'
});
return _enums.NpmClient.npm;
} catch (e) {}
try {
(0, _child_process().execSync)('ayarn --version', {
stdio: 'ignore'
});
return _enums.NpmClient.ayayn;
} catch (e) {}
try {
(0, _child_process().execSync)('tyarn --version', {
stdio: 'ignore'
});
return _enums.NpmClient.tyarn;
} catch (e) {}
try {
(0, _child_process().execSync)('yarn --version', {
stdio: 'ignore'
});
return _enums.NpmClient.yarn;
} catch (e) {}
try {
(0, _child_process().execSync)('pnpm --version', {
stdio: 'ignore'
});
return _enums.NpmClient.pnpm;
} catch (e) {}
return _enums.NpmClient.npm;
} // 默认关闭的环境变量,
const DEFAULT_CLOSE_ENV = ['UMI_UI', 'ANALYZE', 'ANALYZE_REPORT', 'SPEED_MEASURE', 'FORK_TS_CHECKER'];
function formatEnv(env) {
const res = {};
Object.keys(env).forEach(key => {
if (env[key] === null) {
return;
}
if (typeof env[key] === 'boolean') {
if (DEFAULT_CLOSE_ENV.includes(key)) {
// 默认关闭的环境变量,用户打开时才设置
if (env[key]) {
res[key] = '1';
}
} else {
// 默认开启的,不开启时才能 none
if (!env[key]) {
res[key] = 'none';
}
}
} else {
res[key] = env[key];
}
});
return res;
}
// TODO: args 增加参数校验
function parseKeyScript(key, script) {
const result = {
succes: false,
errMsg: '',
envs: [],
bin: '',
args: []
};
if (/\&\&|\|\|/.test(script)) {
return _objectSpread(_objectSpread({}, result), {}, {
errMsg: 'Script contains && or || is not allowed'
});
}
if (!/bigfish|umi/.test(script)) {
return _objectSpread(_objectSpread({}, result), {}, {
errMsg: 'Not umi'
});
}
try {
const bin = /bigfish/.test(script) ? 'bigfish' : 'umi';
const envs = {};
const args = []; // TODO: args 应该取 bin 之后的
script.split(' ').forEach(item => {
if (['bigfish', 'umi'].indexOf(item) > -1) {
return;
}
if (/=/.test(item)) {
const _item$split = item.split('='),
_item$split2 = _slicedToArray(_item$split, 2),
envKey = _item$split2[0],
envValue = _item$split2[1];
envs[envKey] = envValue;
} else if (/(dev|build|test|lint)/.test(item)) {
args.push(item);
}
});
return _objectSpread(_objectSpread({}, result), {}, {
succes: true,
bin,
envs,
args
});
} catch (e) {
return _objectSpread(_objectSpread({}, result), {}, {
errMsg: `Parse ${key} script error. ${e.message}`
});
}
}
function parseScripts(opts) {
const result = {
succes: false,
exist: false
};
const pkgPath = opts.pkgPath,
key = opts.key;
if (!(0, _fs().existsSync)(pkgPath)) {
return result;
}
let pkg;
try {
pkg = require(pkgPath);
} catch (_) {
pkg = null;
}
const script = pkg.scripts && pkg.scripts[key];
if (!script) {
return _objectSpread(_objectSpread({}, result), {}, {
exist: false
});
}
const scriptConfig = parseKeyScript(key, script);
return _objectSpread({
exist: true
}, scriptConfig);
}