-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
134 lines (121 loc) · 3.49 KB
/
setup.js
File metadata and controls
134 lines (121 loc) · 3.49 KB
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
#!/usr/bin/env node
const shell = require('shelljs');
const exec = require('child_process').exec;
const path = require('path');
const fs = require('fs');
const animateProgress = require('./helpers/progress');
const addCheckMark = require('./helpers/checkmark');
const readline = require('readline');
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdout.write('\n');
let interval;
let clearRepo = true;
cleanRepo(() => {
process.stdout.write('\nInstalling dependencies... (This might take a while)');
setTimeout(() => {
readline.cursorTo(process.stdout, 0);
interval = animateProgress('Installing dependencies');
}, 500);
installDeps();
});
/**
* Deletes the .git folder in dir only if cloned from our repo
*/
function cleanRepo(callback) {
fs.readFile('.git/config', 'utf8', (err, data) => {
if (!err) {
const isClonedRepo =
typeof data === 'string' &&
(data.match(/url\s*=/g) || []).length === 1 &&
/react-boilerplate\/react-boilerplate\.git/.test(data);
if (isClonedRepo) {
process.stdout.write('\nDo you want to clear old repository? [Y/n] ');
process.stdin.resume();
process.stdin.on('data', data => {
const val = data.toString().trim();
if (val === 'y' || val === 'Y' || val === '') {
process.stdout.write('Removing old repository');
shell.rm('-rf', '.git/');
addCheckMark(callback);
} else {
dontClearRepo('', callback);
}
});
} else {
dontClearRepo('\n', callback);
}
} else {
callback();
}
});
}
/**
* Function which indicates that we are not cleaning git repo
*/
function dontClearRepo(nl, callback) {
clearRepo = false;
process.stdout.write(`${nl}Leaving your repository untouched`);
addCheckMark(callback);
}
/**
* Initializes git again
*/
function initGit(callback) {
exec('git init && git add . && git commit -m "Initial commit"', addCheckMark.bind(null, callback));
}
/**
* Deletes a file in the current directory
*/
function deleteFileInCurrentDir(file, callback) {
fs.unlink(path.join(__dirname, file), callback);
}
/**
* Installs dependencies
*/
function installDeps() {
exec('node --version', (err, stdout, stderr) => {
const nodeVersion = stdout && parseFloat(stdout.substring(1));
if (nodeVersion < 5 || err) {
installDepsCallback(err || 'Unsupported node.js version, make sure you have the latest version installed.');
} else {
exec('yarn --version', (err, stdout, stderr) => {
if (parseFloat(stdout) < 0.15 || err || process.env.USE_YARN === 'false') {
exec('npm install', addCheckMark.bind(null, installDepsCallback));
} else {
exec('yarn install', addCheckMark.bind(null, installDepsCallback));
}
});
}
});
}
/**
* Callback function after installing dependencies
*/
function installDepsCallback(error) {
clearInterval(interval);
process.stdout.write('\n\n');
if (error) {
process.stderr.write(error);
process.stdout.write('\n');
process.exit(1);
}
deleteFileInCurrentDir('setup.js', () => {
if (clearRepo) {
interval = animateProgress('Initialising new repository');
process.stdout.write('Initialising new repository');
initGit(() => {
clearInterval(interval);
endProcess();
});
}
endProcess();
});
}
/**
* Function which ends setup process
*/
function endProcess() {
process.stdout.write('\nDone!');
process.exit(0);
}