utils.js 17.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.modifierHas = exports.isClass = exports.findExportNode = exports.loadTsConfig = exports.camelProp = exports.formatProp = exports.readJson5 = exports.readJson = exports.getPkgInfo = exports.parseJson = exports.extend = exports.requireFile = exports.moduleExist = exports.resolveModule = exports.removeSameNameJs = exports.toArray = exports.formatPath = exports.getModuleObjByPath = exports.cleanJs = exports.writeFileSync = exports.getImportStr = exports.log = exports.pickFields = exports.strToFn = exports.loadModules = exports.checkMaybeIsJsProj = exports.checkMaybeIsTsProj = exports.writeTsConfig = exports.writeJsConfig = exports.loadFiles = exports.isIdentifierName = exports.convertString = exports.getEggInfo = exports.TS_CONFIG = exports.JS_CONFIG = void 0;
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const mkdirp_1 = tslib_1.__importDefault(require("mkdirp"));
const globby_1 = tslib_1.__importDefault(require("globby"));
const path_1 = tslib_1.__importDefault(require("path"));
const typescript_1 = tslib_1.__importDefault(require("typescript"));
const yn_1 = tslib_1.__importDefault(require("yn"));
const config_1 = require("./config");
const child_process_1 = require("child_process");
const json5_1 = tslib_1.__importDefault(require("json5"));
exports.JS_CONFIG = {
    include: ['**/*'],
};
exports.TS_CONFIG = {
    compilerOptions: {
        target: typescript_1.default.ScriptTarget.ES2017,
        module: typescript_1.default.ModuleKind.CommonJS,
        strict: true,
        noImplicitAny: false,
        experimentalDecorators: true,
        emitDecoratorMetadata: true,
        allowSyntheticDefaultImports: true,
        charset: 'utf8',
        allowJs: false,
        pretty: true,
        lib: ['es6'],
        noEmitOnError: false,
        noUnusedLocals: true,
        noUnusedParameters: true,
        allowUnreachableCode: false,
        allowUnusedLabels: false,
        strictPropertyInitialization: false,
        noFallthroughCasesInSwitch: true,
        skipLibCheck: true,
        skipDefaultLibCheck: true,
        inlineSourceMap: true,
    },
};
const cacheEggInfo = {};
function getEggInfo(option) {
    const { cacheIndex, cwd, customLoader } = option;
    const cacheKey = cacheIndex ? `${cacheIndex}${cwd}` : undefined;
    const caches = cacheKey ? (cacheEggInfo[cacheKey] = cacheEggInfo[cacheKey] || {}) : undefined;
    const end = (json) => {
        if (caches) {
            caches.eggInfo = json;
            caches.cacheTime = Date.now();
        }
        if (option.callback) {
            return option.callback(json);
        }
        return json;
    };
    // check cache
    if (caches) {
        if (caches.cacheTime && (Date.now() - caches.cacheTime) < 1000) {
            return end(caches.eggInfo);
        }
        else if (caches.runningPromise) {
            return caches.runningPromise;
        }
    }
    // get egg info from customLoader
    if (customLoader) {
        return end({
            config: customLoader.config,
            plugins: customLoader.plugins,
            eggPaths: customLoader.eggPaths,
        });
    }
    // prepare options
    const cmd = `node ${path_1.default.resolve(__dirname, './scripts/eggInfo')}`;
    const opt = {
        cwd,
        env: Object.assign(Object.assign(Object.assign({}, process.env), { TS_NODE_TYPE_CHECK: 'false', TS_NODE_TRANSPILE_ONLY: 'true', TS_NODE_FILES: 'false', EGG_TYPESCRIPT: 'true', CACHE_REQUIRE_PATHS_FILE: path_1.default.resolve(config_1.tmpDir, './requirePaths.json') }), option.env),
    };
    if (option.async) {
        // cache promise
        caches.runningPromise = new Promise((resolve, reject) => {
            child_process_1.exec(cmd, opt, err => {
                caches.runningPromise = null;
                if (err)
                    reject(err);
                resolve(end(parseJson(fs_1.default.readFileSync(config_1.eggInfoPath, 'utf-8'))));
            });
        });
        return caches.runningPromise;
    }
    try {
        child_process_1.execSync(cmd, opt);
        return end(parseJson(fs_1.default.readFileSync(config_1.eggInfoPath, 'utf-8')));
    }
    catch (e) {
        return end({});
    }
}
exports.getEggInfo = getEggInfo;
// convert string to same type with default value
function convertString(val, defaultVal) {
    if (val === undefined)
        return defaultVal;
    switch (typeof defaultVal) {
        case 'boolean':
            return yn_1.default(val, { default: defaultVal });
        case 'number':
            const num = +val;
            return (isNaN(num) ? defaultVal : num);
        case 'string':
            return val;
        default:
            return defaultVal;
    }
}
exports.convertString = convertString;
function isIdentifierName(s) {
    return /^[$A-Z_][0-9A-Z_$]*$/i.test(s);
}
exports.isIdentifierName = isIdentifierName;
// load ts/js files
function loadFiles(cwd, pattern) {
    pattern = pattern || '**/*.(js|ts)';
    pattern = Array.isArray(pattern) ? pattern : [pattern];
    const fileList = globby_1.default.sync(pattern.concat(['!**/*.d.ts']), { cwd });
    return fileList.filter(f => {
        // filter same name js/ts
        return !(f.endsWith('.js') &&
            fileList.includes(f.substring(0, f.length - 2) + 'ts'));
    });
}
exports.loadFiles = loadFiles;
// write jsconfig.json to cwd
function writeJsConfig(cwd) {
    const jsconfigUrl = path_1.default.resolve(cwd, './jsconfig.json');
    if (!fs_1.default.existsSync(jsconfigUrl)) {
        fs_1.default.writeFileSync(jsconfigUrl, JSON.stringify(exports.JS_CONFIG, null, 2));
        return jsconfigUrl;
    }
}
exports.writeJsConfig = writeJsConfig;
// write tsconfig.json to cwd
function writeTsConfig(cwd) {
    const tsconfigUrl = path_1.default.resolve(cwd, './tsconfig.json');
    if (!fs_1.default.existsSync(tsconfigUrl)) {
        fs_1.default.writeFileSync(tsconfigUrl, JSON.stringify(exports.TS_CONFIG, null, 2));
        return tsconfigUrl;
    }
}
exports.writeTsConfig = writeTsConfig;
function checkMaybeIsTsProj(cwd, pkgInfo) {
    pkgInfo = pkgInfo || getPkgInfo(cwd);
    return (pkgInfo.egg && pkgInfo.egg.typescript) ||
        fs_1.default.existsSync(path_1.default.resolve(cwd, './tsconfig.json')) ||
        fs_1.default.existsSync(path_1.default.resolve(cwd, './config/config.default.ts')) ||
        fs_1.default.existsSync(path_1.default.resolve(cwd, './config/config.ts'));
}
exports.checkMaybeIsTsProj = checkMaybeIsTsProj;
function checkMaybeIsJsProj(cwd, pkgInfo) {
    pkgInfo = pkgInfo || getPkgInfo(cwd);
    const isJs = !checkMaybeIsTsProj(cwd, pkgInfo) &&
        (fs_1.default.existsSync(path_1.default.resolve(cwd, './config/config.default.js')) ||
            fs_1.default.existsSync(path_1.default.resolve(cwd, './jsconfig.json')));
    return isJs;
}
exports.checkMaybeIsJsProj = checkMaybeIsJsProj;
// load modules to object
function loadModules(cwd, loadDefault, preHandle) {
    const modules = {};
    preHandle = preHandle || (fn => fn);
    fs_1.default
        .readdirSync(cwd)
        .filter(f => f.endsWith('.js'))
        .forEach(f => {
        const name = f.substring(0, f.lastIndexOf('.'));
        const obj = require(path_1.default.resolve(cwd, name));
        if (loadDefault && obj.default) {
            modules[name] = preHandle(obj.default);
        }
        else {
            modules[name] = preHandle(obj);
        }
    });
    return modules;
}
exports.loadModules = loadModules;
// convert string to function
function strToFn(fn) {
    if (typeof fn === 'string') {
        return (...args) => fn.replace(/{{\s*(\d+)\s*}}/g, (_, index) => args[index]);
    }
    return fn;
}
exports.strToFn = strToFn;
// pick fields from object
function pickFields(obj, fields) {
    const newObj = {};
    fields.forEach(f => (newObj[f] = obj[f]));
    return newObj;
}
exports.pickFields = pickFields;
// log
function log(msg, prefix = true) {
    console.info(`${prefix ? '[egg-ts-helper] ' : ''}${msg}`);
}
exports.log = log;
// get import context
function getImportStr(from, to, moduleName, importStar) {
    const extname = path_1.default.extname(to);
    const toPathWithoutExt = to.substring(0, to.length - extname.length);
    const importPath = path_1.default.relative(from, toPathWithoutExt).replace(/\/|\\/g, '/');
    const isTS = extname === '.ts' || fs_1.default.existsSync(`${toPathWithoutExt}.d.ts`);
    const importStartStr = isTS && importStar ? '* as ' : '';
    const fromStr = isTS ? `from '${importPath}'` : `= require('${importPath}')`;
    return `import ${importStartStr}${moduleName} ${fromStr};`;
}
exports.getImportStr = getImportStr;
// write file, using fs.writeFileSync to block io that d.ts can create before egg app started.
function writeFileSync(fileUrl, content) {
    mkdirp_1.default.sync(path_1.default.dirname(fileUrl));
    fs_1.default.writeFileSync(fileUrl, content);
}
exports.writeFileSync = writeFileSync;
// clean same name js/ts
function cleanJs(cwd) {
    const fileList = [];
    globby_1.default
        .sync(['**/*.ts', '**/*.tsx', '!**/*.d.ts', '!**/node_modules'], { cwd })
        .forEach(f => {
        const jf = removeSameNameJs(path_1.default.resolve(cwd, f));
        if (jf)
            fileList.push(path_1.default.relative(cwd, jf));
    });
    if (fileList.length) {
        console.info('[egg-ts-helper] These file was deleted because the same name ts file was exist!\n');
        console.info('  ' + fileList.join('\n  ') + '\n');
    }
}
exports.cleanJs = cleanJs;
// get moduleName by file path
function getModuleObjByPath(f) {
    const props = f.substring(0, f.lastIndexOf('.')).split('/');
    // composing moduleName
    const moduleName = props.map(prop => camelProp(prop, 'upper')).join('');
    return {
        props,
        moduleName,
    };
}
exports.getModuleObjByPath = getModuleObjByPath;
// format path sep to /
function formatPath(str) {
    return str.replace(/\/|\\/g, '/');
}
exports.formatPath = formatPath;
function toArray(pattern) {
    return pattern ? (Array.isArray(pattern) ? pattern : [pattern]) : [];
}
exports.toArray = toArray;
// remove same name js
function removeSameNameJs(f) {
    if (!f.match(/\.tsx?$/) || f.endsWith('.d.ts')) {
        return;
    }
    const jf = f.replace(/tsx?$/, 'js');
    if (fs_1.default.existsSync(jf)) {
        fs_1.default.unlinkSync(jf);
        return jf;
    }
}
exports.removeSameNameJs = removeSameNameJs;
// resolve module
function resolveModule(url) {
    try {
        return require.resolve(url);
    }
    catch (e) {
        return undefined;
    }
}
exports.resolveModule = resolveModule;
// check whether module is exist
function moduleExist(mod, cwd) {
    const nodeModulePath = path_1.default.resolve(cwd || process.cwd(), 'node_modules', mod);
    return fs_1.default.existsSync(nodeModulePath) || resolveModule(mod);
}
exports.moduleExist = moduleExist;
// require modules
function requireFile(url) {
    url = url && resolveModule(url);
    if (!url) {
        return undefined;
    }
    let exp = require(url);
    if (exp.__esModule && 'default' in exp) {
        exp = exp.default;
    }
    return exp;
}
exports.requireFile = requireFile;
// extend
function extend(obj, ...args) {
    args.forEach(source => {
        let descriptor, prop;
        if (source) {
            for (prop in source) {
                descriptor = Object.getOwnPropertyDescriptor(source, prop);
                Object.defineProperty(obj, prop, descriptor);
            }
        }
    });
    return obj;
}
exports.extend = extend;
// parse json
function parseJson(jsonStr) {
    if (jsonStr) {
        try {
            return JSON.parse(jsonStr);
        }
        catch (e) {
            return {};
        }
    }
    else {
        return {};
    }
}
exports.parseJson = parseJson;
// load package.json
function getPkgInfo(cwd) {
    return readJson(path_1.default.resolve(cwd, './package.json'));
}
exports.getPkgInfo = getPkgInfo;
// read json file
function readJson(jsonUrl) {
    if (!fs_1.default.existsSync(jsonUrl))
        return {};
    return parseJson(fs_1.default.readFileSync(jsonUrl, 'utf-8'));
}
exports.readJson = readJson;
function readJson5(jsonUrl) {
    if (!fs_1.default.existsSync(jsonUrl))
        return {};
    return json5_1.default.parse(fs_1.default.readFileSync(jsonUrl, 'utf-8'));
}
exports.readJson5 = readJson5;
// format property
function formatProp(prop) {
    return prop.replace(/[._-][a-z]/gi, s => s.substring(1).toUpperCase());
}
exports.formatProp = formatProp;
// like egg-core/file-loader
function camelProp(property, caseStyle) {
    if (typeof caseStyle === 'function') {
        return caseStyle(property);
    }
    // camel transfer
    property = formatProp(property);
    let first = property[0];
    // istanbul ignore next
    switch (caseStyle) {
        case 'lower':
            first = first.toLowerCase();
            break;
        case 'upper':
            first = first.toUpperCase();
            break;
        case 'camel':
            break;
        default:
            break;
    }
    return first + property.substring(1);
}
exports.camelProp = camelProp;
// load tsconfig.json
function loadTsConfig(tsconfigPath) {
    tsconfigPath = path_1.default.extname(tsconfigPath) === '.json' ? tsconfigPath : `${tsconfigPath}.json`;
    const tsConfig = readJson5(tsconfigPath);
    if (tsConfig.extends) {
        const extendPattern = tsConfig.extends;
        const tsconfigDirName = path_1.default.dirname(tsconfigPath);
        const maybeRealExtendPath = [
            path_1.default.resolve(tsconfigDirName, extendPattern),
            path_1.default.resolve(tsconfigDirName, `${extendPattern}.json`),
        ];
        if (!path_1.default.extname(tsConfig.extends) && !extendPattern.startsWith('.') && !extendPattern.startsWith('/')) {
            maybeRealExtendPath.push(path_1.default.resolve(tsconfigDirName, 'node_modules', extendPattern, 'tsconfig.json'), path_1.default.resolve(process.cwd(), 'node_modules', extendPattern, 'tsconfig.json'));
        }
        const extendRealPath = maybeRealExtendPath.find(f => fs_1.default.existsSync(f));
        if (extendRealPath) {
            const extendTsConfig = loadTsConfig(extendRealPath);
            return Object.assign(Object.assign({}, tsConfig.compilerOptions), extendTsConfig);
        }
    }
    return tsConfig.compilerOptions || {};
}
exports.loadTsConfig = loadTsConfig;
/**
 * ts ast utils
 */
