@eggjs/utils 2.5.0 → 3.0.1

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/README.md CHANGED
@@ -1,24 +1,23 @@
1
- egg-utils
2
- ---------------
1
+ # @eggjs/utils
3
2
 
4
3
  [![NPM version][npm-image]][npm-url]
5
4
  [![CI](https://github.com/eggjs/egg-utils/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-utils/actions/workflows/nodejs.yml)
6
5
  [![Test coverage][codecov-image]][codecov-url]
7
6
  [![npm download][download-image]][download-url]
8
7
 
9
- [npm-image]: https://img.shields.io/npm/v/egg-utils.svg?style=flat-square
10
- [npm-url]: https://npmjs.org/package/egg-utils
8
+ [npm-image]: https://img.shields.io/npm/v/@eggjs/utils.svg?style=flat-square
9
+ [npm-url]: https://npmjs.org/package/@eggjs/utils
11
10
  [codecov-image]: https://codecov.io/github/eggjs/egg-utils/coverage.svg?branch=master
12
11
  [codecov-url]: https://codecov.io/github/eggjs/egg-utils?branch=master
13
- [download-image]: https://img.shields.io/npm/dm/egg-utils.svg?style=flat-square
14
- [download-url]: https://npmjs.org/package/egg-utils
12
+ [download-image]: https://img.shields.io/npm/dm/@eggjs/utils.svg?style=flat-square
13
+ [download-url]: https://npmjs.org/package/@eggjs/utils
15
14
 
16
15
  Utils for all egg projects.
17
16
 
18
17
  ## Installation
19
18
 
20
19
  ```bash
21
- $ npm i egg-utils
20
+ npm i @eggjs/utils
22
21
  ```
23
22
 
24
23
  ## API
@@ -49,3 +48,15 @@ $ npm i egg-utils
49
48
  ## License
50
49
 
51
50
  [MIT](LICENSE)
51
+
52
+ <!-- GITCONTRIBUTOR_START -->
53
+
54
+ ## Contributors
55
+
56
+ |[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/5345266?v=4" width="100px;"/><br/><sub><b>a526672351</b></sub>](https://github.com/a526672351)<br/>|[<img src="https://avatars.githubusercontent.com/u/2842176?v=4" width="100px;"/><br/><sub><b>XadillaX</b></sub>](https://github.com/XadillaX)<br/>|[<img src="https://avatars.githubusercontent.com/u/1974254?v=4" width="100px;"/><br/><sub><b>maqic</b></sub>](https://github.com/maqic)<br/>|
57
+ | :---: | :---: | :---: | :---: | :---: | :---: |
58
+ [<img src="https://avatars.githubusercontent.com/u/7284558?v=4" width="100px;"/><br/><sub><b>duncup</b></sub>](https://github.com/duncup)<br/>
59
+
60
+ This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Mon May 29 2023 14:07:36 GMT+0800`.
61
+
62
+ <!-- GITCONTRIBUTOR_END -->
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Try to get framework dir path
3
+ * If can't find any framework, try to find egg dir path
4
+ *
5
+ * @param {String} cwd - current work path
6
+ * @param {Array} [eggNames] - egg names, default is ['egg']
7
+ * @return {String} framework or egg dir path
8
+ * @deprecated
9
+ */
10
+ export declare function getFrameworkOrEggPath(cwd: string, eggNames?: string[]): string;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getFrameworkOrEggPath = void 0;
7
+ const node_path_1 = __importDefault(require("node:path"));
8
+ const node_fs_1 = require("node:fs");
9
+ const utils_1 = require("./utils");
10
+ /**
11
+ * Try to get framework dir path
12
+ * If can't find any framework, try to find egg dir path
13
+ *
14
+ * @param {String} cwd - current work path
15
+ * @param {Array} [eggNames] - egg names, default is ['egg']
16
+ * @return {String} framework or egg dir path
17
+ * @deprecated
18
+ */
19
+ function getFrameworkOrEggPath(cwd, eggNames) {
20
+ eggNames = eggNames || ['egg'];
21
+ const moduleDir = node_path_1.default.join(cwd, 'node_modules');
22
+ if (!(0, node_fs_1.existsSync)(moduleDir)) {
23
+ return '';
24
+ }
25
+ // try to get framework
26
+ // 1. try to read egg.framework property on package.json
27
+ const pkgFile = node_path_1.default.join(cwd, 'package.json');
28
+ if ((0, node_fs_1.existsSync)(pkgFile)) {
29
+ const pkg = (0, utils_1.readJSONSync)(pkgFile);
30
+ if (pkg.egg && pkg.egg.framework) {
31
+ return node_path_1.default.join(moduleDir, pkg.egg.framework);
32
+ }
33
+ }
34
+ // 2. try the module dependencies includes eggNames
35
+ const names = (0, node_fs_1.readdirSync)(moduleDir);
36
+ for (const name of names) {
37
+ const pkgfile = node_path_1.default.join(moduleDir, name, 'package.json');
38
+ if (!(0, node_fs_1.existsSync)(pkgfile)) {
39
+ continue;
40
+ }
41
+ const pkg = (0, utils_1.readJSONSync)(pkgfile);
42
+ if (pkg.dependencies) {
43
+ for (const eggName of eggNames) {
44
+ if (pkg.dependencies[eggName]) {
45
+ return node_path_1.default.join(moduleDir, name);
46
+ }
47
+ }
48
+ }
49
+ }
50
+ // try to get egg
51
+ for (const eggName of eggNames) {
52
+ const pkgfile = node_path_1.default.join(moduleDir, eggName, 'package.json');
53
+ if ((0, node_fs_1.existsSync)(pkgfile)) {
54
+ return node_path_1.default.join(moduleDir, eggName);
55
+ }
56
+ }
57
+ return '';
58
+ }
59
+ exports.getFrameworkOrEggPath = getFrameworkOrEggPath;
@@ -0,0 +1,16 @@
1
+ interface Options {
2
+ baseDir: string;
3
+ framework?: string;
4
+ }
5
+ /**
6
+ * Find the framework directory, lookup order
7
+ * - specify framework path
8
+ * - get framework name from
9
+ * - use egg by default
10
+ * @param {Object} options - options
11
+ * @param {String} options.baseDir - the current directory of application
12
+ * @param {String} [options.framework] - the directory of framework
13
+ * @return {String} frameworkPath
14
+ */
15
+ export declare function getFrameworkPath(options: Options): string;
16
+ export {};
package/lib/framework.js CHANGED
@@ -1,14 +1,14 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
- const assert = require('assert');
5
- const fs = require('fs');
6
- const utility = require('utility');
7
-
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getFrameworkPath = void 0;
7
+ const node_path_1 = __importDefault(require("node:path"));
8
+ const node_assert_1 = __importDefault(require("node:assert"));
9
+ const node_fs_1 = require("node:fs");
10
+ const utils_1 = require("./utils");
8
11
  const initCwd = process.cwd();
9
-
10
- module.exports = { getFrameworkPath };
11
-
12
12
  /**
13
13
  * Find the framework directory, lookup order
14
14
  * - specify framework path
@@ -19,56 +19,62 @@ module.exports = { getFrameworkPath };
19
19
  * @param {String} [options.framework] - the directory of framework
20
20
  * @return {String} frameworkPath
21
21
  */
22
- function getFrameworkPath({ framework, baseDir }) {
23
- const pkgPath = path.join(baseDir, 'package.json');
24
- assert(fs.existsSync(pkgPath), `${pkgPath} should exist`);
25
-
26
- const moduleDir = path.join(baseDir, 'node_modules');
27
- const pkg = utility.readJSONSync(pkgPath);
28
-
29
- // 1. pass framework or customEgg
30
- if (framework) {
31
- // 1.1 framework is an absolute path
32
- // framework: path.join(baseDir, 'node_modules/${frameworkName}')
33
- if (path.isAbsolute(framework)) {
34
- assert(fs.existsSync(framework), `${framework} should exist`);
35
- return framework;
22
+ function getFrameworkPath(options) {
23
+ const { framework, baseDir } = options;
24
+ const pkgPath = node_path_1.default.join(baseDir, 'package.json');
25
+ (0, node_assert_1.default)((0, node_fs_1.existsSync)(pkgPath), `${pkgPath} should exist`);
26
+ const moduleDir = node_path_1.default.join(baseDir, 'node_modules');
27
+ // 1. pass framework or customEgg
28
+ if (framework) {
29
+ // 1.1 framework is an absolute path
30
+ // framework: path.join(baseDir, 'node_modules/${frameworkName}')
31
+ if (node_path_1.default.isAbsolute(framework)) {
32
+ (0, node_assert_1.default)((0, node_fs_1.existsSync)(framework), `${framework} should exist`);
33
+ return framework;
34
+ }
35
+ // 1.2 framework is a npm package that required by application
36
+ // framework: 'frameworkName'
37
+ return assertAndReturn(framework, moduleDir);
36
38
  }
37
- // 1.2 framework is a npm package that required by application
38
- // framework: 'frameworkName'
39
- return assertAndReturn(framework, moduleDir);
40
- }
41
-
42
- // 2. framework is not specified
43
- // 2.1 use framework name from pkg.egg.framework
44
- if (pkg.egg && pkg.egg.framework) {
45
- return assertAndReturn(pkg.egg.framework, moduleDir);
46
- }
47
-
48
- // 2.2 use egg by default
49
- return assertAndReturn('egg', moduleDir);
39
+ const pkg = (0, utils_1.readJSONSync)(pkgPath);
40
+ // 2. framework is not specified
41
+ // 2.1 use framework name from pkg.egg.framework
42
+ if (pkg.egg?.framework) {
43
+ return assertAndReturn(pkg.egg.framework, moduleDir);
44
+ }
45
+ // 2.2 use egg by default
46
+ return assertAndReturn('egg', moduleDir);
50
47
  }
51
-
48
+ exports.getFrameworkPath = getFrameworkPath;
52
49
  function assertAndReturn(frameworkName, moduleDir) {
53
- const moduleDirs = new Set([
54
- moduleDir,
55
- // find framework from process.cwd, especially for test,
56
- // the application is in test/fixtures/app,
57
- // and framework is install in ${cwd}/node_modules
58
- path.join(process.cwd(), 'node_modules'),
59
- // prevent from mocking process.cwd
60
- path.join(initCwd, 'node_modules'),
61
- ]);
62
- try {
63
- // find framework from global, especially for monorepo
64
- const globalModuleDir = path.join(require.resolve(`${frameworkName}/package.json`), '../..');
65
- moduleDirs.add(globalModuleDir);
66
- } catch (err) {
67
- // ignore
68
- }
69
- for (const moduleDir of moduleDirs) {
70
- const frameworkPath = path.join(moduleDir, frameworkName);
71
- if (fs.existsSync(frameworkPath)) return frameworkPath;
72
- }
73
- throw new Error(`${frameworkName} is not found in ${Array.from(moduleDirs)}`);
50
+ const moduleDirs = new Set([
51
+ moduleDir,
52
+ // find framework from process.cwd, especially for test,
53
+ // the application is in test/fixtures/app,
54
+ // and framework is install in ${cwd}/node_modules
55
+ node_path_1.default.join(process.cwd(), 'node_modules'),
56
+ // prevent from mocking process.cwd
57
+ node_path_1.default.join(initCwd, 'node_modules'),
58
+ ]);
59
+ try {
60
+ // find framework from global, especially for monorepo
61
+ let globalModuleDir;
62
+ // if frameworkName is scoped package, like @ali/egg
63
+ if (frameworkName.startsWith('@') && frameworkName.includes('/')) {
64
+ globalModuleDir = node_path_1.default.join(require.resolve(`${frameworkName}/package.json`), '../../..');
65
+ }
66
+ else {
67
+ globalModuleDir = node_path_1.default.join(require.resolve(`${frameworkName}/package.json`), '../..');
68
+ }
69
+ moduleDirs.add(globalModuleDir);
70
+ }
71
+ catch (err) {
72
+ // ignore
73
+ }
74
+ for (const moduleDir of moduleDirs) {
75
+ const frameworkPath = node_path_1.default.join(moduleDir, frameworkName);
76
+ if ((0, node_fs_1.existsSync)(frameworkPath))
77
+ return frameworkPath;
78
+ }
79
+ throw new Error(`${frameworkName} is not found in ${Array.from(moduleDirs)}`);
74
80
  }
package/lib/index.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { getFrameworkPath } from './framework';
2
+ import { getPlugins, getConfig, getLoadUnits } from './plugin';
3
+ import { getFrameworkOrEggPath } from './deprecated';
4
+ export { getFrameworkPath } from './framework';
5
+ export { getPlugins, getConfig, getLoadUnits } from './plugin';
6
+ export { getFrameworkOrEggPath } from './deprecated';
7
+ declare const _default: {
8
+ getFrameworkPath: typeof getFrameworkPath;
9
+ getPlugins: typeof getPlugins;
10
+ getConfig: typeof getConfig;
11
+ getLoadUnits: typeof getLoadUnits;
12
+ getFrameworkOrEggPath: typeof getFrameworkOrEggPath;
13
+ };
14
+ export default _default;
package/lib/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getFrameworkOrEggPath = exports.getLoadUnits = exports.getConfig = exports.getPlugins = exports.getFrameworkPath = void 0;
4
+ const framework_1 = require("./framework");
5
+ const plugin_1 = require("./plugin");
6
+ const deprecated_1 = require("./deprecated");
7
+ // support import { getFrameworkPath } from '@eggjs/utils'
8
+ var framework_2 = require("./framework");
9
+ Object.defineProperty(exports, "getFrameworkPath", { enumerable: true, get: function () { return framework_2.getFrameworkPath; } });
10
+ var plugin_2 = require("./plugin");
11
+ Object.defineProperty(exports, "getPlugins", { enumerable: true, get: function () { return plugin_2.getPlugins; } });
12
+ Object.defineProperty(exports, "getConfig", { enumerable: true, get: function () { return plugin_2.getConfig; } });
13
+ Object.defineProperty(exports, "getLoadUnits", { enumerable: true, get: function () { return plugin_2.getLoadUnits; } });
14
+ var deprecated_2 = require("./deprecated");
15
+ Object.defineProperty(exports, "getFrameworkOrEggPath", { enumerable: true, get: function () { return deprecated_2.getFrameworkOrEggPath; } });
16
+ // support import utils from '@eggjs/utils'
17
+ exports.default = {
18
+ getFrameworkPath: framework_1.getFrameworkPath,
19
+ getPlugins: plugin_1.getPlugins, getConfig: plugin_1.getConfig, getLoadUnits: plugin_1.getLoadUnits,
20
+ getFrameworkOrEggPath: deprecated_1.getFrameworkOrEggPath,
21
+ };
@@ -0,0 +1,30 @@
1
+ interface LoaderOptions {
2
+ framework: string;
3
+ baseDir: string;
4
+ env?: string;
5
+ }
6
+ interface Plugin {
7
+ name: string;
8
+ version?: string;
9
+ enable: boolean;
10
+ implicitEnable: boolean;
11
+ path: string;
12
+ dependencies: string[];
13
+ optionalDependencies: string[];
14
+ env: string[];
15
+ from: string;
16
+ }
17
+ /**
18
+ * @see https://github.com/eggjs/egg-core/blob/2920f6eade07959d25f5c4f96b154d3fbae877db/lib/loader/mixin/plugin.js#L203
19
+ */
20
+ export declare function getPlugins(options: LoaderOptions): Record<string, Plugin>;
21
+ interface Unit {
22
+ type: 'plugin' | 'framework' | 'app';
23
+ path: string;
24
+ }
25
+ /**
26
+ * @see https://github.com/eggjs/egg-core/blob/2920f6eade07959d25f5c4f96b154d3fbae877db/lib/loader/egg_loader.js#L348
27
+ */
28
+ export declare function getLoadUnits(options: LoaderOptions): Unit[];
29
+ export declare function getConfig(options: LoaderOptions): Record<string, any>;
30
+ export {};
package/lib/plugin.js CHANGED
@@ -1,69 +1,98 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
- const assert = require('assert');
6
- const os = require('os');
7
- const mkdirp = require('mkdirp');
8
-
9
- const tmpDir = os.tmpdir();
10
- const logger = {
11
- debug: noop,
12
- info: noop,
13
- warn: noop,
14
- error: noop,
15
- };
16
-
17
- exports.getPlugins = opt => {
18
- const loader = getLoader(opt);
19
- loader.loadPlugin();
20
- return loader.allPlugins;
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-var-requires */
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
21
5
  };
22
-
23
- exports.getLoadUnits = opt => {
24
- const loader = getLoader(opt);
25
- loader.loadPlugin();
26
- return loader.getLoadUnits();
27
- };
28
-
29
- exports.getConfig = opt => {
30
- const loader = getLoader(opt);
31
- loader.loadPlugin();
32
- loader.loadConfig();
33
- return loader.config;
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.getConfig = exports.getLoadUnits = exports.getPlugins = void 0;
8
+ const node_path_1 = __importDefault(require("node:path"));
9
+ const node_assert_1 = __importDefault(require("node:assert"));
10
+ const node_os_1 = __importDefault(require("node:os"));
11
+ const node_fs_1 = require("node:fs");
12
+ const tmpDir = node_os_1.default.tmpdir();
13
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
14
+ function noop() { }
15
+ const logger = {
16
+ debug: noop,
17
+ info: noop,
18
+ warn: noop,
19
+ error: noop,
34
20
  };
35
-
36
- function getLoader({ framework, baseDir, env }) {
37
- assert(framework, 'framework is required');
38
- assert(fs.existsSync(framework), `${framework} should exist`);
39
- if (!(baseDir && fs.existsSync(baseDir))) {
40
- baseDir = path.join(tmpDir, String(Date.now()), 'tmpapp');
41
- mkdirp.sync(baseDir);
42
- fs.writeFileSync(path.join(baseDir, 'package.json'), JSON.stringify({ name: 'tmpapp' }));
43
- }
44
-
45
- const EggLoader = findEggCore({ baseDir, framework }).EggLoader;
46
- const { Application } = require(framework);
47
- if (env) process.env.EGG_SERVER_ENV = env;
48
- return new EggLoader({
49
- baseDir,
50
- logger,
51
- app: Object.create(Application.prototype),
52
- });
21
+ /**
22
+ * @see https://github.com/eggjs/egg-core/blob/2920f6eade07959d25f5c4f96b154d3fbae877db/lib/loader/mixin/plugin.js#L203
23
+ */
24
+ function getPlugins(options) {
25
+ const loader = getLoader(options);
26
+ loader.loadPlugin();
27
+ return loader.allPlugins;
28
+ }
29
+ exports.getPlugins = getPlugins;
30
+ /**
31
+ * @see https://github.com/eggjs/egg-core/blob/2920f6eade07959d25f5c4f96b154d3fbae877db/lib/loader/egg_loader.js#L348
32
+ */
33
+ function getLoadUnits(options) {
34
+ const loader = getLoader(options);
35
+ loader.loadPlugin();
36
+ return loader.getLoadUnits();
37
+ }
38
+ exports.getLoadUnits = getLoadUnits;
39
+ function getConfig(options) {
40
+ const loader = getLoader(options);
41
+ loader.loadPlugin();
42
+ loader.loadConfig();
43
+ return loader.config;
44
+ }
45
+ exports.getConfig = getConfig;
46
+ function getLoader(options) {
47
+ let { framework, baseDir, env } = options;
48
+ (0, node_assert_1.default)(framework, 'framework is required');
49
+ (0, node_assert_1.default)((0, node_fs_1.existsSync)(framework), `${framework} should exist`);
50
+ if (!(baseDir && (0, node_fs_1.existsSync)(baseDir))) {
51
+ baseDir = node_path_1.default.join(tmpDir, String(Date.now()), 'tmpapp');
52
+ (0, node_fs_1.mkdirSync)(baseDir, { recursive: true });
53
+ (0, node_fs_1.writeFileSync)(node_path_1.default.join(baseDir, 'package.json'), JSON.stringify({ name: 'tmpapp' }));
54
+ }
55
+ const EggLoader = findEggCore({ baseDir, framework });
56
+ const { Application } = require(framework);
57
+ if (env)
58
+ process.env.EGG_SERVER_ENV = env;
59
+ return new EggLoader({
60
+ baseDir,
61
+ logger,
62
+ app: Object.create(Application.prototype),
63
+ });
53
64
  }
54
-
55
65
  function findEggCore({ baseDir, framework }) {
56
- try {
66
+ const baseDirRealpath = (0, node_fs_1.realpathSync)(baseDir);
67
+ const frameworkRealpath = (0, node_fs_1.realpathSync)(framework);
68
+ // custom framework => egg => egg/lib/loader/index.js
69
+ try {
70
+ return require(require.resolve('egg/lib/loader', {
71
+ paths: [frameworkRealpath, baseDirRealpath],
72
+ })).EggLoader;
73
+ }
74
+ catch {
75
+ // ignore
76
+ }
57
77
  const name = 'egg-core';
58
- return require(name);
59
- } catch (_) {
60
- let eggCorePath = path.join(baseDir, 'node_modules/egg-core');
61
- if (!fs.existsSync(eggCorePath)) {
62
- eggCorePath = path.join(framework, 'node_modules/egg-core');
78
+ // egg => egg-core
79
+ try {
80
+ return require(require.resolve(name, {
81
+ paths: [frameworkRealpath, baseDirRealpath],
82
+ })).EggLoader;
83
+ }
84
+ catch {
85
+ // ignore
86
+ }
87
+ try {
88
+ return require(name).EggLoader;
89
+ }
90
+ catch {
91
+ let eggCorePath = node_path_1.default.join(baseDir, `node_modules/${name}`);
92
+ if (!(0, node_fs_1.existsSync)(eggCorePath)) {
93
+ eggCorePath = node_path_1.default.join(framework, `node_modules/${name}`);
94
+ }
95
+ (0, node_assert_1.default)((0, node_fs_1.existsSync)(eggCorePath), `Can't find ${name} from ${baseDir} and ${framework}`);
96
+ return require(eggCorePath).EggLoader;
63
97
  }
64
- assert(fs.existsSync(eggCorePath), `Can't find egg-core from ${baseDir} and ${framework}`);
65
- return require(eggCorePath);
66
- }
67
98
  }
68
-
69
- function noop() {}
package/lib/utils.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function readJSONSync(file: string): any;
package/lib/utils.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readJSONSync = void 0;
4
+ const node_fs_1 = require("node:fs");
5
+ function readJSONSync(file) {
6
+ if (!(0, node_fs_1.existsSync)(file)) {
7
+ throw new Error(`${file} is not found`);
8
+ }
9
+ return JSON.parse((0, node_fs_1.readFileSync)(file, 'utf-8'));
10
+ }
11
+ exports.readJSONSync = readJSONSync;
package/package.json CHANGED
@@ -1,17 +1,22 @@
1
1
  {
2
2
  "name": "@eggjs/utils",
3
- "version": "2.5.0",
3
+ "version": "3.0.1",
4
4
  "description": "Utils for all egg projects",
5
- "main": "index.js",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
6
7
  "files": [
7
- "index.js",
8
8
  "lib"
9
9
  ],
10
10
  "scripts": {
11
11
  "lint": "eslint .",
12
- "test": "npm run lint && egg-bin test",
13
- "cov": "egg-bin cov",
14
- "ci": "npm run lint && npm run cov"
12
+ "pretest": "npm run lint && npm run tsc",
13
+ "test": "egg-bin test",
14
+ "preci": "npm run lint && npm run tsc",
15
+ "ci": "egg-bin cov",
16
+ "tsc": "tsc",
17
+ "clean": "tsc --build --clean",
18
+ "prepublishOnly": "npm run clean && npm run tsc",
19
+ "contributor": "git-contributor"
15
20
  },
16
21
  "keywords": [
17
22
  "egg",
@@ -23,24 +28,24 @@
23
28
  "url": "https://github.com/eggjs/egg-utils.git"
24
29
  },
25
30
  "license": "MIT",
26
- "dependencies": {
27
- "mkdirp": "^0.5.1",
28
- "utility": "^1.15.0"
29
- },
31
+ "dependencies": {},
30
32
  "devDependencies": {
31
- "coffee": "^5.1.0",
32
- "cpy": "^7.0.1",
33
- "egg-bin": "^4.9.0",
34
- "eslint": "^5.9.0",
35
- "eslint-config-egg": "^7.1.0",
36
- "mm": "^2.4.1",
37
- "mz-modules": "^2.1.0",
38
- "npm": "^6.4.1",
39
- "npminstall": "^3.15.0",
40
- "runscript": "^1.3.0"
33
+ "@eggjs/tsconfig": "^1.3.3",
34
+ "@types/mocha": "^10.0.1",
35
+ "@types/node": "^20.2.5",
36
+ "coffee": "^5.5.0",
37
+ "egg-bin": "^6.4.0",
38
+ "eslint": "^8.41.0",
39
+ "eslint-config-egg": "^12.2.1",
40
+ "git-contributor": "^2.1.5",
41
+ "mm": "^3.3.0",
42
+ "npm": "^9.6.7",
43
+ "npminstall": "^7.9.0",
44
+ "runscript": "^1.5.3",
45
+ "typescript": "^5.0.4"
41
46
  },
42
47
  "engine": {
43
- "node": ">=6.0.0"
48
+ "node": ">=16.13.0"
44
49
  },
45
50
  "publishConfig": {
46
51
  "access": "public"
package/index.js DELETED
@@ -1,67 +0,0 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
- const utility = require('utility');
6
-
7
- [
8
- require('./lib/framework'),
9
- require('./lib/plugin'),
10
- { getFrameworkOrEggPath },
11
- ]
12
- .forEach(obj => Object.assign(exports, obj));
13
-
14
- /**
15
- * Try to get framework dir path
16
- * If can't find any framework, try to find egg dir path
17
- *
18
- * @param {String} cwd - current work path
19
- * @param {Array} [eggNames] - egg names, default is ['egg']
20
- * @return {String} framework or egg dir path
21
- * @deprecated
22
- */
23
- function getFrameworkOrEggPath(cwd, eggNames) {
24
- eggNames = eggNames || [ 'egg' ];
25
- const moduleDir = path.join(cwd, 'node_modules');
26
- if (!fs.existsSync(moduleDir)) {
27
- return '';
28
- }
29
-
30
- // try to get framework
31
-
32
- // 1. try to read egg.framework property on package.json
33
- const pkgFile = path.join(cwd, 'package.json');
34
- if (fs.existsSync(pkgFile)) {
35
- const pkg = utility.readJSONSync(pkgFile);
36
- if (pkg.egg && pkg.egg.framework) {
37
- return path.join(moduleDir, pkg.egg.framework);
38
- }
39
- }
40
-
41
- // 2. try the module dependencies includes eggNames
42
- const names = fs.readdirSync(moduleDir);
43
- for (const name of names) {
44
- const pkgfile = path.join(moduleDir, name, 'package.json');
45
- if (!fs.existsSync(pkgfile)) {
46
- continue;
47
- }
48
- const pkg = utility.readJSONSync(pkgfile);
49
- if (pkg.dependencies) {
50
- for (const eggName of eggNames) {
51
- if (pkg.dependencies[eggName]) {
52
- return path.join(moduleDir, name);
53
- }
54
- }
55
- }
56
- }
57
-
58
- // try to get egg
59
- for (const eggName of eggNames) {
60
- const pkgfile = path.join(moduleDir, eggName, 'package.json');
61
- if (fs.existsSync(pkgfile)) {
62
- return path.join(moduleDir, eggName);
63
- }
64
- }
65
-
66
- return '';
67
- }