Skip to content

Commit

Permalink
feat: global config and ghtoken support
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau committed Jan 4, 2020
1 parent 7b4676b commit 599f5e8
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
84 changes: 84 additions & 0 deletions src/config/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict'

const _ = require('lodash')
const Joi = require('@hapi/joi')
const { promisify } = require('es6-promisify')
const utils = require('../utils')

const hookSchema = Joi.object().keys({
pre: Joi.func(),
post: Joi.func()
}).unknown(false)

const envSchema = Joi.object().keys({
browser: hookSchema,
node: hookSchema
}).unknown(false)

const HOOK_ENVS = [
'browser',
'node'
]

const HOOK_STAGES = [
'pre',
'post'
]

function promisifyHooks (hooks) {
Object.keys(hooks).forEach((key) => {
if (hooks[key].length) {
// hook takes args, is expecting a callback so promisify it
hooks[key] = promisify(hooks[key])
}
})

return hooks
}

function normalizeHooks (hooks) {
const keys = Object.keys(hooks)

// no hooks provided
if (keys.length === 0) {
return hooks
}

// same hooks for all envs
if (_.every(keys, (k) => _.includes(HOOK_STAGES, k))) {
const v = promisifyHooks(Joi.attempt(hooks, hookSchema))

const res = {}
HOOK_ENVS.forEach((env) => {
res[env] = v
})

return res
}

// regular per env hook specification
if (_.every(keys, (k) => _.includes(HOOK_ENVS, k))) {
const res = Joi.attempt(hooks, envSchema)
keys.forEach((key) => {
res[key] = promisifyHooks(res[key])
})
return res
}

throw new Error(`Found unknown keys in hook definiton: "${keys.join(' ')}"`)
}

function globalConfig () {
const config = utils.getGlobalConfig()

const global = _.defaultsDeep({}, config, {
hooks: {},
release: {}
})

global.hooks = normalizeHooks(global.hooks)

return global
}

module.exports = globalConfig
31 changes: 30 additions & 1 deletion src/release/prerelease.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const git = require('simple-git')(process.cwd())
const pify = require('pify')
const inquirer = require('inquirer')
const execa = require('execa')
const globalConfig = require('../config/global')

const GHTOKEN_QUESTION = [
{
Expand All @@ -14,13 +16,40 @@ const GHTOKEN_QUESTION = [
}
]

async function evaluateGhtoken (value) {
if (typeof value === 'function') {
return await value()
}

if (typeof value !== 'string') {
throw new TypeError('Unknown type of release.ghtoken property.')
}

if (!value.startsWith('shell:')) {
return value
}

const cmd = value.replace('shell:', '')
const result = await execa(cmd, [], {
shell: true,
})

return result.stdout
}

// Check if there are valid GitHub credentials for publishing this module
async function validGh (opts) {
if (!opts.ghrelease || opts.ghtoken) {
return opts
}

opts.ghtoken = (await inquirer.prompt(GHTOKEN_QUESTION))['ghtoken']
const config = globalConfig()
if (config.release.ghtoken) {
opts.ghtoken = await evaluateGhtoken(config.release.ghtoken)
} else {
opts.ghtoken = (await inquirer.prompt(GHTOKEN_QUESTION))['ghtoken']
}

return opts
}

Expand Down
23 changes: 23 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const arrify = require('arrify')
const _ = require('lodash')
const VerboseRenderer = require('listr-verbose-renderer')
const execa = require('execa')
const os = require('os')

const { package: pkg, path: pkgPath } = readPkgUp.sync({
cwd: fs.realpathSync(process.cwd())
Expand Down Expand Up @@ -86,6 +87,28 @@ exports.getUserConfig = () => {
return conf
}

/**
* @returns {string}
*/
exports.getGlobalConfigPath = () => {
return process.env.TASEGIR_CONFIG || path.join(os.homedir(), '.tasegir.js')
}

/**
* @returns {Object}
*/
exports.getGlobalConfig = () => {
let conf = {}
try {
const path = exports.getGlobalConfigPath()
if (!path) return null
conf = require(path)
} catch (err) {
console.error(err) // eslint-disable-line no-console
}
return conf
}

/**
* Converts the given name from something like `peer-id` to `PeerId`.
*
Expand Down

0 comments on commit 599f5e8

Please sign in to comment.