-
Notifications
You must be signed in to change notification settings - Fork 8
/
ConfigReader.ts
140 lines (132 loc) · 5.38 KB
/
ConfigReader.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as fs from 'fs';
import * as path from 'path';
import * as merge from 'webpack-merge';
import { Builders } from './Builder';
import { ConfigPlugin } from './ConfigPlugin';
import createRequire from './createRequire';
import EnhancedError from './EnhancedError';
import inferConfig from './inferConfig';
import Spin from './Spin';
import Stack from './Stack';
interface ReadConfigOptions {
filePath: string;
inferedConfig?: any;
builderOverrides?: any;
}
export default class ConfigReader {
private spin: Spin;
private plugins: ConfigPlugin[];
constructor(spin: Spin, plugins: ConfigPlugin[]) {
this.spin = spin;
this.plugins = plugins;
}
public readConfig(options: ReadConfigOptions): Builders {
const { filePath, inferedConfig, builderOverrides } = options;
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
const dir = filePath;
const candidates = ['.spinrc.json', '.spinrc', '.spinrc.js', 'package.json'];
for (const fileName of candidates) {
const configPath = path.join(dir, fileName);
const derivedConfig = inferConfig(path.join(dir, 'package.json'));
try {
const builders = this.readConfig({ filePath: configPath, inferedConfig: derivedConfig, builderOverrides });
if (builders) {
return builders;
}
} catch (e) {
e.message = `while processing ${configPath}: ${e.message}`;
throw e;
}
}
} else {
const derivedConfig = inferedConfig || inferConfig(path.join(path.dirname(filePath), 'package.json'));
let configObject: any;
if (fs.existsSync(filePath)) {
process.chdir(path.dirname(filePath));
try {
const extname = path.extname(filePath);
if (['.json', ''].indexOf(extname) >= 0) {
try {
configObject = JSON.parse(fs.readFileSync(filePath).toString());
if (path.basename(filePath) === 'package.json') {
configObject = configObject.spin;
}
} catch (e) {
throw new EnhancedError(`Error parsing ${path.resolve(filePath)}`, e);
}
} else {
const exports = require(path.resolve(filePath));
configObject = exports instanceof Function ? exports(this.spin) : exports;
}
} finally {
process.chdir(this.spin.cwd);
}
}
return typeof configObject === 'undefined' && !fs.existsSync(filePath)
? undefined
: this._createBuilders(
filePath,
configObject && configObject.builders ? configObject : this.spin.merge(derivedConfig, configObject),
builderOverrides
);
}
}
private _createBuilders(filePath: string, config: any, builderOverrides: any): Builders {
if (typeof config === 'string' || (typeof config === 'object' && config.constructor === Array)) {
config = {
builders: {
app: config
}
};
}
config.options = config.options || {};
const relativePath = path.relative(this.spin.cwd, path.dirname(filePath));
const builders: Builders = {};
const { stack, plugins, ...options } = config.options;
for (const name of Object.keys(config.builders || {})) {
const builderVal = config.builders[name];
const builder: any = this.spin.mergeWithInStrategy(
typeof builderVal === 'object' && builderVal.constructor !== Array ? { ...builderVal } : { stack: builderVal },
builderOverrides
);
builder.name = name;
builder.require = createRequire(path.resolve(relativePath));
builder.stack = new Stack(
builder.name,
config.options.stack || [],
typeof builder === 'object' ? builder.stack : builder
);
builder.plugins = (config.plugins || []).concat(builder.plugins || []);
builder.roles = builder.roles || ['build', 'watch'];
const merged = merge(options, builder);
for (const key of Object.keys(merged)) {
builder[key] = merged[key];
}
const builderId = `${relativePath}[${builder.name}]`;
builder.id = builderId;
builder.configPath = filePath;
builders[builderId] = builder;
// TODO: remove backendBuildDir, frontendBuildDir in 0.5.x
builder.buildDir = builder.backendBuildDir || builder.frontendBuildDir ? undefined : builder.buildDir || 'build';
builder.nodeDebugger = typeof builder.nodeDebugger !== 'undefined' ? builder.nodeDebugger : true;
builder.webpackDll = typeof builder.webpackDll !== 'undefined' ? builder.webpackDll : true;
builder.sourceMap = typeof builder.sourceMap !== 'undefined' ? builder.sourceMap : true;
builder.minify = typeof builder.minify !== 'undefined' ? builder.minify : true;
builder.cache =
typeof builder.cache === 'string' && builder.cache !== 'auto'
? builder.cache
: typeof builder.cache !== 'undefined'
? builder.cache
: 'auto';
if (builder.derived) {
builder.dllBuildDir =
builder.dllBuildDir ||
path.join(typeof builder.cache === 'string' && builder.cache !== 'auto' ? builder.cache : '.cache', 'dll');
} else {
builder.dllBuildDir = builder.dllBuildDir || 'build/dll';
}
builder.plugins = this.plugins.concat((builder.plugins || []).map(pluginName => new (require(pluginName))()));
}
return builders;
}
}