// find export node from sourcefile.
function findExportNode(code) {
    const sourceFile = typescript_1.default.createSourceFile('file.ts', code, typescript_1.default.ScriptTarget.ES2017, true);
    const cache = new Map();
    const exportNodeList = [];
    let exportDefaultNode;
    sourceFile.statements.forEach(node => {
        // each node in root scope
        if (modifierHas(node, typescript_1.default.SyntaxKind.ExportKeyword)) {
            if (modifierHas(node, typescript_1.default.SyntaxKind.DefaultKeyword)) {
                // export default
                exportDefaultNode = node;
            }
            else {
                // export variable
                if (typescript_1.default.isVariableStatement(node)) {
                    node.declarationList.declarations.forEach(declare => exportNodeList.push(declare));
                }
                else {
                    exportNodeList.push(node);
                }
            }
        }
        else if (typescript_1.default.isVariableStatement(node)) {
            // cache variable statement
            for (const declaration of node.declarationList.declarations) {
                if (typescript_1.default.isIdentifier(declaration.name) && declaration.initializer) {
                    cache.set(declaration.name.escapedText, declaration.initializer);
                }
            }
        }
        else if ((typescript_1.default.isFunctionDeclaration(node) || typescript_1.default.isClassDeclaration(node)) && node.name) {
            // cache function declaration and class declaration
            cache.set(node.name.escapedText, node);
        }
        else if (typescript_1.default.isExportAssignment(node)) {
            // export default {}
            exportDefaultNode = node.expression;
        }
        else if (typescript_1.default.isExpressionStatement(node) && typescript_1.default.isBinaryExpression(node.expression)) {
            if (typescript_1.default.isPropertyAccessExpression(node.expression.left)) {
                const obj = node.expression.left.expression;
                const prop = node.expression.left.name;
                if (typescript_1.default.isIdentifier(obj)) {
                    if (obj.escapedText === 'exports') {
                        // exports.xxx = {}
                        exportNodeList.push(node.expression);
                    }
                    else if (obj.escapedText === 'module' &&
                        typescript_1.default.isIdentifier(prop) &&
                        prop.escapedText === 'exports') {
                        // module.exports = {}
                        exportDefaultNode = node.expression.right;
                    }
                }
            }
            else if (typescript_1.default.isIdentifier(node.expression.left)) {
                // let exportData;
                // exportData = {};
                // export exportData
                cache.set(node.expression.left.escapedText, node.expression.right);
            }
        }
    });
    while (exportDefaultNode && typescript_1.default.isIdentifier(exportDefaultNode) && cache.size) {
        const mid = cache.get(exportDefaultNode.escapedText);
        cache.delete(exportDefaultNode.escapedText);
        exportDefaultNode = mid;
    }
    return {
        exportDefaultNode,
        exportNodeList,
    };
}
exports.findExportNode = findExportNode;
function isClass(v) {
    return typeof v === 'function' && /^\s*class\s+/.test(v.toString());
}
exports.isClass = isClass;
// check kind in node.modifiers.
function modifierHas(node, kind) {
    return node.modifiers && node.modifiers.find(mod => kind === mod.kind);
}
exports.modifierHas = modifierHas;