UNPKG

1.78 kBJavaScriptView Raw
1const fs = require('fs')
2const path = require('path')
3const pkgConf = require('pkg-conf')
4const execa = require('execa')
5const symbols = require('log-symbols')
6const consts = require('./consts')
7
8const pkg = require('../package.json')
9const debug = require('debug')(pkg.name)
10
11const info = message => exports.debug(`${symbols.info} ${message}`)
12const success = message => exports.debug(`${symbols.success} ${message}`)
13const warning = message => exports.debug(`${symbols.warning} ${message}`)
14const error = message => exports.debug(`${symbols.error} ${message}`)
15
16/**
17 * Execute system command.
18 *
19 * @param {string} cmd - command to be executed
20 * @param {array} args - list of command arguments.
21 * @param {object} options - command options.
22 */
23const execute = (cmd, ...args) =>
24 execa.sync(cmd, [...args], Object.assign({ stdio: 'inherit' }))
25
26/**
27 * Fetch settings from given file name (.js) and package.json#namespace.
28 * 1. If file with the given name found, simply require it & return its values.
29 * 2. If file not found, return package.json#namespace values.
30 * 3. If namespace it also missing, return {}.
31 * @param {String} [options.filename] - file to read under `cwd`.
32 * @param {String} [options.namespace] - namespace to lookup in package.json under `cwd`.
33 **/
34const readSettings = async (options = {}) => {
35 let settings = {}
36 const cwd = options.cwd || consts.cwd
37 const { filename, namespace } = options
38 const config = filename ? path.resolve(cwd, filename) : null
39
40 if (config && fs.existsSync(config)) {
41 settings = require(config) // eslint-disable-line
42 } else if (namespace) {
43 settings = await pkgConf(namespace)
44 }
45
46 return settings
47}
48
49module.exports = {
50 pkg,
51 debug,
52 info,
53 success,
54 warning,
55 error,
56 execute,
57 readSettings
58}