UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

257 lines (256 loc) 7.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const fsExtra = require("fs-extra"); const path = require("path"); const env_1 = require("../env"); const prettyTime = require("pretty-time"); const offsetLinesModule = require("offset-sourcemap-lines"); const CACHED_PATHS = new Map(); function path2Regex(path) { if (CACHED_PATHS.get(path)) { return CACHED_PATHS.get(path); } path = path.replace(/(\.|\/)/, '\\$1'); const re = new RegExp(path); CACHED_PATHS.set(path, re); return re; } exports.path2Regex = path2Regex; function matchAll(regex, str, cb) { let matches; while ((matches = regex.exec(str))) { cb(matches); } } exports.matchAll = matchAll; function removeFolder(userPath) { fsExtra.removeSync(userPath); } exports.removeFolder = removeFolder; function beautifyBundleName(absPath) { absPath = absPath.replace(/(\.\w+)$/i, ''); const items = absPath.split(/(\/|\\)/); const l = items.length; const [a, b] = [items[l - 1], items[l - 3]]; let suggested = b ? `${b}-${a}` : a; if (suggested.length > 20) { suggested = suggested.slice(suggested.length, 20); } return suggested.toLowerCase(); } exports.beautifyBundleName = beautifyBundleName; function offsetLines(obj, amount) { return offsetLinesModule(obj, amount); } exports.offsetLines = offsetLines; function isRegExp(input) { return !!(input && typeof input.test === 'function'); } exports.isRegExp = isRegExp; function createRequireConst(name, variable) { return `var ${variable ? variable : name} = require("${name}");`; } exports.createRequireConst = createRequireConst; function createRequireConstWithObject(name, variable, obj) { return `var ${variable ? variable : name} = require("${name}").${obj};`; } exports.createRequireConstWithObject = createRequireConstWithObject; function createStringConst(name, value) { return `const ${name} = ${JSON.stringify(value)};`; } exports.createStringConst = createStringConst; function createVarString(name, value) { return `var ${name} = ${JSON.stringify(value)};`; } exports.createVarString = createVarString; function ensurePublicExtension(url) { let ext = path.extname(url); if (ext === '.ts') { url = replaceExt(url, '.js'); } if (ext === '.tsx') { url = replaceExt(url, '.jsx'); } return url; } exports.ensurePublicExtension = ensurePublicExtension; function parseVersion(version) { const re = /v?(\d+)/g; let matcher; const versions = []; while ((matcher = re.exec(version))) { versions.push(parseInt(matcher[1])); } return versions; } exports.parseVersion = parseVersion; function replaceExt(npath, ext) { if (!npath) { return npath; } if (/\.[a-z0-9]+$/i.test(npath)) { return npath.replace(/\.[a-z0-9]+$/i, ext); } else { return npath + ext; } } exports.replaceExt = replaceExt; function extractFuseBoxPath(homeDir, targetPath) { homeDir = ensureFuseBoxPath(homeDir); targetPath = ensureFuseBoxPath(targetPath); let result = targetPath.replace(homeDir, ''); if (result[0] === '/') { result = result.slice(1); } return result; } exports.extractFuseBoxPath = extractFuseBoxPath; function fileExists(file) { return fs.existsSync(file); } exports.fileExists = fileExists; function readFile(file) { return fs.readFileSync(file).toString(); } exports.readFile = readFile; function readFileAsBuffer(file) { return fs.readFileSync(file); } exports.readFileAsBuffer = readFileAsBuffer; function removeFile(file) { return fs.unlinkSync(file); } exports.removeFile = removeFile; async function copyFile(file, target) { return fsExtra.copy(file, target); } exports.copyFile = copyFile; function isObject(obj) { return typeof obj === 'object'; } exports.isObject = isObject; function pathJoin(...args) { return path.join(...args); } exports.pathJoin = pathJoin; function getExtension(file) { return path.extname(file); } exports.getExtension = getExtension; function ensureDir(dir) { fsExtra.ensureDirSync(dir); return dir; } exports.ensureDir = ensureDir; function fileStat(file) { return fs.statSync(file); } exports.fileStat = fileStat; function makeFuseBoxPath(homeDir, absPath) { return homeDir && ensurePublicExtension(extractFuseBoxPath(homeDir, absPath)); } exports.makeFuseBoxPath = makeFuseBoxPath; function measureTime(group) { let startTime = process.hrtime(); return { end: () => { return prettyTime(process.hrtime(startTime)); }, }; } exports.measureTime = measureTime; function cleanExistingSourceMappingURL(contents) { return contents.replace(/\/*#\s*sourceMappingURL=\s*([^\s]+)\s*\*\//, ''); } exports.cleanExistingSourceMappingURL = cleanExistingSourceMappingURL; function findReplace(str, re, fn) { return str.replace(re, (...args) => { return fn(args); }); } exports.findReplace = findReplace; function path2RegexPattern(path) { path = path .split('.') .join('\\.') .split('/') .join('\\/'); return new RegExp(path); } exports.path2RegexPattern = path2RegexPattern; function ensureUserPath(userPath, root) { if (!path.isAbsolute(userPath)) { userPath = path.join(root, userPath); } userPath = path.normalize(userPath); let dir = path.dirname(userPath); fsExtra.ensureDirSync(dir); return userPath; } exports.ensureUserPath = ensureUserPath; exports.Concat = require('fuse-concat-with-sourcemaps'); function createConcat(generateSourceMap, outputFileName, seperator) { return new exports.Concat(generateSourceMap, outputFileName, seperator); } exports.createConcat = createConcat; function ensureAbsolutePath(userPath, root) { if (!path.isAbsolute(userPath)) { return path.join(root, userPath); } return userPath; } exports.ensureAbsolutePath = ensureAbsolutePath; function getPathRelativeToConfig(props) { let target = props.fileName ? path.dirname(props.fileName) : props.dirName; const fileName = props.fileName && path.basename(props.fileName); if (!path.isAbsolute(target)) { target = path.join(env_1.env.SCRIPT_PATH, target); } if (props.ensureDirExist) { const baseDir = path.dirname(target); ensureDir(baseDir); } return fileName ? path.join(target, fileName) : target; } exports.getPathRelativeToConfig = getPathRelativeToConfig; function ensureFuseBoxPath(input) { return input.replace(/\\/g, '/').replace(/\/$/, ''); } exports.ensureFuseBoxPath = ensureFuseBoxPath; function joinFuseBoxPath(...any) { let includesProtocol = any[0].includes('://'); let joinedPath = !includesProtocol ? path.join(...any) : any[0].replace(/([^/])$/, '$1/') + path.join(...any.slice(1)); return ensureFuseBoxPath(joinedPath); } exports.joinFuseBoxPath = joinFuseBoxPath; async function writeFile(name, contents) { return new Promise((resolve, reject) => { ensureDir(path.dirname(name)); fs.writeFile(name, contents, err => { if (err) return reject(err); return resolve(); }); }); } exports.writeFile = writeFile; function fastHash(text) { let hash = 0; if (text.length == 0) return ''; for (let i = 0; i < text.length; i++) { let char = text.charCodeAt(i); hash = (hash << 5) - hash + char; hash = hash & hash; // Convert to 32bit integer } let result = hash.toString(16).toString(); if (result.charAt(0) === '-') { result = result.replace(/-/, '0'); } return result; } exports.fastHash = fastHash;