@eggjs/utils 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-present Alibaba Group Holding Limited and other contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ egg-utils
2
+ ---------------
3
+
4
+ [![NPM version][npm-image]][npm-url]
5
+ [![CI](https://github.com/eggjs/egg-utils/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-utils/actions/workflows/nodejs.yml)
6
+ [![Test coverage][codecov-image]][codecov-url]
7
+ [![npm download][download-image]][download-url]
8
+
9
+ [npm-image]: https://img.shields.io/npm/v/egg-utils.svg?style=flat-square
10
+ [npm-url]: https://npmjs.org/package/egg-utils
11
+ [codecov-image]: https://codecov.io/github/eggjs/egg-utils/coverage.svg?branch=master
12
+ [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
15
+
16
+ Utils for all egg projects.
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ $ npm i egg-utils
22
+ ```
23
+
24
+ ## API
25
+
26
+ ### `getPlugins(options)`
27
+
28
+ - {String} baseDir - the current directory of application
29
+ - {String} framework - the directory of framework
30
+ - {String} env - egg environment
31
+
32
+ ### `getLoadUnits(options)`
33
+
34
+ - {String} baseDir - the current directory of application
35
+ - {String} framework - the directory of framework
36
+ - {String} env - egg environment
37
+
38
+ ### `getConfig(options)`
39
+
40
+ - {String} baseDir - the current directory of application
41
+ - {String} framework - the directory of framework
42
+ - {String} env - egg environment
43
+
44
+ ### `getFrameworkPath(options)`
45
+
46
+ - {String} baseDir - the current directory of application
47
+ - {String} framework - the directory of framework
48
+
49
+ ## License
50
+
51
+ [MIT](LICENSE)
package/index.js ADDED
@@ -0,0 +1,67 @@
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
+ }
@@ -0,0 +1,74 @@
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
+
8
+ const initCwd = process.cwd();
9
+
10
+ module.exports = { getFrameworkPath };
11
+
12
+ /**
13
+ * Find the framework directory, lookup order
14
+ * - specify framework path
15
+ * - get framework name from
16
+ * - use egg by default
17
+ * @param {Object} options - options
18
+ * @param {String} options.baseDir - the current directory of application
19
+ * @param {String} [options.framework] - the directory of framework
20
+ * @return {String} frameworkPath
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;
36
+ }
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);
50
+ }
51
+
52
+ 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)}`);
74
+ }
package/lib/plugin.js ADDED
@@ -0,0 +1,69 @@
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;
21
+ };
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;
34
+ };
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
+ });
53
+ }
54
+
55
+ function findEggCore({ baseDir, framework }) {
56
+ try {
57
+ 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');
63
+ }
64
+ assert(fs.existsSync(eggCorePath), `Can't find egg-core from ${baseDir} and ${framework}`);
65
+ return require(eggCorePath);
66
+ }
67
+ }
68
+
69
+ function noop() {}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@eggjs/utils",
3
+ "version": "2.5.0",
4
+ "description": "Utils for all egg projects",
5
+ "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "lib"
9
+ ],
10
+ "scripts": {
11
+ "lint": "eslint .",
12
+ "test": "npm run lint && egg-bin test",
13
+ "cov": "egg-bin cov",
14
+ "ci": "npm run lint && npm run cov"
15
+ },
16
+ "keywords": [
17
+ "egg",
18
+ "utils"
19
+ ],
20
+ "author": "fengmk2 <fengmk2@gmail.com> (https://github.com/fengmk2)",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/eggjs/egg-utils.git"
24
+ },
25
+ "license": "MIT",
26
+ "dependencies": {
27
+ "mkdirp": "^0.5.1",
28
+ "utility": "^1.15.0"
29
+ },
30
+ "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"
41
+ },
42
+ "engine": {
43
+ "node": ">=6.0.0"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ }
48
+ }