-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframework.js
64 lines (54 loc) · 1.93 KB
/
framework.js
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const path = require('path');
const fs = require('fs');
const utility = require('utility');
const initCwd = process.cwd();
// const defaultFramework = path.resolve(__dirname, '../framework');
exports.getFrameworkPath = getFrameworkPath;
exports.assertAndReturn = assertAndReturn;
/**
* Find the framework directory, lookup order
* - specify framework path
* - get framework name from
* - use egg by default
* @param {Object} options - options
* @param {String} options.baseDir - the current directory of application
* @param {String} [options.framework] - the directory of framework
* @return {String} frameworkPath
*/
function getFrameworkPath(framework, baseDir) {
const pkgPath = path.join(baseDir, 'package.json');
if (!fs.existsSync(pkgPath)) {
throw new Error(`${pkgPath} should exist`);
}
const moduleDir = path.join(baseDir, 'node_modules');
const pkg = utility.readJSONSync(pkgPath);
if (framework) {
if (path.isAbsolute(framework)) {
if (!fs.existsSync(framework)) {
throw new Error(`${framework} should exist`);
}
return framework;
}
return assertAndReturn(framework, moduleDir);
}
if (pkg.framework) {
return assertAndReturn(pkg.framework, moduleDir);
}
throw new Error('you should set framework first, then listen the server');
}
function assertAndReturn(frameworkName, moduleDir) {
const moduleDirs = new Set([
moduleDir,
// find framework from process.cwd, especially for test,
// the application is in test/fixtures/app,
// and framework is install in ${cwd}/node_modules
path.join(process.cwd(), 'node_modules'),
// prevent from mocking process.cwd
path.join(initCwd, 'node_modules'),
]);
for (const moduleDir of moduleDirs) {
const frameworkPath = path.join(moduleDir, frameworkName);
if (fs.existsSync(frameworkPath)) return frameworkPath;
}
throw new Error(`${frameworkName} is not found in ${Array.from(moduleDirs)}`);
}