@eggjs/utils 5.0.0-beta.20 → 5.0.0-beta.22
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/deprecated.d.ts +10 -0
- package/dist/deprecated.js +53 -0
- package/dist/error/ImportResolveError.d.ts +5 -0
- package/dist/error/ImportResolveError.js +13 -0
- package/dist/error/index.d.ts +1 -0
- package/dist/error/index.js +2 -0
- package/dist/framework.d.ts +16 -0
- package/dist/framework.js +81 -0
- package/dist/import.d.ts +12 -0
- package/dist/import.js +385 -0
- package/dist/index.d.ts +21 -115
- package/dist/index.js +49 -483
- package/dist/plugin.d.ts +49 -0
- package/dist/plugin.js +118 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +18 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,121 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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
|
-
//#region src/plugin.d.ts
|
|
19
|
-
interface LoaderOptions {
|
|
20
|
-
framework: string;
|
|
21
|
-
baseDir: string;
|
|
22
|
-
env?: string;
|
|
23
|
-
}
|
|
24
|
-
interface Plugin {
|
|
25
|
-
name: string;
|
|
26
|
-
version?: string;
|
|
27
|
-
enable: boolean;
|
|
28
|
-
implicitEnable: boolean;
|
|
29
|
-
path: string;
|
|
30
|
-
dependencies: string[];
|
|
31
|
-
optionalDependencies: string[];
|
|
32
|
-
env: string[];
|
|
33
|
-
from: string;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* @see https://github.com/eggjs/egg-core/blob/2920f6eade07959d25f5c4f96b154d3fbae877db/lib/loader/mixin/plugin.js#L203
|
|
37
|
-
*/
|
|
38
|
-
declare function getPlugins(options: LoaderOptions): Promise<Record<string, Plugin>>;
|
|
39
|
-
interface Unit {
|
|
40
|
-
type: 'plugin' | 'framework' | 'app';
|
|
41
|
-
path: string;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* @see https://github.com/eggjs/egg-core/blob/2920f6eade07959d25f5c4f96b154d3fbae877db/lib/loader/egg_loader.js#L348
|
|
45
|
-
*/
|
|
46
|
-
declare function getLoadUnits(options: LoaderOptions): Promise<Unit[]>;
|
|
47
|
-
declare function getConfig(options: LoaderOptions): Promise<Record<string, any>>;
|
|
48
|
-
interface IEggLoader {
|
|
49
|
-
loadPlugin(): Promise<void>;
|
|
50
|
-
loadConfig(): Promise<void>;
|
|
51
|
-
config: Record<string, any>;
|
|
52
|
-
getLoadUnits(): Unit[];
|
|
53
|
-
allPlugins: Record<string, Plugin>;
|
|
54
|
-
}
|
|
55
|
-
interface IEggLoaderOptions {
|
|
56
|
-
baseDir: string;
|
|
57
|
-
app: unknown;
|
|
58
|
-
logger: object;
|
|
59
|
-
EggCoreClass?: unknown;
|
|
60
|
-
}
|
|
61
|
-
type EggLoaderImplClass<T = IEggLoader> = new (options: IEggLoaderOptions) => T;
|
|
62
|
-
declare function getLoader(options: LoaderOptions): Promise<IEggLoader>;
|
|
63
|
-
declare function findEggCore(options: LoaderOptions): Promise<{
|
|
64
|
-
EggCore?: object;
|
|
65
|
-
EggLoader: EggLoaderImplClass;
|
|
66
|
-
}>;
|
|
67
|
-
//#endregion
|
|
68
|
-
//#region src/deprecated.d.ts
|
|
69
|
-
/**
|
|
70
|
-
* Try to get framework dir path
|
|
71
|
-
* If can't find any framework, try to find egg dir path
|
|
72
|
-
*
|
|
73
|
-
* @param {String} cwd - current work path
|
|
74
|
-
* @param {Array} [eggNames] - egg names, default is ['egg']
|
|
75
|
-
* @return {String} framework or egg dir path
|
|
76
|
-
* @deprecated
|
|
77
|
-
*/
|
|
78
|
-
declare function getFrameworkOrEggPath(cwd: string, eggNames?: string[]): string;
|
|
79
|
-
//#endregion
|
|
80
|
-
//#region src/import.d.ts
|
|
81
|
-
interface ImportResolveOptions {
|
|
82
|
-
paths?: string[];
|
|
83
|
-
}
|
|
84
|
-
interface ImportModuleOptions extends ImportResolveOptions {
|
|
85
|
-
importDefaultOnly?: boolean;
|
|
86
|
-
}
|
|
87
|
-
declare let isESM: boolean;
|
|
88
|
-
declare function getRequire(): NodeRequire;
|
|
89
|
-
declare function getExtensions(): NodeJS.RequireExtensions;
|
|
90
|
-
declare function isSupportTypeScript(): boolean;
|
|
91
|
-
declare function importResolve(filepath: string, options?: ImportResolveOptions): string;
|
|
92
|
-
declare function importModule(filepath: string, options?: ImportModuleOptions): Promise<any>;
|
|
93
|
-
//#endregion
|
|
94
|
-
//#region src/error/ImportResolveError.d.ts
|
|
95
|
-
declare class ImportResolveError extends Error {
|
|
96
|
-
filepath: string;
|
|
97
|
-
paths: string[];
|
|
98
|
-
constructor(filepath: string, paths: string[], error: Error);
|
|
99
|
-
}
|
|
100
|
-
//#endregion
|
|
101
|
-
//#region src/index.d.ts
|
|
1
|
+
import { getFrameworkPath } from './framework.ts';
|
|
2
|
+
import { getPlugins, getConfig, getLoadUnits } from './plugin.ts';
|
|
3
|
+
import { getFrameworkOrEggPath } from './deprecated.ts';
|
|
4
|
+
export { getFrameworkPath } from './framework.ts';
|
|
5
|
+
export { getPlugins, getConfig, getLoadUnits, getLoader, findEggCore } from './plugin.ts';
|
|
6
|
+
export { getFrameworkOrEggPath } from './deprecated.ts';
|
|
7
|
+
export * from './import.ts';
|
|
8
|
+
export * from './error/index.ts';
|
|
102
9
|
declare const _default: {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
10
|
+
getFrameworkPath: typeof getFrameworkPath;
|
|
11
|
+
getPlugins: typeof getPlugins;
|
|
12
|
+
getConfig: typeof getConfig;
|
|
13
|
+
getLoadUnits: typeof getLoadUnits;
|
|
14
|
+
getFrameworkOrEggPath: typeof getFrameworkOrEggPath;
|
|
108
15
|
};
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
16
|
+
export default _default;
|
|
17
|
+
export declare const EggType: {
|
|
18
|
+
readonly framework: "framework";
|
|
19
|
+
readonly plugin: "plugin";
|
|
20
|
+
readonly application: "application";
|
|
21
|
+
readonly unknown: "unknown";
|
|
114
22
|
};
|
|
115
|
-
type EggType = (typeof EggType)[keyof typeof EggType];
|
|
23
|
+
export type EggType = (typeof EggType)[keyof typeof EggType];
|
|
116
24
|
/**
|
|
117
25
|
* Detect the type of egg project
|
|
118
26
|
*/
|
|
119
|
-
declare function detectType(baseDir: string): Promise<keyof typeof EggType>;
|
|
120
|
-
//#endregion
|
|
121
|
-
export { EggType, ImportModuleOptions, ImportResolveError, ImportResolveOptions, _default as default, detectType, findEggCore, getConfig, getExtensions, getFrameworkOrEggPath, getFrameworkPath, getLoadUnits, getLoader, getPlugins, getRequire, importModule, importResolve, isESM, isSupportTypeScript };
|
|
27
|
+
export declare function detectType(baseDir: string): Promise<keyof typeof EggType>;
|
package/dist/index.js
CHANGED
|
@@ -1,486 +1,52 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
//#endregion
|
|
21
|
-
//#region src/error/ImportResolveError.ts
|
|
22
|
-
var ImportResolveError = class extends Error {
|
|
23
|
-
filepath;
|
|
24
|
-
paths;
|
|
25
|
-
constructor(filepath, paths, error) {
|
|
26
|
-
const message = `${error.message}, paths: ${JSON.stringify(paths)}`;
|
|
27
|
-
super(message, { cause: error });
|
|
28
|
-
this.name = this.constructor.name;
|
|
29
|
-
this.filepath = filepath;
|
|
30
|
-
this.paths = paths;
|
|
31
|
-
Error.captureStackTrace(this, this.constructor);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
//#endregion
|
|
36
|
-
//#region src/import.ts
|
|
37
|
-
const debug$2 = debuglog("egg/utils/import");
|
|
38
|
-
let isESM = true;
|
|
39
|
-
try {
|
|
40
|
-
if (typeof import.meta !== "undefined") isESM = true;
|
|
41
|
-
} catch {
|
|
42
|
-
isESM = false;
|
|
43
|
-
}
|
|
44
|
-
const supportImportMetaResolve = parseInt(process.versions.node.split(".", 1)[0], 10) >= 18;
|
|
45
|
-
let _customRequire;
|
|
46
|
-
function getRequire() {
|
|
47
|
-
if (!_customRequire) if (typeof __require !== "undefined") _customRequire = __require;
|
|
48
|
-
else _customRequire = createRequire(process.cwd());
|
|
49
|
-
return _customRequire;
|
|
50
|
-
}
|
|
51
|
-
function getExtensions() {
|
|
52
|
-
return getRequire().extensions;
|
|
53
|
-
}
|
|
54
|
-
let _supportTypeScript;
|
|
55
|
-
function isSupportTypeScript() {
|
|
56
|
-
if (_supportTypeScript === void 0) {
|
|
57
|
-
const extensions = getExtensions();
|
|
58
|
-
_supportTypeScript = extensions[".ts"] !== void 0 || process.env.VITEST === "true" || process.env.EGG_TS_ENABLE === "true" || parseInt(process.versions.node.split(".", 1)[0], 10) >= 22;
|
|
59
|
-
debug$2("[isSupportTypeScript] %o, extensions: %j, process.env.VITEST: %j, process.env.EGG_TS_ENABLE: %j, node version: %s", _supportTypeScript, Object.keys(extensions), process.env.VITEST, process.env.EGG_TS_ENABLE, process.versions.node);
|
|
60
|
-
}
|
|
61
|
-
return _supportTypeScript;
|
|
62
|
-
}
|
|
63
|
-
function tryToResolveFromFile(filepath) {
|
|
64
|
-
const type = isESM ? "module" : "commonjs";
|
|
65
|
-
let mainIndexFile = "";
|
|
66
|
-
if (type === "module") {
|
|
67
|
-
mainIndexFile = filepath + ".mjs";
|
|
68
|
-
if (fs$1.existsSync(mainIndexFile)) {
|
|
69
|
-
debug$2("[tryToResolveFromFile] %o, use index.mjs, type: %o", mainIndexFile, type);
|
|
70
|
-
return mainIndexFile;
|
|
71
|
-
}
|
|
72
|
-
mainIndexFile = filepath + ".js";
|
|
73
|
-
if (fs$1.existsSync(mainIndexFile)) {
|
|
74
|
-
debug$2("[tryToResolveFromFile] %o, use index.js, type: %o", mainIndexFile, type);
|
|
75
|
-
return mainIndexFile;
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
78
|
-
mainIndexFile = filepath + ".cjs";
|
|
79
|
-
if (fs$1.existsSync(mainIndexFile)) {
|
|
80
|
-
debug$2("[tryToResolveFromFile] %o, use index.cjs, type: %o", mainIndexFile, type);
|
|
81
|
-
return mainIndexFile;
|
|
82
|
-
}
|
|
83
|
-
mainIndexFile = filepath + ".js";
|
|
84
|
-
if (fs$1.existsSync(mainIndexFile)) {
|
|
85
|
-
debug$2("[tryToResolveFromFile] %o, use index.js, type: %o", mainIndexFile, type);
|
|
86
|
-
return mainIndexFile;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
if (!isSupportTypeScript()) return;
|
|
90
|
-
mainIndexFile = filepath + ".ts";
|
|
91
|
-
if (fs$1.existsSync(mainIndexFile)) {
|
|
92
|
-
debug$2("[tryToResolveFromFile] %o, use index.ts, type: %o", mainIndexFile, type);
|
|
93
|
-
return mainIndexFile;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
function tryToResolveByDirnameFromPackage(dirname, pkg) {
|
|
97
|
-
const defaultMainFile = isESM ? pkg.module ?? pkg.main : pkg.main;
|
|
98
|
-
if (defaultMainFile) {
|
|
99
|
-
const mainIndexFilePath$1 = path.join(dirname, defaultMainFile);
|
|
100
|
-
if (fs$1.existsSync(mainIndexFilePath$1)) {
|
|
101
|
-
debug$2("[tryToResolveByDirnameFromPackage] %o, use pkg.main or pkg.module: %o, isESM: %s", mainIndexFilePath$1, defaultMainFile, isESM);
|
|
102
|
-
return mainIndexFilePath$1;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (pkg.exports?.["."]) {
|
|
106
|
-
const pkgType = pkg.type ?? "commonjs";
|
|
107
|
-
const defaultExport = pkg.exports["."];
|
|
108
|
-
let mainIndexFilePath$1 = "";
|
|
109
|
-
if (typeof defaultExport === "string") mainIndexFilePath$1 = path.join(dirname, defaultExport);
|
|
110
|
-
else if (pkgType === "module") {
|
|
111
|
-
if (typeof defaultExport.import === "string") mainIndexFilePath$1 = path.join(dirname, defaultExport.import);
|
|
112
|
-
else if (typeof defaultExport.import?.default === "string") mainIndexFilePath$1 = path.join(dirname, defaultExport.import.default);
|
|
113
|
-
} else if (typeof defaultExport.require === "string") mainIndexFilePath$1 = path.join(dirname, defaultExport.require);
|
|
114
|
-
else if (typeof defaultExport.require?.default === "string") mainIndexFilePath$1 = path.join(dirname, defaultExport.require.default);
|
|
115
|
-
if (mainIndexFilePath$1 && fs$1.existsSync(mainIndexFilePath$1)) {
|
|
116
|
-
debug$2("[tryToResolveByDirnameFromPackage] %o, use pkg.exports[.]: %o, pkg.type: %o", mainIndexFilePath$1, defaultExport, pkgType);
|
|
117
|
-
return mainIndexFilePath$1;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
const type = pkg?.type ?? (isESM ? "module" : "commonjs");
|
|
121
|
-
if (type === "module") {
|
|
122
|
-
const mainIndexFilePath$1 = path.join(dirname, "index.mjs");
|
|
123
|
-
if (fs$1.existsSync(mainIndexFilePath$1)) {
|
|
124
|
-
debug$2("[tryToResolveByDirnameFromPackage] %o, use index.mjs, pkg.type: %o", mainIndexFilePath$1, type);
|
|
125
|
-
return mainIndexFilePath$1;
|
|
126
|
-
}
|
|
127
|
-
const mainIndexMjsFilePath = path.join(dirname, "index.js");
|
|
128
|
-
if (fs$1.existsSync(mainIndexMjsFilePath)) {
|
|
129
|
-
debug$2("[tryToResolveByDirnameFromPackage] %o, use index.js, pkg.type: %o", mainIndexMjsFilePath, type);
|
|
130
|
-
return mainIndexMjsFilePath;
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
const mainIndexFilePath$1 = path.join(dirname, "index.cjs");
|
|
134
|
-
if (fs$1.existsSync(mainIndexFilePath$1)) {
|
|
135
|
-
debug$2("[tryToResolveByDirnameFromPackage] %o, use index.cjs, pkg.type: %o", mainIndexFilePath$1, type);
|
|
136
|
-
return mainIndexFilePath$1;
|
|
137
|
-
}
|
|
138
|
-
const mainIndexCjsFilePath = path.join(dirname, "index.js");
|
|
139
|
-
if (fs$1.existsSync(mainIndexCjsFilePath)) {
|
|
140
|
-
debug$2("[tryToResolveByDirnameFromPackage] %o, use index.js, pkg.type: %o", mainIndexCjsFilePath, type);
|
|
141
|
-
return mainIndexCjsFilePath;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (!isSupportTypeScript()) return;
|
|
145
|
-
const mainIndexFile = pkg.tshy?.exports?.["."] ?? "index.ts";
|
|
146
|
-
const mainIndexFilePath = path.join(dirname, mainIndexFile);
|
|
147
|
-
if (fs$1.existsSync(mainIndexFilePath)) return mainIndexFilePath;
|
|
148
|
-
}
|
|
149
|
-
function tryToResolveByDirname(dirname) {
|
|
150
|
-
let pkg = {};
|
|
151
|
-
const pkgFile = path.join(dirname, "package.json");
|
|
152
|
-
if (fs$1.existsSync(pkgFile)) pkg = JSON.parse(fs$1.readFileSync(pkgFile, "utf-8"));
|
|
153
|
-
return tryToResolveByDirnameFromPackage(dirname, pkg);
|
|
154
|
-
}
|
|
155
|
-
function isRelativePath(filepath) {
|
|
156
|
-
return filepath.startsWith("./") || filepath.startsWith("../") || filepath.startsWith(".\\") || filepath.startsWith("..\\");
|
|
157
|
-
}
|
|
158
|
-
function tryToResolveFromAbsoluteFile(filepath) {
|
|
159
|
-
let moduleFilePath;
|
|
160
|
-
const stat$1 = fs$1.statSync(filepath, { throwIfNoEntry: false });
|
|
161
|
-
if (stat$1?.isDirectory()) {
|
|
162
|
-
moduleFilePath = tryToResolveByDirname(filepath);
|
|
163
|
-
if (moduleFilePath) return moduleFilePath;
|
|
164
|
-
} else if (stat$1?.isFile()) return filepath;
|
|
165
|
-
moduleFilePath = tryToResolveFromFile(filepath);
|
|
166
|
-
if (moduleFilePath) return moduleFilePath;
|
|
167
|
-
const parentDir = path.dirname(filepath);
|
|
168
|
-
const basename = path.basename(filepath);
|
|
169
|
-
const pkgFile = path.join(parentDir, "package.json");
|
|
170
|
-
if (fs$1.existsSync(pkgFile)) {
|
|
171
|
-
const pkg = JSON.parse(fs$1.readFileSync(pkgFile, "utf-8"));
|
|
172
|
-
const key = `./${basename}`;
|
|
173
|
-
if (pkg.exports?.[key]) return path.join(parentDir, pkg.exports[key]);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
function importResolve(filepath, options) {
|
|
177
|
-
const paths = options?.paths ?? [process.cwd()];
|
|
178
|
-
debug$2("[importResolve] filepath: %o, options: %j, paths: %j", filepath, options, paths);
|
|
179
|
-
let moduleFilePath;
|
|
180
|
-
const isAbsolute = path.isAbsolute(filepath);
|
|
181
|
-
if (isAbsolute) {
|
|
182
|
-
moduleFilePath = tryToResolveFromAbsoluteFile(filepath);
|
|
183
|
-
if (moduleFilePath) {
|
|
184
|
-
debug$2("[importResolve:isAbsolute] %o => %o", filepath, moduleFilePath);
|
|
185
|
-
return moduleFilePath;
|
|
186
|
-
}
|
|
187
|
-
} else if (isRelativePath(filepath)) for (const p of paths) {
|
|
188
|
-
const resolvedPath = path.resolve(p, filepath);
|
|
189
|
-
moduleFilePath = tryToResolveFromAbsoluteFile(resolvedPath);
|
|
190
|
-
if (moduleFilePath) {
|
|
191
|
-
debug$2("[importResolve:isRelativePath] %o => %o => %o", filepath, resolvedPath, moduleFilePath);
|
|
192
|
-
return moduleFilePath;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
for (const p of paths) {
|
|
196
|
-
let resolvedPath = path.join(p, "node_modules", filepath);
|
|
197
|
-
moduleFilePath = tryToResolveFromAbsoluteFile(resolvedPath);
|
|
198
|
-
if (moduleFilePath) {
|
|
199
|
-
debug$2("[importResolve:node_modules] %o => %o => %o", filepath, resolvedPath, moduleFilePath);
|
|
200
|
-
return moduleFilePath;
|
|
201
|
-
}
|
|
202
|
-
let parentPath = path.dirname(p);
|
|
203
|
-
if (path.basename(parentPath) === "node_modules") {
|
|
204
|
-
resolvedPath = path.join(parentPath, filepath);
|
|
205
|
-
moduleFilePath = tryToResolveFromAbsoluteFile(resolvedPath);
|
|
206
|
-
if (moduleFilePath) {
|
|
207
|
-
debug$2("[importResolve:node_modules] %o => %o => %o", filepath, resolvedPath, moduleFilePath);
|
|
208
|
-
return moduleFilePath;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
parentPath = path.dirname(parentPath);
|
|
212
|
-
if (path.basename(parentPath) === "node_modules") {
|
|
213
|
-
resolvedPath = path.join(parentPath, filepath);
|
|
214
|
-
moduleFilePath = tryToResolveFromAbsoluteFile(resolvedPath);
|
|
215
|
-
if (moduleFilePath) {
|
|
216
|
-
debug$2("[importResolve:node_modules] %o => %o => %o", filepath, resolvedPath, moduleFilePath);
|
|
217
|
-
return moduleFilePath;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
const extname = path.extname(filepath);
|
|
222
|
-
if (!isAbsolute && extname === ".json" || !isESM) moduleFilePath = getRequire().resolve(filepath, { paths });
|
|
223
|
-
else if (supportImportMetaResolve) {
|
|
224
|
-
try {
|
|
225
|
-
moduleFilePath = import.meta.resolve(filepath);
|
|
226
|
-
} catch (err) {
|
|
227
|
-
debug$2("[importResolve:error] import.meta.resolve %o => %o, options: %o", filepath, err, options);
|
|
228
|
-
throw new ImportResolveError(filepath, paths, err);
|
|
229
|
-
}
|
|
230
|
-
if (moduleFilePath.startsWith("file://")) moduleFilePath = fileURLToPath(moduleFilePath);
|
|
231
|
-
debug$2("[importResolve] import.meta.resolve %o => %o", filepath, moduleFilePath);
|
|
232
|
-
if (!fs$1.statSync(moduleFilePath, { throwIfNoEntry: false })?.isFile()) throw new TypeError(`Cannot find module ${filepath}, because ${moduleFilePath} does not exists`);
|
|
233
|
-
} else moduleFilePath = getRequire().resolve(filepath);
|
|
234
|
-
debug$2("[importResolve:success] %o, options: %o => %o, isESM: %s", filepath, options, moduleFilePath, isESM);
|
|
235
|
-
return moduleFilePath;
|
|
236
|
-
}
|
|
237
|
-
async function importModule(filepath, options) {
|
|
238
|
-
const moduleFilePath = importResolve(filepath, options);
|
|
239
|
-
let obj;
|
|
240
|
-
if (isESM) {
|
|
241
|
-
const fileUrl = pathToFileURL(moduleFilePath).toString();
|
|
242
|
-
obj = await import(fileUrl);
|
|
243
|
-
debug$2("[importModule:success] await import %o", fileUrl);
|
|
244
|
-
if (obj?.default?.__esModule === true && "default" in obj?.default) obj = obj.default;
|
|
245
|
-
if (options?.importDefaultOnly) {
|
|
246
|
-
if ("default" in obj) obj = obj.default;
|
|
247
|
-
}
|
|
248
|
-
} else {
|
|
249
|
-
obj = __require(moduleFilePath);
|
|
250
|
-
debug$2("[importModule] require %o", moduleFilePath);
|
|
251
|
-
if (obj?.__esModule === true && "default" in obj) obj = obj.default;
|
|
252
|
-
}
|
|
253
|
-
if (debug$2.enabled) debug$2("[importModule] return %o => keys: %j, typeof obj: %s", filepath, obj ? Object.keys(obj) : obj, typeof obj);
|
|
254
|
-
return obj;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
//#endregion
|
|
258
|
-
//#region src/framework.ts
|
|
259
|
-
const debug$1 = debuglog("egg/utils/framework");
|
|
260
|
-
const initCwd = process.cwd();
|
|
261
|
-
/**
|
|
262
|
-
* Find the framework directory, lookup order
|
|
263
|
-
* - specify framework path
|
|
264
|
-
* - get framework name from
|
|
265
|
-
* - use egg by default
|
|
266
|
-
* @param {Object} options - options
|
|
267
|
-
* @param {String} options.baseDir - the current directory of application
|
|
268
|
-
* @param {String} [options.framework] - the directory of framework
|
|
269
|
-
* @return {String} frameworkPath
|
|
270
|
-
*/
|
|
271
|
-
function getFrameworkPath(options) {
|
|
272
|
-
const { framework, baseDir } = options;
|
|
273
|
-
const pkgPath = path.join(baseDir, "package.json");
|
|
274
|
-
assert(existsSync(pkgPath), `${pkgPath} should exist`);
|
|
275
|
-
const moduleDir = path.join(baseDir, "node_modules");
|
|
276
|
-
if (framework) {
|
|
277
|
-
if (path.isAbsolute(framework)) {
|
|
278
|
-
assert(existsSync(framework), `${framework} should exist`);
|
|
279
|
-
return framework;
|
|
280
|
-
}
|
|
281
|
-
return assertAndReturn(framework, moduleDir, baseDir);
|
|
282
|
-
}
|
|
283
|
-
const pkg = readJSONSync(pkgPath);
|
|
284
|
-
if (pkg.egg?.framework) return assertAndReturn(pkg.egg.framework, moduleDir, baseDir);
|
|
285
|
-
return assertAndReturn("egg", moduleDir, baseDir);
|
|
286
|
-
}
|
|
287
|
-
function assertAndReturn(frameworkName, moduleDir, baseDir) {
|
|
288
|
-
const moduleDirs = new Set([
|
|
289
|
-
moduleDir,
|
|
290
|
-
path.join(process.cwd(), "node_modules"),
|
|
291
|
-
path.join(initCwd, "node_modules")
|
|
292
|
-
]);
|
|
293
|
-
try {
|
|
294
|
-
let globalModuleDir;
|
|
295
|
-
if (frameworkName.startsWith("@") && frameworkName.includes("/")) globalModuleDir = path.join(importResolve(`${frameworkName}/package.json`, { paths: [baseDir] }), "../../..");
|
|
296
|
-
else globalModuleDir = path.join(importResolve(`${frameworkName}/package.json`, { paths: [baseDir] }), "../..");
|
|
297
|
-
moduleDirs.add(globalModuleDir);
|
|
298
|
-
} catch {}
|
|
299
|
-
for (const moduleDir$1 of moduleDirs) {
|
|
300
|
-
const frameworkPath = path.join(moduleDir$1, frameworkName);
|
|
301
|
-
if (existsSync(frameworkPath)) {
|
|
302
|
-
debug$1("[assertAndReturn] frameworkPath: %s, moduleDirs: %o", frameworkPath, moduleDirs);
|
|
303
|
-
return frameworkPath;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
throw new Error(`${frameworkName} is not found in ${Array.from(moduleDirs)}`);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
//#endregion
|
|
310
|
-
//#region src/plugin.ts
|
|
311
|
-
const debug = debuglog("egg/utils/plugin");
|
|
312
|
-
const tmpDir = os.tmpdir();
|
|
313
|
-
function noop() {}
|
|
314
|
-
const logger = {
|
|
315
|
-
debug: noop,
|
|
316
|
-
info: noop,
|
|
317
|
-
warn: noop,
|
|
318
|
-
error: noop
|
|
319
|
-
};
|
|
320
|
-
/**
|
|
321
|
-
* @see https://github.com/eggjs/egg-core/blob/2920f6eade07959d25f5c4f96b154d3fbae877db/lib/loader/mixin/plugin.js#L203
|
|
322
|
-
*/
|
|
323
|
-
async function getPlugins(options) {
|
|
324
|
-
const loader = await getLoader(options);
|
|
325
|
-
await loader.loadPlugin();
|
|
326
|
-
return loader.allPlugins;
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* @see https://github.com/eggjs/egg-core/blob/2920f6eade07959d25f5c4f96b154d3fbae877db/lib/loader/egg_loader.js#L348
|
|
330
|
-
*/
|
|
331
|
-
async function getLoadUnits(options) {
|
|
332
|
-
const loader = await getLoader(options);
|
|
333
|
-
await loader.loadPlugin();
|
|
334
|
-
return loader.getLoadUnits();
|
|
335
|
-
}
|
|
336
|
-
async function getConfig(options) {
|
|
337
|
-
const loader = await getLoader(options);
|
|
338
|
-
await loader.loadPlugin();
|
|
339
|
-
await loader.loadConfig();
|
|
340
|
-
return loader.config;
|
|
341
|
-
}
|
|
342
|
-
async function exists(filepath) {
|
|
343
|
-
try {
|
|
344
|
-
await stat(filepath);
|
|
345
|
-
return true;
|
|
346
|
-
} catch {
|
|
347
|
-
return false;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
async function getLoader(options) {
|
|
351
|
-
assert(options.framework, "framework is required");
|
|
352
|
-
assert(await exists(options.framework), `${options.framework} should exist`);
|
|
353
|
-
if (!(options.baseDir && await exists(options.baseDir))) {
|
|
354
|
-
options.baseDir = path.join(tmpDir, "egg_utils", `${Date.now()}`, "tmp_app");
|
|
355
|
-
await mkdir(options.baseDir, { recursive: true });
|
|
356
|
-
await writeFile(path.join(options.baseDir, "package.json"), JSON.stringify({
|
|
357
|
-
name: "tmp_app",
|
|
358
|
-
type: "module"
|
|
359
|
-
}));
|
|
360
|
-
debug("[getLoader] create baseDir: %o", options.baseDir);
|
|
361
|
-
}
|
|
362
|
-
const { EggCore, EggLoader } = await findEggCore(options);
|
|
363
|
-
const mod = await importModule(options.framework);
|
|
364
|
-
const Application = mod.Application ?? mod.default?.Application;
|
|
365
|
-
assert(Application, `Application not export on ${options.framework}`);
|
|
366
|
-
if (options.env) process.env.EGG_SERVER_ENV = options.env;
|
|
367
|
-
return new EggLoader({
|
|
368
|
-
baseDir: options.baseDir,
|
|
369
|
-
logger,
|
|
370
|
-
app: Object.create(Application.prototype),
|
|
371
|
-
EggCoreClass: EggCore
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
async function findEggCore(options) {
|
|
375
|
-
const baseDirRealpath = await realpath(options.baseDir);
|
|
376
|
-
const paths = [await realpath(options.framework), baseDirRealpath];
|
|
377
|
-
try {
|
|
378
|
-
const { EggCore, EggLoader } = await importModule("egg", { paths });
|
|
379
|
-
if (EggLoader) return {
|
|
380
|
-
EggCore,
|
|
381
|
-
EggLoader
|
|
382
|
-
};
|
|
383
|
-
} catch (err) {
|
|
384
|
-
debug("[findEggCore] import \"egg\" from paths:%o error: %o", paths, err);
|
|
385
|
-
}
|
|
386
|
-
const names = ["@eggjs/core", "egg-core"];
|
|
387
|
-
for (const name of names) {
|
|
388
|
-
try {
|
|
389
|
-
const { EggCore, EggLoader } = await importModule(name, { paths });
|
|
390
|
-
if (EggLoader) return {
|
|
391
|
-
EggCore,
|
|
392
|
-
EggLoader
|
|
393
|
-
};
|
|
394
|
-
} catch (err) {
|
|
395
|
-
debug("[findEggCore] import \"%s\" from paths:%o error: %o", name, paths, err);
|
|
396
|
-
}
|
|
397
|
-
try {
|
|
398
|
-
const { EggCore, EggLoader } = await importModule(name);
|
|
399
|
-
if (EggLoader) return {
|
|
400
|
-
EggCore,
|
|
401
|
-
EggLoader
|
|
402
|
-
};
|
|
403
|
-
} catch (err) {
|
|
404
|
-
debug("[findEggCore] import \"%s\" error: %o", name, err);
|
|
405
|
-
}
|
|
406
|
-
let eggCorePath = path.join(options.baseDir, `node_modules/${name}`);
|
|
407
|
-
if (!await exists(eggCorePath)) eggCorePath = path.join(options.framework, `node_modules/${name}`);
|
|
408
|
-
if (await exists(eggCorePath)) return await importModule(eggCorePath);
|
|
409
|
-
}
|
|
410
|
-
assert(false, `Can't find ${names.join(" or ")} from ${options.baseDir} and ${options.framework}`);
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
//#endregion
|
|
414
|
-
//#region src/deprecated.ts
|
|
415
|
-
/**
|
|
416
|
-
* Try to get framework dir path
|
|
417
|
-
* If can't find any framework, try to find egg dir path
|
|
418
|
-
*
|
|
419
|
-
* @param {String} cwd - current work path
|
|
420
|
-
* @param {Array} [eggNames] - egg names, default is ['egg']
|
|
421
|
-
* @return {String} framework or egg dir path
|
|
422
|
-
* @deprecated
|
|
423
|
-
*/
|
|
424
|
-
function getFrameworkOrEggPath(cwd, eggNames) {
|
|
425
|
-
eggNames = eggNames || ["egg"];
|
|
426
|
-
const moduleDir = path.join(cwd, "node_modules");
|
|
427
|
-
if (!existsSync(moduleDir)) return "";
|
|
428
|
-
const pkgFile = path.join(cwd, "package.json");
|
|
429
|
-
if (existsSync(pkgFile)) {
|
|
430
|
-
const pkg = readJSONSync(pkgFile);
|
|
431
|
-
if (pkg.egg && pkg.egg.framework) return path.join(moduleDir, pkg.egg.framework);
|
|
432
|
-
}
|
|
433
|
-
const names = readdirSync(moduleDir);
|
|
434
|
-
for (const name of names) {
|
|
435
|
-
const pkgfile = path.join(moduleDir, name, "package.json");
|
|
436
|
-
if (!existsSync(pkgfile)) continue;
|
|
437
|
-
const pkg = readJSONSync(pkgfile);
|
|
438
|
-
if (pkg.dependencies) {
|
|
439
|
-
for (const eggName of eggNames) if (pkg.dependencies[eggName]) return path.join(moduleDir, name);
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
for (const eggName of eggNames) {
|
|
443
|
-
const pkgfile = path.join(moduleDir, eggName, "package.json");
|
|
444
|
-
if (existsSync(pkgfile)) return path.join(moduleDir, eggName);
|
|
445
|
-
}
|
|
446
|
-
return "";
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
//#endregion
|
|
450
|
-
//#region src/index.ts
|
|
451
|
-
var src_default = {
|
|
452
|
-
getFrameworkPath,
|
|
453
|
-
getPlugins,
|
|
454
|
-
getConfig,
|
|
455
|
-
getLoadUnits,
|
|
456
|
-
getFrameworkOrEggPath
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import { getFrameworkPath } from "./framework.js";
|
|
4
|
+
import { getPlugins, getConfig, getLoadUnits } from "./plugin.js";
|
|
5
|
+
import { getFrameworkOrEggPath } from "./deprecated.js";
|
|
6
|
+
// support import { getFrameworkPath } from '@eggjs/utils'
|
|
7
|
+
export { getFrameworkPath } from "./framework.js";
|
|
8
|
+
export { getPlugins, getConfig, getLoadUnits, getLoader, findEggCore } from "./plugin.js";
|
|
9
|
+
export { getFrameworkOrEggPath } from "./deprecated.js";
|
|
10
|
+
export * from "./import.js";
|
|
11
|
+
export * from "./error/index.js";
|
|
12
|
+
// support import utils from '@eggjs/utils'
|
|
13
|
+
export default {
|
|
14
|
+
getFrameworkPath,
|
|
15
|
+
getPlugins,
|
|
16
|
+
getConfig,
|
|
17
|
+
getLoadUnits,
|
|
18
|
+
getFrameworkOrEggPath,
|
|
457
19
|
};
|
|
458
|
-
const EggType = {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
20
|
+
export const EggType = {
|
|
21
|
+
framework: 'framework',
|
|
22
|
+
plugin: 'plugin',
|
|
23
|
+
application: 'application',
|
|
24
|
+
unknown: 'unknown',
|
|
463
25
|
};
|
|
464
26
|
/**
|
|
465
|
-
* Detect the type of egg project
|
|
466
|
-
*/
|
|
467
|
-
async function detectType(baseDir) {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
27
|
+
* Detect the type of egg project
|
|
28
|
+
*/
|
|
29
|
+
export async function detectType(baseDir) {
|
|
30
|
+
const pkgFile = path.join(baseDir, 'package.json');
|
|
31
|
+
let pkg;
|
|
32
|
+
try {
|
|
33
|
+
await fs.access(pkgFile);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return EggType.unknown;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
pkg = JSON.parse(await fs.readFile(pkgFile, 'utf-8'));
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return EggType.unknown;
|
|
43
|
+
}
|
|
44
|
+
if (pkg.egg?.framework) {
|
|
45
|
+
return EggType.framework;
|
|
46
|
+
}
|
|
47
|
+
if (pkg.eggPlugin?.name) {
|
|
48
|
+
return EggType.plugin;
|
|
49
|
+
}
|
|
50
|
+
return EggType.application;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxJQUFJLE1BQU0sV0FBVyxDQUFDO0FBQzdCLE9BQU8sRUFBRSxNQUFNLGtCQUFrQixDQUFDO0FBQ2xDLE9BQU8sRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLGdCQUFnQixDQUFDO0FBQ2xELE9BQU8sRUFBRSxVQUFVLEVBQUUsU0FBUyxFQUFFLFlBQVksRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUNsRSxPQUFPLEVBQUUscUJBQXFCLEVBQUUsTUFBTSxpQkFBaUIsQ0FBQztBQUV4RCwwREFBMEQ7QUFDMUQsT0FBTyxFQUFFLGdCQUFnQixFQUFFLE1BQU0sZ0JBQWdCLENBQUM7QUFDbEQsT0FBTyxFQUFFLFVBQVUsRUFBRSxTQUFTLEVBQUUsWUFBWSxFQUFFLFNBQVMsRUFBRSxXQUFXLEVBQUUsTUFBTSxhQUFhLENBQUM7QUFDMUYsT0FBTyxFQUFFLHFCQUFxQixFQUFFLE1BQU0saUJBQWlCLENBQUM7QUFDeEQsY0FBYyxhQUFhLENBQUM7QUFDNUIsY0FBYyxrQkFBa0IsQ0FBQztBQUVqQywyQ0FBMkM7QUFDM0MsZUFBZTtJQUNiLGdCQUFnQjtJQUNoQixVQUFVO0lBQ1YsU0FBUztJQUNULFlBQVk7SUFDWixxQkFBcUI7Q0FDdEIsQ0FBQztBQUVGLE1BQU0sQ0FBQyxNQUFNLE9BQU8sR0FBRztJQUNyQixTQUFTLEVBQUUsV0FBVztJQUN0QixNQUFNLEVBQUUsUUFBUTtJQUNoQixXQUFXLEVBQUUsYUFBYTtJQUMxQixPQUFPLEVBQUUsU0FBUztDQUNWLENBQUM7QUFHWDs7R0FFRztBQUNILE1BQU0sQ0FBQyxLQUFLLFVBQVUsVUFBVSxDQUFDLE9BQWU7SUFDOUMsTUFBTSxPQUFPLEdBQUcsSUFBSSxDQUFDLElBQUksQ0FBQyxPQUFPLEVBQUUsY0FBYyxDQUFDLENBQUM7SUFDbkQsSUFBSSxHQU9ILENBQUM7SUFDRixJQUFJLENBQUM7UUFDSCxNQUFNLEVBQUUsQ0FBQyxNQUFNLENBQUMsT0FBTyxDQUFDLENBQUM7SUFDM0IsQ0FBQztJQUFDLE1BQU0sQ0FBQztRQUNQLE9BQU8sT0FBTyxDQUFDLE9BQU8sQ0FBQztJQUN6QixDQUFDO0lBQ0QsSUFBSSxDQUFDO1FBQ0gsR0FBRyxHQUFHLElBQUksQ0FBQyxLQUFLLENBQUMsTUFBTSxFQUFFLENBQUMsUUFBUSxDQUFDLE9BQU8sRUFBRSxPQUFPLENBQUMsQ0FBQyxDQUFDO0lBQ3hELENBQUM7SUFBQyxNQUFNLENBQUM7UUFDUCxPQUFPLE9BQU8sQ0FBQyxPQUFPLENBQUM7SUFDekIsQ0FBQztJQUNELElBQUksR0FBRyxDQUFDLEdBQUcsRUFBRSxTQUFTLEVBQUUsQ0FBQztRQUN2QixPQUFPLE9BQU8sQ0FBQyxTQUFTLENBQUM7SUFDM0IsQ0FBQztJQUNELElBQUksR0FBRyxDQUFDLFNBQVMsRUFBRSxJQUFJLEVBQUUsQ0FBQztRQUN4QixPQUFPLE9BQU8sQ0FBQyxNQUFNLENBQUM7SUFDeEIsQ0FBQztJQUNELE9BQU8sT0FBTyxDQUFDLFdBQVcsQ0FBQztBQUM3QixDQUFDIn0=
|