@frozencrow/create-api 0.6.0 → 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. Personalize config/default.json (app name + database name).
146
- const defaultConfigPath = path.join(targetDir, 'config', 'default.json')
147
- if (fs.existsSync(defaultConfigPath)) {
148
- const cfg = JSON.parse(fs.readFileSync(defaultConfigPath, 'utf8'))
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/${pkgName}`
151
- fs.writeFileSync(defaultConfigPath, JSON.stringify(cfg, null, 4) + '\n')
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.6.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"
@@ -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 ./
@@ -1,5 +1,8 @@
1
1
  {
2
- "port": "PORT",
2
+ "port": {
3
+ "__name": "PORT",
4
+ "__format": "number"
5
+ },
3
6
  "mongodb": "MONGODB_URI",
4
7
  "clientHost": "CLIENT_HOST",
5
8
  "origins": {
@@ -0,0 +1 @@
1
+ {}
@@ -1,3 +1,7 @@
1
+ # Explicit project name (rewritten by the scaffolder) so containers and volumes
2
+ # are namespaced per project rather than per folder name.
3
+ name: my-api
4
+
1
5
  services:
2
6
  api:
3
7
  build: .
@@ -19,7 +19,7 @@
19
19
  "config": "3.3.9"
20
20
  },
21
21
  "dependencies": {
22
- "@frozencrow/api-core": "^0.6.0",
22
+ "@frozencrow/api-core": "^0.6.1",
23
23
  "@feathersjs/configuration": "^5.0.37",
24
24
  "@feathersjs/feathers": "^5.0.37",
25
25
  "@feathersjs/mongodb": "^5.0.37",