-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeletons.js
206 lines (186 loc) · 3.99 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
203
204
205
206
let fs = require("fs-extra")
let os = require("os")
let inquirer = require("inquirer")
exports.files = {
".ssh": {
authorized_keys (_snoot, {authorizedKeys}) {
return authorizedKeys
}
},
".gitconfig" (snoot) {
return `[user]
name = ${snoot}
email ${snoot}@snoot.club`
},
repo: {
hooks: {
"post-receive" () {
return `#!/bin/sh
npx @snootclub/post-receive`
}
}
},
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>
<p>
if you are <strong>${snoot}</strong>, you can now ssh or ftp into your account!
</p>
<p>
the page you are reading right now is the file located at
<code>./application/website/index.html</code>
</p>
<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>
<p>
i promise that's cool and fun
</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,
getPermissions = () => ({uid, gid, mode})
} = 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
let permissions = getPermissions({filePath, fileType}) || {}
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))
await fs.chmod(filePath, permissions.mode || 0o644)
} else {
// this is a directory node
let files = value
await fs.mkdirp(filePath)
await fs.chmod(filePath, permissions.mode || 0o775)
await write(merge(options, {
resolver: fileResolver,
files
}))
}
await fs.chown(
filePath,
permissions.uid || uid,
permissions.gid || gid
)
}
}