-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
186 lines (151 loc) · 5.16 KB
/
init.js
File metadata and controls
186 lines (151 loc) · 5.16 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
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
const fs = require("fs-extra");
const readlineSync = require("readline-sync");
const constants = require("./constants");
const {getGlobalServerDefinitions, hostFromServer} = require("./utils.js");
function serverToStrLong(server) {
return hostFromServer(server) + (server.hostname ? ` (${server.hostname})` : "");
}
function getServerKey(server) {
if (server.hostname) {
return server.hostname;
} else {
return hostFromServer(server);
}
}
function getAllPossibleServerKeys(servers) {
let keys = new Set();
for (let server of servers) {
for (let keyName of constants.VALID_SERVER_KEYS) {
server[keyName] && keys.add(server[keyName]);
}
}
return keys;
}
module.exports = function() {
const deployConfigPath = constants.LOCAL_CONFIG_FILE;
console.log("This will create a simple configuration for deploying a single app to a single server.");
if (fs.existsSync(deployConfigPath)) {
console.log(`*** Warning: The deploy config file ${deployConfigPath} already exists, will not continue! ***`);
return;
}
const servers = getGlobalServerDefinitions();
const serverNames = servers.map(server => serverToStrLong(server));
const allPossibleServerKeys = getAllPossibleServerKeys(servers);
if (servers.length == 0) {
console.log("Error: Please add at least one remote server using <appcontrol addserver> first!");
return;
}
let deployServerKey = getServerKey(servers[0]);
let masterServerKey = getServerKey(servers[0]);
if (servers.length > 1) {
let serverIndex;
console.log("\nAvailable servers:", serverNames);
deployServerKey = readlineSync.question(
"\nPlease enter a server to host your app (hostname or IP address):\n> ",
{limit : input => input}
);
if (!allPossibleServerKeys.has(deployServerKey)) {
console.log(`\n*** Notice: server ${deployServerKey} not found in server definitions. Please add `
+ "it before you deploy! ***");
}
masterServerKey = readlineSync.question("\nPlease enter a master server (hostname or IP address, default:"
+ " same as the app server):\n> ");
if (masterServerKey) {
if (!allPossibleServerKeys.has(masterServerKey)) {
console.log(`\n*** Notice: server ${masterServerKey} not found in server definitions. `
+ "Please add it before you deploy! ***");
}
} else {
masterServerKey = deployServerKey;
}
}
const subdirs = fs.readdirSync("./").filter(name => name != constants.LOCAL_DATA_DIR);
console.log("\nAvailable sub directories:", subdirs);
let appName = readlineSync.question(
"\nEnter the name of the sub directory which will contain your app to be deployed:\n> ",
{limit : input => input}
);
let mainFile = null;
let nodeVersion = null;
if (!readlineSync.keyInYNStrict("\nIs your app a client side web app? (Not a NodeJS server app)\n> ")) {
mainFile = readlineSync.question(
"\nYour app is a server side NodeJS app. What is the app entry point or main file? (default: server.js)\n> ",
{defaultInput : "server.js"}
);
nodeVersion = readlineSync.questionInt(
"\nWhat Node version are you targeting? (e.g. 20, default: always the latest version)\n> ",
{defaultInput : 0}
);
if (nodeVersion == 0) {
nodeVersion = null;
}
}
let buildCmd = null;
let buildDir = null;
if (readlineSync.keyInYNStrict("\nDoes your app have a build command or script?\n> ")) {
buildCmd = readlineSync.question(
'\nPlease enter the build command (default: "npm build"):\n> ',
{defaultInput : "npm build"}
);
buildDir = readlineSync.question(
'\nPlease enter the build directory (default: "build"):\n> ',
{defaultInput : "build"}
);
}
let domain = null;
let webPath = null;
if (readlineSync.keyInYNStrict("\nDoes your app have a domain name?\n> ")) {
domain = readlineSync.question(
'\nPlease enter the domain name (e.g. "my.example.com"):\n> ',
{defaultInput : null}
);
webPath = readlineSync.question(
'\nWhat web path will your app be at? (e.g. "/" or "/my-app", default is "/"):\n> ',
{defaultInput : "/"}
);
}
// Create the config files
let deployConfig = {
"deployments" : {
"production" : {
"masterServer" : masterServerKey,
"servers" : {
}
}
}
};
deployConfig.deployments.production.servers[deployServerKey] = {
"apps" : [
{
"app" : appName,
"domain" : domain || undefined,
"webPath" : (domain && webPath) ? webPath : undefined
}
]
};
let appConfig = {
runtime : mainFile ? (nodeVersion ? `node:${nodeVersion}` : "node") : undefined,
main : mainFile || undefined,
buildCmd : buildCmd || undefined,
buildDir : buildDir || undefined
};
// create appcontrol.json with a basic deploy config
fs.writeJsonSync(deployConfigPath, deployConfig, {spaces : "\t"});
// create an app.json within the app dir
const appConfigPath = `${appName}/${constants.APP_CONFIG_FILE}`;
fs.ensureDirSync(appName); // ensure dir
fs.writeJsonSync(appConfigPath, appConfig, {spaces : "\t"});
console.log("\nAnswer summary:", {
deployServerKey,
masterServerKey,
appName,
mainFile,
buildCmd,
buildDir,
domain,
webPath
});
console.log("\nCreated deploy config", deployConfigPath);
console.log("Created app config", appConfigPath);
console.log("\nALL DONE!");
}