create-sip 1.2.2 → 1.3.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/expressapi/op CHANGED
@@ -52,7 +52,7 @@ yargs(hideBin(process.argv))
52
52
  }
53
53
  )
54
54
  .command('conf:generate',
55
- 'Generates a new config file: config/default.json from config/default.json.example',
55
+ 'Generate a new config file: .env',
56
56
  (yargs) => {
57
57
  return yargs
58
58
  },
@@ -60,6 +60,15 @@ yargs(hideBin(process.argv))
60
60
  startGenerateConf();
61
61
  }
62
62
  )
63
+ .command('testconf:generate',
64
+ 'Generate a new config file: .env.test',
65
+ (yargs) => {
66
+ return yargs
67
+ },
68
+ async (argv) => {
69
+ startGenerateTestConf();
70
+ }
71
+ )
63
72
  .command('admin:generate',
64
73
  'Generates a new admin user in database table: users',
65
74
  (yargs) => {
@@ -241,45 +250,64 @@ function capitalizeFirstLetter(word) {
241
250
  return word.charAt(0).toUpperCase() + word.slice(1);
242
251
  }
243
252
 
244
- function startGenerateKey() {
245
- console.log('Generate key...');
246
- const fileName = 'config/default.json'
247
- fsp.readFile(fileName)
248
- .then(body => JSON.parse(body))
249
- .then(json => {
250
- json.app.key = generateApiKey({method: 'bytes', length: 32})
251
- return json
252
- })
253
- .then(json => JSON.stringify(json, null, 4))
254
- .then(body => fse.writeFile(fileName, body, 'utf8'))
255
- .catch(error => console.log(error))
253
+ async function startGenerateKey() {
254
+ try {
255
+ const envFile = '.env'
256
+ let content = await fse.readFile(envFile, 'utf8')
257
+ const key = generateApiKey({method: 'bytes', length: 32})
258
+
259
+ const regex = /^APP_KEY *=.*$/m
260
+ if(regex.test(content)) {
261
+ content = content.replace(regex, `APP_KEY=${key}`)
262
+ }else {
263
+ if(content.trim() !== '') {
264
+ content += `\nAPP_KEY=${key}\n`
265
+ }else {
266
+ content = `APP_KEY=${key}\n`
267
+ }
268
+ }
269
+ fse.writeFile(envFile, content, 'utf8')
270
+ } catch (error) {
271
+ console.error(error)
272
+ }
256
273
  }
257
274
 
258
275
  async function startGenerateConf() {
259
- console.log('Generate conf...');
260
- const sourceFileName = 'config/default.json.example';
261
- const destinationFileName = 'config/default.json';
276
+ const content = `
277
+ APP_PORT=8000
278
+ APP_KEY=
279
+ APP_LOG=console.log
280
+
281
+ DB_DIALECT=sqlite
282
+ DB_HOST=127.0.0.1
283
+ DB_NAME=
284
+ DB_USER=
285
+ DB_PASS=
286
+ DB_PATH=:memory:
287
+ `
288
+ const destinationFileName = '.env'
262
289
  if(await startCheckIfFileExists(destinationFileName)) {
263
290
  process.exit(1);
264
291
  }
265
- try {
266
- await fse.copyFile(sourceFileName, destinationFileName)
267
- } catch (error) {
268
- console.log(error)
269
- }
270
-
292
+
293
+ await fse.writeFile(destinationFileName, content, 'utf8')
271
294
  }
272
295
 
273
- async function loadConfig() {
274
- try {
275
- const filePath = path.resolve('./config/default.json');
276
- const data = await fsp.readFile(filePath, 'utf8');
277
- const config = JSON.parse(data);
278
- return config;
279
- } catch (error) {
280
- console.error('Error loading config:', error);
281
- return null;
296
+ async function startGenerateTestConf() {
297
+ const content = `
298
+ APP_PORT=8000
299
+ APP_KEY=my_secret_key
300
+ APP_LOG=console.log
301
+
302
+ DB_DIALECT=sqlite
303
+ DB_PATH=:memory:
304
+ `
305
+ const destinationFileName = '.env.test'
306
+ if(await startCheckIfFileExists(destinationFileName)) {
307
+ process.exit(1);
282
308
  }
309
+
310
+ await fse.writeFile(destinationFileName, content, 'utf8')
283
311
  }
284
312
 
285
313
  async function inputPassword() {
@@ -292,33 +320,33 @@ async function inputPassword() {
292
320
  }
293
321
 
294
322
  async function startGenerateAdmin() {
295
- console.log('Generate admin...')
296
323
  try {
297
- const config = await loadConfig();
298
- const isMemoryDb = config.db.path === ':memory:';
324
+ const { default: User } = await import('./app/models/user.js')
325
+ await User.sync()
326
+
327
+ await import('dotenv').then((dotenv) => dotenv.config())
328
+ const isMemoryDb = process.env.DB_PATH === ':memory:';
299
329
  if(isMemoryDb) {
300
330
  console.log('Admin cannot be created in memory db!')
301
- }else {
302
- const { default: User } = await import('./app/models/user.js')
303
- await User.sync()
331
+ return;
332
+ }
304
333
 
305
- const isUserExist = await User.findOne({ where: { name: 'admin' } })
306
- if (isUserExist) {
307
- console.log('Admin already exists!')
308
- return;
309
- }
310
- const password = await inputPassword()
311
- const hashedPassword = await bcrypt.hash(password, 10);
312
- await User.create({
313
- name: 'admin',
314
- email: 'admin',
315
- password: hashedPassword
316
- })
317
- console.log('Admin created!')
334
+ const isUserExist = await User.findOne({ where: { name: 'admin' } })
335
+ if (isUserExist) {
336
+ console.log('Admin already exists!')
337
+ return;
318
338
  }
319
- } catch( err) {
339
+ const password = await inputPassword()
340
+ const hashedPassword = await bcrypt.hash(password, 10);
341
+ await User.create({
342
+ name: 'admin',
343
+ email: 'admin',
344
+ password: hashedPassword
345
+ })
346
+ console.log('Admin created!')
347
+ } catch (error) {
320
348
  console.error('Error creating admin!')
321
- console.error(err)
349
+ console.error(error)
322
350
  }
323
351
  }
324
352