-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (55 loc) · 2.16 KB
/
index.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
"use strict";
const Plugin = require('broccoli-plugin');
const fs = require('fs');
const path = require('path');
const INTERFACE = `
import { ResolverConfiguration } from '@glimmer/resolver';
declare let config: ResolverConfiguration;
export default config;
`;
class ResolverConfigurationBuilder extends Plugin {
constructor(config, options) {
options = options || {};
super([config], {
annotation: options.annotation
});
this.options = options;
}
build() {
// Attempt to read config file
let configPath = path.join(this.inputPaths[0], this.options.configPath);
let config;
if (fs.existsSync(configPath)) {
let configContents = fs.readFileSync(configPath, { encoding: 'utf8' });
config = JSON.parse(configContents);
} else {
config = {};
}
let moduleConfig = config.moduleConfiguration || this.options.defaultModuleConfiguration;
if (!moduleConfig) {
throw new Error(`The module configuration could not be found. Please add a config file to '${configPath}' and export an object with a 'moduleConfiguration' member.`);
}
let modulePrefix = config.modulePrefix || this.options.defaultModulePrefix;
if (!modulePrefix) {
throw new Error(`The module prefix could not be found. Add a config file to '${configPath}' and export an object with a 'modulePrefix' member.`);
}
let rootName = modulePrefix;
let name = this.options.name || modulePrefix;
let resolverConfiguration = {
app: { name, rootName },
types: moduleConfig.types,
collections: moduleConfig.collections
}
if (this.options.logResult) {
this.result = resolverConfiguration;
}
let destPath = path.join(this.outputPath, 'config');
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath);
}
let contents = "export default " + JSON.stringify(resolverConfiguration) + ";" + '\n';
fs.writeFileSync(path.join(this.outputPath, 'config', 'resolver-configuration.js'), contents, { encoding: 'utf8' });
fs.writeFileSync(path.join(this.outputPath, 'config', 'resolver-configuration.d.ts'), INTERFACE, { encoding: 'utf8' });
}
}
module.exports = ResolverConfigurationBuilder;