UNPKG

bit-ship

Version:

Bit-Ship CLI is tool that analyses your code and generates a custom environment for your needs You can use if to local development, CI/CD or even production.

1,493 lines (1,472 loc) 301 kB
import * as process$1 from 'node:process'; import { fileURLToPath } from 'node:url'; import { win32, posix } from 'node:path'; import { realpathSync as realpathSync$1, readlinkSync, readdirSync, readdir as readdir$1, lstatSync, existsSync } from 'fs'; import * as actualFS from 'node:fs'; import { realpath, readlink, readdir, lstat } from 'node:fs/promises'; import { EventEmitter } from 'node:events'; import Stream from 'node:stream'; import { StringDecoder } from 'node:string_decoder'; import { readFile, writeFile } from 'fs/promises'; import { exec } from 'child_process'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; const LogLevels = { fatal: 0, error: 0, warn: 1, log: 2, info: 3, success: 3, fail: 3, debug: 4, trace: 5, verbose: Number.POSITIVE_INFINITY }; const LogTypes = { // Silent silent: { level: -1 }, // Level 0 fatal: { level: LogLevels.fatal }, error: { level: LogLevels.error }, // Level 1 warn: { level: LogLevels.warn }, // Level 2 log: { level: LogLevels.log }, // Level 3 info: { level: LogLevels.info }, success: { level: LogLevels.success }, fail: { level: LogLevels.fail }, ready: { level: LogLevels.info }, start: { level: LogLevels.info }, box: { level: LogLevels.info }, // Level 4 debug: { level: LogLevels.debug }, // Level 5 trace: { level: LogLevels.trace }, // Verbose verbose: { level: LogLevels.verbose } }; function isPlainObject$1(value) { if (value === null || typeof value !== "object") { return false; } const prototype = Object.getPrototypeOf(value); if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) { return false; } if (Symbol.iterator in value) { return false; } if (Symbol.toStringTag in value) { return Object.prototype.toString.call(value) === "[object Module]"; } return true; } function _defu(baseObject, defaults, namespace = ".", merger) { if (!isPlainObject$1(defaults)) { return _defu(baseObject, {}, namespace); } const object = Object.assign({}, defaults); for (const key in baseObject) { if (key === "__proto__" || key === "constructor") { continue; } const value = baseObject[key]; if (value === null || value === void 0) { continue; } if (Array.isArray(value) && Array.isArray(object[key])) { object[key] = [...value, ...object[key]]; } else if (isPlainObject$1(value) && isPlainObject$1(object[key])) { object[key] = _defu( value, object[key], (namespace ? `${namespace}.` : "") + key.toString()); } else { object[key] = value; } } return object; } function createDefu(merger) { return (...arguments_) => ( // eslint-disable-next-line unicorn/no-array-reduce arguments_.reduce((p, c) => _defu(p, c, ""), {}) ); } const defu = createDefu(); function isPlainObject(obj) { return Object.prototype.toString.call(obj) === "[object Object]"; } function isLogObj(arg) { if (!isPlainObject(arg)) { return false; } if (!arg.message && !arg.args) { return false; } if (arg.stack) { return false; } return true; } let paused = false; const queue = []; class Consola { options; _lastLog; _mockFn; /** * Creates an instance of Consola with specified options or defaults. * * @param {Partial<ConsolaOptions>} [options={}] - Configuration options for the Consola instance. */ constructor(options = {}) { const types = options.types || LogTypes; this.options = defu( { ...options, defaults: { ...options.defaults }, level: _normalizeLogLevel(options.level, types), reporters: [...options.reporters || []] }, { types: LogTypes, throttle: 1e3, throttleMin: 5, formatOptions: { date: true, colors: false, compact: true } } ); for (const type in types) { const defaults = { type, ...this.options.defaults, ...types[type] }; this[type] = this._wrapLogFn(defaults); this[type].raw = this._wrapLogFn( defaults, true ); } if (this.options.mockFn) { this.mockTypes(); } this._lastLog = {}; } /** * Gets the current log level of the Consola instance. * * @returns {number} The current log level. */ get level() { return this.options.level; } /** * Sets the minimum log level that will be output by the instance. * * @param {number} level - The new log level to set. */ set level(level) { this.options.level = _normalizeLogLevel( level, this.options.types, this.options.level ); } /** * Displays a prompt to the user and returns the response. * Throw an error if `prompt` is not supported by the current configuration. * * @template T * @param {string} message - The message to display in the prompt. * @param {T} [opts] - Optional options for the prompt. See {@link PromptOptions}. * @returns {promise<T>} A promise that infer with the prompt options. See {@link PromptOptions}. */ prompt(message, opts) { if (!this.options.prompt) { throw new Error("prompt is not supported!"); } return this.options.prompt(message, opts); } /** * Creates a new instance of Consola, inheriting options from the current instance, with possible overrides. * * @param {Partial<ConsolaOptions>} options - Optional overrides for the new instance. See {@link ConsolaOptions}. * @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}. */ create(options) { const instance = new Consola({ ...this.options, ...options }); if (this._mockFn) { instance.mockTypes(this._mockFn); } return instance; } /** * Creates a new Consola instance with the specified default log object properties. * * @param {InputLogObject} defaults - Default properties to include in any log from the new instance. See {@link InputLogObject}. * @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}. */ withDefaults(defaults) { return this.create({ ...this.options, defaults: { ...this.options.defaults, ...defaults } }); } /** * Creates a new Consola instance with a specified tag, which will be included in every log. * * @param {string} tag - The tag to include in each log of the new instance. * @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}. */ withTag(tag) { return this.withDefaults({ tag: this.options.defaults.tag ? this.options.defaults.tag + ":" + tag : tag }); } /** * Adds a custom reporter to the Consola instance. * Reporters will be called for each log message, depending on their implementation and log level. * * @param {ConsolaReporter} reporter - The reporter to add. See {@link ConsolaReporter}. * @returns {Consola} The current Consola instance. */ addReporter(reporter) { this.options.reporters.push(reporter); return this; } /** * Removes a custom reporter from the Consola instance. * If no reporter is specified, all reporters will be removed. * * @param {ConsolaReporter} reporter - The reporter to remove. See {@link ConsolaReporter}. * @returns {Consola} The current Consola instance. */ removeReporter(reporter) { if (reporter) { const i = this.options.reporters.indexOf(reporter); if (i !== -1) { return this.options.reporters.splice(i, 1); } } else { this.options.reporters.splice(0); } return this; } /** * Replaces all reporters of the Consola instance with the specified array of reporters. * * @param {ConsolaReporter[]} reporters - The new reporters to set. See {@link ConsolaReporter}. * @returns {Consola} The current Consola instance. */ setReporters(reporters) { this.options.reporters = Array.isArray(reporters) ? reporters : [reporters]; return this; } wrapAll() { this.wrapConsole(); this.wrapStd(); } restoreAll() { this.restoreConsole(); this.restoreStd(); } /** * Overrides console methods with Consola logging methods for consistent logging. */ wrapConsole() { for (const type in this.options.types) { if (!console["__" + type]) { console["__" + type] = console[type]; } console[type] = this[type].raw; } } /** * Restores the original console methods, removing Consola overrides. */ restoreConsole() { for (const type in this.options.types) { if (console["__" + type]) { console[type] = console["__" + type]; delete console["__" + type]; } } } /** * Overrides standard output and error streams to redirect them through Consola. */ wrapStd() { this._wrapStream(this.options.stdout, "log"); this._wrapStream(this.options.stderr, "log"); } _wrapStream(stream, type) { if (!stream) { return; } if (!stream.__write) { stream.__write = stream.write; } stream.write = (data) => { this[type].raw(String(data).trim()); }; } /** * Restores the original standard output and error streams, removing the Consola redirection. */ restoreStd() { this._restoreStream(this.options.stdout); this._restoreStream(this.options.stderr); } _restoreStream(stream) { if (!stream) { return; } if (stream.__write) { stream.write = stream.__write; delete stream.__write; } } /** * Pauses logging, queues incoming logs until resumed. */ pauseLogs() { paused = true; } /** * Resumes logging, processing any queued logs. */ resumeLogs() { paused = false; const _queue = queue.splice(0); for (const item of _queue) { item[0]._logFn(item[1], item[2]); } } /** * Replaces logging methods with mocks if a mock function is provided. * * @param {ConsolaOptions["mockFn"]} mockFn - The function to use for mocking logging methods. See {@link ConsolaOptions["mockFn"]}. */ mockTypes(mockFn) { const _mockFn = mockFn || this.options.mockFn; this._mockFn = _mockFn; if (typeof _mockFn !== "function") { return; } for (const type in this.options.types) { this[type] = _mockFn(type, this.options.types[type]) || this[type]; this[type].raw = this[type]; } } _wrapLogFn(defaults, isRaw) { return (...args) => { if (paused) { queue.push([this, defaults, args, isRaw]); return; } return this._logFn(defaults, args, isRaw); }; } _logFn(defaults, args, isRaw) { if ((defaults.level || 0) > this.level) { return false; } const logObj = { date: /* @__PURE__ */ new Date(), args: [], ...defaults, level: _normalizeLogLevel(defaults.level, this.options.types) }; if (!isRaw && args.length === 1 && isLogObj(args[0])) { Object.assign(logObj, args[0]); } else { logObj.args = [...args]; } if (logObj.message) { logObj.args.unshift(logObj.message); delete logObj.message; } if (logObj.additional) { if (!Array.isArray(logObj.additional)) { logObj.additional = logObj.additional.split("\n"); } logObj.args.push("\n" + logObj.additional.join("\n")); delete logObj.additional; } logObj.type = typeof logObj.type === "string" ? logObj.type.toLowerCase() : "log"; logObj.tag = typeof logObj.tag === "string" ? logObj.tag : ""; const resolveLog = (newLog = false) => { const repeated = (this._lastLog.count || 0) - this.options.throttleMin; if (this._lastLog.object && repeated > 0) { const args2 = [...this._lastLog.object.args]; if (repeated > 1) { args2.push(`(repeated ${repeated} times)`); } this._log({ ...this._lastLog.object, args: args2 }); this._lastLog.count = 1; } if (newLog) { this._lastLog.object = logObj; this._log(logObj); } }; clearTimeout(this._lastLog.timeout); const diffTime = this._lastLog.time && logObj.date ? logObj.date.getTime() - this._lastLog.time.getTime() : 0; this._lastLog.time = logObj.date; if (diffTime < this.options.throttle) { try { const serializedLog = JSON.stringify([ logObj.type, logObj.tag, logObj.args ]); const isSameLog = this._lastLog.serialized === serializedLog; this._lastLog.serialized = serializedLog; if (isSameLog) { this._lastLog.count = (this._lastLog.count || 0) + 1; if (this._lastLog.count > this.options.throttleMin) { this._lastLog.timeout = setTimeout( resolveLog, this.options.throttle ); return; } } } catch { } } resolveLog(true); } _log(logObj) { for (const reporter of this.options.reporters) { reporter.log(logObj, { options: this.options }); } } } function _normalizeLogLevel(input, types = {}, defaultLevel = 3) { if (input === void 0) { return defaultLevel; } if (typeof input === "number") { return input; } if (types[input] && types[input].level !== void 0) { return types[input].level; } return defaultLevel; } Consola.prototype.add = Consola.prototype.addReporter; Consola.prototype.remove = Consola.prototype.removeReporter; Consola.prototype.clear = Consola.prototype.removeReporter; Consola.prototype.withScope = Consola.prototype.withTag; Consola.prototype.mock = Consola.prototype.mockTypes; Consola.prototype.pause = Consola.prototype.pauseLogs; Consola.prototype.resume = Consola.prototype.resumeLogs; function createConsola$1(options = {}) { return new Consola(options); } class BrowserReporter { options; defaultColor; levelColorMap; typeColorMap; constructor(options) { this.options = { ...options }; this.defaultColor = "#7f8c8d"; this.levelColorMap = { 0: "#c0392b", // Red 1: "#f39c12", // Yellow 3: "#00BCD4" // Cyan }; this.typeColorMap = { success: "#2ecc71" // Green }; } _getLogFn(level) { if (level < 1) { return console.__error || console.error; } if (level === 1) { return console.__warn || console.warn; } return console.__log || console.log; } log(logObj) { const consoleLogFn = this._getLogFn(logObj.level); const type = logObj.type === "log" ? "" : logObj.type; const tag = logObj.tag || ""; const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor; const style = ` background: ${color}; border-radius: 0.5em; color: white; font-weight: bold; padding: 2px 0.5em; `; const badge = `%c${[tag, type].filter(Boolean).join(":")}`; if (typeof logObj.args[0] === "string") { consoleLogFn( `${badge}%c ${logObj.args[0]}`, style, // Empty string as style resets to default console style "", ...logObj.args.slice(1) ); } else { consoleLogFn(badge, style, ...logObj.args); } } } function createConsola(options = {}) { const consola2 = createConsola$1({ reporters: options.reporters || [new BrowserReporter({})], prompt(message, options2 = {}) { if (options2.type === "confirm") { return Promise.resolve(confirm(message)); } return Promise.resolve(prompt(message)); }, ...options }); return consola2; } const consola = createConsola(); const balanced = (a, b, str) => { const ma = a instanceof RegExp ? maybeMatch(a, str) : a; const mb = b instanceof RegExp ? maybeMatch(b, str) : b; const r = ma !== null && mb != null && range(ma, mb, str); return (r && { start: r[0], end: r[1], pre: str.slice(0, r[0]), body: str.slice(r[0] + ma.length, r[1]), post: str.slice(r[1] + mb.length), }); }; const maybeMatch = (reg, str) => { const m = str.match(reg); return m ? m[0] : null; }; const range = (a, b, str) => { let begs, beg, left, right = undefined, result; let ai = str.indexOf(a); let bi = str.indexOf(b, ai + 1); let i = ai; if (ai >= 0 && bi > 0) { if (a === b) { return [ai, bi]; } begs = []; left = str.length; while (i >= 0 && !result) { if (i === ai) { begs.push(i); ai = str.indexOf(a, i + 1); } else if (begs.length === 1) { const r = begs.pop(); if (r !== undefined) result = [r, bi]; } else { beg = begs.pop(); if (beg !== undefined && beg < left) { left = beg; right = bi; } bi = str.indexOf(b, i + 1); } i = ai < bi && ai >= 0 ? ai : bi; } if (begs.length && right !== undefined) { result = [left, right]; } } return result; }; const escSlash = '\0SLASH' + Math.random() + '\0'; const escOpen = '\0OPEN' + Math.random() + '\0'; const escClose = '\0CLOSE' + Math.random() + '\0'; const escComma = '\0COMMA' + Math.random() + '\0'; const escPeriod = '\0PERIOD' + Math.random() + '\0'; const escSlashPattern = new RegExp(escSlash, 'g'); const escOpenPattern = new RegExp(escOpen, 'g'); const escClosePattern = new RegExp(escClose, 'g'); const escCommaPattern = new RegExp(escComma, 'g'); const escPeriodPattern = new RegExp(escPeriod, 'g'); const slashPattern = /\\\\/g; const openPattern = /\\{/g; const closePattern = /\\}/g; const commaPattern = /\\,/g; const periodPattern = /\\./g; function numeric(str) { return !isNaN(str) ? parseInt(str, 10) : str.charCodeAt(0); } function escapeBraces(str) { return str .replace(slashPattern, escSlash) .replace(openPattern, escOpen) .replace(closePattern, escClose) .replace(commaPattern, escComma) .replace(periodPattern, escPeriod); } function unescapeBraces(str) { return str .replace(escSlashPattern, '\\') .replace(escOpenPattern, '{') .replace(escClosePattern, '}') .replace(escCommaPattern, ',') .replace(escPeriodPattern, '.'); } /** * Basically just str.split(","), but handling cases * where we have nested braced sections, which should be * treated as individual members, like {a,{b,c},d} */ function parseCommaParts(str) { if (!str) { return ['']; } const parts = []; const m = balanced('{', '}', str); if (!m) { return str.split(','); } const { pre, body, post } = m; const p = pre.split(','); p[p.length - 1] += '{' + body + '}'; const postParts = parseCommaParts(post); if (post.length) { p[p.length - 1] += postParts.shift(); p.push.apply(p, postParts); } parts.push.apply(parts, p); return parts; } function expand(str) { if (!str) { return []; } // I don't know why Bash 4.3 does this, but it does. // Anything starting with {} will have the first two bytes preserved // but *only* at the top level, so {},a}b will not expand to anything, // but a{},b}c will be expanded to [a}c,abc]. // One could argue that this is a bug in Bash, but since the goal of // this module is to match Bash's rules, we escape a leading {} if (str.slice(0, 2) === '{}') { str = '\\{\\}' + str.slice(2); } return expand_(escapeBraces(str), true).map(unescapeBraces); } function embrace(str) { return '{' + str + '}'; } function isPadded(el) { return /^-?0\d/.test(el); } function lte(i, y) { return i <= y; } function gte(i, y) { return i >= y; } function expand_(str, isTop) { /** @type {string[]} */ const expansions = []; const m = balanced('{', '}', str); if (!m) return [str]; // no need to expand pre, since it is guaranteed to be free of brace-sets const pre = m.pre; const post = m.post.length ? expand_(m.post, false) : ['']; if (/\$$/.test(m.pre)) { for (let k = 0; k < post.length; k++) { const expansion = pre + '{' + m.body + '}' + post[k]; expansions.push(expansion); } } else { const isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body); const isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body); const isSequence = isNumericSequence || isAlphaSequence; const isOptions = m.body.indexOf(',') >= 0; if (!isSequence && !isOptions) { // {a},b} if (m.post.match(/,(?!,).*\}/)) { str = m.pre + '{' + m.body + escClose + m.post; return expand_(str); } return [str]; } let n; if (isSequence) { n = m.body.split(/\.\./); } else { n = parseCommaParts(m.body); if (n.length === 1 && n[0] !== undefined) { // x{{a,b}}y ==> x{a}y x{b}y n = expand_(n[0], false).map(embrace); //XXX is this necessary? Can't seem to hit it in tests. /* c8 ignore start */ if (n.length === 1) { return post.map(p => m.pre + n[0] + p); } /* c8 ignore stop */ } } // at this point, n is the parts, and we know it's not a comma set // with a single entry. let N; if (isSequence && n[0] !== undefined && n[1] !== undefined) { const x = numeric(n[0]); const y = numeric(n[1]); const width = Math.max(n[0].length, n[1].length); let incr = n.length === 3 && n[2] !== undefined ? Math.abs(numeric(n[2])) : 1; let test = lte; const reverse = y < x; if (reverse) { incr *= -1; test = gte; } const pad = n.some(isPadded); N = []; for (let i = x; test(i, y); i += incr) { let c; if (isAlphaSequence) { c = String.fromCharCode(i); if (c === '\\') { c = ''; } } else { c = String(i); if (pad) { const need = width - c.length; if (need > 0) { const z = new Array(need + 1).join('0'); if (i < 0) { c = '-' + z + c.slice(1); } else { c = z + c; } } } } N.push(c); } } else { N = []; for (let j = 0; j < n.length; j++) { N.push.apply(N, expand_(n[j], false)); } } for (let j = 0; j < N.length; j++) { for (let k = 0; k < post.length; k++) { const expansion = pre + N[j] + post[k]; if (!isTop || isSequence || expansion) { expansions.push(expansion); } } } } return expansions; } const MAX_PATTERN_LENGTH = 1024 * 64; const assertValidPattern = (pattern) => { if (typeof pattern !== 'string') { throw new TypeError('invalid pattern'); } if (pattern.length > MAX_PATTERN_LENGTH) { throw new TypeError('pattern is too long'); } }; // translate the various posix character classes into unicode properties // this works across all unicode locales // { <posix class>: [<translation>, /u flag required, negated] const posixClasses = { '[:alnum:]': ['\\p{L}\\p{Nl}\\p{Nd}', true], '[:alpha:]': ['\\p{L}\\p{Nl}', true], '[:ascii:]': ['\\x' + '00-\\x' + '7f', false], '[:blank:]': ['\\p{Zs}\\t', true], '[:cntrl:]': ['\\p{Cc}', true], '[:digit:]': ['\\p{Nd}', true], '[:graph:]': ['\\p{Z}\\p{C}', true, true], '[:lower:]': ['\\p{Ll}', true], '[:print:]': ['\\p{C}', true], '[:punct:]': ['\\p{P}', true], '[:space:]': ['\\p{Z}\\t\\r\\n\\v\\f', true], '[:upper:]': ['\\p{Lu}', true], '[:word:]': ['\\p{L}\\p{Nl}\\p{Nd}\\p{Pc}', true], '[:xdigit:]': ['A-Fa-f0-9', false], }; // only need to escape a few things inside of brace expressions // escapes: [ \ ] - const braceEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&'); // escape all regexp magic characters const regexpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); // everything has already been escaped, we just have to join const rangesToString = (ranges) => ranges.join(''); // takes a glob string at a posix brace expression, and returns // an equivalent regular expression source, and boolean indicating // whether the /u flag needs to be applied, and the number of chars // consumed to parse the character class. // This also removes out of order ranges, and returns ($.) if the // entire class just no good. const parseClass = (glob, position) => { const pos = position; /* c8 ignore start */ if (glob.charAt(pos) !== '[') { throw new Error('not in a brace expression'); } /* c8 ignore stop */ const ranges = []; const negs = []; let i = pos + 1; let sawStart = false; let uflag = false; let escaping = false; let negate = false; let endPos = pos; let rangeStart = ''; WHILE: while (i < glob.length) { const c = glob.charAt(i); if ((c === '!' || c === '^') && i === pos + 1) { negate = true; i++; continue; } if (c === ']' && sawStart && !escaping) { endPos = i + 1; break; } sawStart = true; if (c === '\\') { if (!escaping) { escaping = true; i++; continue; } // escaped \ char, fall through and treat like normal char } if (c === '[' && !escaping) { // either a posix class, a collation equivalent, or just a [ for (const [cls, [unip, u, neg]] of Object.entries(posixClasses)) { if (glob.startsWith(cls, i)) { // invalid, [a-[] is fine, but not [a-[:alpha]] if (rangeStart) { return ['$.', false, glob.length - pos, true]; } i += cls.length; if (neg) negs.push(unip); else ranges.push(unip); uflag = uflag || u; continue WHILE; } } } // now it's just a normal character, effectively escaping = false; if (rangeStart) { // throw this range away if it's not valid, but others // can still match. if (c > rangeStart) { ranges.push(braceEscape(rangeStart) + '-' + braceEscape(c)); } else if (c === rangeStart) { ranges.push(braceEscape(c)); } rangeStart = ''; i++; continue; } // now might be the start of a range. // can be either c-d or c-] or c<more...>] or c] at this point if (glob.startsWith('-]', i + 1)) { ranges.push(braceEscape(c + '-')); i += 2; continue; } if (glob.startsWith('-', i + 1)) { rangeStart = c; i += 2; continue; } // not the start of a range, just a single character ranges.push(braceEscape(c)); i++; } if (endPos < i) { // didn't see the end of the class, not a valid class, // but might still be valid as a literal match. return ['', false, 0, false]; } // if we got no ranges and no negates, then we have a range that // cannot possibly match anything, and that poisons the whole glob if (!ranges.length && !negs.length) { return ['$.', false, glob.length - pos, true]; } // if we got one positive range, and it's a single character, then that's // not actually a magic pattern, it's just that one literal character. // we should not treat that as "magic", we should just return the literal // character. [_] is a perfectly valid way to escape glob magic chars. if (negs.length === 0 && ranges.length === 1 && /^\\?.$/.test(ranges[0]) && !negate) { const r = ranges[0].length === 2 ? ranges[0].slice(-1) : ranges[0]; return [regexpEscape(r), false, endPos - pos, false]; } const sranges = '[' + (negate ? '^' : '') + rangesToString(ranges) + ']'; const snegs = '[' + (negate ? '' : '^') + rangesToString(negs) + ']'; const comb = ranges.length && negs.length ? '(' + sranges + '|' + snegs + ')' : ranges.length ? sranges : snegs; return [comb, uflag, endPos - pos, true]; }; /** * Un-escape a string that has been escaped with {@link escape}. * * If the {@link windowsPathsNoEscape} option is used, then square-brace * escapes are removed, but not backslash escapes. For example, it will turn * the string `'[*]'` into `*`, but it will not turn `'\\*'` into `'*'`, * becuase `\` is a path separator in `windowsPathsNoEscape` mode. * * When `windowsPathsNoEscape` is not set, then both brace escapes and * backslash escapes are removed. * * Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot be escaped * or unescaped. */ const unescape = (s, { windowsPathsNoEscape = false, } = {}) => { return windowsPathsNoEscape ? s.replace(/\[([^\/\\])\]/g, '$1') : s.replace(/((?!\\).|^)\[([^\/\\])\]/g, '$1$2').replace(/\\([^\/])/g, '$1'); }; // parse a single path portion const types = new Set(['!', '?', '+', '*', '@']); const isExtglobType = (c) => types.has(c); // Patterns that get prepended to bind to the start of either the // entire string, or just a single path portion, to prevent dots // and/or traversal patterns, when needed. // Exts don't need the ^ or / bit, because the root binds that already. const startNoTraversal = '(?!(?:^|/)\\.\\.?(?:$|/))'; const startNoDot = '(?!\\.)'; // characters that indicate a start of pattern needs the "no dots" bit, // because a dot *might* be matched. ( is not in the list, because in // the case of a child extglob, it will handle the prevention itself. const addPatternStart = new Set(['[', '.']); // cases where traversal is A-OK, no dot prevention needed const justDots = new Set(['..', '.']); const reSpecials = new Set('().*{}+?[]^$\\!'); const regExpEscape$1 = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); // any single thing other than / const qmark$1 = '[^/]'; // * => any number of characters const star$1 = qmark$1 + '*?'; // use + when we need to ensure that *something* matches, because the * is // the only thing in the path portion. const starNoEmpty = qmark$1 + '+?'; // remove the \ chars that we added if we end up doing a nonmagic compare // const deslash = (s: string) => s.replace(/\\(.)/g, '$1') class AST { type; #root; #hasMagic; #uflag = false; #parts = []; #parent; #parentIndex; #negs; #filledNegs = false; #options; #toString; // set to true if it's an extglob with no children // (which really means one child of '') #emptyExt = false; constructor(type, parent, options = {}) { this.type = type; // extglobs are inherently magical if (type) this.#hasMagic = true; this.#parent = parent; this.#root = this.#parent ? this.#parent.#root : this; this.#options = this.#root === this ? options : this.#root.#options; this.#negs = this.#root === this ? [] : this.#root.#negs; if (type === '!' && !this.#root.#filledNegs) this.#negs.push(this); this.#parentIndex = this.#parent ? this.#parent.#parts.length : 0; } get hasMagic() { /* c8 ignore start */ if (this.#hasMagic !== undefined) return this.#hasMagic; /* c8 ignore stop */ for (const p of this.#parts) { if (typeof p === 'string') continue; if (p.type || p.hasMagic) return (this.#hasMagic = true); } // note: will be undefined until we generate the regexp src and find out return this.#hasMagic; } // reconstructs the pattern toString() { if (this.#toString !== undefined) return this.#toString; if (!this.type) { return (this.#toString = this.#parts.map(p => String(p)).join('')); } else { return (this.#toString = this.type + '(' + this.#parts.map(p => String(p)).join('|') + ')'); } } #fillNegs() { /* c8 ignore start */ if (this !== this.#root) throw new Error('should only call on root'); if (this.#filledNegs) return this; /* c8 ignore stop */ // call toString() once to fill this out this.toString(); this.#filledNegs = true; let n; while ((n = this.#negs.pop())) { if (n.type !== '!') continue; // walk up the tree, appending everthing that comes AFTER parentIndex let p = n; let pp = p.#parent; while (pp) { for (let i = p.#parentIndex + 1; !pp.type && i < pp.#parts.length; i++) { for (const part of n.#parts) { /* c8 ignore start */ if (typeof part === 'string') { throw new Error('string part in extglob AST??'); } /* c8 ignore stop */ part.copyIn(pp.#parts[i]); } } p = pp; pp = p.#parent; } } return this; } push(...parts) { for (const p of parts) { if (p === '') continue; /* c8 ignore start */ if (typeof p !== 'string' && !(p instanceof AST && p.#parent === this)) { throw new Error('invalid part: ' + p); } /* c8 ignore stop */ this.#parts.push(p); } } toJSON() { const ret = this.type === null ? this.#parts.slice().map(p => (typeof p === 'string' ? p : p.toJSON())) : [this.type, ...this.#parts.map(p => p.toJSON())]; if (this.isStart() && !this.type) ret.unshift([]); if (this.isEnd() && (this === this.#root || (this.#root.#filledNegs && this.#parent?.type === '!'))) { ret.push({}); } return ret; } isStart() { if (this.#root === this) return true; // if (this.type) return !!this.#parent?.isStart() if (!this.#parent?.isStart()) return false; if (this.#parentIndex === 0) return true; // if everything AHEAD of this is a negation, then it's still the "start" const p = this.#parent; for (let i = 0; i < this.#parentIndex; i++) { const pp = p.#parts[i]; if (!(pp instanceof AST && pp.type === '!')) { return false; } } return true; } isEnd() { if (this.#root === this) return true; if (this.#parent?.type === '!') return true; if (!this.#parent?.isEnd()) return false; if (!this.type) return this.#parent?.isEnd(); // if not root, it'll always have a parent /* c8 ignore start */ const pl = this.#parent ? this.#parent.#parts.length : 0; /* c8 ignore stop */ return this.#parentIndex === pl - 1; } copyIn(part) { if (typeof part === 'string') this.push(part); else this.push(part.clone(this)); } clone(parent) { const c = new AST(this.type, parent); for (const p of this.#parts) { c.copyIn(p); } return c; } static #parseAST(str, ast, pos, opt) { let escaping = false; let inBrace = false; let braceStart = -1; let braceNeg = false; if (ast.type === null) { // outside of a extglob, append until we find a start let i = pos; let acc = ''; while (i < str.length) { const c = str.charAt(i++); // still accumulate escapes at this point, but we do ignore // starts that are escaped if (escaping || c === '\\') { escaping = !escaping; acc += c; continue; } if (inBrace) { if (i === braceStart + 1) { if (c === '^' || c === '!') { braceNeg = true; } } else if (c === ']' && !(i === braceStart + 2 && braceNeg)) { inBrace = false; } acc += c; continue; } else if (c === '[') { inBrace = true; braceStart = i; braceNeg = false; acc += c; continue; } if (!opt.noext && isExtglobType(c) && str.charAt(i) === '(') { ast.push(acc); acc = ''; const ext = new AST(c, ast); i = AST.#parseAST(str, ext, i, opt); ast.push(ext); continue; } acc += c; } ast.push(acc); return i; } // some kind of extglob, pos is at the ( // find the next | or ) let i = pos + 1; let part = new AST(null, ast); const parts = []; let acc = ''; while (i < str.length) { const c = str.charAt(i++); // still accumulate escapes at this point, but we do ignore // starts that are escaped if (escaping || c === '\\') { escaping = !escaping; acc += c; continue; } if (inBrace) { if (i === braceStart + 1) { if (c === '^' || c === '!') { braceNeg = true; } } else if (c === ']' && !(i === braceStart + 2 && braceNeg)) { inBrace = false; } acc += c; continue; } else if (c === '[') { inBrace = true; braceStart = i; braceNeg = false; acc += c; continue; } if (isExtglobType(c) && str.charAt(i) === '(') { part.push(acc); acc = ''; const ext = new AST(c, part); part.push(ext); i = AST.#parseAST(str, ext, i, opt); continue; } if (c === '|') { part.push(acc); acc = ''; parts.push(part); part = new AST(null, ast); continue; } if (c === ')') { if (acc === '' && ast.#parts.length === 0) { ast.#emptyExt = true; } part.push(acc); acc = ''; ast.push(...parts, part); return i; } acc += c; } // unfinished extglob // if we got here, it was a malformed extglob! not an extglob, but // maybe something else in there. ast.type = null; ast.#hasMagic = undefined; ast.#parts = [str.substring(pos - 1)]; return i; } static fromGlob(pattern, options = {}) { const ast = new AST(null, undefined, options); AST.#parseAST(pattern, ast, 0, options); return ast; } // returns the regular expression if there's magic, or the unescaped // string if not. toMMPattern() { // should only be called on root /* c8 ignore start */ if (this !== this.#root) return this.#root.toMMPattern(); /* c8 ignore stop */ const glob = this.toString(); const [re, body, hasMagic, uflag] = this.toRegExpSource(); // if we're in nocase mode, and not nocaseMagicOnly, then we do // still need a regular expression if we have to case-insensitively // match capital/lowercase characters. const anyMagic = hasMagic || this.#hasMagic || (this.#options.nocase && !this.#options.nocaseMagicOnly && glob.toUpperCase() !== glob.toLowerCase()); if (!anyMagic) { return body; } const flags = (this.#options.nocase ? 'i' : '') + (uflag ? 'u' : ''); return Object.assign(new RegExp(`^${re}$`, flags), { _src: re, _glob: glob, }); } get options() { return this.#options; } // returns the string match, the regexp source, whether there's magic // in the regexp (so a regular expression is required) and whether or // not the uflag is needed for the regular expression (for posix classes) // TODO: instead of injecting the start/end at this point, just return // the BODY of the regexp, along with the start/end portions suitable // for binding the start/end in either a joined full-path makeRe context // (where we bind to (^|/), or a standalone matchPart context (where // we bind to ^, and not /). Otherwise slashes get duped! // // In part-matching mode, the start is: // - if not isStart: nothing // - if traversal possible, but not allowed: ^(?!\.\.?$) // - if dots allowed or not possible: ^ // - if dots possible and not allowed: ^(?!\.) // end is: // - if not isEnd(): nothing // - else: $ // // In full-path matching mode, we put the slash at the START of the // pattern, so start is: // - if first pattern: same as part-matching mode // - if not isStart(): nothing // - if traversal possible, but not allowed: /(?!\.\.?(?:$|/)) // - if dots allowed or not possible: / // - if dots possible and not allowed: /(?!\.) // end is: // - if last pattern, same as part-matching mode // - else nothing // // Always put the (?:$|/) on negated tails, though, because that has to be // there to bind the end of the negated pattern portion, and it's easier to // just stick it in now rather than try to inject it later in the middle of // the pattern. // // We can just always return the same end, and leave it up to the caller // to know whether it's going to be used joined or in parts. // And, if the start is adjusted slightly, can do the same there: // - if not isStart: nothing // - if traversal possible, but not allowed: (?:/|^)(?!\.\.?$) // - if dots allowed or not possible: (?:/|^) // - if dots possible and not allowed: (?:/|^)(?!\.) // // But it's better to have a simpler binding without a conditional, for // performance, so probably better to return both start options. // // Then the caller just ignores the end if it's not the first pattern, // and the start always gets applied. // // But that's always going to be $ if it's the ending pattern, or nothing, // so the caller can just attach $ at the end of the pattern when building. // // So the todo is: // - better detect what kind of start is needed // - return both flavors of starting pattern // - attach $ at the end of the pattern when creating the actual RegExp // // Ah, but wait, no, that all only applies to the root when the first pattern // is not an extglob. If the first pattern IS an extglob, then we need all // that dot prevention biz to live in the extglob portions, because eg // +(*|.x*) can match .xy but not .yx. // // So, return the two flavors if it's #root and the first child is not an // AST, otherwise leave it to the child AST to handle it, and there, // use the (?:^|/) style of start binding. // // Even simplified further: // - Since the start for a join is eg /(?!\.) and the start for a part // is ^(?!\.), we can just prepend (?!\.) to the pattern (either root // or start or whatever) and prepend ^ or / at the Regexp construction. toRegExpSource(allowDot) { const dot = allowDot ?? !!this.#options.dot; if (this.#root === this) this.#fillNegs(); if (!this.type) { const noEmpty = this.isStart() && this.isE