UNPKG

chenv

Version:

cli to manage Chrome Web Store item

252 lines (240 loc) 6.89 kB
#!/usr/bin/env node 'use strict' function _interopDefault(ex) { return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex } var program = _interopDefault(require('commander')) var enquirer = _interopDefault(require('enquirer')) var opn = _interopDefault(require('opn')) var dotenv = _interopDefault(require('dotenv')) var path = require('path') var __ = require('..') var packageJson = _interopDefault(require('../package.json')) function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }) } else { obj[key] = value } return obj } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {} var ownKeys = Object.keys(source) if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat( Object.getOwnPropertySymbols(source).filter(function(sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable }) ) } ownKeys.forEach(function(key) { _defineProperty(target, key, source[key]) }) } return target } const cwd = process.cwd() const CONFIG = 'chenv.config.js' const loadCredentials = envValue => { const { error } = dotenv.config({ path: envValue && typeof envValue === 'string' ? envValue : path.join(cwd, '.env') }) if (error) console.warn(error.message) const { CLIENT_ID: client_id, CLIENT_SECRET: client_secret, REFRESH_TOKEN: refresh_token } = process.env return { client_id, client_secret, refresh_token } } const loadConfig = configValue => { let config = {} if (configValue) { config = require(path.join(cwd, configValue)) } else { try { config = require(path.join(cwd, CONFIG)) } catch (err) { if (!err.message.includes(CONFIG)) throw err try { const { chenv = {} } = require(path.join(cwd, 'package.json')) || {} config = chenv } catch (err) { if (!err.message.includes('package.json')) throw err } } } return 'default' in config ? config['default'] : config } const action = action => Promise.resolve() .then(action) .catch(err => { console.error(err) process.exit(1) }) const promiseOrder = (args, action, index = 0) => args[index] && Promise.resolve() .then(() => action(args[index], index, args)) .then(() => promiseOrder(args, action, index + 1)) const order = (items, action) => !items.length ? console.log('\n > no item\n') : promiseOrder(items, async (item, index, items) => { console.log(item) await action(item, index, items) return index !== items.length - 1 && console.log('-') }) const auth = ({ app, env }) => action(async () => { const { client_id, client_secret } = loadCredentials(env) if (!client_id || !client_secret) { return console.log('\n > Please set CLIENT_ID and CLIENT_SECRET\n') } const auth_uri = __.authURL(client_id) opn(auth_uri, { wait: false, app: app ? app.split(',') : undefined }) const { code } = await enquirer.prompt({ name: 'code', type: 'input', message: 'Tell me code by authorize:' + '\n' + auth_uri + '\n' }) const refresh_token = await __.getRefreshToken({ client_id, client_secret, code }) return console.log(`\n > REFRESH_TOKEN=${refresh_token}\n`) }) const upload = (src, id, options) => action(() => { const { aliasName, all, publish, publishTt, config, env } = options const chenv = new __.Chenv(loadCredentials(env)) const { items: itemMap = {} } = loadConfig(config) const items = [ { src, id }, ...createItems(itemMap, all, aliasName) ].filter(({ src }) => src) return order(items, ({ src, id }) => !id ? chenv.insertItem(src).then(console.log) : chenv .updateItem(id, src) .then(console.log) .then(() => publish ? chenv.publishItem(id, false).then(console.log) : publishTt ? chenv.publishItem(id, true).then(console.log) : false ) ) }) const remove = (id, options) => action(() => { const { aliasName, all, config, env } = options const chenv = new __.Chenv(loadCredentials(env)) const { items: itemMap = {} } = loadConfig(config) const items = [ { id }, ...createItems(itemMap, all, aliasName) ].filter(({ id }) => id) return order(items, ({ id }) => chenv.removeItem(id).then(console.log)) }) const createItems = (map, all, aliasName) => all ? map2items(map) : alias2items(aliasName, map) const map2items = map => Object.entries(map).map(([name, item]) => _objectSpread( { name }, item ) ) const alias2items = (aliasName, map) => !aliasName ? [] : aliasName .split(',') .filter(name => name) .map(name => _objectSpread( { name }, map[name] ) ) const options = { env: ['-e, --env <file>', 'env filepath (default: ".env")'], app: ['--app <name..>', 'specify the app to open the url (sindresorhus/opn)'], aliasName: ['-a, --alias-name <name..>', 'config.alias[name] will be used'], all: ['--all', 'config.alias will be used'], config: [ '-c, --config <file>', `config filepath (default: "${CONFIG}" || package.json)` ], publish: [ '-p, --publish', `(ignored when !id) publish item directly after update` ], publishTt: [ '-t, --publish-tt', `(ignored when !id) publish item directly after update to only "trustedTesters"` ], draft: ['-d, --draft', `projection=DRAFT`], published: ['-p, --published', `projection=PUBLISHED`] } program.version(packageJson.version) program .command(`auth`) .description('get REFRESH_TOKEN easily') .option(...options['app']) .option(...options['env']) .action(auth) program .command(`upload [src] [id]`) .description('upload item (!id ? insert : update)') .option(...options['aliasName']) .option(...options['all']) .option(...options['publish']) .option(...options['publishTt']) .option(...options['config']) .option(...options['env']) .action(upload) program .command(`remove [id]`) .description('not remove but update item as "removed-like"') .option(...options['aliasName']) .option(...options['all']) .option(...options['config']) .option(...options['env']) .action(remove) program.on('--help', () => console.log('')) program.parse(process.argv) if (!program.args.length) { program.help() }