UNPKG

makit

Version:

Make in JavaScript done right!

56 lines (55 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Target = exports.TargetType = void 0; const string_1 = require("../utils/string"); const util_1 = require("util"); const inspectSymbol = Symbol.for('nodejs.util.inspect.custom') || 'inspect'; const extglob = require('extglob'); const isGlob = require('is-glob'); var TargetType; (function (TargetType) { TargetType[TargetType["glob"] = 0] = "glob"; TargetType[TargetType["regexp"] = 1] = "regexp"; TargetType[TargetType["filepath"] = 2] = "filepath"; })(TargetType = exports.TargetType || (exports.TargetType = {})); class Target { constructor(target) { this._decl = target; if (typeof target === 'string') { if (target.indexOf('(') > -1) { // Contains matching groups this.rTarget = new RegExp('^' + extglob(target).replace(/\(\?:/g, '(') + '$'); } else { this.glob = target; } } else { this.rTarget = target; } this.targetType = target instanceof RegExp ? TargetType.regexp : (isGlob(target) ? TargetType.glob : TargetType.filepath); } get decl() { return this._decl; } isFilePath() { return this.targetType === TargetType.filepath; } exec(targetFile) { if (this.rTarget) return this.rTarget.exec(targetFile); return extglob.isMatch(targetFile, this.glob) ? Target.execArrayFromString(targetFile) : null; } [inspectSymbol]() { return string_1.inline(util_1.inspect(this._decl)); } static execArrayFromString(str) { const arr = [str]; arr.input = str; arr.index = 0; return arr; } } exports.Target = Target;