-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
multi_config_path.ts
76 lines (60 loc) · 2.15 KB
/
multi_config_path.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
import { isAbsolute, resolve, join, extname } from 'path';
import { existsSync, readFileSync, writeFileSync } from 'hexo-fs';
import yml from 'js-yaml';
import { deepMerge } from 'hexo-util';
import type Hexo from './index';
export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths: string, outputDir: string) {
const { log } = ctx;
const defaultPath = join(base, '_config.yml');
if (!configPaths) {
log.w('No config file entered.');
return join(base, '_config.yml');
}
let paths: string[];
// determine if comma or space separated
if (configPaths.includes(',')) {
paths = configPaths.replace(' ', '').split(',');
} else {
// only one config
let configPath = isAbsolute(configPaths) ? configPaths : resolve(base, configPaths);
if (!existsSync(configPath)) {
log.w(`Config file ${configPaths} not found, using default.`);
configPath = defaultPath;
}
return configPath;
}
const numPaths = paths.length;
// combine files
let combinedConfig = {};
let count = 0;
for (let i = 0; i < numPaths; i++) {
const configPath = isAbsolute(paths[i]) ? paths[i] : join(base, paths[i]);
if (!existsSync(configPath)) {
log.w(`Config file ${paths[i]} not found.`);
continue;
}
// files read synchronously to ensure proper overwrite order
const file = readFileSync(configPath);
const ext = extname(paths[i]).toLowerCase();
if (ext === '.yml') {
combinedConfig = deepMerge(combinedConfig, yml.load(file));
count++;
} else if (ext === '.json') {
combinedConfig = deepMerge(combinedConfig, yml.load(file, {json: true}));
count++;
} else {
log.w(`Config file ${paths[i]} not supported type.`);
}
}
if (count === 0) {
log.e('No config files found. Using _config.yml.');
return defaultPath;
}
log.i('Config based on', count.toString(), 'files');
const multiconfigRoot = outputDir || base;
const outputPath = join(multiconfigRoot, '_multiconfig.yml');
log.d(`Writing _multiconfig.yml to ${outputPath}`);
writeFileSync(outputPath, yml.dump(combinedConfig));
// write file and return path
return outputPath;
};