@markuplint/file-resolver 3.3.1 → 3.5.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.
@@ -3,6 +3,6 @@ import type { Ruleset, AnyMLRule } from '@markuplint/ml-core';
3
3
  * @deprecated
4
4
  */
5
5
  export declare function autoLoadRules(ruleset: Ruleset): Promise<{
6
- rules: AnyMLRule[];
7
- errors: unknown[];
6
+ rules: AnyMLRule[];
7
+ errors: unknown[];
8
8
  }>;
@@ -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;
@@ -2,12 +2,12 @@ import type { MLFile } from './ml-file';
2
2
  import type { ConfigSet, Nullable } from './types';
3
3
  import type { Config } from '@markuplint/ml-config';
4
4
  export declare class ConfigProvider {
5
- #private;
6
- resolve(targetFile: MLFile, names: Nullable<string>[], cache?: boolean): Promise<ConfigSet>;
7
- search(targetFile: MLFile): Promise<string | null>;
8
- set(config: Config, key?: string): string;
9
- private _load;
10
- private _mergeConfigs;
11
- private _pathResolve;
12
- private _recursiveLoad;
5
+ #private;
6
+ resolve(targetFile: Readonly<MLFile>, names: readonly Nullable<string>[], cache?: boolean): Promise<ConfigSet>;
7
+ search(targetFile: Readonly<MLFile>): Promise<string | null>;
8
+ set(config: Config, key?: string): string;
9
+ private _load;
10
+ private _mergeConfigs;
11
+ private _pathResolve;
12
+ private _recursiveLoad;
13
13
  }
