/
folderStructure.js
60 lines (59 loc) · 2.33 KB
/
folderStructure.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
'use strict';
const path = require('path');
const fs = require('fs-extra');
const __STRUCTURE_DIR = path.resolve(__dirname, '../../__STRUCTURE');
const clusterModule = require('cluster');
/**
* this will setup a periodic application folder structure if one doesnt exist, it will not overwrite existing configs
*
* @returns Promise setUpFolderStructure will copy folders from the application __STRUCTURE directory to initialize the application
*/
function setUpFolderStructure() {
const cluster = this.cluster || clusterModule;
return new Promise((resolve, reject) => {
try {
if (process.env.npm_config_skip_cli_reconfig || this.config.skip_reconfig) {
return resolve(true);
} else if (cluster.isMaster===false) {
return resolve(true);
} else {
fs.copy(__STRUCTURE_DIR, this.config.app_root, {
overwrite: false,
clobber: false,
preserveTimestamps: true,
})
.then(() => { //check to make sure there arent two config files (both a json and a js file, if there are both, use the json file)
return Promise.all([
fs.copy(path.join(__STRUCTURE_DIR, 'public'), path.join(this.config.app_root, 'public'), {
overwrite: true,
clobber: true,
preserveTimestamps: true,
filter: (src, dest) => {
return src.indexOf('favicon') === -1;
},
}),
fs.copy(path.join(__STRUCTURE_DIR, 'app'), path.join(this.config.app_root, 'app'), {
overwrite: true,
clobber: true,
preserveTimestamps: true,
}),
]);
})
.then(() => { //check to make sure there arent two config files (both a json and a js file, if there are both, use the json file)
return fs.readdir(path.join(this.config.app_root, 'content/config'));
})
.then(configFiles => {
if (configFiles.indexOf('config.json') !== -1 && configFiles.indexOf('config.js') !== -1) {
resolve(fs.remove(path.join(this.config.app_root, 'content/config/config.js')));
} else {
resolve(true);
}
})
.catch(reject);
}
} catch (e) {
reject(e);
}
});
}
module.exports = setUpFolderStructure;