/
Configuration.js
330 lines (287 loc) · 9.36 KB
/
Configuration.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// @flow
import crypto from 'crypto';
import os from 'os';
import path from 'path';
import has from 'lodash/has';
import globals from 'globals';
import semver from 'semver';
import FileUtils from './FileUtils';
import JsModule from './JsModule';
import findPackageDependencies from './findPackageDependencies';
import meteorEnvironment from './environments/meteorEnvironment';
import nodeEnvironment from './environments/nodeEnvironment';
import normalizePath from './normalizePath';
import version from './version';
const JSON_CONFIG_FILE = '.importjs.json';
const JS_CONFIG_FILE = '.importjs.js';
function findGlobalsFromEnvironments(environments: Array<string>): Array<string> {
const result = Object.keys(globals.builtin);
environments.forEach((environment: string) => {
const envGlobals = globals[environment];
if (!envGlobals) {
return;
}
result.push(...Object.keys(envGlobals));
});
return result;
}
const DEFAULT_CONFIG = {
aliases: {},
declarationKeyword: 'import',
cacheLocation: ({ config }: Object): string => {
const hash = crypto
.createHash('md5')
.update(`${config.workingDirectory}-v4`)
.digest('hex');
return path.join(os.tmpdir(), `import-js-${hash}.db`);
},
coreModules: [],
namedExports: {},
environments: [],
excludes: [],
globals: ({ config }: Object): Array<string> =>
findGlobalsFromEnvironments(config.get('environments')),
groupImports: true,
ignorePackagePrefixes: [],
importDevDependencies: false,
importFunction: 'require',
importStatementFormatter: ({ importStatement }: Object): string =>
importStatement,
logLevel: 'info',
maxLineLength: 80,
minimumVersion: '0.0.0',
moduleNameFormatter: ({ moduleName }: Object): string => moduleName,
moduleSideEffectImports: (): Array<string> => [],
sortImports: true,
emptyLineBetweenGroups: true,
stripFileExtensions: ['.js', '.jsx', '.ts', '.tsx'],
danglingCommas: true,
tab: ' ',
useRelativePaths: true,
packageDependencies: ({ config }: Object): Set<string> =>
findPackageDependencies(
config.workingDirectory,
config.get('importDevDependencies'),
),
// Default configuration options, and options inherited from environment
// configuration are overridden if they appear in user config. Some options,
// however, get merged with the parent configuration. This list specifies which
// ones are merged.
mergableOptions: {
aliases: true,
coreModules: true,
namedExports: true,
globals: true,
},
};
const KNOWN_CONFIGURATION_OPTIONS = [
'aliases',
'cacheLocation',
'coreModules',
'declarationKeyword',
'environments',
'excludes',
'globals',
'groupImports',
'ignorePackagePrefixes',
'importDevDependencies',
'importFunction',
'importStatementFormatter',
'logLevel',
'maxLineLength',
'minimumVersion',
'moduleNameFormatter',
'moduleSideEffectImports',
'namedExports',
'sortImports',
'stripFileExtensions',
'tab',
'useRelativePaths',
'mergableOptions',
'danglingCommas',
'emptyLineBetweenGroups',
];
const DEPRECATED_CONFIGURATION_OPTIONS = [];
const ENVIRONMENTS = {
node: nodeEnvironment,
meteor: meteorEnvironment,
};
function checkForUnknownConfiguration(config: Object): Array<string> {
const messages = [];
Object.keys(config).forEach((option: string) => {
if (KNOWN_CONFIGURATION_OPTIONS.indexOf(option) === -1) {
messages.push(`Unknown configuration: \`${option}\``);
}
});
return messages;
}
function checkForDeprecatedConfiguration(config: Object): Array<string> {
const messages = [];
Object.keys(config).forEach((option: string) => {
if (DEPRECATED_CONFIGURATION_OPTIONS.indexOf(option) !== -1) {
messages.push(`Using ${option} to configure ImportJS is deprecated and ` +
'will go away in a future version.');
}
});
return messages;
}
/**
* Checks that the current version is bigger than the `minimumVersion`
* defined in config.
* @throws Error if current version is less than the `minimumVersion` defined
* in config.
*/
function checkCurrentVersion(minimumVersion: string) {
if (semver.gte(version(), minimumVersion)) {
return;
}
throw Error(`The configuration file for this project requires version ${minimumVersion} or newer. You are using ${version()}.`);
}
function mergedValue(values: Array<any>, key: string, options: Object): any {
let mergedResult;
for (let i = 0; i < values.length; i += 1) {
let value = values[i];
if (typeof value === 'function') {
value = value(options);
}
// Prevent an endless loop of mergedValue calls
// The mergableOptions key will get merged by skipping this check
if (key !== 'mergableOptions') {
const mergableOptions = options.config.get('mergableOptions');
if (mergableOptions[key] !== true) {
// This key shouldn't be merged
return value;
}
}
if (Array.isArray(value)) {
mergedResult = (mergedResult || []).concat(value);
} else if (typeof value === 'object') {
mergedResult = Object.assign({}, value, mergedResult || {});
} else {
// Neither an object nor an array, so we just return the first value we
// have.
return value;
}
}
return mergedResult;
}
// Class that initializes configuration from a .importjs.js file
export default class Configuration {
pathToCurrentFile: string;
messages: Array<string>;
configs: Array<Object>;
workingDirectory: string;
constructor(
pathToCurrentFile: string,
workingDirectory: string = process.cwd(),
) {
this.workingDirectory = workingDirectory;
this.pathToCurrentFile = normalizePath(pathToCurrentFile, workingDirectory);
this.messages = [];
this.configs = [];
let userConfig;
try {
userConfig = this.loadUserConfig();
} catch (error) {
this.messages.push(`Unable to parse configuration file. Reason:\n${error.stack}`);
}
if (userConfig) {
this.configs.push(userConfig);
this.messages.push(...checkForUnknownConfiguration(userConfig));
this.messages.push(...checkForDeprecatedConfiguration(userConfig));
// Add configurations for the environments specified in the user config
// file.
// Don't use `this.get` because the config hasn't finished initalizing.
// Use userConfig instead since it's the only one declared
if (typeof userConfig.environments === 'function') {
userConfig.environments = userConfig.environments({
config: this,
pathToCurrentFile: this.pathToCurrentFile,
});
}
(userConfig.environments || []).forEach((environment: string) => {
const envConfig = ENVIRONMENTS[environment];
if (envConfig) {
this.configs.push(envConfig);
}
});
}
this.configs.push(DEFAULT_CONFIG);
checkCurrentVersion(this.get('minimumVersion'));
}
get(
key: string,
{
pathToImportedModule,
moduleName,
importStatement,
}: {
pathToImportedModule?: string,
moduleName?: string,
importStatement?: string,
} = {},
): any {
const applyingConfigs = this.configs.filter((config: Object): boolean =>
Object.prototype.hasOwnProperty.call(config, key));
return mergedValue(
applyingConfigs.map((config: Object): any => config[key]),
key,
{
pathToImportedModule,
moduleName,
config: this,
pathToCurrentFile: this.pathToCurrentFile,
importStatement,
},
);
}
loadUserConfig(): ?Object {
const jsConfig = FileUtils.readJsFile(path.join(this.workingDirectory, JS_CONFIG_FILE));
if (jsConfig && Object.keys(jsConfig).length === 0) {
// If you forget to use `module.exports`, the config object will be `{}`.
// To prevent subtle errors from happening, we surface an error message to
// the user.
throw new Error(`Nothing exported from ${JS_CONFIG_FILE}. You need to use \`module.exports\` to specify what gets exported from the file.`);
}
if (jsConfig) {
return jsConfig;
}
const jsonConfig = FileUtils.readJsonFile(path.join(this.workingDirectory, JSON_CONFIG_FILE));
if (jsonConfig) {
this.messages.push('Using JSON to configure ImportJS is deprecated and will go away in a future version. Use an `.importjs.js` file instead.');
return jsonConfig;
}
const globalConfig = FileUtils.readJsFile(path.join(os.homedir(), JS_CONFIG_FILE));
return globalConfig;
}
resolveAlias(variableName: string): ?string {
if (!has(this.get('aliases'), variableName)) {
return null;
}
let importPath = this.get('aliases')[variableName];
importPath = importPath.path || importPath; // path may be an object
if (this.pathToCurrentFile !== './') {
// aliases can have dynamic `{filename}` parts
importPath = importPath.replace(
/\{filename\}/,
path.basename(this.pathToCurrentFile, path.extname(this.pathToCurrentFile)),
);
}
return importPath;
}
resolveNamedExports(variableName: string): ?JsModule {
const allNamedExports = this.get('namedExports');
const importPath = Object.keys(allNamedExports)
.find((key: string): boolean =>
allNamedExports[key].indexOf(variableName) !== -1);
if (!importPath) {
return undefined;
}
const jsModule = new JsModule({
importPath,
hasNamedExports: true,
variableName,
});
return jsModule;
}
}