forked from rethinkdb/horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
192 lines (166 loc) · 5.86 KB
/
init.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
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
187
188
189
190
191
192
'use strict';
const fs = require('fs');
const crypto = require('crypto');
const path = require('path');
const makeIndexHTML = (projectName) => `\
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script src="/horizon/horizon.js"></script>
<script>
var horizon = Horizon();
horizon.onConnected(function() {
document.querySelector('h1').innerHTML = '${projectName} works!'
});
horizon.connect();
</script>
</head>
<body>
<marquee><h1></h1></marquee>
</body>
</html>
`;
const makeDefaultConfig = (projectName) => `\
# This is a TOML file
###############################################################################
# IP options
# 'bind' controls which local interfaces will be listened on
# 'port' controls which port will be listened on
#------------------------------------------------------------------------------
# bind = [ "localhost" ]
# port = 8181
###############################################################################
# HTTPS Options
# 'insecure' will disable HTTPS and use HTTP instead
# 'key_file' and 'cert_file' are required for serving HTTPS
#------------------------------------------------------------------------------
# insecure = true
# key_file = "key.pem"
# cert_file = "cert.pem"
###############################################################################
# App Options
# 'project' will change to the given directory
# 'serve_static' will serve files from the given directory over HTTP/HTTPS
#------------------------------------------------------------------------------
project = "${projectName}"
# serve_static = "dist"
###############################################################################
# Data Options
# WARNING: these should probably not be enabled on a publically accessible
# service. Tables and indexes are not lightweight objects, and allowing them
# to be created like this could open the service up to denial-of-service
# attacks.
# 'auto_create_table' creates a table when one is needed but does not exist
# 'auto_create_index' creates an index when one is needed but does not exist
#------------------------------------------------------------------------------
# auto_create_table = true
# auto_create_index = true
###############################################################################
# RethinkDB Options
# These options are mutually exclusive
# 'connect' will connect to an existing RethinkDB instance
# 'start_rethinkdb' will run an internal RethinkDB instance
#------------------------------------------------------------------------------
# connect = "localhost:28015"
# start_rethinkdb = false
###############################################################################
# Debug Options
# 'debug' enables debug log statements
#------------------------------------------------------------------------------
# debug = true
###############################################################################
# Authentication Options
# Each auth subsection will add an endpoint for authenticating through the
# specified provider.
# 'token_secret' is the key used to sign jwts
# 'allow_anonymous' issues new accounts to users without an auth provider
# 'allow_unauthenticated' allows connections that are not tied to a user id
# 'auth_redirect' specifies where users will be redirected to after login
#------------------------------------------------------------------------------
token_secret = "${crypto.randomBytes(64).toString('base64')}"
# allow_anonymous = true
# allow_unauthenticated = true
# auth_redirect = "/"
#
# [auth.facebook]
# id = "000000000000000"
# secret = "00000000000000000000000000000000"
#
# [auth.google]
# id = "00000000000-00000000000000000000000000000000.apps.googleusercontent.com"
# secret = "000000000000000000000000"
#
# [auth.twitter]
# id = "0000000000000000000000000"
# secret = "00000000000000000000000000000000000000000000000000"
#
# [auth.github]
# id = "00000000000000000000"
# secret = "0000000000000000000000000000000000000000"
#
# [auth.twitch]
# id = "0000000000000000000000000000000"
# secret = "0000000000000000000000000000000"
`;
const addArguments = (parser) => {
parser.addArgument([ 'projectName' ],
{ action: 'store',
help: 'Name of directory to create. Defaults to current directory',
nargs: '?',
}
);
};
const fileExists = (pathName) => {
try {
fs.statSync(pathName);
return true;
} catch (e) {
return false;
}
};
const processConfig = (parsed) => parsed;
const runCommand = (parsed) => {
const runInSubdir = parsed.projectName != null;
const subdirExists = fileExists(parsed.projectName);
const projectDirName = parsed.projectName ?
path.join(process.cwd(), parsed.projectName) :
process.cwd();
const projectName = parsed.projectName || path.basename(process.cwd());
if (runInSubdir && !subdirExists) {
fs.mkdirSync(projectName);
console.info(`Created new project directory ${parsed.projectName}`);
} else {
console.info('Creating new project in current directory');
}
if (runInSubdir) {
process.chdir(projectDirName);
}
// Before we create things, check if the directory is empty
const dirWasPopulated = fs.readdirSync(process.cwd()).length !== 0;
if (!dirWasPopulated && !fileExists('src')) {
fs.mkdirSync('src');
}
if (!dirWasPopulated && !fileExists('dist')) {
fs.mkdirSync('dist');
fs.appendFileSync('./dist/index.html', makeIndexHTML(projectName));
}
if (!fileExists('.hz')) {
fs.mkdirSync('.hz');
console.info('Created .hz directory');
}
if (!fileExists('.hz/config.toml')) {
fs.appendFileSync('.hz/config.toml', makeDefaultConfig(projectName), {
encoding: 'utf8',
mode: 0o600, // Secrets are put in this config, so set it user
// read/write only
});
} else {
console.info('.hz/config.toml already exists, not touching it.');
}
};
module.exports = {
addArguments,
runCommand,
processConfig,
};