@@ -39,7 +39,7 @@ class ConfigProvider {
39
39
  switch (prefix) {
40
40
  case 'plugin:': {
41
41
  const plugin = plugins.find(plugin => plugin.name === namespace);
42
- const config = (_a = plugin === null || plugin === void 0 ? void 0 : plugin.configs) === null || _a === void 0 ? void 0 : _a[name];
42
+ const config = name && ((_a = plugin === null || plugin === void 0 ? void 0 : plugin.configs) === null || _a === void 0 ? void 0 : _a[name]);
43
43
  if (config) {
44
44
  this.set(config, held);
45
45
  }
@@ -56,9 +56,10 @@ class ConfigProvider {
56
56
  const globs = Object.keys(overrides);
57
57
  for (const glob of globs) {
58
58
  const isMatched = targetFile.matches(glob);
59
- if (isMatched) {
59
+ const config = overrides[glob];
60
+ if (isMatched && config) {
60
61
  // Note: Original config disappears
61
- configSet.config = overrides[glob];
62
+ configSet.config = config;
62
63
  }
63
64
  }
64
65
  }
@@ -94,7 +95,7 @@ class ConfigProvider {
94
95
  }
95
96
  if (isPreset(filePath)) {
96
97
  const [, name] = filePath.match(/^markuplint:(.+)$/i) || [];
97
- const config = await (0, ml_core_1.getPreset)(name);
98
+ const config = await (0, ml_core_1.getPreset)(name !== null && name !== void 0 ? name : filePath);
98
99
  const pathResolvedConfig = this._pathResolve(config, filePath);
99
100
  tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
100
101
  return pathResolvedConfig;
@@ -189,9 +190,8 @@ class ConfigProvider {
189
190
  exports.ConfigProvider = ConfigProvider;
190
191
  _ConfigProvider_cache = new WeakMap(), _ConfigProvider_held = new WeakMap(), _ConfigProvider_recursiveLoadKeyAndDepth = new WeakMap(), _ConfigProvider_store = new WeakMap();
191
192
  async function load(filePath, cache) {
192
- var _a;
193
193
  if (!(0, utils_1.fileExists)(filePath) && moduleExists(filePath)) {
194
- const mod = await (_a = filePath, Promise.resolve().then(() => tslib_1.__importStar(require(_a))));
194
+ const mod = await Promise.resolve(`${filePath}`).then(s => tslib_1.__importStar(require(s)));
195
195
  const config = (mod === null || mod === void 0 ? void 0 : mod.default) || null;
196
196
  return config;
197
197
  }
@@ -1,17 +1,11 @@
1
1
  import type { LoaderSync } from 'cosmiconfig';
2
2
  type CosmiConfig = ReturnType<LoaderSync>;
3
- export declare function search<T = CosmiConfig>(
4
- dir: string,
5
- cacheClear: boolean,
6
- ): Promise<{
7
- filePath: string;
8
- config: T;
3
+ export declare function search<T = CosmiConfig>(dir: string, cacheClear: boolean): Promise<{
4
+ filePath: string;
5
+ config: T;
9
6
  } | null>;
10
- export declare function load<T = CosmiConfig>(
11
- filePath: string,
12
- cacheClear: boolean,
13
- ): Promise<{
14
- filePath: string;
15
- config: T;
7
+ export declare function load<T = CosmiConfig>(filePath: string, cacheClear: boolean): Promise<{
8
+ filePath: string;
9
+ config: T;
16
10
  } | null>;
17
11
  export {};
@@ -1,23 +1,23 @@
1
1
  import type { Target } from '../types';
2
2
  export declare class MLFile {
3
- #private;
4
- constructor(target: Target);
5
- get dirname(): string;
6
- /**
7
- * Normalized `MLFile.dirname`
8
- */
9
- get nDirname(): string;
10
- /**
11
- * Normalized `MLFile.path`
12
- */
13
- get nPath(): string;
14
- get path(): string;
15
- dirExists(): Promise<boolean>;
16
- getCode(): Promise<string>;
17
- isExist(): Promise<boolean>;
18
- isFile(): Promise<boolean>;
19
- matches(globPath: string): boolean;
20
- setCode(code: string): void;
21
- private _fetch;
22
- private _stat;
3
+ #private;
4
+ constructor(target: Target);
5
+ get dirname(): string;
6
+ /**
7
+ * Normalized `MLFile.dirname`
8
+ */
9
+ get nDirname(): string;
10
+ /**
11
+ * Normalized `MLFile.path`
12
+ */
13
+ get nPath(): string;
14
+ get path(): string;
15
+ dirExists(): Promise<boolean>;
16
+ getCode(): Promise<string>;
17
+ isExist(): Promise<boolean>;
18
+ isFile(): Promise<boolean>;
19
+ matches(globPath: string): boolean;
20
+ setCode(code: string): void;
21
+ private _fetch;
22
+ private _stat;
23
23
  }
@@ -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[]>;
@@ -1,13 +1,9 @@
1
1
  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
- export declare function resolveParser(
5
- file: MLFile,
6
- parserConfig?: ParserConfig,
7
- parserOptions?: ParserOptions,
8
- ): Promise<{
9
- parserModName: string;
10
- parser: MLMarkupLanguageParser;
11
- parserOptions: ParserOptions;
12
- matched: boolean;
4
+ export declare function resolveParser(file: Readonly<MLFile>, parserConfig?: ParserConfig, parserOptions?: ParserOptions): Promise<{
5
+ parserModName: string;
6
+ parser: MLMarkupLanguageParser;
7
+ parserOptions: ParserOptions;
8
+ matched: boolean;
13
9
  }>;
@@ -16,7 +16,11 @@ async function resolveParser(file, parserConfig, parserOptions) {
16
16
  if (parserConfig) {
17
17
  for (const pattern of Object.keys(parserConfig)) {
18
18
  if (path_1.default.basename(file.path).match((0, utils_1.toRegexp)(pattern))) {
19
- parserModName = parserConfig[pattern];
19
+ const modName = parserConfig[pattern];
20
+ if (!modName) {
21
+ continue;
22
+ }
23
+ parserModName = modName;
20
24
  matched = true;
21
25
  break;
22
26
  }
@@ -32,11 +36,10 @@ async function resolveParser(file, parserConfig, parserOptions) {
32
36
  }
33
37
  exports.resolveParser = resolveParser;
34
38
  async function importParser(parserModName) {
35
- var _a;
36
39
  const entity = parsers.get(parserModName);
37
40
  if (entity) {
38
41
  return entity;
39
42
  }
40
- const parser = await (_a = parserModName, Promise.resolve().then(() => tslib_1.__importStar(require(_a))));
43
+ const parser = await Promise.resolve(`${parserModName}`).then(s => tslib_1.__importStar(require(s)));
41
44
  return parser;
42
45
  }
@@ -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,6 @@
1
1
  import type { AnyMLRule, Ruleset, Plugin } from '@markuplint/ml-core';
2
- import { MLRule } from '@markuplint/ml-core';
3
- export declare function resolveRules(
4
- plugins: Plugin[],
5
- ruleset: Ruleset,
6
- importPreset: boolean,
7
- /**
8
- * @deprecated
9
- */
10
- autoLoad: boolean,
11
- ): Promise<AnyMLRule[] | MLRule<any, any>[]>;
2
+ export declare function resolveRules(plugins: readonly Plugin[], ruleset: Ruleset, importPreset: boolean,
3
+ /**
4
+ * @deprecated
5
+ */
6
+ autoLoad: boolean): 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,
@@ -30,9 +30,6 @@ import type { ExtendedSpec, MLMLSpec } from '@markuplint/ml-spec';
30
30
  * @param specConfig The `spec` property part of the config
31
31
  * @returns
32
32
  */
33
- export declare function resolveSpecs(
34
- filePath: string,
35
- specConfig?: SpecConfig,
36
- ): Promise<{
37
- schemas: readonly [MLMLSpec, ...ExtendedSpec[]];
33
+ export declare function resolveSpecs(filePath: string, specConfig?: SpecConfig): Promise<{
34
+ schemas: readonly [MLMLSpec, ...ExtendedSpec[]];
38
35
  }>;
@@ -43,16 +43,13 @@ 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))) {
55
49
  const specModName = specConfig[pattern];
50
+ if (!specModName) {
51
+ continue;
52
+ }
56
53
  const spec = await importSpecs(specModName);
57
54
  extendedSpecs.push(spec);
58
55
  }
@@ -66,7 +63,6 @@ async function resolveSpecs(filePath, specConfig) {
66
63
  }
67
64
  exports.resolveSpecs = resolveSpecs;
68
65
  async function importSpecs(specModName) {
69
- var _a;
70
66
  {
71
67
  // @ts-ignore
72
68
  const spec = caches.get(specModName);
@@ -74,7 +70,7 @@ async function importSpecs(specModName) {
74
70
  return spec;
75
71
  }
76
72
  }
77
- 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;
78
74
  // @ts-ignore
79
75
  caches.set(specModName, spec);
80
76
  return spec;
package/lib/types.d.ts CHANGED
@@ -2,24 +2,22 @@ import type { Config } from '@markuplint/ml-config';
2
2
  import type { Plugin } from '@markuplint/ml-core';
3
3
  export type Nullable<T> = T | null | undefined;
4
4
  export interface ConfigSet {
5
- config: Config;
6
- plugins: Plugin[];
7
- files: Set<string>;
8
- errs: Error[];
5
+ readonly config: Config;
6
+ readonly plugins: readonly Plugin[];
7
+ readonly files: ReadonlySet<string>;
8
+ readonly errs: readonly Readonly<Error>[];
9
9
  }
10
- export type Target =
11
- | string
12
- | {
13
- /**
14
- * Target source code of evaluation
15
- */
16
- sourceCode: string;
17
- /**
18
- * File names when `sourceCodes`
19
- */
20
- name?: string;
21
- /**
22
- * Workspace path when `sourceCodes`
23
- */
24
- workspace?: string;
25
- };
10
+ export type Target = string | {
11
+ /**
12
+ * Target source code of evaluation
13
+ */
14
+ readonly sourceCode: string;
15
+ /**
16
+ * File names when `sourceCodes`
17
+ */
18
+ readonly name?: string;
19
+ /**
20
+ * Workspace path when `sourceCodes`
21
+ */
22
+ readonly workspace?: string;
23
+ };
package/lib/utils.js CHANGED
@@ -20,7 +20,7 @@ function fileExists(filePath) {
20
20
  exports.fileExists = fileExists;
21
21
  function toRegexp(pattern) {
22
22
  const matched = pattern.match(/^\/(.+)\/([ig]*)$/i);
23
- if (matched) {
23
+ if (matched && matched[1]) {
24
24
  return new RegExp(matched[1], matched[2]);
25
25
  }
26
26
  return pattern;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/file-resolver",
3
- "version": "3.3.1",
3
+ "version": "3.5.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,16 @@
24
24
  "@types/node": "latest"
25
25
  },
26
26
  "dependencies": {
27
- "@markuplint/html-parser": "3.3.1",
28
- "@markuplint/ml-ast": "3.0.0",
29
- "@markuplint/ml-config": "3.3.0",
30
- "@markuplint/ml-core": "3.3.1",
31
- "@markuplint/ml-spec": "3.3.0",
27
+ "@markuplint/html-parser": "3.5.0",
28
+ "@markuplint/ml-ast": "3.1.0",
29
+ "@markuplint/ml-config": "3.5.0",
30
+ "@markuplint/ml-core": "3.5.0",
31
+ "@markuplint/ml-spec": "3.5.0",
32
32
  "cosmiconfig": "^8.0.0",
33
33
  "glob": "^8.0.3",
34
34
  "jsonc": "^2.0.0",
35
35
  "minimatch": "^5.1.1",
36
36
  "tslib": "^2.4.1"
37
37
  },
38
- "gitHead": "f19e7de726cf01d53342bc5513c30da75d28da63"
38
+ "gitHead": "0c47b2c2722f6823a17f36edbab98486275f8ab4"
39
39
  }
@@ -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();