UNPKG

@kcws/lintstaged-config

Version:
273 lines 9.26 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (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()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Config = void 0; const default_1 = require("../constants/default"); const array_1 = require("../utils/array"); const promise_1 = require("../utils/promise"); const map_1 = require("../utils/map"); const DEBUG_MODE = "debug"; /** * Config builder. This class should not be created manually. * Use {@link Config.builder} or {@link Config.default} instead. * * @public */ class Builder { /** * Do not use this constructor directly. * Use {@link Config.builder} or {@link Config.default} instead. */ constructor() { this._result = new Map(); this._settings = new Map(); } /** * enable debug mode when loading configuration * * @returns this object * * @public */ debugMode() { this._settings.set(DEBUG_MODE, "true"); return this; } /** * append default actions to current builder * * @returns this object * * @public */ default(custom) { return (0, default_1.defineDefaultConfig)(this, custom); } /** * append value to list of regex or actions if needed. * If input key never created yet, * it will create with input value * * @param key - group key * @param value - config value associate with input key * @returns this object * * @public */ append(key, value) { if (this._result.has(key)) { const cached = this._result.get(key); return this.set(key, this._mergeValue(cached, value)); } else { return this.set(key, value); } } /** * add new config group to current configuration * * @param key - group key * @param value - config value associate with input key * @returns this object * * @public */ set(key, value) { this._result.set(key, this._fillValue(value)); return this; } /** * build Config with current builder * * @returns this object * * @public */ build() { // I disable eslint here because Config is not used yet // eslint-disable-next-line @typescript-eslint/no-use-before-define return new Config(this._result, this._settings); } /** * delete group by key * * @param key - group key * @returns this object * * @public */ delete(key) { this._result.delete(key); return this; } _fillValue(value) { var _a, _b, _c; return { regexs: (_a = value.regexs) !== null && _a !== void 0 ? _a : [], actions: (_b = value.actions) !== null && _b !== void 0 ? _b : [], actionFn: (_c = value.actionFn) !== null && _c !== void 0 ? _c : (() => []), }; } _mergeValue(value, addon) { var _a, _b; const a = (0, array_1.toArray)(value.actions).map(action => (0, promise_1.toPromise)(action).then(array_1.toArray)); const b = (0, array_1.toArray)(addon.actions).map(action => (0, promise_1.toPromise)(action).then(array_1.toArray)); return { regexs: [...value.regexs, ...((_a = addon.regexs) !== null && _a !== void 0 ? _a : [])], actions: [...a, ...b], actionFn: (_b = addon.actionFn) !== null && _b !== void 0 ? _b : value.actionFn, }; } } /** * Configuration object to generate list of command * needed for specify values on {@link ConfigCondition} * * @public */ class Config { /** * create config builder with empty value. * * @returns config builder * * @public */ static builder() { return new Builder(); } /** * create config builder with default group predefined. * * @returns config builder */ static default() { return new Builder().default(); } constructor(config, settings) { this._config = config; this._settings = settings; } /** * length of configuration. * * @remarks * This can be check to ensure * we setting config object correctly * * @public */ get length() { return this._config.size; } /** * is debug mode enabled? */ get _isDebug() { return (this._settings.has(DEBUG_MODE) && this._settings.get(DEBUG_MODE) === "true"); } /** * {@inheritDoc IConfig.getCommands} * @override */ getCommands(condition) { var _a, e_1, _b, _c; var _d; return __awaiter(this, void 0, void 0, function* () { const results = []; try { for (var _e = true, _f = __asyncValues(this._config.entries()), _g; _g = yield _f.next(), _a = _g.done, !_a; _e = true) { _c = _g.value; _e = false; const [key, value] = _c; if (this._isDebug) console.log(`verifying ${key}...`); const files = condition(key, value.regexs); if (files.length > 0) { if (this._isDebug) console.log(`found matched files [${files.join(",")}]`); // Resolve static actions const staticActions = yield this._resolveAction(value.actions); if (this._isDebug) console.log(`static action: [${staticActions.join(",")}]`); // Resolve dynamic actions const dynamicActions = yield this._resolveAction((_d = value.actionFn) === null || _d === void 0 ? void 0 : _d.call(value, files)); if (this._isDebug) console.log(`dynamic action: [${dynamicActions.join(",")}]`); results.push(...staticActions, ...dynamicActions); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_e && !_a && (_b = _f.return)) yield _b.call(_f); } finally { if (e_1) throw e_1.error; } } return results; }); } /** * Empty implementation as Config already IConfig, * so we can just return itself. * * @returns this object * @override * * @public */ build() { return this; } /** * Compare current config with another config. * This function WILL ignore all function from comparison * * @param c - another config * @returns true if this config is equal to other config * * @beta */ compare(c) { if (this._settings.size !== c._settings.size) return false; if (!(0, map_1.isSameMap)(this._settings, c._settings)) return false; if (this._config.size !== c._config.size) return false; if (!(0, map_1.isSameMap)(this._config, c._config, (a, b) => a.toString() === b.toString())) return false; return true; } _resolveAction(actions) { return __awaiter(this, void 0, void 0, function* () { if (this._isDebug) console.log(`resolve action: ${actions}`); if (!actions) return []; // Resolve to list of promise of list of string const promises = (0, array_1.toArray)(actions).map(action => (0, promise_1.toPromise)(action).then(array_1.toArray)); // Resolve to promise of list of string return Promise.all(promises).then(v => v.flat()); }); } } exports.Config = Config; //# sourceMappingURL=Config.js.map