-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstaller.js
155 lines (135 loc) · 7.57 KB
/
installer.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
var exec = require('child_process').exec;
var fs = require('fs');
packageoriginal = require('path').join(__dirname, 'package.json');
packagesqlite3 = require('path').join(__dirname, 'package_sqlite3.json');
packagefs = require('path').join(__dirname, 'package_filesession.json');
if (process.platform == 'win32') {
//#################################################################################################
console.log('This will try to install the application for you.');
console.log('This includes installing node-windows if not already installed. Linking directories for this and running npm install to install dependencies.');
console.log('The end result is a service running with this program.');
console.log('------------------------------------------------------');
console.log('Checking to see if node-windows is installed');
exec('npm list -g node-windows', null, function(error, stdout, stderr) {
if (stdout.toString().indexOf('empty') !== -1) {
console.log('node-windows is not installed globally.');
// Install node-windows if it's not installed.
console.log('Running "npm install -g node-windows');
exec('npm install -g node-windows', null, function(error, stdout, stderr) {
console.log('------------------------------------------------------');
console.log('Running "npm link node-windows"');
exec('npm link node-windows', null, function(error, stdout, stderr) {
console.log(stdout);
console.log('Linked node-windows with current directory.');
console.log('------------------------------------------------------');
console.log('Trying to installing required modules');
console.log('Renaming package_sqlite3.json to package.json before continuing');
fs.rename(packagesqlite3, packageoriginal, function(err) {
console.log('Running "npm install"');
exec('npm install', null, function(error, stdout, stderr) {
console.log(stdout);
console.log('Installed the dependencies for the application');
console.log('------------------------------------------------------');
console.log('Creating service. This will need elevated permissions but that will be asked for when needed.');
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name: 'TellstickNode',
description: 'The TellstickNode Service.',
script: require('path').join(__dirname, 'TellstickNode.js')
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install', function() {
svc.start();
});
svc.install();
console.log('Installed the service');
console.log('The service should be started automatically. Otherwise you can start it by going into services and start the "TellstickNode" service. If there is an error, check in the daemon folder.');
console.log('Check if you can reach the application on the predefined address: http://127.0.0.1:8888');
fs.renameSync(packageoriginal, packagesqlite3);
});
});
});
});
} else {
console.log('node-windows is already installed. No need to install it twice.');
console.log('Running "npm link node-windows"');
exec('npm link node-windows', null, function(error, stdout, stderr) {
console.log(stdout);
console.log('------------------------------------------------------');
console.log('Trying to installing required modules');
console.log('Renaming package_sqlite3.json to package.json before continuing');
fs.rename(packagesqlite3, packageoriginal, function(err) {
console.log(err);
console.log('Running "npm install"');
exec('npm install', null, function(error, stdout, stderr) {
console.log(stdout);
console.log('Installed the dependencies for the application');
console.log('------------------------------------------------------');
console.log('Creating service. This will need elevated permissions but that will be asked for when needed.');
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name: 'TellstickNode',
description: 'The TellstickNode Service.',
script: require('path').join(__dirname, 'TellstickNode.js')
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install', function() {
svc.start();
});
svc.install();
console.log('Installed the service');
console.log('The service should be started automatically. Otherwise you can start it by going into services and start the "TellstickNode" service. If there is an error, check in the daemon folder.');
console.log('Check if you can reach the application on the predefined address: http://127.0.0.1:8888');
fs.renameSync(packageoriginal, packagesqlite3);
});
});
});
}
});
//#################################################################################################
} else {
console.log('This will try to install the application for you.');
console.log('This includes installing forever and forever-service and running npm install to install dependencies.');
console.log('The end result is a service running with this program.');
// if using an arm processor, stick with filesessionstorage because of issue installing sqlite3 on raspberrypi.
if (process.arch == 'arm') {
console.log('Platform is arm architecture. Lets use filesession.');
fs.renameSync(packagefs, packageoriginal);
} else {
console.log('Platform is not arm architecture. Lets use sqlite3.');
fs.renameSync(packagesqlite3, packageoriginal);
}
console.log('Starting installation of dependencies');
exec('npm install', null, function(error, stdout, stderr) {
console.log(stderr);
console.log('Starting installation of forever');
exec('sudo npm install -g forever', null, function(error, stdout, stderr) {
console.log(stderr);
console.log('Done. Forever has been installed.');
console.log('I recommend you to create a Cron job with the following: ');
console.log("PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
console.log("@reboot /usr/local/bin/forever -a -l " + __dirname + "/forever.log -o " + __dirname + "/tellsticknode_out.log -e " + __dirname + "/tellsticknode_error.log start " + __dirname + "/TellstickNode.js > " + __dirname + "/cron1.log 2>" + __dirname + "/cron2.log");
/*
console.log('Starting installation of forever-service');
exec('sudo npm install -g forever-service', null, function (error,stdout,stderr) {
console.log(stderr);
console.log('Setting up the service');
exec('sudo forever-service install tellsticknode --script TellstickNode.js',null, function (error,stdout,stderr) {
console.log(stderr);
console.log(stdout);
console.log('Done');
if(process.arch == 'arm'){
fs.renameSync(packageoriginal, packagefs);
} else {
fs.renameSync(packageoriginal, packagesqlite3);
}
});
});
*/
});
});
}