@eggjs/utils 5.0.0-beta.35 → 5.0.0-beta.36
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/dist/_virtual/rolldown_runtime.js +7 -0
- package/dist/deprecated.d.ts +12 -9
- package/dist/deprecated.js +35 -50
- package/dist/error/ImportResolveError.d.ts +7 -4
- package/dist/error/ImportResolveError.js +16 -13
- package/dist/framework.d.ts +15 -13
- package/dist/framework.js +54 -75
- package/dist/import.d.ts +13 -10
- package/dist/import.js +214 -371
- package/dist/index.d.ts +24 -23
- package/dist/index.js +39 -46
- package/dist/plugin.d.ts +39 -37
- package/dist/plugin.js +97 -102
- package/dist/utils.js +11 -17
- package/package.json +21 -25
- package/dist/error/index.d.ts +0 -1
- package/dist/error/index.js +0 -2
- package/dist/utils.d.ts +0 -2
package/dist/deprecated.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
//#region src/deprecated.d.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
* Try to get framework dir path
|
|
4
|
+
* If can't find any framework, try to find egg dir path
|
|
5
|
+
*
|
|
6
|
+
* @param {String} cwd - current work path
|
|
7
|
+
* @param {Array} [eggNames] - egg names, default is ['egg']
|
|
8
|
+
* @return {String} framework or egg dir path
|
|
9
|
+
* @deprecated
|
|
10
|
+
*/
|
|
11
|
+
declare function getFrameworkOrEggPath(cwd: string, eggNames?: string[]): string;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { getFrameworkOrEggPath };
|
package/dist/deprecated.js
CHANGED
|
@@ -1,53 +1,38 @@
|
|
|
1
|
-
import { existsSync, readdirSync } from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
1
|
import { readJSONSync } from "./utils.js";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { existsSync, readdirSync } from "node:fs";
|
|
4
|
+
|
|
5
|
+
//#region src/deprecated.ts
|
|
4
6
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
const pkg = readJSONSync(pkgfile);
|
|
36
|
-
if (pkg.dependencies) {
|
|
37
|
-
for (const eggName of eggNames) {
|
|
38
|
-
if (pkg.dependencies[eggName]) {
|
|
39
|
-
return path.join(moduleDir, name);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
// try to get egg
|
|
45
|
-
for (const eggName of eggNames) {
|
|
46
|
-
const pkgfile = path.join(moduleDir, eggName, 'package.json');
|
|
47
|
-
if (existsSync(pkgfile)) {
|
|
48
|
-
return path.join(moduleDir, eggName);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return '';
|
|
7
|
+
* Try to get framework dir path
|
|
8
|
+
* If can't find any framework, try to find egg dir path
|
|
9
|
+
*
|
|
10
|
+
* @param {String} cwd - current work path
|
|
11
|
+
* @param {Array} [eggNames] - egg names, default is ['egg']
|
|
12
|
+
* @return {String} framework or egg dir path
|
|
13
|
+
* @deprecated
|
|
14
|
+
*/
|
|
15
|
+
function getFrameworkOrEggPath(cwd, eggNames) {
|
|
16
|
+
eggNames = eggNames || ["egg"];
|
|
17
|
+
const moduleDir = path.join(cwd, "node_modules");
|
|
18
|
+
if (!existsSync(moduleDir)) return "";
|
|
19
|
+
const pkgFile = path.join(cwd, "package.json");
|
|
20
|
+
if (existsSync(pkgFile)) {
|
|
21
|
+
const pkg = readJSONSync(pkgFile);
|
|
22
|
+
if (pkg.egg && pkg.egg.framework) return path.join(moduleDir, pkg.egg.framework);
|
|
23
|
+
}
|
|
24
|
+
const names = readdirSync(moduleDir);
|
|
25
|
+
for (const name of names) {
|
|
26
|
+
const pkgfile = path.join(moduleDir, name, "package.json");
|
|
27
|
+
if (!existsSync(pkgfile)) continue;
|
|
28
|
+
const pkg = readJSONSync(pkgfile);
|
|
29
|
+
if (pkg.dependencies) {
|
|
30
|
+
for (const eggName of eggNames) if (pkg.dependencies[eggName]) return path.join(moduleDir, name);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
for (const eggName of eggNames) if (existsSync(path.join(moduleDir, eggName, "package.json"))) return path.join(moduleDir, eggName);
|
|
34
|
+
return "";
|
|
52
35
|
}
|
|
53
|
-
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { getFrameworkOrEggPath };
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
//#region src/error/ImportResolveError.d.ts
|
|
2
|
+
declare class ImportResolveError extends Error {
|
|
3
|
+
filepath: string;
|
|
4
|
+
paths: string[];
|
|
5
|
+
constructor(filepath: string, paths: string[], error: Error);
|
|
5
6
|
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { ImportResolveError };
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
1
|
+
//#region src/error/ImportResolveError.ts
|
|
2
|
+
var ImportResolveError = class extends Error {
|
|
3
|
+
filepath;
|
|
4
|
+
paths;
|
|
5
|
+
constructor(filepath, paths, error) {
|
|
6
|
+
const message = `${error.message}, paths: ${JSON.stringify(paths)}`;
|
|
7
|
+
super(message, { cause: error });
|
|
8
|
+
this.name = this.constructor.name;
|
|
9
|
+
this.filepath = filepath;
|
|
10
|
+
this.paths = paths;
|
|
11
|
+
Error.captureStackTrace(this, this.constructor);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { ImportResolveError };
|
package/dist/framework.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
+
//#region src/framework.d.ts
|
|
1
2
|
interface Options {
|
|
2
|
-
|
|
3
|
-
|
|
3
|
+
baseDir: string;
|
|
4
|
+
framework?: string;
|
|
4
5
|
}
|
|
5
6
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
* Find the framework directory, lookup order
|
|
8
|
+
* - specify framework path
|
|
9
|
+
* - get framework name from
|
|
10
|
+
* - use egg by default
|
|
11
|
+
* @param {Object} options - options
|
|
12
|
+
* @param {String} options.baseDir - the current directory of application
|
|
13
|
+
* @param {String} [options.framework] - the directory of framework
|
|
14
|
+
* @return {String} frameworkPath
|
|
15
|
+
*/
|
|
16
|
+
declare function getFrameworkPath(options: Options): string;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { getFrameworkPath };
|
package/dist/framework.js
CHANGED
|
@@ -1,81 +1,60 @@
|
|
|
1
|
-
import assert from 'node:assert';
|
|
2
|
-
import { existsSync } from 'node:fs';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { debuglog } from 'node:util';
|
|
5
|
-
import { importResolve } from "./import.js";
|
|
6
1
|
import { readJSONSync } from "./utils.js";
|
|
7
|
-
|
|
2
|
+
import { importResolve } from "./import.js";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import assert from "node:assert";
|
|
6
|
+
import { debuglog } from "node:util";
|
|
7
|
+
|
|
8
|
+
//#region src/framework.ts
|
|
9
|
+
const debug = debuglog("egg/utils/framework");
|
|
8
10
|
const initCwd = process.cwd();
|
|
9
11
|
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return assertAndReturn(framework, moduleDir, baseDir);
|
|
35
|
-
}
|
|
36
|
-
const pkg = readJSONSync(pkgPath);
|
|
37
|
-
// 2. framework is not specified
|
|
38
|
-
// 2.1 use framework name from pkg.egg.framework
|
|
39
|
-
if (pkg.egg?.framework) {
|
|
40
|
-
return assertAndReturn(pkg.egg.framework, moduleDir, baseDir);
|
|
41
|
-
}
|
|
42
|
-
// 2.2 use egg by default
|
|
43
|
-
return assertAndReturn('egg', moduleDir, baseDir);
|
|
12
|
+
* Find the framework directory, lookup order
|
|
13
|
+
* - specify framework path
|
|
14
|
+
* - get framework name from
|
|
15
|
+
* - use egg by default
|
|
16
|
+
* @param {Object} options - options
|
|
17
|
+
* @param {String} options.baseDir - the current directory of application
|
|
18
|
+
* @param {String} [options.framework] - the directory of framework
|
|
19
|
+
* @return {String} frameworkPath
|
|
20
|
+
*/
|
|
21
|
+
function getFrameworkPath(options) {
|
|
22
|
+
const { framework, baseDir } = options;
|
|
23
|
+
const pkgPath = path.join(baseDir, "package.json");
|
|
24
|
+
assert(existsSync(pkgPath), `${pkgPath} should exist`);
|
|
25
|
+
const moduleDir = path.join(baseDir, "node_modules");
|
|
26
|
+
if (framework) {
|
|
27
|
+
if (path.isAbsolute(framework)) {
|
|
28
|
+
assert(existsSync(framework), `${framework} should exist`);
|
|
29
|
+
return framework;
|
|
30
|
+
}
|
|
31
|
+
return assertAndReturn(framework, moduleDir, baseDir);
|
|
32
|
+
}
|
|
33
|
+
const pkg = readJSONSync(pkgPath);
|
|
34
|
+
if (pkg.egg?.framework) return assertAndReturn(pkg.egg.framework, moduleDir, baseDir);
|
|
35
|
+
return assertAndReturn("egg", moduleDir, baseDir);
|
|
44
36
|
}
|
|
45
37
|
function assertAndReturn(frameworkName, moduleDir, baseDir) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
moduleDirs.add(globalModuleDir);
|
|
66
|
-
}
|
|
67
|
-
catch {
|
|
68
|
-
// ignore
|
|
69
|
-
// debug('importResolve %s on %s error: %s', frameworkName, moduleDir, err);
|
|
70
|
-
}
|
|
71
|
-
for (const moduleDir of moduleDirs) {
|
|
72
|
-
const frameworkPath = path.join(moduleDir, frameworkName);
|
|
73
|
-
if (existsSync(frameworkPath)) {
|
|
74
|
-
debug('[assertAndReturn] frameworkPath: %s, moduleDirs: %o', frameworkPath, moduleDirs);
|
|
75
|
-
return frameworkPath;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
// console.error('framework: %o is not found in: %j, cwd: %s, baseDir: %s', frameworkName, Array.from(moduleDirs), process.cwd(), baseDir);
|
|
79
|
-
throw new Error(`${frameworkName} is not found in ${Array.from(moduleDirs)}`);
|
|
38
|
+
const moduleDirs = new Set([
|
|
39
|
+
moduleDir,
|
|
40
|
+
path.join(process.cwd(), "node_modules"),
|
|
41
|
+
path.join(initCwd, "node_modules")
|
|
42
|
+
]);
|
|
43
|
+
try {
|
|
44
|
+
let globalModuleDir;
|
|
45
|
+
if (frameworkName.startsWith("@") && frameworkName.includes("/")) globalModuleDir = path.join(importResolve(`${frameworkName}/package.json`, { paths: [baseDir] }), "../../..");
|
|
46
|
+
else globalModuleDir = path.join(importResolve(`${frameworkName}/package.json`, { paths: [baseDir] }), "../..");
|
|
47
|
+
moduleDirs.add(globalModuleDir);
|
|
48
|
+
} catch {}
|
|
49
|
+
for (const moduleDir$1 of moduleDirs) {
|
|
50
|
+
const frameworkPath = path.join(moduleDir$1, frameworkName);
|
|
51
|
+
if (existsSync(frameworkPath)) {
|
|
52
|
+
debug("[assertAndReturn] frameworkPath: %s, moduleDirs: %o", frameworkPath, moduleDirs);
|
|
53
|
+
return frameworkPath;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
throw new Error(`${frameworkName} is not found in ${Array.from(moduleDirs)}`);
|
|
80
57
|
}
|
|
81
|
-
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
export { getFrameworkPath };
|
package/dist/import.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/import.d.ts
|
|
2
|
+
interface ImportResolveOptions {
|
|
3
|
+
paths?: string[];
|
|
3
4
|
}
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
interface ImportModuleOptions extends ImportResolveOptions {
|
|
6
|
+
importDefaultOnly?: boolean;
|
|
6
7
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
declare let isESM: boolean;
|
|
9
|
+
declare function getRequire(): NodeRequire;
|
|
10
|
+
declare function getExtensions(): NodeJS.RequireExtensions;
|
|
11
|
+
declare function isSupportTypeScript(): boolean;
|
|
12
|
+
declare function importResolve(filepath: string, options?: ImportResolveOptions): string;
|
|
13
|
+
declare function importModule(filepath: string, options?: ImportModuleOptions): Promise<any>;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { ImportModuleOptions, ImportResolveOptions, getExtensions, getRequire, importModule, importResolve, isESM, isSupportTypeScript };
|