@markuplint/file-resolver 3.15.0 → 4.0.0-alpha.10
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.
- package/LICENSE +1 -1
- package/lib/auto-load-rules.d.ts +2 -2
- package/lib/auto-load-rules.js +12 -39
- package/lib/config-load-error.d.ts +6 -0
- package/lib/config-load-error.js +8 -0
- package/lib/config-provider.d.ts +14 -11
- package/lib/config-provider.js +146 -198
- package/lib/cosmiconfig.d.ts +8 -13
- package/lib/cosmiconfig.js +25 -33
- package/lib/debug.d.ts +3 -0
- package/lib/debug.js +2 -0
- package/lib/force-import-json-in-module.d.ts +1 -0
- package/lib/force-import-json-in-module.js +31 -0
- package/lib/general-import.d.ts +1 -0
- package/lib/general-import.js +40 -0
- package/lib/get-preset.d.ts +2 -0
- package/lib/get-preset.js +13 -0
- package/lib/index.d.ts +7 -7
- package/lib/index.js +6 -9
- package/lib/is-plugin-module-name.d.ts +1 -0
- package/lib/is-plugin-module-name.js +3 -0
- package/lib/is-preset-module-name.d.ts +1 -0
- package/lib/is-preset-module-name.js +3 -0
- package/lib/ml-file/get-anonymous-file.d.ts +1 -1
- package/lib/ml-file/get-anonymous-file.js +3 -7
- package/lib/ml-file/get-file.d.ts +1 -1
- package/lib/ml-file/get-file.js +3 -7
- package/lib/ml-file/get-files.d.ts +2 -2
- package/lib/ml-file/get-files.js +7 -9
- package/lib/ml-file/index.d.ts +4 -4
- package/lib/ml-file/index.js +3 -6
- package/lib/ml-file/ml-file.d.ts +22 -22
- package/lib/ml-file/ml-file.js +54 -48
- package/lib/module-exists.d.ts +1 -0
- package/lib/module-exists.js +49 -0
- package/lib/path-to-abs-or-name.d.ts +1 -0
- package/lib/path-to-abs-or-name.js +38 -0
- package/lib/resolve-files.d.ts +3 -3
- package/lib/resolve-files.js +4 -8
- package/lib/resolve-name-or-abs-path.d.ts +1 -0
- package/lib/resolve-name-or-abs-path.js +14 -0
- package/lib/resolve-parser.d.ts +7 -11
- package/lib/resolve-parser.js +16 -12
- package/lib/resolve-plugins.js +24 -56
- package/lib/resolve-rules.d.ts +5 -9
- package/lib/resolve-rules.js +18 -44
- package/lib/resolve-specs.d.ts +2 -5
- package/lib/resolve-specs.js +10 -11
- package/lib/types.d.ts +18 -20
- package/lib/types.js +1 -2
- package/lib/utils.js +6 -13
- package/package.json +22 -18
package/lib/cosmiconfig.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const jsonc_1 = require("jsonc");
|
|
10
|
-
const explorer = (0, cosmiconfig_1.cosmiconfig)('markuplint', {
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { ConfigParserError } from '@markuplint/parser-utils';
|
|
3
|
+
import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
|
|
4
|
+
import { jsonc } from 'jsonc';
|
|
5
|
+
import { ConfigLoadError } from './config-load-error.js';
|
|
6
|
+
import { log } from './debug.js';
|
|
7
|
+
const searchLog = log.extend('search');
|
|
8
|
+
const explorer = cosmiconfig('markuplint', {
|
|
11
9
|
loaders: {
|
|
12
|
-
'.ts': (0, cosmiconfig_typescript_loader_1.TypeScriptLoader)(),
|
|
13
10
|
noExt: ((path, content) => {
|
|
14
11
|
try {
|
|
15
|
-
return
|
|
12
|
+
return jsonc.parse(content);
|
|
16
13
|
}
|
|
17
14
|
catch (error) {
|
|
18
15
|
if (error instanceof Error && error.name === 'JSONError') {
|
|
19
|
-
return
|
|
16
|
+
return defaultLoaders['noExt'](path, content);
|
|
20
17
|
}
|
|
21
18
|
throw error;
|
|
22
19
|
}
|
|
23
20
|
}),
|
|
24
21
|
},
|
|
22
|
+
searchStrategy: 'project',
|
|
25
23
|
});
|
|
26
|
-
async function search(
|
|
24
|
+
export async function search(filePath, cacheClear) {
|
|
27
25
|
if (cacheClear) {
|
|
28
26
|
explorer.clearCaches();
|
|
29
27
|
}
|
|
30
|
-
dir =
|
|
31
|
-
|
|
28
|
+
const dir = path.dirname(filePath);
|
|
29
|
+
searchLog('Search dir: %s', dir);
|
|
30
|
+
const result = await explorer.search(dir).catch(cacheConfigError(dir, filePath));
|
|
31
|
+
searchLog('Search result: %O', result);
|
|
32
32
|
if (!result || result.isEmpty) {
|
|
33
33
|
return null;
|
|
34
34
|
}
|
|
@@ -37,39 +37,31 @@ async function search(dir, cacheClear) {
|
|
|
37
37
|
config: result.config,
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
async function load(filePath, cacheClear) {
|
|
40
|
+
export async function load(filePath, cacheClear, referrer) {
|
|
42
41
|
if (cacheClear) {
|
|
43
42
|
explorer.clearCaches();
|
|
44
43
|
}
|
|
45
|
-
const result = await explorer.load(filePath).catch(cacheConfigError(filePath));
|
|
44
|
+
const result = await explorer.load(filePath).catch(cacheConfigError(filePath, referrer));
|
|
46
45
|
if (!result || result.isEmpty) {
|
|
47
|
-
return
|
|
46
|
+
return new ConfigLoadError('Config file is empty', filePath, referrer);
|
|
48
47
|
}
|
|
49
48
|
return {
|
|
50
49
|
filePath: result.filepath,
|
|
51
50
|
config: result.config,
|
|
52
51
|
};
|
|
53
52
|
}
|
|
54
|
-
|
|
55
|
-
class ConfigLoadError extends Error {
|
|
56
|
-
constructor(message, filePath) {
|
|
57
|
-
super(message + ` in ${filePath}`);
|
|
58
|
-
this.name = 'ConfigLoadError';
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
function cacheConfigError(fileOrDirPath) {
|
|
53
|
+
function cacheConfigError(fileOrDirPath, referrer) {
|
|
62
54
|
return (reason) => {
|
|
63
|
-
var _a;
|
|
64
55
|
if (reason instanceof Error) {
|
|
65
56
|
switch (reason.name) {
|
|
66
|
-
case 'YAMLException':
|
|
67
|
-
throw new
|
|
57
|
+
case 'YAMLException': {
|
|
58
|
+
throw new ConfigParserError(reason.message, {
|
|
68
59
|
// @ts-ignore
|
|
69
|
-
filePath:
|
|
60
|
+
filePath: reason.filepath ?? fileOrDirPath,
|
|
70
61
|
});
|
|
62
|
+
}
|
|
71
63
|
}
|
|
72
|
-
throw new ConfigLoadError(reason.message, fileOrDirPath);
|
|
64
|
+
throw new ConfigLoadError(reason.message, fileOrDirPath, referrer);
|
|
73
65
|
}
|
|
74
66
|
throw reason;
|
|
75
67
|
};
|
package/lib/debug.d.ts
ADDED
package/lib/debug.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function forceImportJsonInModule(modPath: string): Promise<any>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { log } from './debug.js';
|
|
4
|
+
const fLog = log.extend('force-import-json-in-module');
|
|
5
|
+
export async function forceImportJsonInModule(modPath) {
|
|
6
|
+
const error = await import(modPath).catch(error => error);
|
|
7
|
+
if (error instanceof Error) {
|
|
8
|
+
fLog('Error in forceImportJsonInModule: %O', error);
|
|
9
|
+
if (!('code' in error)) {
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
if (error.code !== 'ERR_IMPORT_ASSERTION_TYPE_MISSING') {
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
const searchPath = /module\s"([^"]+)"\sneeds/i.exec(error.message);
|
|
16
|
+
const absPath = searchPath?.[1] ?? null;
|
|
17
|
+
fLog('Extract path: %s', absPath);
|
|
18
|
+
if (!absPath) {
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
const normalizePath = absPath
|
|
22
|
+
.replace(/^file:\/\//, '')
|
|
23
|
+
.replaceAll('/', path.sep)
|
|
24
|
+
// Windows
|
|
25
|
+
.replace(/^[/\\][a-z]:/i, '');
|
|
26
|
+
fLog('Find JSON file path: %s', normalizePath);
|
|
27
|
+
const fileContent = await readFile(normalizePath, { encoding: 'utf8' });
|
|
28
|
+
return JSON.parse(fileContent);
|
|
29
|
+
}
|
|
30
|
+
return error.default ?? error;
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function generalImport<T>(name: string): Promise<T | null>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { log } from './debug.js';
|
|
3
|
+
const gLog = log.extend('general-import');
|
|
4
|
+
const cache = new Map();
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
export async function generalImport(name) {
|
|
7
|
+
if (cache.has(name)) {
|
|
8
|
+
return cache.get(name);
|
|
9
|
+
}
|
|
10
|
+
try {
|
|
11
|
+
const imported = await import(name);
|
|
12
|
+
const mod = imported?.default ?? imported ?? null;
|
|
13
|
+
cache.set(name, mod);
|
|
14
|
+
return mod;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
'code' in error &&
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
error.code === 'ERR_IMPORT_ASSERTION_TYPE_MISSING') {
|
|
22
|
+
try {
|
|
23
|
+
const mod = require(name) ?? null;
|
|
24
|
+
cache.set(name, mod);
|
|
25
|
+
return mod;
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error instanceof Error && /^parse failure/i.test(error.message)) {
|
|
29
|
+
gLog('Error in `createRequire(import.meta.url)()`: %O', error);
|
|
30
|
+
cache.set(name, null);
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
gLog('Error in generalImport: %O', error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
gLog('Error in `import()`: %O', error);
|
|
37
|
+
cache.set(name, null);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { forceImportJsonInModule } from './force-import-json-in-module.js';
|
|
2
|
+
const cache = new Map();
|
|
3
|
+
export async function getPreset(name) {
|
|
4
|
+
if (cache.has(name)) {
|
|
5
|
+
return cache.get(name);
|
|
6
|
+
}
|
|
7
|
+
const json = await forceImportJsonInModule(`@markuplint/config-presets/preset.${name}.json`);
|
|
8
|
+
if (json instanceof Error) {
|
|
9
|
+
throw new ReferenceError(`Preset markuplint:${name} is not found`);
|
|
10
|
+
}
|
|
11
|
+
cache.set(name, json);
|
|
12
|
+
return json;
|
|
13
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export type { MLFile } from './ml-file';
|
|
2
|
-
export * from './config-provider';
|
|
3
|
-
export * from './resolve-files';
|
|
4
|
-
export * from './resolve-parser';
|
|
5
|
-
export * from './resolve-rules';
|
|
6
|
-
export * from './resolve-specs';
|
|
7
|
-
export * from './types';
|
|
1
|
+
export type { MLFile } from './ml-file/index.js';
|
|
2
|
+
export * from './config-provider.js';
|
|
3
|
+
export * from './resolve-files.js';
|
|
4
|
+
export * from './resolve-parser.js';
|
|
5
|
+
export * from './resolve-rules.js';
|
|
6
|
+
export * from './resolve-specs.js';
|
|
7
|
+
export * from './types.js';
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
tslib_1.__exportStar(require("./resolve-rules"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./resolve-specs"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./types"), exports);
|
|
1
|
+
export * from './config-provider.js';
|
|
2
|
+
export * from './resolve-files.js';
|
|
3
|
+
export * from './resolve-parser.js';
|
|
4
|
+
export * from './resolve-rules.js';
|
|
5
|
+
export * from './resolve-specs.js';
|
|
6
|
+
export * from './types.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isPluginModuleName(name: string): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isPresetModuleName(name: string): boolean;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { MLFile } from './ml-file';
|
|
1
|
+
import { MLFile } from './ml-file.js';
|
|
2
2
|
export declare function getAnonymousFile(context: string, workspace?: string, name?: string): MLFile;
|
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const ml_file_1 = require("./ml-file");
|
|
5
|
-
function getAnonymousFile(context, workspace, name) {
|
|
6
|
-
const file = new ml_file_1.MLFile({
|
|
1
|
+
import { MLFile } from './ml-file.js';
|
|
2
|
+
export function getAnonymousFile(context, workspace, name) {
|
|
3
|
+
const file = new MLFile({
|
|
7
4
|
sourceCode: context,
|
|
8
5
|
workspace,
|
|
9
6
|
name,
|
|
10
7
|
});
|
|
11
8
|
return file;
|
|
12
9
|
}
|
|
13
|
-
exports.getAnonymousFile = getAnonymousFile;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { MLFile } from './ml-file';
|
|
1
|
+
import { MLFile } from './ml-file.js';
|
|
2
2
|
export declare function getFile(filePath: string): MLFile;
|
package/lib/ml-file/get-file.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const ml_file_1 = require("./ml-file");
|
|
5
|
-
function getFile(filePath) {
|
|
6
|
-
const file = new ml_file_1.MLFile(filePath);
|
|
1
|
+
import { MLFile } from './ml-file.js';
|
|
2
|
+
export function getFile(filePath) {
|
|
3
|
+
const file = new MLFile(filePath);
|
|
7
4
|
return file;
|
|
8
5
|
}
|
|
9
|
-
exports.getFile = getFile;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MLFile } from './ml-file';
|
|
1
|
+
import type { MLFile } from './ml-file.js';
|
|
2
2
|
/**
|
|
3
3
|
* Get files
|
|
4
4
|
*
|
|
@@ -6,4 +6,4 @@ import type { MLFile } from './ml-file';
|
|
|
6
6
|
*
|
|
7
7
|
* @param filePathOrGlob
|
|
8
8
|
*/
|
|
9
|
-
export declare function getFiles(filePathOrGlob: string): Promise<MLFile[]>;
|
|
9
|
+
export declare function getFiles(filePathOrGlob: string, ignoreGlob?: string): Promise<MLFile[]>;
|
package/lib/ml-file/get-files.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const glob_1 = require("glob");
|
|
5
|
-
const get_file_1 = require("./get-file");
|
|
1
|
+
import { glob } from 'glob';
|
|
2
|
+
import { minimatch } from 'minimatch';
|
|
3
|
+
import { getFile } from './get-file.js';
|
|
6
4
|
/**
|
|
7
5
|
* Get files
|
|
8
6
|
*
|
|
@@ -10,8 +8,8 @@ const get_file_1 = require("./get-file");
|
|
|
10
8
|
*
|
|
11
9
|
* @param filePathOrGlob
|
|
12
10
|
*/
|
|
13
|
-
async function getFiles(filePathOrGlob) {
|
|
14
|
-
const fileList = await
|
|
15
|
-
|
|
11
|
+
export async function getFiles(filePathOrGlob, ignoreGlob) {
|
|
12
|
+
const fileList = await glob(filePathOrGlob, {}).catch(() => []);
|
|
13
|
+
const filtered = fileList.filter(fileName => !minimatch(fileName, ignoreGlob ?? ''));
|
|
14
|
+
return filtered.map(fileName => getFile(fileName));
|
|
16
15
|
}
|
|
17
|
-
exports.getFiles = getFiles;
|
package/lib/ml-file/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { MLFile } from './ml-file';
|
|
2
|
-
export * from './get-anonymous-file';
|
|
3
|
-
export * from './get-file';
|
|
4
|
-
export * from './get-files';
|
|
1
|
+
export type { MLFile } from './ml-file.js';
|
|
2
|
+
export * from './get-anonymous-file.js';
|
|
3
|
+
export * from './get-file.js';
|
|
4
|
+
export * from './get-files.js';
|
package/lib/ml-file/index.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
tslib_1.__exportStar(require("./get-anonymous-file"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./get-file"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./get-files"), exports);
|
|
1
|
+
export * from './get-anonymous-file.js';
|
|
2
|
+
export * from './get-file.js';
|
|
3
|
+
export * from './get-files.js';
|
package/lib/ml-file/ml-file.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import type { Target } from '../types';
|
|
1
|
+
import type { Target } from '../types.js';
|
|
2
2
|
export declare class MLFile {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
+
ignored(globPath: string | readonly string[]): any;
|
|
18
|
+
isExist(): Promise<boolean>;
|
|
19
|
+
isFile(): Promise<boolean>;
|
|
20
|
+
matches(globPath: string): boolean;
|
|
21
|
+
setCode(code: string): void;
|
|
22
|
+
private _fetch;
|
|
23
|
+
private _stat;
|
|
24
24
|
}
|
package/lib/ml-file/ml-file.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
2
12
|
var _MLFile_basename, _MLFile_code, _MLFile_dirname, _MLFile_stat, _MLFile_type;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const ignore_1 = tslib_1.__importDefault(require("ignore"));
|
|
9
|
-
const minimatch_1 = require("minimatch");
|
|
10
|
-
class MLFile {
|
|
13
|
+
import { promises as fs } from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import ignore from 'ignore';
|
|
16
|
+
import { minimatch } from 'minimatch';
|
|
17
|
+
export class MLFile {
|
|
11
18
|
constructor(target) {
|
|
12
|
-
var _a, _b;
|
|
13
19
|
_MLFile_basename.set(this, void 0);
|
|
14
20
|
_MLFile_code.set(this, void 0);
|
|
15
21
|
_MLFile_dirname.set(this, void 0);
|
|
@@ -21,25 +27,25 @@ class MLFile {
|
|
|
21
27
|
_MLFile_stat.set(this, undefined);
|
|
22
28
|
_MLFile_type.set(this, void 0);
|
|
23
29
|
if (typeof target === 'string') {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
__classPrivateFieldSet(this, _MLFile_basename, path.basename(target), "f");
|
|
31
|
+
__classPrivateFieldSet(this, _MLFile_dirname, path.dirname(target), "f");
|
|
32
|
+
__classPrivateFieldSet(this, _MLFile_code, null, "f");
|
|
33
|
+
__classPrivateFieldSet(this, _MLFile_type, 'file-base', "f");
|
|
28
34
|
return;
|
|
29
35
|
}
|
|
30
|
-
if (!target.workspace && target.name &&
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
if (!target.workspace && target.name && path.isAbsolute(target.name)) {
|
|
37
|
+
__classPrivateFieldSet(this, _MLFile_basename, path.basename(target.name), "f");
|
|
38
|
+
__classPrivateFieldSet(this, _MLFile_dirname, path.dirname(target.name), "f");
|
|
33
39
|
}
|
|
34
40
|
else {
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
__classPrivateFieldSet(this, _MLFile_basename, target.name ?? '<AnonymousFile>', "f");
|
|
42
|
+
__classPrivateFieldSet(this, _MLFile_dirname, target.workspace ?? process.cwd(), "f");
|
|
37
43
|
}
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
__classPrivateFieldSet(this, _MLFile_code, target.sourceCode, "f");
|
|
45
|
+
__classPrivateFieldSet(this, _MLFile_type, 'code-base', "f");
|
|
40
46
|
}
|
|
41
47
|
get dirname() {
|
|
42
|
-
return
|
|
48
|
+
return __classPrivateFieldGet(this, _MLFile_dirname, "f");
|
|
43
49
|
}
|
|
44
50
|
/**
|
|
45
51
|
* Normalized `MLFile.dirname`
|
|
@@ -54,16 +60,16 @@ class MLFile {
|
|
|
54
60
|
return pathNormalize(this.path);
|
|
55
61
|
}
|
|
56
62
|
get path() {
|
|
57
|
-
return
|
|
63
|
+
return path.resolve(__classPrivateFieldGet(this, _MLFile_dirname, "f"), __classPrivateFieldGet(this, _MLFile_basename, "f"));
|
|
58
64
|
}
|
|
59
65
|
async dirExists() {
|
|
60
|
-
return !!(await stat(
|
|
66
|
+
return !!(await stat(__classPrivateFieldGet(this, _MLFile_dirname, "f")));
|
|
61
67
|
}
|
|
62
68
|
async getCode() {
|
|
63
|
-
if (
|
|
64
|
-
return
|
|
69
|
+
if (__classPrivateFieldGet(this, _MLFile_code, "f") != null) {
|
|
70
|
+
return __classPrivateFieldGet(this, _MLFile_code, "f");
|
|
65
71
|
}
|
|
66
|
-
if (
|
|
72
|
+
if (__classPrivateFieldGet(this, _MLFile_type, "f") === 'file-base' && (await this.isExist())) {
|
|
67
73
|
return await this._fetch();
|
|
68
74
|
}
|
|
69
75
|
return '';
|
|
@@ -71,61 +77,61 @@ class MLFile {
|
|
|
71
77
|
ignored(globPath) {
|
|
72
78
|
globPath = typeof globPath === 'string' ? [globPath] : globPath;
|
|
73
79
|
const normalizedPaths = globPath.map(p => pathNormalize(p, true));
|
|
74
|
-
|
|
80
|
+
// @ts-ignore
|
|
81
|
+
const ig = ignore().add(normalizedPaths);
|
|
75
82
|
const ignored = ig.ignores(pathNormalize(this.nPath, true));
|
|
76
83
|
return ignored;
|
|
77
84
|
}
|
|
78
85
|
async isExist() {
|
|
79
|
-
if (
|
|
86
|
+
if (__classPrivateFieldGet(this, _MLFile_type, "f") === 'code-base') {
|
|
80
87
|
return true;
|
|
81
88
|
}
|
|
82
89
|
const stat = await this._stat();
|
|
83
90
|
return !!stat;
|
|
84
91
|
}
|
|
85
92
|
async isFile() {
|
|
86
|
-
if (
|
|
93
|
+
if (__classPrivateFieldGet(this, _MLFile_type, "f") === 'code-base') {
|
|
87
94
|
return true;
|
|
88
95
|
}
|
|
89
96
|
const stat = await this._stat();
|
|
90
97
|
return !!stat && stat.isFile();
|
|
91
98
|
}
|
|
92
99
|
matches(globPath) {
|
|
93
|
-
return
|
|
100
|
+
return minimatch(this.nPath, pathNormalize(globPath));
|
|
94
101
|
}
|
|
95
102
|
setCode(code) {
|
|
96
|
-
if (
|
|
103
|
+
if (__classPrivateFieldGet(this, _MLFile_type, "f") === 'file-base') {
|
|
97
104
|
throw new Error(`This file object is readonly (File-base: ${this.path})`);
|
|
98
105
|
}
|
|
99
|
-
|
|
106
|
+
__classPrivateFieldSet(this, _MLFile_code, code, "f");
|
|
100
107
|
}
|
|
101
108
|
async _fetch() {
|
|
102
|
-
const code = await
|
|
103
|
-
|
|
109
|
+
const code = await fs.readFile(this.path, { encoding: 'utf8' });
|
|
110
|
+
__classPrivateFieldSet(this, _MLFile_code, code, "f");
|
|
104
111
|
return code;
|
|
105
112
|
}
|
|
106
113
|
async _stat() {
|
|
107
|
-
if (
|
|
108
|
-
return
|
|
114
|
+
if (__classPrivateFieldGet(this, _MLFile_stat, "f")) {
|
|
115
|
+
return __classPrivateFieldGet(this, _MLFile_stat, "f");
|
|
109
116
|
}
|
|
110
|
-
|
|
111
|
-
return
|
|
117
|
+
__classPrivateFieldSet(this, _MLFile_stat, await stat(this.path), "f");
|
|
118
|
+
return __classPrivateFieldGet(this, _MLFile_stat, "f");
|
|
112
119
|
}
|
|
113
120
|
}
|
|
114
|
-
exports.MLFile = MLFile;
|
|
115
121
|
_MLFile_basename = new WeakMap(), _MLFile_code = new WeakMap(), _MLFile_dirname = new WeakMap(), _MLFile_stat = new WeakMap(), _MLFile_type = new WeakMap();
|
|
116
122
|
async function stat(filePath) {
|
|
117
123
|
try {
|
|
118
|
-
return await
|
|
124
|
+
return await fs.stat(filePath);
|
|
119
125
|
}
|
|
120
|
-
catch (
|
|
126
|
+
catch (error) {
|
|
121
127
|
if (
|
|
122
128
|
// @ts-ignore
|
|
123
|
-
'code' in
|
|
129
|
+
'code' in error &&
|
|
124
130
|
// @ts-ignore
|
|
125
|
-
|
|
131
|
+
error.code === 'ENOENT') {
|
|
126
132
|
return null;
|
|
127
133
|
}
|
|
128
|
-
throw
|
|
134
|
+
throw error;
|
|
129
135
|
}
|
|
130
136
|
}
|
|
131
137
|
function pathNormalize(filePath, relative = false) {
|
|
@@ -134,14 +140,14 @@ function pathNormalize(filePath, relative = false) {
|
|
|
134
140
|
filePath = filePath.slice(1);
|
|
135
141
|
}
|
|
136
142
|
// Remove the local disk scheme of Windows OS
|
|
137
|
-
if (
|
|
143
|
+
if (path.isAbsolute(filePath)) {
|
|
138
144
|
filePath = filePath.replace(/^[a-z]+:/i, '');
|
|
139
145
|
if (relative) {
|
|
140
|
-
filePath =
|
|
146
|
+
filePath = path.relative(path.sep, filePath);
|
|
141
147
|
}
|
|
142
148
|
}
|
|
143
149
|
// Replace the separator of Windows OS
|
|
144
|
-
filePath = filePath.split(
|
|
150
|
+
filePath = filePath.split(path.sep).join('/');
|
|
145
151
|
if (hasBang) {
|
|
146
152
|
filePath = `!${filePath}`;
|
|
147
153
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function moduleExists(name: string): Promise<boolean>;
|