-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.js
101 lines (89 loc) · 2.57 KB
/
config.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
import fs from 'fs';
import merge from 'deepmerge';
import { rootPath } from './paths';
/**
* specifies the default config
*/
const defaultConfig = {
npm: {
do: 'install',
fallback: 'install',
command: 'npm prune && npm install',
files: ['package.json', 'package-lock.json', 'npm-shrinkwrap.json'],
excludedFolders: ['node_modules'],
},
bower: {
do: 'install',
fallback: 'install',
command: 'bower install',
files: ['bower.json'],
excludedFolders: ['bower_components'],
},
composer: {
do: 'install',
fallback: 'install',
command: 'composer install',
files: ['composer.json', 'composer.lock'],
excludedFolders: ['vendor'],
},
userConfig: 'autoinstaller.json',
};
/**
* config for deep merging the default config with the user config
*/
const mergeConfig = {
arrayMerge: (dest, source) => source,
};
/**
* load config
*
* @desc load the npm-autoinstaller config from the package.json file and custom user configs
* @return {object}
*/
export const loadConfig = () => {
return loadUserConfig(defaultConfig, 'package.json', 'autoinstaller');
};
/**
* load user config
*
* @desc load the config from the given file and merge it with the previous one
* @param {object} currentConfig - current config
* @param {file} file - filename of the user config
* @param {string} topLevelProp - name of the property in which the config is stored inside the file (optional)
* @param {boolean} recursive - if it should recursively load user configs (optional)
* @return {object}
*/
export const loadUserConfig = (currentConfig, file, topLevelProp = null, recursive = true) => {
const fileContent = loadFile(file);
// return current config if file does not exist
if (fileContent === null) {
return currentConfig;
}
const userConfig = topLevelProp === null ? fileContent : (fileContent[topLevelProp] || {});
const mergedConfig = merge(currentConfig, userConfig, mergeConfig);
if (mergedConfig.userConfig && mergedConfig.userConfig !== file && recursive) {
return loadUserConfig(mergedConfig, mergedConfig.userConfig);
}
return mergedConfig;
};
/**
* load file
*
* @desc load and parse a json config file
* @param {string} file - path to the file in the project root
* @return {object}
*/
export const loadFile = (file) => {
const path = `${rootPath}/${file}`;
if (!fs.existsSync(path)) {
return null;
}
const content = fs.readFileSync(path);
// parse the file
try {
return JSON.parse(content);
} catch (e) {
return null;
}
};
export const config = loadConfig();