diff --git a/lib/cmd.js b/lib/cmd.js index 925f022..186a400 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -1,6 +1,7 @@ +const yargonaut = require('yargonaut') const yargs = require('yargs') const util = require('util') -const chalk = require('chalk') +const chalk = yargonaut.chalk() const config = require('./config') const { __ } = require('./i18n') @@ -75,7 +76,8 @@ exports = module.exports = yargs .options({ 'dryrun': { description: chalk.cyan.bold(__('set dry run mode')), - type: 'boolean' + type: 'boolean', + alias: 'd', } }) .options({ @@ -87,7 +89,8 @@ exports = module.exports = yargs .options({ 'nocolor': { description: chalk.cyan.bold(__('disable color mode')), - type: 'boolean' + type: 'boolean', + alias: 'n', } }) .options({ diff --git a/lib/config.js b/lib/config.js index dfdb6a1..62eb8e0 100644 --- a/lib/config.js +++ b/lib/config.js @@ -3,7 +3,8 @@ const fs = require('fs') const _ = require('lodash') const YAML = require('yamljs') const tty = require('tty') -const chalk = require('chalk') +const yargonaut = require('yargonaut') +const chalk = yargonaut.chalk() const pkg = require('../package') const isWin = /^win/.test(process.platform) const userHome = process.env[isWin ? 'USERPROFILE' : 'HOME'] diff --git a/lib/grql.js b/lib/grql.js index 9e8236f..9f47a0b 100644 --- a/lib/grql.js +++ b/lib/grql.js @@ -3,7 +3,8 @@ const util = require('util') const { Writable } = require('stream') const Promise = require('bluebird') const _ = require('lodash') -const chalk = require('chalk') +const yargonaut = require('yargonaut') +const chalk = yargonaut.chalk() const Lokka = require('lokka').Lokka const { Transport } = require('lokka-transport-http') const basicAuthHeader = require('basic-auth-header') @@ -66,25 +67,9 @@ const grql = (...args) => Promise.resolve() Object.assign(env, _.pickBy(_.pick(argv, config.envFields), _.identity)) const aliasMap = {query: 'queries', 'mutate': 'mutations'} const aliasKey = aliasMap[command] - const data = (function loadData() { - let data = argv._[1] || - config.stdinContent || - (aliasKey && _.get(config.settings, `${aliasKey}.${argv.alias}`)) - if (aliasKey) { - data = (function loadFragment(data) { - const fragmentRe = /(\$\{[^\}]+\})/g - const fragmentMatch = data.match(fragmentRe) || [] - return fragmentMatch.reduce((data, fragmentRef) => { - const fragmentNameMatch = fragmentRef.match(/\$\{([^\}]+)\}/) - const fragmentName = fragmentNameMatch && fragmentNameMatch[1] - const fragment = fragmentName && - client.createFragment(_.get(config.settings, `fragments.${fragmentName}`)) - return fragment ? data.replace(fragmentRef, fragment) : data - }, data) - })(data) - } - return data - })() + const data = argv._[1] || + config.stdinContent || + (aliasKey && _.get(config.settings, `${aliasKey}.${argv.alias}`)) if (argv.alias && aliasKey) { _.set(config.settings, `${aliasKey}.${argv.alias}`, data) } @@ -97,6 +82,7 @@ const grql = (...args) => Promise.resolve() if (argv.verbose) { h.print.out(chalk.gray(__('environment details :'))) h.render(_.omit(env, 'password'), renderOpts) + h.print.out(chalk.gray(__('command : ') + chalk.white.bold(command))) h.print.out(chalk.gray(__('request headers :'))) h.render(_.get(transport, '_httpOptions.headers'), renderOpts) h.print.out(chalk.gray(__('data :'))) @@ -131,7 +117,7 @@ const grql = (...args) => Promise.resolve() h.render(_.get(config.settings, `mutations`), renderOpts) } if (!argv.save) { - throw new Error(__('Error : missing argument')) + throw new Error(__('Error : missing argument (try --help)')) } } if (argv.dryrun) { @@ -163,7 +149,7 @@ const grql = (...args) => Promise.resolve() h.render(result, {yaml: argv.yaml, noColor: argv.nocolor}) }) case 'mutate': - return client.query(data, vars) + return client.mutate(data, vars) .then(result => { h.render(result, renderOpts) }) diff --git a/lib/helper.js b/lib/helper.js index f6fb700..fe24fd0 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -1,17 +1,18 @@ const Promise = require('bluebird') -const chalk = require('chalk') +const yargonaut = require('yargonaut') +const chalk = yargonaut.chalk() const util = require('util') const prettyjson = require('prettyjson') const { __ } = require('./i18n') const h = { + stdout: process.stdout, + stderr: process.stderr, exit: (exitCode = 0) => { process.nextTick(() => { process.exit(exitCode) }) }, - stdout: process.stdout, - stderr: process.stderr, print: { out: (...args) => { const msg = util.format(...args) @@ -48,9 +49,6 @@ const h = { process.stdin.destroy() }) }, - toPrettyJson: (data, noColor) => prettyjson.render(data, { - noColor, emptyArrayMsg: __('(empty array)') - }), render: (data, opt = {}) => { const { yaml, noColor } = opt if (yaml) { @@ -60,7 +58,13 @@ const h = { } else { h.print.out(chalk.bold(JSON.stringify(data, null, 2))) } - } + }, + toPrettyJson: (data, noColor) => (typeof data === 'undefined' || data === null ? + __('null') : + prettyjson.render(data, { + noColor, emptyArrayMsg: __('(empty array)') + }) + ), } exports = module.exports = h diff --git a/locales/en.json b/locales/en.json index 15abf61..e7bbec3 100644 --- a/locales/en.json +++ b/locales/en.json @@ -1,33 +1,33 @@ { - "for more information, contact": "for more information, contact openhoat@gmail.com", - "Usage": "Usage", "(empty array)": "(empty array)", - "select environment": "select environment", - "basic authentication user": "basic authentication user", + "Error : missing argument (try --help)": "Error : missing argument (try --help)", + "Error: query '%s' not found": "Error: query '%s' not found", + "Usage": "Usage", "basic authentication password": "basic authentication password", + "basic authentication user": "basic authentication user", + "command": "command", + "create a fragment": "create a fragment", + "data": "data", + "data :": "data :", + "disable color mode": "disable color mode", + "empty": "empty", + "environment details :": "environment details :", + "environments :": "environments :", + "for more information, contact": "for more information, contact openhoat@gmail.com", "get or set query name": "get or set query name", - "persists options to file (%s)": "persists options to file (%s)", - "show result in pretty YAML format": "show result in pretty YAML format", "graphql server base url": "graphql server base url", - "Error: query '%s' not found": "Error: query '%s' not found", + "make a graphql query": "make a graphql query", "options successfully saved": "options successfully saved", - "set verbose mode": "set verbose mode", + "persists options to file (%s)": "persists options to file (%s)", + "request headers :": "request headers :", + "select environment": "select environment", + "send graphql mutation": "send graphql mutation", + "set a variable used in query (format : key=value)": "set a variable used in query (format : key=value)", "set dry run mode": "set dry run mode", + "set verbose mode": "set verbose mode", + "show result in pretty YAML format": "show result in pretty YAML format", "show version": "show version", - "environments :": "environments :", - "disable color mode": "disable color mode", - "Error : missing argument": "Error : missing argument", - "set a variable used in query (format : key=value)": "set a variable used in query (format : key=value)", - "create a fragment": "create a fragment", - "send graphql mutation": "send graphql mutation", - "command": "command", - "data": "data", - "make a graphql query": "make a graphql query", - "empty": "empty", + "use or set query alias": "use or set query alias", "using environment %s": "using environment %s", - "environment details :": "environment details :", - "request headers :": "request headers :", - "data :": "data :", - "variables :": "variables :", - "use or set query alias": "use or set query alias" + "variables :": "variables :" } diff --git a/locales/fr.json b/locales/fr.json index 0c1e92b..e393624 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -1,33 +1,33 @@ { - "for more information, contact": "pour plus d'information, contacter openhoat@gmail.com", - "Usage": "Utilisation", "(empty array)": "(liste vide)", - "select environment": "sélectionne l'environnement", - "basic authentication user": "utilisateur de l'authentification basique", + "Error : missing argument (try --help)": "Erreur : argument manquant (essayer --help)", + "Error: query '%s' not found": "Erreur : requête '%s' non trouvée", + "Usage": "Utilisation", "basic authentication password": "mot de passe de l'authentification basique", + "basic authentication user": "utilisateur de l'authentification basique", + "command": "commande", + "create a fragment": "crée un fragment", + "data": "données", + "data :": "données :", + "disable color mode": "désactive le mode couleur", + "empty": "vide", + "environment details :": "détail de l'environnement :", + "environments :": "environnements :", + "for more information, contact": "pour plus d'information, contacter openhoat@gmail.com", "get or set query name": "nom de la requête à définir ou utiliser", - "save options to file (%s)": "savegarde les options dans le fichier (%s)", - "show result in pretty YAML format": "montre le résultat au format YAML", "graphql server base url": "url de base du serveur graphql", - "Error: query '%s' not found": "Erreur : requête '%s' non trouvée", + "make a graphql query": "joue une requête graphql", "options successfully saved": "options enregistrées avec succès", - "set verbose mode": "active le mode verbose", + "request headers :": "entêtes de requête :", + "save options to file (%s)": "savegarde les options dans le fichier (%s)", + "select environment": "sélectionne l'environnement", + "send graphql mutation": "envoie une mutation graphql", + "set a variable used in query (format : key=value)": "définit une variable à utiliser dans la requête (format : key=value)", "set dry run mode": "active le mode dry run", + "set verbose mode": "active le mode verbose", + "show result in pretty YAML format": "montre le résultat au format YAML", "show version": "affiche la version", - "environments :": "environnements :", - "disable color mode": "désactive le mode couleur", - "Error : missing argument": "Erreur : argument manquant", - "set a variable used in query (format : key=value)": "définit une variable à utiliser dans la requête (format : key=value)", - "create a fragment": "crée un fragment", - "send graphql mutation": "envoie une mutation graphql", - "command": "commande", - "data": "données", - "make a graphql query": "joue une requête graphql", - "empty": "vide", + "use or set query alias": "utilise ou définit un alias de requête", "using environment %s": "utilisation de l'environment %s", - "environment details :": "détail de l'environnement :", - "request headers :": "entêtes de requête :", - "data :": "données :", - "variables :": "variables :", - "use or set query alias": "utilise ou définit un alias de requête" + "variables :": "variables :" } diff --git a/package.json b/package.json index 18cf90e..2aeda3a 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "dependencies": { "basic-auth-header": "^1.0.1", "bluebird": "^3.5.0", - "chalk": "^1.1.3", "fetch-graphql-schema": "^0.2.1", "lodash": "^4.17.4", "lokka": "^1.7.0", @@ -47,6 +46,7 @@ "prettyjson": "^1.2.1", "y18n": "^3.2.1", "yamljs": "^0.2.10", + "yargonaut": "^1.1.2", "yargs": "^8.0.1" }, "devDependencies": { diff --git a/test/grql-test.js b/test/grql-test.js index 5c4aed9..348e7c7 100644 --- a/test/grql-test.js +++ b/test/grql-test.js @@ -36,7 +36,7 @@ describe('grql', function() { }, err => { expect(err).to.be.an('error') - expect(err).to.have.property('message', grql.__('Error : missing argument')) + expect(err).to.have.property('message', grql.__('Error : missing argument (try --help)')) } ) ) diff --git a/yarn.lock b/yarn.lock index fecc24a..5372c34 100644 --- a/yarn.lock +++ b/yarn.lock @@ -747,6 +747,10 @@ fetch-graphql-schema@^0.2.1: mkdirp "^0.5.1" node-fetch "^1.6.3" +figlet@^1.1.1: + version "1.2.0" + resolved "https://registry.yarnpkg.com/figlet/-/figlet-1.2.0.tgz#6c46537378fab649146b5a6143dda019b430b410" + figures@^1.3.5: version "1.7.0" resolved "https://registry.yarnpkg.com/figures/-/figures-1.7.0.tgz#cbe1e3affcf1cd44b80cadfed28dc793a9701d2e" @@ -979,14 +983,10 @@ http-signature@~1.1.0: jsprim "^1.2.2" sshpk "^1.7.0" -iconv-lite@0.4.15: +iconv-lite@0.4.15, iconv-lite@~0.4.13: version "0.4.15" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.15.tgz#fe265a218ac6a57cfe854927e9d04c19825eddeb" -iconv-lite@~0.4.13: - version "0.4.17" - resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.17.tgz#4fdaa3b38acbc2c031b045d0edcdfe1ecab18c8d" - ignore@^3.2.0: version "3.3.3" resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.3.tgz#432352e57accd87ab3110e82d3fea0e47812156d" @@ -1582,6 +1582,10 @@ p-locate@^2.0.0: dependencies: p-limit "^1.1.0" +parent-require@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/parent-require/-/parent-require-1.0.0.tgz#746a167638083a860b0eef6732cb27ed46c32977" + parse-json@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-2.2.0.tgz#f480f40434ef80741f8469099f8dea18f55a4dc9" @@ -2254,6 +2258,14 @@ yamljs@^0.2.10: argparse "^1.0.7" glob "^7.0.5" +yargonaut@^1.1.2: + version "1.1.2" + resolved "https://registry.yarnpkg.com/yargonaut/-/yargonaut-1.1.2.tgz#ee7b89e98121a3f241fa926a2a6e1b6641c81b3f" + dependencies: + chalk "^1.1.1" + figlet "^1.1.1" + parent-require "^1.0.0" + yargs-parser@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-7.0.0.tgz#8d0ac42f16ea55debd332caf4c4038b3e3f5dfd9"