|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const indentString = require('indent-string') |
| 5 | +const terminalLink = require('terminal-link') |
| 6 | +const beautyError = require('beauty-error') |
| 7 | +const prettyBytes = require('pretty-bytes') |
| 8 | +const existsFile = require('exists-file') |
| 9 | +const clipboardy = require('clipboardy') |
| 10 | +const sizeof = require('object-sizeof') |
| 11 | +const prettyMs = require('pretty-ms') |
| 12 | +const timeSpan = require('time-span') |
| 13 | +const { isEmpty } = require('lodash') |
| 14 | +const path = require('path') |
| 15 | + |
| 16 | +const { gray, jsome, gradient, pink } = require('./colors') |
| 17 | + |
| 18 | +const pkg = require('../package.json') |
| 19 | + |
| 20 | +require('update-notifier')({ pkg }).notify() |
| 21 | + |
| 22 | +const cli = require('meow')({ |
| 23 | + pkg, |
| 24 | + description: false, |
| 25 | + help: require('./help') |
| 26 | +}) |
| 27 | + |
| 28 | +const exitOnError = err => { |
| 29 | + console.log() |
| 30 | + console.error(beautyError(err)) |
| 31 | + process.exit(1) |
| 32 | +} |
| 33 | + |
| 34 | +const main = async () => { |
| 35 | + const [exampleName] = cli.input |
| 36 | + |
| 37 | + if (!exampleName) cli.showHelp() |
| 38 | + |
| 39 | + const filepath = path.resolve(__dirname, '../examples', exampleName) |
| 40 | + |
| 41 | + if (!existsFile.sync(filepath)) { |
| 42 | + throw TypeError(`Example '${exampleName}' not exist.`) |
| 43 | + } |
| 44 | + |
| 45 | + const fn = require(filepath) |
| 46 | + |
| 47 | + if (isEmpty(cli.flags)) { |
| 48 | + console.log(gray(`\n${indentString(fn.help, 2)}`)) |
| 49 | + console.log(gray(` \n Flags${indentString(fn.flags, 2)}`)) |
| 50 | + process.exit() |
| 51 | + } |
| 52 | + |
| 53 | + const end = timeSpan() |
| 54 | + return [await fn({ query: cli.flags }), end()] |
| 55 | +} |
| 56 | + |
| 57 | +main() |
| 58 | + .then(([data, time]) => { |
| 59 | + jsome(data) |
| 60 | + |
| 61 | + if (!cli.flags.quiet) { |
| 62 | + console.log(`\n `, gradient('Microlink Query Language'), '\n') |
| 63 | + console.log( |
| 64 | + ` ${pink('source:')} ${terminalLink( |
| 65 | + `examples/${cli.input[0]}`, |
| 66 | + `https://github.com/microlinkhq/mql/tree/master/examples/${ |
| 67 | + cli.input[0] |
| 68 | + }` |
| 69 | + )}` |
| 70 | + ) |
| 71 | + console.log(` ${pink('status:')} success`) |
| 72 | + console.log(` ${pink('size:')} ${prettyBytes(sizeof(data))}`) |
| 73 | + console.log(` ${pink('time:')} ${prettyMs(time)}`) |
| 74 | + } |
| 75 | + |
| 76 | + if (cli.flags.copy) { |
| 77 | + clipboardy.writeSync(JSON.stringify(data, null, 2)) |
| 78 | + console.log(`\n ${gray('Copied to clipboard!')}`) |
| 79 | + } |
| 80 | + |
| 81 | + process.exit() |
| 82 | + }) |
| 83 | + .catch(exitOnError) |
0 commit comments