UNPKG

@areslabs/alita-core

Version:

alita-core

170 lines (135 loc) 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.geneOrder = geneOrder; exports.getGenericName = getGenericName; exports.isStaticRes = isStaticRes; exports.getRootPathPrefix = getRootPathPrefix; exports.isEventProp = isEventProp; exports.emptyDir = emptyDir; exports.miscNameToJSName = miscNameToJSName; exports.judgeLibPath = judgeLibPath; exports.getLibPath = getLibPath; exports.RootPrefixPlaceHolader = void 0; var path = _interopRequireWildcard(require("path")); var fse = _interopRequireWildcard(require("fs-extra")); var _configure = _interopRequireDefault(require("../configure")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /** * Copyright (c) Areslabs. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ const low = 'abcdefghijklmnopqrstuvwxyz'; const allChars = (low + low.toUpperCase()).split(''); /** * 产生顺序字符串: a --> b --> c .....--> z --> A ... --> Z --> aa --> ab ... --> ZZ --> aaa ...-> ZZZ * * @returns {any} */ function geneOrder(suffix = '') { let v = ['a']; return { get next() { let index = v.length - 1; while (incre(index, v)) { index--; if (index === -1) { v.unshift('a'); break; } } // $ 保证不会和用户数据重复 return v.join('') + suffix; } }; } function incre(index, v) { const char = v[index]; if (char === 'Z') { v[index] = 'a'; return true; } else { v[index] = allChars[allChars.indexOf(char) + 1]; return false; } } function getGenericName(name) { if (name.length > 20) { return name.substr(0, 18) + 'CPT'; } else { return name + 'CPT'; } } function isStaticRes(source) { //png、jpg、jpeg、bmp、gif、webp const extname = path.extname(source).toUpperCase(); if (extname === '.PNG' || extname === '.JPG' || extname === '.JPEG' || extname === '.BMP' || extname === '.GIF' || extname === '.WEBP') { // is PNG return true; } } function getRootPathPrefix(filepath) { let rootpath = path.dirname(filepath).replace(_configure.default.outputFullpath, '').replace(/\\/g, '/'); rootpath = rootpath === '' ? '.' : rootpath.substring(1).replace(/[\w-@.]+/g, '..'); return rootpath; } const RootPrefixPlaceHolader = "____RootPrefixPlaceHolader____"; /** * onX * @param name * @returns {boolean} */ exports.RootPrefixPlaceHolader = RootPrefixPlaceHolader; function isEventProp(name) { if (!name || name.length <= 3) return false; if (("" + name).toLowerCase() === 'buttononclick') return true; const trCode = name.charCodeAt(2); return name.charCodeAt(0) === 111 && name.charCodeAt(1) === 110 && trCode >= 65 && trCode <= 90; } function emptyDir(dir, ignoreArr) { const allFiles = fse.readdirSync(dir); for (let i = 0; i < allFiles.length; i++) { const partPath = allFiles[i]; if (ignoreArr.has(partPath)) { continue; } const absolutePath = path.resolve(dir, partPath); if (ignoreArr.has(absolutePath)) { continue; } fse.removeSync(absolutePath); } } function miscNameToJSName(filepath) { const extname = path.extname(filepath); return filepath.replace(extname, '.js').replace('.wx.js', '.js').replace('node_modules', 'npm'); } function judgeLibPath(relativePath) { if (relativePath.startsWith('/') || relativePath.startsWith('.')) { return false; } return true; } function getLibPath(path) { if (path.charAt(0) === '@') { const index = path.indexOf('/'); const twoIndex = path.indexOf('/', index + 1); if (twoIndex === -1) { return path; } else { return path.substring(0, twoIndex); } } else { const index = path.indexOf('/'); if (index === -1) { return path; } else { return path.substring(0, index); } } }