/
skeletons.js
202 lines (180 loc) · 3.86 KB
/
skeletons.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
193
194
195
196
197
198
199
200
201
202
let fs = require("fs-extra")
let inquirer = require("inquirer")
exports.files = {
".ssh": {
authorized_keys (_snoot, {authorizedKeys}) {
return authorizedKeys
}
},
".gitconfig" (snoot) {
return `[user]
name = ${snoot}
email ${snoot}@snoot.club`
},
application: {
boops: {
".gitkeep": () => "a polite request to git to keep this empty directory"
},
".gitignore" () {
return `node_modules/
.cache/
`
},
"package.json" (snoot) {
return `{
"name": "${snoot}-application",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"install": "boop",
"watch": "boop",
"build": "boop",
"start": "micro -l unix:sock"
},
"author": "${snoot} <${snoot}@snoot.club>",
"license": "GPL-3.0+",
"description": "${snoot} application on snoot.club",
"dependencies": {
"@snootclub/boop": "^0.0.14",
"micro": "^9.3.3"
}
}
`
},
"index.js" () {
return `let boop = require("@snootclub/boop")
module.exports = (request, response) =>
boop(request, response)
`
},
website: {
"index.html" (snoot) {
return `<!doctype html>
<meta charset="utf-8">
<title>${snoot}'s a snoot</title>
<style>
* {
scrollbar-color: #cc3669 #ffe9ed;
}
::selection {
background: black;
color: white;
}
body {
background: #ffe9ed;
color: #c36;
font-size: 1.6em;
line-height: 1.2;
width: 80vw;
margin: auto;
}
hr {
height: 30px;
border-style: solid;
border-color: #c36;
border-width: 0;
border-top-width: 2px;
border-radius: 10px;
position: relative;
}
hr:before {
display: block;
content: "";
height: 100%;
transform: translateY(-100%);
border-style: solid;
border-color: #c36;
border-width: 0;
border-bottom-width: 2px;
border-radius: inherit;
}
code {
color: #4abe;
}
</style>
<h1>
Welcome to ${snoot}'s homepage
</h1>
<h2>getting started:</h2>
<p>
if you are <strong>${snoot}</strong>, you can now ssh or ftp into your account!
</p>
<p>
if you want to host a webpage, put files in the directory
<code>application/website</code>
</p>
<p>
the page you are reading right now is the file located at
<code>./application/website/index.html</code>
</p>
<h2>advanced users:</h2>
<p>
the start script in your <code>package.json</code> will be run automatically.
you can replace it with anything, and as long as it creates and listens on a
unix domain socket at <code>./application/sock</code>, it'll be served. the
initial setup already does this, and serves files in
<code>./application/website</code> and apps in
<code>./application/boops</code> using <a
href="https://github.com/snootclub/boop">boops</a>.
</p>
`
}
}
}
}
let fileTypes = {
directory: Symbol("directory"),
file: Symbol("file")
}
exports.fileTypes = fileTypes
let merge = (a, b) =>
Object.assign({}, a, b)
exports.write = async function write (options) {
let {
resolver,
render,
files = exports.files,
uid,
gid
} = options
for (let [key, value] of Object.entries(files)) {
let fileResolver = resolver(key)
let fileType = typeof value == "function"
? fileTypes.file
: fileTypes.directory
let filePath = fileResolver.path
out:
if (fileType == fileTypes.file) { // if this is a file node
let fileCreator = value
if (await fs.pathExists(filePath)) {
let {shouldContinue} = await inquirer.prompt({
type: "confirm",
name: "shouldContinue",
message: `🎺 whümf and wetch? "${filePath}" already exists, should we overwrite?`,
default: false
})
if (!shouldContinue) {
break out
}
}
await fs.outputFile(filePath, render(fileCreator))
let rw_r__r__ = 0o644
await fs.chmod(filePath, rw_r__r__)
} else {
// this is a directory node
let files = value
let rwxrwxr_x = 0o775
await fs.mkdirp(filePath)
await fs.chmod(filePath, rwxrwxr_x)
await write(merge(options, {
resolver: fileResolver,
files
}))
}
await fs.chown(
filePath,
uid,
gid
)
}
}