Skip to content

Commit 4142302

Browse files
authored
Merge pull request #5 from nitaking/add-option
✨ Add --models-path & config
2 parents b0a0988 + 2f77c9e commit 4142302

6 files changed

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

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: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
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 {
811
async run() {
9-
const {flags} = this.parse(SequelizeTinkerCommand)
12+
const {flags, args} = this.parse(SequelizeTinkerCommand)
1013

1114
if (flags === _initialFlags) {
1215
return
1316
}
1417

18+
// show config
19+
if (args.firstArg === 'config') {
20+
jq.run('.', getAll(), {input: 'json'})
21+
.then(output => {
22+
this.log(output)
23+
})
24+
.catch(error => {
25+
this.error(error)
26+
})
27+
return
28+
}
29+
30+
if (flags['models-path']) {
31+
setModelsPath(flags['models-path'])
32+
}
33+
34+
// repl entry
1535
const replUtil = new Repl({initializeContext})
1636
replUtil.start()
1737
}
@@ -27,6 +47,17 @@ SequelizeTinkerCommand.flags = {
2747
// add --help flag to show CLI version
2848
help: flags.help({char: 'h'}),
2949
// name: flags.string({ char: 'n', description: 'name to print' })
50+
'models-path': flags.string({char: 'p', description: 'Set Sequelize models\'s dir path.'}),
3051
}
3152

53+
SequelizeTinkerCommand.args = [
54+
{name: 'firstArg'},
55+
{name: 'secondArg'},
56+
]
57+
SequelizeTinkerCommand.examples = [
58+
'$ sequelize-tinker',
59+
'$ sequelize-tinker --models-path ./src/models',
60+
'$ sequelize-tinker config',
61+
]
62+
3263
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)