This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
configure.js
executable file
·125 lines (111 loc) · 3.06 KB
/
configure.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
#!/usr/bin/env babel-node --
import program from 'commander'
import fs from 'fs'
import envfile from 'envfile'
import { V2 as encoder, SymmetricKey} from 'paseto.js'
import Knex from 'knex'
import { Model } from 'objection'
const sourcePath = '.env'
let env = envfile.parseFileSync(sourcePath)
const knexConfig = require('../knexfile')
const knex = Knex(knexConfig[env.APP_ENV])
Model.knex(knex)
import User from '../src/api/models/user.ts'
const generateNewKey = async () => {
try {
let sk = new SymmetricKey(new encoder)
sk.generate().then(() => {
let b64 = sk.encode()
env.PASETO_KEY = b64
let output = envfile.stringifySync(env)
fs.writeFileSync(sourcePath, output)
console.log('New PASETO key has been sucessfully generated.')
});
} catch (err) {
throw err
}
}
const databaseFunctions = async (method, argument) => {
switch (method) {
case 'rollback':
await knex.migrate.rollback({}, true)
console.log('Database has been rolled back')
break
case 'migrate':
await knex.migrate.latest()
console.log('Database has been migrated')
break
case 'refresh':
await knex.migrate.rollback({}, true)
await knex.migrate.latest()
console.log('Database has been refreshed')
break
case 'seed':
let config = knexConfig[env.APP_ENV]
if (argument) {
console.log('Seeding from folder: ' + argument)
let pathArray = config.seeds.directory.split('/')
pathArray[pathArray.length - 1] = argument
config.seeds.directory = pathArray.join('/')
}
await knex.seed.run(config)
console.log('Database has been seeded')
break
default:
console.log('No method specified')
}
knex.destroy()
}
const assignRole = async (email, roleName) => {
let user
try {
user = await User.query().findOne('email', email).throwIfNotFound()
await user.assignRole(roleName)
} catch (err) {
console.error(err)
knex.destroy()
return
}
knex.destroy()
console.log('Role successfully added')
}
program
.version('0.0.1')
.command('generate-key')
.description('Generate a new PASETO key')
.action(generateNewKey)
program
.command('database:migrate')
.description('Migrate database')
.action(function() {
databaseFunctions('migrate')
})
program
.command('database:rollback')
.description('Rollback database')
.action(function() {
databaseFunctions('rollback')
})
program
.command('database:refresh')
.description('Refresh database')
.action(function() {
databaseFunctions('refresh')
})
program
.command('database:seed [folder]')
.description('Seed database')
.action(function(folder) {
databaseFunctions('seed', folder)
})
program
.command('user:assign-role [email] [role]')
.description('Asign role to user')
.action(function(email, role) {
assignRole(email, role)
})
program.on('command:*', function () {
console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
process.exit(1);
});
program.parse(process.argv);