@markuplint/file-resolver 3.4.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var _a, _b;
26
25
  Object.defineProperty(exports, "__esModule", { value: true });
27
26
  exports.autoLoadRules = void 0;
28
27
  const ml_core_1 = require("@markuplint/ml-core");
@@ -41,7 +40,7 @@ async function autoLoadRules(ruleset) {
41
40
  }
42
41
  let seed = null;
43
42
  try {
44
- const _module = await (_a = `@markuplint/rule-${ruleName}`, Promise.resolve().then(() => __importStar(require(_a))));
43
+ const _module = await Promise.resolve(`${`@markuplint/rule-${ruleName}`}`).then(s => __importStar(require(s)));
45
44
  seed = _module.default;
46
45
  if (!(seed && 'defaultValue' in seed && 'defaultOptions' in seed && 'verify' in seed)) {
47
46
  seed = null;
@@ -60,7 +59,7 @@ async function autoLoadRules(ruleset) {
60
59
  continue;
61
60
  }
62
61
  try {
63
- const _module = await (_b = `markuplint-rule-${ruleName}`, Promise.resolve().then(() => __importStar(require(_b))));
62
+ const _module = await Promise.resolve(`${`markuplint-rule-${ruleName}`}`).then(s => __importStar(require(s)));
64
63
  seed = _module.default;
65
64
  if (!(seed && 'defaultValue' in seed && 'defaultOptions' in seed && 'verify' in seed)) {
66
65
  seed = null;
@@ -1,10 +1,11 @@
1
1
  import type { MLFile } from './ml-file';
2
- import type { ConfigSet, Nullable } from './types';
2
+ import type { ConfigSet } from './types';
3
3
  import type { Config } from '@markuplint/ml-config';
4
+ import type { Nullable } from '@markuplint/shared';
4
5
  export declare class ConfigProvider {
5
6
  #private;
6
- resolve(targetFile: MLFile, names: Nullable<string>[], cache?: boolean): Promise<ConfigSet>;
7
- search(targetFile: MLFile): Promise<string | null>;
7
+ resolve(targetFile: Readonly<MLFile>, names: readonly Nullable<string>[], cache?: boolean): Promise<ConfigSet>;
8
+ search(targetFile: Readonly<MLFile>): Promise<string | null>;
8
9
  set(config: Config, key?: string): string;
9
10
  private _load;
10
11
  private _mergeConfigs;
@@ -6,6 +6,7 @@ const tslib_1 = require("tslib");
6
6
  const path_1 = tslib_1.__importDefault(require("path"));
7
7
  const ml_config_1 = require("@markuplint/ml-config");
8
8
  const ml_core_1 = require("@markuplint/ml-core");
9
+ const shared_1 = require("@markuplint/shared");
9
10
  const cosmiconfig_1 = require("./cosmiconfig");
10
11
  const resolve_plugins_1 = require("./resolve-plugins");
11
12
  const utils_1 = require("./utils");
@@ -18,13 +19,13 @@ class ConfigProvider {
18
19
  _ConfigProvider_store.set(this, new Map());
19
20
  }
20
21
  async resolve(targetFile, names, cache = true) {
21
- var _a;
22
+ var _a, _b;
22
23
  if (!cache) {
23
24
  tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").clear();
24
25
  tslib_1.__classPrivateFieldGet(this, _ConfigProvider_cache, "f").clear();
25
26
  (0, resolve_plugins_1.cacheClear)();
26
27
  }
27
- const keys = names.filter(utils_1.nonNullableFilter);
28
+ const keys = names.filter(shared_1.nonNullableFilter);
28
29
  const key = keys.join(KEY_SEPARATOR);
29
30
  const currentConfig = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_cache, "f").get(key);
30
31
  if (currentConfig) {
@@ -32,14 +33,14 @@ class ConfigProvider {
32
33
  }
33
34
  let configSet = await this._mergeConfigs(keys, cache);
34
35
  const plugins = await (0, resolve_plugins_1.resolvePlugins)(configSet.config.plugins);
35
- if (tslib_1.__classPrivateFieldGet(this, _ConfigProvider_held, "f").size) {
36
+ if (tslib_1.__classPrivateFieldGet(this, _ConfigProvider_held, "f").size > 0) {
36
37
  const extendHelds = Array.from(tslib_1.__classPrivateFieldGet(this, _ConfigProvider_held, "f").values());
37
38
  for (const held of extendHelds) {
38
- const [, prefix, namespace, name] = held.match(/^([a-z]+:)([^/]+)(?:\/(.+))?$/) || [];
39
+ const [, prefix, namespace, name] = (_a = held.match(/^([a-z]+:)([^/]+)(?:\/(.+))?$/)) !== null && _a !== void 0 ? _a : [];
39
40
  switch (prefix) {
40
41
  case 'plugin:': {
41
42
  const plugin = plugins.find(plugin => plugin.name === namespace);
42
- const config = name && ((_a = plugin === null || plugin === void 0 ? void 0 : plugin.configs) === null || _a === void 0 ? void 0 : _a[name]);
43
+ const config = (_b = plugin === null || plugin === void 0 ? void 0 : plugin.configs) === null || _b === void 0 ? void 0 : _b[name !== null && name !== void 0 ? name : ''];
43
44
  if (config) {
44
45
  this.set(config, held);
45
46
  }
@@ -84,17 +85,18 @@ class ConfigProvider {
84
85
  return filePath;
85
86
  }
86
87
  set(config, key) {
87
- key = key || (0, utils_1.uuid)();
88
+ key = key !== null && key !== void 0 ? key : (0, utils_1.uuid)();
88
89
  tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(key, config);
89
90
  return key;
90
91
  }
91
92
  async _load(filePath, cache) {
93
+ var _a;
92
94
  const entity = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(filePath);
93
95
  if (entity) {
94
96
  return entity;
95
97
  }
96
98
  if (isPreset(filePath)) {
97
- const [, name] = filePath.match(/^markuplint:(.+)$/i) || [];
99
+ const [, name] = (_a = filePath.match(/^markuplint:(.+)$/i)) !== null && _a !== void 0 ? _a : [];
98
100
  const config = await (0, ml_core_1.getPreset)(name !== null && name !== void 0 ? name : filePath);
99
101
  const pathResolvedConfig = this._pathResolve(config, filePath);
100
102
  tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
@@ -130,7 +132,7 @@ class ConfigProvider {
130
132
  }
131
133
  const configs = Array.from(resolvedKeys)
132
134
  .map(name => tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(name))
133
- .filter(utils_1.nonNullableFilter);
135
+ .filter(shared_1.nonNullableFilter);
134
136
  let resultConfig = {};
135
137
  for (const config of configs) {
136
138
  resultConfig = (0, ml_config_1.mergeConfig)(resultConfig, config);
@@ -154,6 +156,7 @@ class ConfigProvider {
154
156
  };
155
157
  }
156
158
  async _recursiveLoad(key, cache, depth = 1) {
159
+ var _a;
157
160
  const stack = new Set();
158
161
  const errs = [];
159
162
  const ancestorDepth = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").get(key);
@@ -164,14 +167,14 @@ class ConfigProvider {
164
167
  };
165
168
  }
166
169
  tslib_1.__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").set(key, depth);
167
- let config = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(key) || null;
170
+ let config = (_a = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(key)) !== null && _a !== void 0 ? _a : null;
168
171
  if (!config) {
169
172
  config = await this._load(key, cache);
170
173
  }
171
174
  if (!config) {
172
175
  return { stack, errs: null };
173
176
  }
174
- const depKeys = config.extends ? (Array.isArray(config.extends) ? config.extends : [config.extends]) : null;
177
+ const depKeys = config.extends !== null ? (0, shared_1.toNoEmptyStringArrayFromStringOrArray)(config.extends) : null;
175
178
  if (depKeys) {
176
179
  for (const depKey of depKeys) {
177
180
  const keys = await this._recursiveLoad(depKey, cache, depth + 1);
@@ -192,8 +195,8 @@ _ConfigProvider_cache = new WeakMap(), _ConfigProvider_held = new WeakMap(), _Co
192
195
  async function load(filePath, cache) {
193
196
  var _a;
194
197
  if (!(0, utils_1.fileExists)(filePath) && moduleExists(filePath)) {
195
- const mod = await (_a = filePath, Promise.resolve().then(() => tslib_1.__importStar(require(_a))));
196
- const config = (mod === null || mod === void 0 ? void 0 : mod.default) || null;
198
+ const mod = await Promise.resolve(`${filePath}`).then(s => tslib_1.__importStar(require(s)));
199
+ const config = (_a = mod === null || mod === void 0 ? void 0 : mod.default) !== null && _a !== void 0 ? _a : null;
197
200
  return config;
198
201
  }
199
202
  const res = await (0, cosmiconfig_1.load)(filePath, !cache);
@@ -8,6 +8,7 @@ const path_1 = tslib_1.__importDefault(require("path"));
8
8
  const minimatch_1 = tslib_1.__importDefault(require("minimatch"));
9
9
  class MLFile {
10
10
  constructor(target) {
11
+ var _a, _b;
11
12
  _MLFile_basename.set(this, void 0);
12
13
  _MLFile_code.set(this, void 0);
13
14
  _MLFile_dirname.set(this, void 0);
@@ -30,8 +31,8 @@ class MLFile {
30
31
  tslib_1.__classPrivateFieldSet(this, _MLFile_dirname, path_1.default.dirname(target.name), "f");
31
32
  }
32
33
  else {
33
- tslib_1.__classPrivateFieldSet(this, _MLFile_basename, target.name || '<AnonymousFile>', "f");
34
- tslib_1.__classPrivateFieldSet(this, _MLFile_dirname, target.workspace || process.cwd(), "f");
34
+ tslib_1.__classPrivateFieldSet(this, _MLFile_basename, (_a = target.name) !== null && _a !== void 0 ? _a : '<AnonymousFile>', "f");
35
+ tslib_1.__classPrivateFieldSet(this, _MLFile_dirname, (_b = target.workspace) !== null && _b !== void 0 ? _b : process.cwd(), "f");
35
36
  }
36
37
  tslib_1.__classPrivateFieldSet(this, _MLFile_code, target.sourceCode, "f");
37
38
  tslib_1.__classPrivateFieldSet(this, _MLFile_type, 'code-base', "f");
@@ -1,3 +1,3 @@
1
1
  import type { MLFile } from './ml-file';
2
2
  import type { Target } from './types';
3
- export declare function resolveFiles(targetList: Target[]): Promise<MLFile[]>;
3
+ export declare function resolveFiles(targetList: readonly Readonly<Target>[]): Promise<MLFile[]>;
@@ -2,7 +2,7 @@ import type { MLFile } from './ml-file';
2
2
  import type { MLMarkupLanguageParser, ParserOptions } from '@markuplint/ml-ast';
3
3
  import type { ParserConfig } from '@markuplint/ml-config';
4
4
  export declare function resolveParser(
5
- file: MLFile,
5
+ file: Readonly<MLFile>,
6
6
  parserConfig?: ParserConfig,
7
7
  parserOptions?: ParserOptions,
8
8
  ): Promise<{
@@ -10,20 +10,18 @@ async function resolveParser(file, parserConfig, parserOptions) {
10
10
  ...parserConfig,
11
11
  '/\\.html?$/i': '@markuplint/html-parser',
12
12
  };
13
- parserOptions = parserOptions || {};
13
+ parserOptions = parserOptions !== null && parserOptions !== void 0 ? parserOptions : {};
14
14
  let parserModName = '@markuplint/html-parser';
15
15
  let matched = false;
16
- if (parserConfig) {
17
- for (const pattern of Object.keys(parserConfig)) {
18
- if (path_1.default.basename(file.path).match((0, utils_1.toRegexp)(pattern))) {
19
- const modName = parserConfig[pattern];
20
- if (!modName) {
21
- continue;
22
- }
23
- parserModName = modName;
24
- matched = true;
25
- break;
16
+ for (const pattern of Object.keys(parserConfig)) {
17
+ if (path_1.default.basename(file.path).match((0, utils_1.toRegexp)(pattern))) {
18
+ const modName = parserConfig[pattern];
19
+ if (!modName) {
20
+ continue;
26
21
  }
22
+ parserModName = modName;
23
+ matched = true;
24
+ break;
27
25
  }
28
26
  }
29
27
  const parser = await importParser(parserModName);
@@ -36,11 +34,10 @@ async function resolveParser(file, parserConfig, parserOptions) {
36
34
  }
37
35
  exports.resolveParser = resolveParser;
38
36
  async function importParser(parserModName) {
39
- var _a;
40
37
  const entity = parsers.get(parserModName);
41
38
  if (entity) {
42
39
  return entity;
43
40
  }
44
- const parser = await (_a = parserModName, Promise.resolve().then(() => tslib_1.__importStar(require(_a))));
41
+ const parser = await Promise.resolve(`${parserModName}`).then(s => tslib_1.__importStar(require(s)));
45
42
  return parser;
46
43
  }
@@ -1,4 +1,4 @@
1
1
  import type { PluginConfig } from '@markuplint/ml-config';
2
2
  import type { Plugin } from '@markuplint/ml-core';
3
- export declare function resolvePlugins(pluginPaths?: (string | PluginConfig)[]): Promise<Plugin[]>;
3
+ export declare function resolvePlugins(pluginPaths?: readonly (string | PluginConfig)[]): Promise<Plugin[]>;
4
4
  export declare function cacheClear(): void;
@@ -55,15 +55,19 @@ async function importPlugin(pluginPath) {
55
55
  ...pluginCreator.create(config.settings),
56
56
  };
57
57
  cache.set(plugin.name, plugin);
58
- if (!plugin.name) {
59
- plugin.name = config.name
58
+ let name = plugin.name;
59
+ if (!name) {
60
+ name = config.name
60
61
  .toLowerCase()
61
62
  .replace(/^(?:markuplint-rule-|@markuplint\/rule-)/i, '')
62
63
  .replace(/\s+|\/|\\|\./g, '-');
63
64
  // eslint-disable-next-line no-console
64
- console.info(`The plugin name became "${plugin.name}"`);
65
+ console.info(`The plugin name became "${name}"`);
65
66
  }
66
- return plugin;
67
+ return {
68
+ ...plugin,
69
+ name,
70
+ };
67
71
  }
68
72
  function getPluginConfig(pluginPath) {
69
73
  if (typeof pluginPath === 'string') {
@@ -72,8 +76,7 @@ function getPluginConfig(pluginPath) {
72
76
  return pluginPath;
73
77
  }
74
78
  async function failSafeImport(name) {
75
- var _a;
76
- const res = await (_a = name, Promise.resolve().then(() => __importStar(require(_a)))).catch(e => e);
79
+ const res = await Promise.resolve(`${name}`).then(s => __importStar(require(s))).catch(e => e);
77
80
  if ('code' in res && res === 'MODULE_NOT_FOUND') {
78
81
  return null;
79
82
  }
@@ -1,11 +1,10 @@
1
1
  import type { AnyMLRule, Ruleset, Plugin } from '@markuplint/ml-core';
2
- import { MLRule } from '@markuplint/ml-core';
3
2
  export declare function resolveRules(
4
- plugins: Plugin[],
3
+ plugins: readonly Plugin[],
5
4
  ruleset: Ruleset,
6
5
  importPreset: boolean,
7
6
  /**
8
7
  * @deprecated
9
8
  */
10
9
  autoLoad: boolean,
11
- ): Promise<AnyMLRule[] | MLRule<any, any>[]>;
10
+ ): Promise<Readonly<AnyMLRule>[]>;
@@ -56,12 +56,11 @@ autoLoad) {
56
56
  }
57
57
  exports.resolveRules = resolveRules;
58
58
  async function importPresetRules() {
59
- var _a;
60
59
  if (cachedPresetRules) {
61
60
  return cachedPresetRules.slice();
62
61
  }
63
62
  const modName = '@markuplint/rules';
64
- const presetRules = (await (_a = modName, Promise.resolve().then(() => __importStar(require(_a))))).default;
63
+ const presetRules = (await Promise.resolve(`${modName}`).then(s => __importStar(require(s)))).default;
65
64
  const ruleList = Object.entries(presetRules).map(([name, seed]) => {
66
65
  const rule = new ml_core_1.MLRule({
67
66
  name,
@@ -43,12 +43,6 @@ async function resolveSpecs(filePath, specConfig) {
43
43
  const spec = await importSpecs(specConfig);
44
44
  extendedSpecs.push(spec);
45
45
  }
46
- else if (Array.isArray(specConfig)) {
47
- for (const specModName of specConfig) {
48
- const spec = await importSpecs(specModName);
49
- extendedSpecs.push(spec);
50
- }
51
- }
52
46
  else {
53
47
  for (const pattern of Object.keys(specConfig)) {
54
48
  if (path_1.default.basename(filePath).match((0, utils_1.toRegexp)(pattern))) {
@@ -69,7 +63,6 @@ async function resolveSpecs(filePath, specConfig) {
69
63
  }
70
64
  exports.resolveSpecs = resolveSpecs;
71
65
  async function importSpecs(specModName) {
72
- var _a;
73
66
  {
74
67
  // @ts-ignore
75
68
  const spec = caches.get(specModName);
@@ -77,7 +70,7 @@ async function importSpecs(specModName) {
77
70
  return spec;
78
71
  }
79
72
  }
80
- const spec = (await (_a = specModName, Promise.resolve().then(() => tslib_1.__importStar(require(_a))))).default;
73
+ const spec = (await Promise.resolve(`${specModName}`).then(s => tslib_1.__importStar(require(s)))).default;
81
74
  // @ts-ignore
82
75
  caches.set(specModName, spec);
83
76
  return spec;
package/lib/types.d.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import type { Config } from '@markuplint/ml-config';
2
2
  import type { Plugin } from '@markuplint/ml-core';
3
- export type Nullable<T> = T | null | undefined;
4
3
  export interface ConfigSet {
5
- config: Config;
6
- plugins: Plugin[];
7
- files: Set<string>;
8
- errs: Error[];
4
+ readonly config: Config;
5
+ readonly plugins: readonly Plugin[];
6
+ readonly files: ReadonlySet<string>;
7
+ readonly errs: readonly Readonly<Error>[];
9
8
  }
10
9
  export type Target =
11
10
  | string
@@ -13,13 +12,13 @@ export type Target =
13
12
  /**
14
13
  * Target source code of evaluation
15
14
  */
16
- sourceCode: string;
15
+ readonly sourceCode: string;
17
16
  /**
18
17
  * File names when `sourceCodes`
19
18
  */
20
- name?: string;
19
+ readonly name?: string;
21
20
  /**
22
21
  * Workspace path when `sourceCodes`
23
22
  */
24
- workspace?: string;
23
+ readonly workspace?: string;
25
24
  };
package/lib/utils.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import type { Nullable } from './types';
2
- export declare function nonNullableFilter<T>(item: Nullable<T>): item is T;
3
1
  export declare function uuid(): string;
4
2
  export declare function fileExists(filePath: string): boolean;
5
3
  export declare function toRegexp(pattern: string): string | RegExp;
package/lib/utils.js CHANGED
@@ -1,12 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toRegexp = exports.fileExists = exports.uuid = exports.nonNullableFilter = void 0;
3
+ exports.toRegexp = exports.fileExists = exports.uuid = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fs_1 = tslib_1.__importDefault(require("fs"));
6
- function nonNullableFilter(item) {
7
- return !!item;
8
- }
9
- exports.nonNullableFilter = nonNullableFilter;
10
6
  let uuidNum = 0;
11
7
  function uuid() {
12
8
  const out = `${uuidNum}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/file-resolver",
3
- "version": "3.4.0",
3
+ "version": "3.6.0",
4
4
  "description": "The file resolver of markuplint",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -24,16 +24,17 @@
24
24
  "@types/node": "latest"
25
25
  },
26
26
  "dependencies": {
27
- "@markuplint/html-parser": "3.4.0",
28
- "@markuplint/ml-ast": "3.0.0",
29
- "@markuplint/ml-config": "3.4.0",
30
- "@markuplint/ml-core": "3.4.0",
31
- "@markuplint/ml-spec": "3.4.0",
27
+ "@markuplint/html-parser": "3.6.0",
28
+ "@markuplint/ml-ast": "3.1.0",
29
+ "@markuplint/ml-config": "3.6.0",
30
+ "@markuplint/ml-core": "3.6.0",
31
+ "@markuplint/ml-spec": "3.6.0",
32
+ "@markuplint/shared": "3.5.0",
32
33
  "cosmiconfig": "^8.0.0",
33
34
  "glob": "^8.0.3",
34
35
  "jsonc": "^2.0.0",
35
36
  "minimatch": "^5.1.1",
36
37
  "tslib": "^2.4.1"
37
38
  },
38
- "gitHead": "a83e0f5f214a9bbcc0286b9e269074ddca6189e7"
39
+ "gitHead": "715dd53d3b1064a9bcf616c1533921cad9e3b187"
39
40
  }
@@ -1,22 +0,0 @@
1
- import type { OptimizedConfig, Pretender } from '@markuplint/ml-config';
2
- type PretendersConfig = OptimizedConfig['pretenders'];
3
- type PretenderWatchCallback = (pretenders: Pretender[]) => void;
4
- export declare class PretenderProvider {
5
- #private;
6
- getPretenders(): Promise<Pretender[]>;
7
- setConfig(config: PretendersConfig): Promise<void>;
8
- /**
9
- * Stops watching.
10
- * It does nothing if it is not in watching mode or it doesn't have scanning files.
11
- */
12
- unwatch(): Promise<void>;
13
- /**
14
- * Starts watching related files.
15
- * It passes the results of pretenders to callback every file changed.
16
- * It requires calling `setConfig` method in advance before calling this.
17
- *
18
- * @param callback
19
- */
20
- watch(callback: PretenderWatchCallback): Promise<void>;
21
- }
22
- export {};
@@ -1,146 +0,0 @@
1
- "use strict";
2
- var _PretenderProvider_staticData, _PretenderProvider_scanners, _PretenderProvider_watchCallback, _Scanner_cache, _Scanner_options, _Scanner_watcher;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.PretenderProvider = void 0;
5
- const tslib_1 = require("tslib");
6
- const cache_1 = require("@markuplint/cache");
7
- const watcher_1 = require("./watcher");
8
- const DIDNT_SPECIFY_CONFIG_ERROR_MSG = 'Cannot return pretenders because it did not specify the configuration yet. It might be the wrong order to call this method';
9
- class PretenderProvider {
10
- constructor() {
11
- _PretenderProvider_staticData.set(this, null);
12
- _PretenderProvider_scanners.set(this, []);
13
- _PretenderProvider_watchCallback.set(this, null);
14
- }
15
- async getPretenders() {
16
- if (!tslib_1.__classPrivateFieldGet(this, _PretenderProvider_staticData, "f")) {
17
- throw new ReferenceError(DIDNT_SPECIFY_CONFIG_ERROR_MSG);
18
- }
19
- const result = [...tslib_1.__classPrivateFieldGet(this, _PretenderProvider_staticData, "f")];
20
- for (const scanner of tslib_1.__classPrivateFieldGet(this, _PretenderProvider_scanners, "f")) {
21
- const pretenders = await scanner.scan();
22
- result.push(...pretenders);
23
- }
24
- return result;
25
- }
26
- async setConfig(config) {
27
- const [data, scanConfig] = await resolvePretendersConfig(config);
28
- tslib_1.__classPrivateFieldSet(this, _PretenderProvider_staticData, data, "f");
29
- if (scanConfig) {
30
- for (const scan of scanConfig) {
31
- if (scan.type === 'jsx') {
32
- const { jsxScanner } = await Promise.resolve().then(() => tslib_1.__importStar(require('@markuplint/pretenders/lib/jsx')));
33
- const scanner = new Scanner(scan.type, scan.files, jsxScanner, scan.options);
34
- tslib_1.__classPrivateFieldGet(this, _PretenderProvider_scanners, "f").push(scanner);
35
- }
36
- }
37
- }
38
- // It regards to be enabled watch mode if it has `#watchCallback`.
39
- // And call the watch method then add the callback to each scanner.
40
- if (tslib_1.__classPrivateFieldGet(this, _PretenderProvider_watchCallback, "f")) {
41
- await this.watch(tslib_1.__classPrivateFieldGet(this, _PretenderProvider_watchCallback, "f"));
42
- }
43
- }
44
- /**
45
- * Stops watching.
46
- * It does nothing if it is not in watching mode or it doesn't have scanning files.
47
- */
48
- async unwatch() {
49
- for (const scanner of tslib_1.__classPrivateFieldGet(this, _PretenderProvider_scanners, "f")) {
50
- await scanner.unwatch();
51
- }
52
- }
53
- /**
54
- * Starts watching related files.
55
- * It passes the results of pretenders to callback every file changed.
56
- * It requires calling `setConfig` method in advance before calling this.
57
- *
58
- * @param callback
59
- */
60
- async watch(callback) {
61
- tslib_1.__classPrivateFieldSet(this, _PretenderProvider_watchCallback, callback, "f");
62
- for (const scanner of tslib_1.__classPrivateFieldGet(this, _PretenderProvider_scanners, "f")) {
63
- await scanner.watch(async () => {
64
- var _a;
65
- if (!tslib_1.__classPrivateFieldGet(this, _PretenderProvider_staticData, "f")) {
66
- throw new ReferenceError(DIDNT_SPECIFY_CONFIG_ERROR_MSG);
67
- }
68
- const allPretenders = await this.getPretenders();
69
- (_a = tslib_1.__classPrivateFieldGet(this, _PretenderProvider_watchCallback, "f")) === null || _a === void 0 ? void 0 : _a.call(this, allPretenders);
70
- });
71
- }
72
- }
73
- }
74
- exports.PretenderProvider = PretenderProvider;
75
- _PretenderProvider_staticData = new WeakMap(), _PretenderProvider_scanners = new WeakMap(), _PretenderProvider_watchCallback = new WeakMap();
76
- class Scanner {
77
- constructor(type, glob, method, options) {
78
- _Scanner_cache.set(this, void 0);
79
- _Scanner_options.set(this, void 0);
80
- _Scanner_watcher.set(this, null);
81
- tslib_1.__classPrivateFieldSet(this, _Scanner_options, options, "f");
82
- tslib_1.__classPrivateFieldSet(this, _Scanner_cache, new cache_1.Cache(type, glob, files => {
83
- return method(Array.from(files), options);
84
- }), "f");
85
- }
86
- /**
87
- * Scans component files.
88
- *
89
- * @param newFileList
90
- * @returns
91
- */
92
- async scan() {
93
- const pretenders = await tslib_1.__classPrivateFieldGet(this, _Scanner_cache, "f").get();
94
- return pretenders;
95
- }
96
- /**
97
- * Stops watching.
98
- * It does nothing if it is not in watching mode.
99
- */
100
- async unwatch() {
101
- var _a;
102
- await ((_a = tslib_1.__classPrivateFieldGet(this, _Scanner_watcher, "f")) === null || _a === void 0 ? void 0 : _a.close());
103
- }
104
- /**
105
- * Starts watching related files.
106
- * It passes the results of pretenders to callback every file changed.
107
- *
108
- * @param callback
109
- */
110
- async watch(callback) {
111
- tslib_1.__classPrivateFieldSet(this, _Scanner_watcher, await watcher_1.Watcher.create(tslib_1.__classPrivateFieldGet(this, _Scanner_cache, "f").glob, tslib_1.__classPrivateFieldGet(this, _Scanner_options, "f")), "f");
112
- tslib_1.__classPrivateFieldGet(this, _Scanner_watcher, "f").addChangeListener(async (file, type) => {
113
- const pretenders = type === 'unlink' ? await tslib_1.__classPrivateFieldGet(this, _Scanner_cache, "f").delete(file) : await tslib_1.__classPrivateFieldGet(this, _Scanner_cache, "f").update(file);
114
- if (!pretenders) {
115
- return;
116
- }
117
- callback();
118
- });
119
- }
120
- }
121
- _Scanner_cache = new WeakMap(), _Scanner_options = new WeakMap(), _Scanner_watcher = new WeakMap();
122
- function resolvePretendersConfig(config) {
123
- if (!config) {
124
- return Promise.resolve([[], []]);
125
- }
126
- const data = [];
127
- if (config.files) {
128
- for (const file of config.files) {
129
- // eslint-disable-next-line @typescript-eslint/no-var-requires
130
- const pretenderFile = require(file);
131
- data.push(...pretenderFile.data);
132
- }
133
- }
134
- // Experimental
135
- if (config.imports) {
136
- for (const module of config.imports) {
137
- // eslint-disable-next-line @typescript-eslint/no-var-requires
138
- const pretenderFile = require(`${module}/pretenders.json`);
139
- data.push(...pretenderFile.data);
140
- }
141
- }
142
- if (config.data) {
143
- data.push(...config.data);
144
- }
145
- return Promise.resolve([data, config.scan]);
146
- }
@@ -1,4 +0,0 @@
1
- import type { OptimizedConfig, Pretender } from '@markuplint/ml-config';
2
- type PretendersConfig = OptimizedConfig['pretenders'];
3
- export declare function resolvePretenders(config: PretendersConfig): Promise<Pretender[]>;
4
- export {};
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolvePretenders = void 0;
4
- async function resolvePretenders(config) {
5
- if (!config) {
6
- return [];
7
- }
8
- const data = [];
9
- if (config.files) {
10
- for (const file of config.files) {
11
- // eslint-disable-next-line @typescript-eslint/no-var-requires
12
- const pretenderFile = require(file);
13
- data.push(...pretenderFile.data);
14
- }
15
- }
16
- // Experimental
17
- if (config.imports) {
18
- for (const module of config.imports) {
19
- // eslint-disable-next-line @typescript-eslint/no-var-requires
20
- const pretenderFile = require(`${module}/pretenders.json`);
21
- data.push(...pretenderFile.data);
22
- }
23
- }
24
- if (config.data) {
25
- data.push(...config.data);
26
- }
27
- // It wraps `Promise` currently due to needing to use `import()` someday.
28
- return await Promise.resolve(data);
29
- }
30
- exports.resolvePretenders = resolvePretenders;
package/lib/watcher.d.ts DELETED
@@ -1,15 +0,0 @@
1
- type Options = {
2
- cwd?: string;
3
- };
4
- type ChangeListener = (this: Watcher, filePath: string, type: 'change' | 'add' | 'unlink') => void;
5
- export declare class Watcher {
6
- #private;
7
- static create(pattern: string, options?: Options): Promise<Watcher>;
8
- private constructor();
9
- get files(): string[];
10
- addChangeListener(changeListener: ChangeListener): void;
11
- close(): Promise<void>;
12
- private onEvent;
13
- private update;
14
- }
15
- export {};
package/lib/watcher.js DELETED
@@ -1,64 +0,0 @@
1
- "use strict";
2
- var _Watcher_changeListener, _Watcher_cwd, _Watcher_filesObserver;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.Watcher = void 0;
5
- const tslib_1 = require("tslib");
6
- const node_path_1 = tslib_1.__importDefault(require("node:path"));
7
- const chokidar_1 = require("chokidar");
8
- const watcherOptions = {
9
- persistent: true,
10
- ignored: ['**/.git/**/*', '**/node_modules/**/*'],
11
- };
12
- class Watcher {
13
- static async create(pattern, options) {
14
- const watcher = new Watcher(options);
15
- await watcher.update(pattern);
16
- return watcher;
17
- }
18
- constructor(options) {
19
- var _a;
20
- _Watcher_changeListener.set(this, null);
21
- _Watcher_cwd.set(this, void 0);
22
- _Watcher_filesObserver.set(this, new chokidar_1.FSWatcher(watcherOptions));
23
- tslib_1.__classPrivateFieldSet(this, _Watcher_cwd, (_a = options === null || options === void 0 ? void 0 : options.cwd) !== null && _a !== void 0 ? _a : process.cwd(), "f");
24
- }
25
- get files() {
26
- return Object.entries(tslib_1.__classPrivateFieldGet(this, _Watcher_filesObserver, "f").getWatched())
27
- .map(([dir, files]) => files.map(file => node_path_1.default.join(dir, file)))
28
- .flat()
29
- .sort();
30
- }
31
- addChangeListener(changeListener) {
32
- tslib_1.__classPrivateFieldSet(this, _Watcher_changeListener, changeListener, "f");
33
- }
34
- async close() {
35
- await tslib_1.__classPrivateFieldGet(this, _Watcher_filesObserver, "f").close();
36
- }
37
- onEvent(eventName, filePath) {
38
- var _a;
39
- switch (eventName) {
40
- case 'change':
41
- case 'add':
42
- case 'unlink':
43
- (_a = tslib_1.__classPrivateFieldGet(this, _Watcher_changeListener, "f")) === null || _a === void 0 ? void 0 : _a.call(this, filePath, eventName);
44
- break;
45
- default:
46
- break;
47
- }
48
- }
49
- async update(pattern) {
50
- const absPattern = node_path_1.default.isAbsolute(pattern) ? pattern : node_path_1.default.resolve(tslib_1.__classPrivateFieldGet(this, _Watcher_cwd, "f"), pattern);
51
- await tslib_1.__classPrivateFieldGet(this, _Watcher_filesObserver, "f").close();
52
- tslib_1.__classPrivateFieldGet(this, _Watcher_filesObserver, "f").add(absPattern);
53
- return new Promise(resolve => {
54
- const onReady = () => {
55
- tslib_1.__classPrivateFieldGet(this, _Watcher_filesObserver, "f").on('all', this.onEvent.bind(this));
56
- tslib_1.__classPrivateFieldGet(this, _Watcher_filesObserver, "f").off('ready', onReady);
57
- resolve();
58
- };
59
- tslib_1.__classPrivateFieldGet(this, _Watcher_filesObserver, "f").on('ready', onReady);
60
- });
61
- }
62
- }
63
- exports.Watcher = Watcher;
64
- _Watcher_changeListener = new WeakMap(), _Watcher_cwd = new WeakMap(), _Watcher_filesObserver = new WeakMap();