Skip to content

Commit 610a758

Browse files
committed
✨ Add --models-path & config
Add option: models-path, Add args config
1 parent 50c079b commit 610a758

6 files changed

Lines changed: 1019 additions & 15 deletions

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"dependencies": {
1111
"@oclif/command": "^1",
1212
"@oclif/config": "^1",
13-
"@oclif/plugin-help": "^2"
13+
"@oclif/plugin-help": "^2",
14+
"conf": "^6.0.1",
15+
"node-jq": "^1.10.3"
1416
},
1517
"devDependencies": {
1618
"@oclif/test": "^1",

src/config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const Conf = require('conf')
2+
const constants = require('./constants')
3+
4+
5+
const config = new Conf()
6+
7+
const getModelsPath = ()/* :void */ => config.get(constants.MODELS_PATH, constants.BASE_MODELS_PATH)
8+
// const getModelsPath = ()/* :void */ => constants.BASE_MODELS_PATH
9+
const setModelsPath = (modelsPath/* :string */)/* :string */ => config.set(constants.MODELS_PATH, modelsPath)
10+
11+
const getAll = () => {
12+
let result = {}
13+
constants.CONFIG_KEYS.forEach(key => {
14+
result[key] = config.get(key)
15+
})
16+
return result;
17+
}
18+
19+
module.exports = {
20+
getModelsPath,
21+
setModelsPath,
22+
config,
23+
getAll,
24+
}

src/constants.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const {workDir} = require('./path')
2+
3+
// config key
4+
const MODELS_PATH = 'models-path'
5+
const CONFIG_KEYS = [MODELS_PATH];
6+
7+
// values
8+
const BASE_MODELS_PATH = `${workDir}/models`
9+
10+
module.exports = {
11+
CONFIG_KEYS,
12+
MODELS_PATH,
13+
BASE_MODELS_PATH,
14+
}

src/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
const {Command, flags} = require('@oclif/command')
2+
const jq = require('node-jq')
3+
24
const Repl = require('./repl')
35
const {initializeContext} = require('./sequelize-context')
6+
const { setModelsPath, getAll } = require('./config')
47

58
const _initialFlags = {}
69

710
class SequelizeTinkerCommand extends Command {
11+
812
async run() {
9-
const {flags} = this.parse(SequelizeTinkerCommand)
13+
const {flags, args} = this.parse(SequelizeTinkerCommand)
1014

1115
if (flags === _initialFlags) {
1216
return
1317
}
1418

19+
// show config
20+
if (args.firstArg === 'config') {
21+
jq.run('.', getAll(), { input: 'json' })
22+
.then((output) => {
23+
this.log(output)
24+
})
25+
.catch((error) => {
26+
this.error(error)
27+
})
28+
return
29+
}
30+
31+
if (flags['models-path']) {
32+
setModelsPath(flags['models-path'])
33+
}
34+
35+
// repl entry
1536
const replUtil = new Repl({initializeContext})
1637
replUtil.start()
1738
}
@@ -27,6 +48,17 @@ SequelizeTinkerCommand.flags = {
2748
// add --help flag to show CLI version
2849
help: flags.help({char: 'h'}),
2950
// name: flags.string({ char: 'n', description: 'name to print' })
51+
'models-path': flags.string({char: 'p', description: `Set Sequelize models's dir path.`}),
3052
}
3153

54+
SequelizeTinkerCommand.args = [
55+
{name: 'firstArg'},
56+
{name: 'secondArg'},
57+
]
58+
SequelizeTinkerCommand.examples = [
59+
'$ sequelize-tinker',
60+
'$ sequelize-tinker --models-path ./src/models',
61+
'$ sequelize-tinker config',
62+
]
63+
3264
module.exports = SequelizeTinkerCommand

src/sequelize-context.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const {workDir} = require('./path')
1+
22
const {secureRequire} = require('./secure-require')
3-
const models = secureRequire(`${workDir}/models`) // todo: import setting
3+
const config = require('./config')
4+
const models = secureRequire(config.getModelsPath()) // todo: import setting
45
const sequelize = models && models.sequelize
56

67
function initializeContext(context) {

0 commit comments

Comments
 (0)