/
config.ts
82 lines (79 loc) · 3.38 KB
/
config.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
import {readFileSync} from 'fs';
import {jsonc} from 'jsonc';
import {join} from 'path';
import {findAngularJsonPath} from './findAngularJsonPath';
import {ScullyConfig} from './interfacesandenums';
import {logError, logWarn, yellow} from './log';
import {validateConfig} from './validateConfig';
import {compileConfig} from './compileConfig';
export const angularRoot = findAngularJsonPath();
export const scullyConfig: ScullyConfig = {} as ScullyConfig;
const loadIt = async () => {
// const userConfigFileLocation = join(angularRoot, 'scully.json');
// const userConfigRaw = readFileSync(userConfigFileLocation).toString() || '';
const compiledConfig = await compileConfig();
// let unCheckedUserConfig = {};
let angularConfig = {} as any;
let distFolder = join(angularRoot, './dist/browser');
// console.log({compiledConfig});
// try {
// unCheckedUserConfig = jsonc.parse(userConfigRaw);
// } catch {
// logError(`Scully config file could not be parsed!`);
// process.exit(0);
// }
// const userConfig = await validateConfig((unCheckedUserConfig as unknown) as ScullyConfig);
try {
angularConfig = jsonc.parse(readFileSync(join(angularRoot, 'angular.json')).toString());
// TODO: make scully handle other projects as just the default one.
const defaultProject = angularConfig.defaultProject;
distFolder = angularConfig.projects[defaultProject].architect.build.options.outputPath;
if (distFolder.endsWith('dist') && !distFolder.includes('/')) {
logError(
`Your distribution files are in "${yellow(distFolder)}". Please change that to include a subfolder`
);
process.exit(15);
}
} catch (e) {
logError(`Angular config file could not be parsed!`, e);
process.exit(15);
}
if (compiledConfig.hostUrl && compiledConfig.hostUrl.endsWith('/')) {
compiledConfig.hostUrl = compiledConfig.hostUrl.substr(0, compiledConfig.hostUrl.length - 1);
} else if (compiledConfig.hostUrl === undefined) {
compiledConfig.hostUrl = null;
}
// TODO: update types in interfacesandenums to force correct types in here.
// tslint:disable-next-line: no-unused-expression
Object.assign(
scullyConfig,
{
homeFolder: angularRoot,
outDir: join(angularRoot, './dist/static/'),
distFolder,
appPort: /** 1864 */ 'herodevs'.split('').reduce((sum, token) => (sum += token.charCodeAt(0)), 1000),
staticport: /** 1771 */ 'scully'.split('').reduce((sum, token) => (sum += token.charCodeAt(0)), 1000),
defaultPostRenderers: [],
// tslint:disable-next-line:no-bitwise
hostUrl: compiledConfig.hostUrl,
}
// compiledConfig
) as ScullyConfig;
await updateScullyConfig(compiledConfig);
return scullyConfig;
};
export const loadConfig = loadIt();
export const updateScullyConfig = async (config: Partial<ScullyConfig>) => {
/** note, an invalid config will abort the entire program. */
const newConfig = Object.assign({}, scullyConfig, config);
if (config.outDir === undefined) {
logWarn(`The option outDir isn't configured, using default folder "${yellow(scullyConfig.outDir)}".`);
} else {
config.outDir = join(angularRoot, config.outDir);
}
const validatedConfig = await validateConfig(newConfig as ScullyConfig);
if (validatedConfig) {
const mergedRoutes = {...scullyConfig.routes, ...validatedConfig.routes};
Object.assign(scullyConfig, config, {routes: mergedRoutes});
}
};