UNPKG

@alifd/fusion-collector

Version:

Collect info for fusion based project

227 lines (186 loc) 6.77 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.analizeCallExpressionOfVariableDeclaration = analizeCallExpressionOfVariableDeclaration; exports.analizeImportDeclarationAST = analizeImportDeclarationAST; exports.getGitConfig = getGitConfig; exports.getImportedOrRequiredModules = getImportedOrRequiredModules; exports.gitFormat = gitFormat; exports.normalizeModuleName = normalizeModuleName; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _debug = _interopRequireDefault(require("debug")); var _gracefulFs = _interopRequireDefault(require("graceful-fs")); var _os = _interopRequireDefault(require("os")); var _path = _interopRequireDefault(require("path")); var _ini = _interopRequireDefault(require("ini")); /** * Created at 16/7/11. * @Author Ling. * @Email i@zeroling.com */ var debug = (0, _debug["default"])('next:collect'); /** * 格式化去重gitUrl */ function gitFormat(gitUrl) { // const sshReg = /git@(.*):(.*)\.git/g; var dotGitReg = /(.*)(.git)$/; // 是否以git结尾 var gitTokenReg = /(.*):\/\/(.*)@git.(.*?)\/(.*)\.git/g; var httpReg = /(.*):\/\/(.*?)\/(.*)\.git/g; var gitUrlWithDot = dotGitReg.test(gitUrl) ? gitUrl : "".concat(gitUrl, ".git"); var sshGitUrl = gitTokenReg.test(gitUrlWithDot) ? gitUrlWithDot.replace(gitTokenReg, function (all, s1, s2, s3, s4) { return "git@git.".concat(s3, ":").concat(s4, ".git"); }) : httpReg.test(gitUrlWithDot) ? gitUrlWithDot.replace(httpReg, function (all, s1, s2, s3) { return "git@".concat(s2, ":").concat(s3, ".git"); }) : gitUrlWithDot; return sshGitUrl; } /** * 获取 git 信息:包括本次提交者花名,仓库地址等 */ function getGitConfig(dir) { var homedir = _os["default"].homedir(); var gitGlobalPath = _path["default"].join(homedir, '.gitconfig'); var gitGlobalConfig = {}; var gitLocalConfig = {}; if (_gracefulFs["default"].existsSync(gitGlobalPath)) { try { gitGlobalConfig = _ini["default"].parse(_gracefulFs["default"].readFileSync(gitGlobalPath, 'utf-8')); } catch (err) { debug('git config 错误:', err.message); gitGlobalConfig = {}; } } var gitLocalPath = _path["default"].join(dir, '.git/config'); if (_gracefulFs["default"].existsSync(gitLocalPath)) { try { gitLocalConfig = _ini["default"].parse(_gracefulFs["default"].readFileSync(gitLocalPath, 'utf-8')); } catch (err) { debug('git config 错误:', err.message); gitLocalConfig = {}; } } var url = (gitLocalConfig['remote "origin"'] || {})['url'] || ''; var repoName = ''; if (url) { repoName = (url.match(/.*\.com[:\/](.*\/.*)\.git/) || [,])[1]; } var userName = (gitLocalConfig.user || gitGlobalConfig.user || {})['name']; return { url: url, repoName: repoName, userName: userName }; } /** * 分析 import/require 语句 * @param ast * @returns {*} */ function getImportedOrRequiredModules(ast) { var handlerMap = { ImportDeclaration: analizeImportDeclarationAST, VariableDeclaration: analizeCallExpressionOfVariableDeclaration }; return ast.program.body.reduce(function (ret, currentBody) { var handler = handlerMap[currentBody.type]; var analized = handler && handler(currentBody); return analized ? [].concat((0, _toConsumableArray2["default"])(ret), (0, _toConsumableArray2["default"])(analized)) : ret; }, []); } /** * 分析 import 语句 * @param importDeclaration * @returns {{importedValue: Array, fromLiteral: *, fromModule}} */ function analizeImportDeclarationAST(importDeclaration) { var specifiers = importDeclaration.specifiers || []; var fromLiteral = importDeclaration.source.value; var fromModule = normalizeModuleName(fromLiteral); if ('' === fromModule) { // 不处理业务模块 return null; } var objResult = {}; specifiers.forEach(function (specifier) { var specifierType = specifier.type; if (!specifierType) return; if (specifierType === 'ImportDefaultSpecifier') { objResult['ImportDefaultSpecifier'] = { importedValue: specifiers[0]['local']['name'], fromLiteral: fromLiteral, fromModule: fromModule }; } else if (specifierType === 'ImportSpecifier') { if (!objResult['ImportSpecifier']) { objResult['ImportSpecifier'] = { importedValue: [specifier['imported']['name']], fromLiteral: fromLiteral, fromModule: fromModule }; return; } objResult['ImportSpecifier'].importedValue.push(specifier['imported']['name']); } }); var result = Object.keys(objResult).map(function (item) { return objResult[item]; }); return result; } /** * 处理 const Button = require('@ali/ice/lib/button') 这种情况 * @param variableDeclaration * @returns {{importedValue: Array, fromLiteral: string, fromModule: string}} */ function analizeCallExpressionOfVariableDeclaration(variableDeclaration) { var result = []; var declaration = variableDeclaration && variableDeclaration.declarations && variableDeclaration.declarations[0]; if ( // 判断处理条件 declaration && declaration.type === 'VariableDeclarator' && // 1. 是变量声明语句 declaration.init && declaration.init.type === 'CallExpression' && declaration.init.callee.name === 'require' // 2. 是由 require 初始化的 ) { var fromLiteral = declaration.init['arguments'][0]['value']; var fromModule = normalizeModuleName(fromLiteral); if ('' === fromModule) { // 不处理业务模块 return null; } var identifier = declaration.id; if (identifier && identifier.type === 'Identifier') { result.push({ importedValue: identifier.name, fromLiteral: fromLiteral, fromModule: fromModule }); } else if (identifier && identifier.type === 'ObjectPattern') { result.push({ importedValue: identifier.properties.map(function (property) { return property.value.name; }), fromLiteral: fromLiteral, fromModule: fromModule }); } // debug(result); return result; } } /** * 得到原始包名称 * @param rawModuleLiteral * @returns string */ function normalizeModuleName(rawModuleLiteral) { if (rawModuleLiteral[0] === '.') { // 暂时不处理本地的引用 return ''; } // normalize module name if (rawModuleLiteral[0] === '@') { // scoped module like '@ali/ice' return (rawModuleLiteral.match(/(@\w*\/[^\/]*)/) || [])[0]; } else { return (rawModuleLiteral.match(/[^\/]*/) || [])[0]; } }