Skip to content

Commit

Permalink
feat(conf): config is loaded from package.json
Browse files Browse the repository at this point in the history
Configuration loaded initially from package.json. The nearest
package.json is used starting from `require.main`. If require.main is
not defined, process.cwd() is used.

Configuration still be manually changed using seeli.get + seeli.set
  • Loading branch information
esatterwhite committed Feb 16, 2021
1 parent 48cc6e6 commit 25a1c2b
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 40 deletions.
1 change: 1 addition & 0 deletions examples/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
11 changes: 2 additions & 9 deletions examples/cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#!/usr/bin/env node

'use strict'
const cli = require('../')

cli.set({
exitOnError: false
, color: 'green'
, name: 'example'
})

const cli = require('seeli')
const commands = require('./commands')

for (const [name, command] of Object.entries(commands)) {
if (command.options.name) {
cli.use(command)
Expand All @@ -19,5 +13,4 @@ for (const [name, command] of Object.entries(commands)) {
}

if (require.main === module) cli.run()

module.exports = cli
2 changes: 1 addition & 1 deletion examples/commands/flip.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const cli = require('../../')
const cli = require('seeli')
const CHARS = {
// uppercase (incomplete)
'A': '∀'
Expand Down
2 changes: 1 addition & 1 deletion examples/commands/foaas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const https = require('https')

const cli = require('../../')
const cli = require('seeli')
const Command = cli.Command

const NAME = cli.get('name')
Expand Down
2 changes: 1 addition & 1 deletion examples/commands/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const tty = require('tty')
const os = require('os')
const cli = require('../../')
const cli = require('seeli')

module.exports = new cli.Command({
description: 'displays a simple hello world command'
Expand Down
1 change: 0 additions & 1 deletion examples/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ module.exports = {
, flip: require('./flip')
, ui: require('./ui')
, password: require('./password-confirm')
, sub: require('./sub')
}
2 changes: 1 addition & 1 deletion examples/commands/password-confirm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const cli = require('../../')
const cli = require('seeli')

module.exports = new cli.Command({
description: 'Set your password'
Expand Down
2 changes: 1 addition & 1 deletion examples/commands/ui.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const toArray = require('mout/lang/toArray')
const cli = require('../../')
const cli = require('seeli')
const spinners = require('cli-spinners')
module.exports = new cli.Command({
description: 'Allows you to play with progress spinners and displays'
Expand Down
5 changes: 0 additions & 5 deletions examples/package-lock.json

This file was deleted.

12 changes: 11 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,15 @@
"scripts": {},
"keywords": [],
"author": "Eric Satterwhite <[email protected]>",
"license": "ISC"
"license": "ISC",
"dependencies": {
"cli-spinners": "^2.5.0",
"debug": "*",
"seeli": "file:../"
},
"seeli": {
"color": "magenta",
"name": "fake",
"help": "./commands/ui"
}
}
53 changes: 34 additions & 19 deletions lib/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,58 @@
const os = require('os')
const path = require('path')
const chalk = require('chalk')
const pkgup = require('pkg-up')
const set = require('mout/object/set')
const get = require('mout/object/get')
const isObject = require('mout/lang/isPlainObject')

const debug = require('debug')('seeli:conf')
const CWD = process.cwd()
const filename = process.argv[1]

let username = 'seeli'
let host = 'local'

try {
const info = os.userInfo()
host = os.hostname()
username = info.username
} catch (_) {}

const PS1 = `${username}@${host}`
const name = filename
? path.basename(filename, '.js')
: 'seeli'

const defaults = {
let config = {
color: 'green'
, prompt: `[${chalk.green(PS1)} ${chalk.bold(name)}]$ `
, name: name
, help: path.resolve(path.join(__dirname, 'commands', 'help'))
, exitOnError: false
, exitOnContent: false
}

exports.get = function(key) {
return get(defaults, key)
try {
const cwd = get(require, 'main.path') || CWD
const pkgjson = pkgup.sync({cwd})
debug('loading configuration from %s', pkgjson)
const pkg = require(pkgjson)
const override = pkg.seeli || {}
const help = resolveHelp(override.help, cwd)
config = {
...config
, ...override
, help: help
}
} catch (e) {
debug('unable to load configuration. using config', e)
}

exports.set = function(key, value) {
if (!isObject(key)) return set(defaults, key, value)
module.exports = {
get: (key) => {
return get(config, key)
}

, set: (key, value) => {
if (!isObject(key)) return set(config, key, value)

for (const [k, v] of Object.entries(key)) {
set(defaults, k, v)
for (const [k, v] of Object.entries(key)) {
set(config, k, v)
}
}
}

function resolveHelp(location, cwd) {
if (!location) return config.help
if (path.isAbsolute(location)) return config.help
return path.join(cwd, location)
}

0 comments on commit 25a1c2b

Please sign in to comment.