create-sip 0.12.2 → 0.13.0

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.
@@ -1,7 +1,9 @@
1
1
  import bcrypt from 'bcryptjs'
2
2
  import jwt from 'jsonwebtoken'
3
3
  import User from '../models/user.js'
4
- import config from '../../config/default.json' assert { type: 'json' }
4
+ import { readJson } from '../../tools/readjson.js'
5
+
6
+ const config = await readJson('config/default.json')
5
7
 
6
8
  const AuthController = {
7
9
  async register(req, res) {
@@ -1,11 +1,12 @@
1
1
  import Sequelize from 'sequelize'
2
- import config from '../../config/default.json' assert { type: 'json' }
2
+ import { readJson } from '../../tools/readjson.js'
3
+
4
+ const config = await readJson('config/default.json')
3
5
 
4
6
  if(config.app.log != false) {
5
7
  var log = console.log
6
8
  }
7
9
 
8
- console.log(typeof console.log)
9
10
  const sequelize = new Sequelize(
10
11
  config.db.name,
11
12
  config.db.user,
@@ -3,7 +3,9 @@ import morgan from 'morgan'
3
3
  import cors from 'cors'
4
4
  import fs from 'fs'
5
5
  import router from './routes/api.js'
6
- import config from '../config/default.json' assert { type: 'json' }
6
+ import { readJson } from '../tools/readjson.js'
7
+
8
+ const config = await readJson('config/default.json')
7
9
 
8
10
  const app = express()
9
11
 
@@ -1,5 +1,7 @@
1
1
  import jwt from 'jsonwebtoken';
2
- import config from '../../config/default.json' assert { type: 'json' };
2
+ import { readJson } from '../../tools/readjson.js'
3
+
4
+ const config = await readJson('config/default.json')
3
5
 
4
6
  const verifyToken = (req, res, next) => {
5
7
  let authData = req.headers.authorization;
package/expressapi/op CHANGED
@@ -4,12 +4,15 @@ import fs from 'fs-extra';
4
4
  import fsp from 'fs/promises'
5
5
  import { generateApiKey } from 'generate-api-key'
6
6
 
7
- console.log(process.argv[2])
7
+ import bcrypt from 'bcryptjs';
8
+ import path from 'path';
8
9
 
9
10
  if(process.argv.length == 3 && process.argv[2] == 'key:generate') {
10
11
  startGenerateKey();
11
12
  } else if(process.argv.length == 3 && process.argv[2] == 'conf:generate') {
12
13
  startGenerateConf();
14
+ } else if(process.argv.length == 3 && process.argv[2] == 'admin:generate') {
15
+ startGenerateAdmin();
13
16
  } else if(process.argv.length < 4) {
14
17
  console.log('Usage:');
15
18
  console.log('node op <command> <type> <name>');
@@ -102,3 +105,46 @@ function startGenerateConf() {
102
105
  fs.copyFile(sourceFileName, destinationFileName)
103
106
  .catch(error => console.log(error));
104
107
  }
108
+
109
+ async function loadConfig() {
110
+ try {
111
+ const filePath = path.resolve('./config/default.json');
112
+ const data = await fsp.readFile(filePath, 'utf8');
113
+ const config = JSON.parse(data);
114
+ return config;
115
+ } catch (error) {
116
+ console.error('Error loading config:', error);
117
+ return null;
118
+ }
119
+ }
120
+
121
+ async function startGenerateAdmin() {
122
+ console.log('Generate admin...')
123
+ try {
124
+ const config = await loadConfig();
125
+ const isMemoryDb = config.db.path === ':memory:';
126
+ if(isMemoryDb) {
127
+ console.log('Admin cannot be created in memory db!')
128
+ }else {
129
+ const { default: User } = await import('./app/models/user.js')
130
+ await User.sync()
131
+
132
+ const isUserExist = await User.findOne({ where: { name: 'admin' } })
133
+ if (isUserExist) {
134
+ console.log('Admin already exists!')
135
+ return;
136
+ }
137
+
138
+ const hashedPassword = await bcrypt.hash('admin', 10);
139
+ await User.create({
140
+ name: 'admin',
141
+ email: 'admin',
142
+ password: hashedPassword
143
+ })
144
+ console.log('Admin created!')
145
+ }
146
+ } catch( err) {
147
+ console.error('Error creating admin!')
148
+ console.error(err)
149
+ }
150
+ }
@@ -0,0 +1,17 @@
1
+
2
+ import fsp from 'fs/promises'
3
+ import path from 'path'
4
+
5
+ async function readJson(dirPath) {
6
+ try {
7
+ const filePath = path.resolve(dirPath);
8
+ const data = await fsp.readFile(filePath, 'utf8');
9
+ const dataObject = JSON.parse(data);
10
+ return dataObject;
11
+ } catch (error) {
12
+ console.error('Error loading config:', error);
13
+ return null;
14
+ }
15
+ }
16
+
17
+ export { readJson }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-sip",
3
- "version": "0.12.2",
3
+ "version": "0.13.0",
4
4
  "main": "index.js",
5
5
  "bin": {
6
6
  "create-sip": "create-sip.js"