-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.js
201 lines (182 loc) · 6.35 KB
/
setup.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* eslint-disable */
const childProcess = require("child_process");
/**
* Starts the same process with same arguments and exits the current process
*
* @returns {void}
*/
function restartProcess() {
childProcess.execSync(`node ${__filename} ${process.argv.slice(2).join(" ")}`, {
stdio: 'inherit'
});
process.exit();
}
try {
// Check if minimum dependency is installed and if not make a npm install
require.resolve("check-dependencies");
} catch (error) {
if (error.code === "MODULE_NOT_FOUND") {
console.warn("First execution? installing dependencies!");
childProcess.execSync('npm install', { stdio: 'inherit' });
restartProcess();
}
}
// Check if all dependencies are installed and if not make a npm install && npm prune
const checkDependencies = require("check-dependencies");
const checkResult = checkDependencies.sync();
if (!(checkResult instanceof Promise) && checkResult.error.length) {
console.log(checkResult.error);
console.warn("Not all dependencies are installed! Installing missing dependencies");
childProcess.execSync('npm install && npm prune', { stdio: 'inherit' });
restartProcess();
}
const fs = require('graceful-fs');
const arp = require('app-root-path');
const path = require('path');
const colors = require('ansicolor');
const commandLineArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage');
let VERBOSE = false;
let settings = {
junctions: {
ignoredPaths: [path.resolve(arp.path, 'out/app/client/css'), path.resolve(arp.path, 'out/app/client/js')]
}
};
/**
* Iterates recursively through a folder tree
*
* @param {string} dir The folder which should be walked recursively
* @param {string[]} [dirList] Already existing folder list
* @returns {string[]} Listing of all folders
*/
function walkDir(dir, dirList) {
let dirs = fs.readdirSync(dir);
dirList = dirList || [];
for (const subDir of dirs) {
let newDir = dir + `${path.sep}${subDir}`;
let isDir = false;
// Check if is directory. This fails on windows, when the subsystem has created a symlink
try {
isDir = fs.statSync(newDir).isDirectory() && fs.realpathSync(newDir) === newDir;
} catch (error) {
if (VERBOSE) console.log(`${colors.yellow('WARNING')}: ${colors.cyan(newDir)} marked as ${colors.red('UNKNOWN')}!`);
}
if (isDir) {
dirList.push(newDir);
walkDir(newDir, dirList);
}
}
return dirList;
}
/**
* Creates junctions from all folders in out to source, which are not contained
* in source at the same place except it is contained in "ignoredPaths".
*
* @returns {void}
*/
function createJunctions() {
console.log(colors.bright.green("create junctions"));
let outPath = path.join(arp.path, 'out');
let sourcePath = path.join(arp.path, 'source');
let outPaths = walkDir(outPath);
let sourcePaths = walkDir(sourcePath);
let lastJunction = null;
for (const item of outPaths) {
let target = item.replace(outPath, sourcePath);
if (!sourcePaths.includes(item.replace(outPath, sourcePath))) {
if (!item.includes(lastJunction) && !settings.junctions.ignoredPaths.includes(item)) {
lastJunction = item;
if (VERBOSE) console.log(colors.cyan.bright('create junction:'), item, colors.cyan('<=>'), target);
fs.symlink(item, target, 'junction', (error) => {
if (error && error.code === 'EEXIST') {
fs.unlinkSync(target);
fs.symlinkSync(item, target, 'junction');
}
});
}
}
}
}
/**
* Installs all dependencies, creates junctions and runs a basic build
*
* @returns {void}
*/
function install() {
console.log(colors.bright.green("installing project"));
childProcess.execSync('npm install && npm prune', {
stdio: VERBOSE ? 'inherit' : "ignore"
});
createJunctions();
console.log(colors.bright.green("building project"));
childProcess.execSync('npm run build', {
stdio: VERBOSE ? 'inherit' : "ignore"
});
}
if (require && require.main === module) {
let optionList = [{
name: 'help',
alias: 'h',
type: Boolean,
defaultValue: false,
description: 'This message'
},
{
name: 'verbose',
alias: 'v',
type: Boolean,
defaultValue: false,
description: 'Offers more output'
},
{
name: 'install',
alias: 'i',
type: Boolean,
defaultValue: false,
description: 'installs als dependencies, proceeds a base build for development and creates junctions of folders from the out folder which are not contained in source'
},
{
name: 'junctions',
alias: 'j',
type: Boolean,
defaultValue: false,
description: 'creates junctions of folders from the out folder which are not contained in source'
}
];
let sections = [{
header: 'The post install script',
content: 'Creates a developer environment and sets up basic attributes.'
},
{
header: 'Options',
optionList: optionList
}
];
let options = commandLineArgs(optionList);
VERBOSE = options.verbose;
const providedArguments = [];
for (const option in options) {
if (option in options) {
const element = options[option];
if (element) providedArguments.push(option);
}
}
if (providedArguments.length) {
if (providedArguments.length === 1 && providedArguments.includes("verbose")) options.help = true;
if (providedArguments.length > 1 && providedArguments.includes("help")) options.help = false;
} else options.help = true;
if (options.help) console.log(commandLineUsage(sections));
if (options.install) install();
if (options.junctions) createJunctions();
if (!options.help) {
if (options.junctions || options.install) {
console.log(
`\n${colors.magenta.bright(
'NOTE'
)}: Execute junction creation on original OS to see junctions in the editors tree.`
);
console.log(' Type node setup.js --help for more information!');
}
console.log(colors.green('\nFINISHED!'));
}
}