forked from chickenbug/stitch-cli-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testInstall.js
104 lines (92 loc) · 2.53 KB
/
testInstall.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
#!/usr/bin/env node
const os = require('os');
const fs = require('fs');
const path = require('path');
const spawnSync = require('child_process').spawnSync;
function directoryExists(file) {
try {
const stat = fs.lstatSync(file);
return stat.isDirectory();
} catch (err) {
return false;
}
}
function fileExists(file) {
try {
const stat = fs.lstatSync(file);
return stat.isFile();
} catch (err) {
return false;
}
}
function removeFolder(dir) {
if (!fs.existsSync(dir)) return;
fs.readdirSync(dir).forEach(file => {
const curPath = dir + path.sep + file;
if (fs.lstatSync(curPath).isDirectory()) {
removeFolder(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(dir);
}
function checkSpawn(spawnInfo) {
if (spawnInfo.stdout) {
if (typeof spawnInfo.stdout !== 'string') {
console.log(spawnInfo.stdout.toString('utf8'));
} else {
console.log(spawnInfo.stdout);
}
}
if (spawnInfo.stderr) {
if (typeof spawnInfo.error !== 'string') {
console.error(spawnInfo.stderr.toString('utf8'));
} else {
console.error(spawnInfo.stderr);
}
}
if (spawnInfo.status !== 0 || spawnInfo.error) {
console.error('Failed when spawning.');
process.exit(1);
}
if (typeof spawnInfo.stdout !== 'string') {
return spawnInfo.stdout.toString('utf8');
}
return spawnInfo.stdout;
}
function sleep(milliseconds) {
const inAFewMilliseconds = new Date(new Date().getTime() + milliseconds);
while (inAFewMilliseconds > new Date()) {
// wait for completion
}
}
const tempInstallPath = path.resolve(os.tmpdir(), 'stitch-cli-test');
if (directoryExists(tempInstallPath)) {
console.log(`Deleting directory '${tempInstallPath}'.`);
removeFolder(tempInstallPath);
}
fs.mkdirSync(tempInstallPath);
if (process.platform === 'win32') {
sleep(2000); // wait 2 seconds until everything is in place
checkSpawn(spawnSync('cmd.exe', ['/c', `npm i ${__dirname}`], { cwd: tempInstallPath }));
} else {
checkSpawn(spawnSync('npm', ['i', `${__dirname}`], { cwd: tempInstallPath }));
}
const executable = path.resolve(
tempInstallPath,
'node_modules',
'stitch-cli',
`stitch-cli${os.platform() === 'win32' ? '.exe' : ''}`
);
if (fileExists(executable)) {
console.log(`Stitch CLI installed fine.`);
} else {
console.error(`STITCH CLI did not install correctly, file '${executable}' was not found.`);
process.exit(2);
}
try {
removeFolder(tempInstallPath);
} catch (err) {
console.error(`Could not delete folder '${tempInstallPath}'.`);
}