@frozencrow/create-api 0.5.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/create-api.js
CHANGED
|
@@ -58,6 +58,31 @@ function copyDir(src, dest) {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
// npm names may contain `.` and `~`, but MongoDB database names can't contain
|
|
62
|
+
// `.`, and compose project names allow only [a-z0-9_-] starting with a letter or
|
|
63
|
+
// digit. Derive one safe slug for both.
|
|
64
|
+
function toSlug(name) {
|
|
65
|
+
const slug = name.toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^[^a-z0-9]+/, '')
|
|
66
|
+
return slug || 'app'
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// MongoDB database names must be under 64 bytes; leave room for the `-test` suffix.
|
|
70
|
+
const MAX_DB_BASE_LENGTH = 58
|
|
71
|
+
|
|
72
|
+
function updateJson(file, mutate) {
|
|
73
|
+
if (!fs.existsSync(file)) return
|
|
74
|
+
const data = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
75
|
+
mutate(data)
|
|
76
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 4) + '\n')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function replaceInFile(file, replacements) {
|
|
80
|
+
if (!fs.existsSync(file)) return
|
|
81
|
+
let text = fs.readFileSync(file, 'utf8')
|
|
82
|
+
for (const [from, to] of replacements) text = text.replace(from, to)
|
|
83
|
+
fs.writeFileSync(file, text)
|
|
84
|
+
}
|
|
85
|
+
|
|
61
86
|
function ask(question) {
|
|
62
87
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
63
88
|
return new Promise((resolve) => rl.question(question, (answer) => {
|
|
@@ -142,14 +167,27 @@ async function main() {
|
|
|
142
167
|
pkg.name = pkgName
|
|
143
168
|
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 4) + '\n')
|
|
144
169
|
|
|
145
|
-
// 2.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
170
|
+
// 2. Replace the template's `my-api` everywhere it appears, so each project
|
|
171
|
+
// gets its own databases and compose project instead of sharing the template's.
|
|
172
|
+
const slug = toSlug(pkgName)
|
|
173
|
+
const dbName = slug.slice(0, MAX_DB_BASE_LENGTH)
|
|
174
|
+
|
|
175
|
+
updateJson(path.join(targetDir, 'config', 'default.json'), (cfg) => {
|
|
149
176
|
cfg.appName = pkgName
|
|
150
|
-
cfg.mongodb = `mongodb://localhost:27017/${
|
|
151
|
-
|
|
152
|
-
|
|
177
|
+
cfg.mongodb = `mongodb://localhost:27017/${dbName}`
|
|
178
|
+
})
|
|
179
|
+
updateJson(path.join(targetDir, 'config', 'test.json'), (cfg) => {
|
|
180
|
+
cfg.appName = `${pkgName} (test)`
|
|
181
|
+
cfg.mongodb = `mongodb://localhost:27017/${dbName}-test`
|
|
182
|
+
})
|
|
183
|
+
replaceInFile(path.join(targetDir, 'docker-compose.yml'), [
|
|
184
|
+
[/^name: my-api$/m, `name: ${slug}`],
|
|
185
|
+
['mongodb://mongodb:27017/my-api', `mongodb://mongodb:27017/${dbName}`]
|
|
186
|
+
])
|
|
187
|
+
replaceInFile(path.join(targetDir, 'README.md'), [[/^# My API$/m, `# ${pkgName}`]])
|
|
188
|
+
replaceInFile(path.join(targetDir, 'public', 'index.html'), [
|
|
189
|
+
['<title>My API</title>', `<title>${pkgName}</title>`]
|
|
190
|
+
])
|
|
153
191
|
|
|
154
192
|
// 3. Generate a strong secret into config/local.json (git-ignored). The config
|
|
155
193
|
// module (config/*.json) loads local.json last, so this overrides the default
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frozencrow/create-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Scaffold a new API project powered by @frozencrow/api-core",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Eric Uldall",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"template"
|
|
33
33
|
],
|
|
34
34
|
"scripts": {
|
|
35
|
-
"build": "node -e \"process.exit(0)\""
|
|
35
|
+
"build": "node -e \"process.exit(0)\"",
|
|
36
|
+
"test": "node --test test/scaffold.test.js"
|
|
36
37
|
},
|
|
37
38
|
"publishConfig": {
|
|
38
39
|
"access": "public"
|
package/template/Dockerfile
CHANGED
|
@@ -12,6 +12,11 @@ RUN npm run compile
|
|
|
12
12
|
# Production stage
|
|
13
13
|
FROM node:20-alpine
|
|
14
14
|
|
|
15
|
+
# Run as production: api-core refuses to boot with a missing/weak/placeholder
|
|
16
|
+
# AUTH_SECRET only when NODE_ENV=production. (docker-compose.yml overrides this
|
|
17
|
+
# to `development` for local use.)
|
|
18
|
+
ENV NODE_ENV=production
|
|
19
|
+
|
|
15
20
|
WORKDIR /app
|
|
16
21
|
|
|
17
22
|
COPY package*.json ./
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{}
|
package/template/package.json
CHANGED