-
Notifications
You must be signed in to change notification settings - Fork 4
/
snapinit.js
executable file
·106 lines (85 loc) · 3.55 KB
/
snapinit.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
#!/usr/bin/env node
var os = require('os');
var exec = require('child_process').exec;
var debug = (process.execArgv.find(function (e) { return e.startsWith('--debug'); }) !== undefined);
// Load settings
var SettingsHelper = require("./lib/SettingsHelper.js");
var settingsHelper = new SettingsHelper();
var packagePath = settingsHelper.nodePackagePath;
console.log('init process.env.NODE_PATH : ' + process.env.NODE_PATH);
if(!process.env.NODE_PATH){
process.env.NODE_PATH = packagePath;
}
else{
if( !process.env.NODE_PATH.split(':').find(function(p){return p = packagePath;})){
process.env.NODE_PATH = packagePath;
}
}
process.env.NODE_PATH = process.env.NODE_PATH + ":" + packagePath;
process.env.HOME = os.userInfo().homedir;
console.log('done process.env.NODE_PATH : ' + process.env.NODE_PATH);
require('app-module-path').addPath(packagePath);
require('module').globalPaths.push(packagePath);
var SnapInitHandler = new SnapInitHandler(settingsHelper);
SnapInitHandler.start(settingsHelper.isFirstStart());
function SnapInitHandler(settingsHelper) {
console.log("STARTSNAP: SnapInitHandler started");
var self = this;
this.interval = null;
this.start = function (isFirstStart) {
const argv = require('minimist')(process.argv.slice(2));
const cOrNSupplied = (argv.c || argv.n)
if(!isFirstStart) {
console.log('Stored data/settings for node exist.');
// let's skip the confirmation dialog if the process argument '-y' is supplied.
if(!('y' in argv)) {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
if(!cOrNSupplied) {
console.warn("No '-c' (code) or '-n' (node name) argument specified. Only removing node data & settings.")
}
rl.question('Are you sure you want to remove all previous data and settings for mSB node? [yes/no] ', (answer) => {
if(!(answer.toLowerCase() == 'y' || answer.toLowerCase() == 'yes')) {
console.log("Exiting without resetting node data.");
rl.close();
process.exit(1);
}
else {
// Reset all node's settings & data.
resetNodeData();
// Only start node if '-c' or '-'n' was supplied.
if(cOrNSupplied) {
startNode();
}
}
});
}
else {
// '-y' argument supplied. Let's clean up all data and start node.
resetNodeData();
startNode();
}
}
else {
console.log('No stored data/settings for node exist. Nothing to clean.');
if(cOrNSupplied) {
startNode();
}
}
}
function resetNodeData() {
// backup process arguments and let's apply them after resetting node.
const mainArgv = process.argv;
// let's simulate the 'restore.js -all' command.
process.argv = [ process.argv[0], process.argv[1], '-all'];
require('./restore.js');
// restore argv's send to the snap.
process.argv = mainArgv;
}
function startNode() {
require('./start.js');
}
}