This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostinstall.js
256 lines (232 loc) · 7.19 KB
/
postinstall.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* eslint-disable no-console */
let fs = require('fs');
let path = require('path');
let exec = require('child_process').execSync;
let postInstall = {
data: {
alreadyBeenRan: false,
anErrorOccured: false,
// __dirname is the path to the folder that contains postinstall.js
originalDir: __dirname + '',
clonedLocation: path.join(__dirname, 'src', 'vue-devtools'),
builtExtension: path.join(__dirname, 'src', 'vue-devtools', 'shells', 'chrome'),
buildConfig: path.join(__dirname, 'src', 'vue-devtools', 'shells', 'createConfig.js'),
destination: path.join(__dirname, 'extension'),
flagFile: path.join(__dirname, 'extension', 'flag.txt')
},
/**
* Attempts to synchronously run an executable in a child process.
* Catches errors and logs them.
* @param {string} args The command to be ran in the command prompt/terminal.
*/
runner: function (args) {
try {
let runner = exec(args);
let output = runner.toString().trim();
if (output) {
// console.log('Vue-DevTools:', output);
}
} catch (err) {
this.data.anErrorOccured = true;
console.log('Vue-DevTools:', err);
if (err.stderr) {
console.log('STDERR: ' + err.stderr.toString());
}
if (err.stdout) {
console.log('STDOUT: ' + err.stdout.toString());
}
}
},
/**
* Checks if the flag file exists, signalling that everything else ran successfully.
*/
checkIfPreviouslySuccessful: function () {
if (fs.existsSync(this.data.flagFile)) {
this.data.alreadyBeenRan = true;
}
},
cleanClonedLocation: function () {
let src = path.join(this.data.originalDir, 'src');
if (process.platform === 'win32') {
this.runner('rd /s /q ' + src);
} else {
this.runner('rm -r -f ' + src);
}
},
/**
* Attempt to clone Vue-DevTools with HTTPS, if that fails,
* try cloning with SSH, if that fails, log error.
*/
gitClone: function () {
if (fs.existsSync(this.data.builtExtension)) {
return;
}
if (fs.existsSync(this.data.clonedLocation)) {
this.cleanClonedLocation();
}
if (this.data.anErrorOccured) {
return;
}
console.log('Vue-DevTools: Downloading latest Vue-DevTools source code');
let executable = 'git clone --quiet';
let url = 'https://github.com/vuejs/vue-devtools.git';
let branch = '-b master';
let clonedLocation;
if (process.platform === 'win32') {
clonedLocation = '"' + this.data.clonedLocation + '"';
} else {
// Regex replaces spaces with a escaped spaces
// 'path with space' => 'path\ with\ \ space'
clonedLocation = this.data.clonedLocation.replace(/[ ]/g, '\\ ');
}
let args = [
executable,
url,
branch,
clonedLocation
].join(' ').trim();
try {
// httpsRunner
exec(args);
} catch (err) {
url = '[email protected]:vuejs/vue-devtools.git';
args = [
executable,
url,
branch,
clonedLocation
].join(' ').trim();
try {
// sshRunner
exec(args);
} catch (error) {
this.data.anErrorOccured = true;
console.log('Vue-DevTools: Error downloading Vue-DevTools.');
console.log(error);
}
}
},
npmInstall: function () {
if (this.data.anErrorOccured) {
return;
}
console.log('Vue-DevTools: Installing Vue-DevTools dependencies');
process.chdir(this.data.clonedLocation);
this.runner('npm install');
process.chdir(this.data.originalDir);
},
modifyWebpackConfig: function () {
if (this.data.anErrorOccured) {
return;
}
console.log('Vue-DevTools: Modifying Webpack Configuration');
let config = '';
try {
config = fs.readFileSync(this.data.buildConfig);
} catch (err) {
console.log('Vue-DevTools: Error reading Vue-Devtools config file.');
this.data.anErrorOccured = true;
return;
}
config = String(config);
// Convert CRLF to LF
config = config.split('\r\n').join('\n');
config = config.split('\n');
config = config.filter(function (line) {
return !line.includes('exclude: /node_modules');
});
config = config.join('\n');
try {
fs.writeFileSync(this.data.buildConfig, config);
} catch (err) {
console.log('Vue-DevTools: Error writing Vue-Devtools config file.');
this.data.anErrorOccured = true;
return;
}
},
npmRunBuild: function () {
if (this.data.anErrorOccured) {
return;
}
console.log('Vue-DevTools: Building Vue-DevTools');
process.chdir(this.data.builtExtension);
if (process.platform === 'win32') {
this.runner('..\\..\\node_modules\\.bin\\webpack.cmd --hide-modules');
} else {
this.runner('node ../../node_modules/.bin/webpack --hide-modules');
}
process.chdir(this.data.originalDir);
},
relocateDevTools: function () {
if (this.data.anErrorOccured) {
return;
}
console.log('Vue-DevTools: Installing Vue-DevTools in NW.js');
try {
fs.renameSync(this.data.builtExtension, this.data.destination);
} catch (err) {
this.data.anErrorOccured = true;
console.log('Vue-DevTools:', err);
}
},
/**
* Fix manifest.json permissions to allow proxy injection in nw.js > 0.31.5
* @see https://github.com/nwjs/nw.js/issues/6744#issuecomment-410476111
*/
fixPermissions: function () {
if (this.data.anErrorOccured) {
return;
}
console.log('Vue-DevTools: Fixing Vue-DevTools permissions');
const manifestFilePath = path.resolve(this.data.destination, 'manifest.json');
const manifest = require(manifestFilePath);
manifest.permissions.push('*://*/*');
try {
fs.writeFileSync(manifestFilePath, JSON.stringify(manifest, null, 2));
} catch (error) {
this.data.anErrorOccured = true;
console.log('Vue-DevTools: Error saving manifest.json file');
console.log('Vue-DevTools:', error);
}
},
/**
* Create a text file in the extension directory to
* signify the operation completed successfully.
*/
setSuccessFlag: function () {
if (this.data.anErrorOccured) {
return;
}
try {
fs.writeFileSync(this.data.flagFile, 'Success.');
} catch (err) {
this.data.anErrorOccured = true;
console.log('Vue-DevTools: Error creating flag file');
console.log('Vue-DevTools:', err);
}
},
runEverything: function () {
this.checkIfPreviouslySuccessful();
if (this.data.alreadyBeenRan) {
return;
}
this.gitClone();
this.npmInstall();
this.modifyWebpackConfig();
this.npmRunBuild();
this.relocateDevTools();
this.fixPermissions();
this.setSuccessFlag();
if (this.data.anErrorOccured) {
console.log('Vue-DevTools: Finished with errors.');
} else {
console.log('Vue-DevTools: Success.');
}
console.log(' ----------------------------------------- ');
console.log('| DEPRECATION NOTICE |');
console.log('| nw-vue-devtools is no longer maintained |');
console.log('| switch to nw-vue-devtools-prebuilt |');
console.log(' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ');
}
};
postInstall.runEverything();