.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / node_modules / meow / index.js
1 'use strict';
2 const path = require('path');
3 const buildMinimistOptions = require('minimist-options');
4 const minimist = require('minimist');
5 const camelcaseKeys = require('camelcase-keys');
6 const decamelizeKeys = require('decamelize-keys');
7 const trimNewlines = require('trim-newlines');
8 const redent = require('redent');
9 const readPkgUp = require('read-pkg-up');
10 const loudRejection = require('loud-rejection');
11 const normalizePackageData = require('normalize-package-data');
12
13 // Prevent caching of this module so module.parent is always accurate
14 delete require.cache[__filename];
15 const parentDir = path.dirname(module.parent.filename);
16
17 module.exports = (helpMessage, opts) => {
18         loudRejection();
19
20         if (typeof helpMessage === 'object' && !Array.isArray(helpMessage)) {
21                 opts = helpMessage;
22                 helpMessage = '';
23         }
24
25         opts = Object.assign({
26                 pkg: readPkgUp.sync({
27                         cwd: parentDir,
28                         normalize: false
29                 }).pkg || {},
30                 argv: process.argv.slice(2),
31                 inferType: false,
32                 input: 'string',
33                 help: helpMessage,
34                 autoHelp: true,
35                 autoVersion: true
36         }, opts);
37
38         let minimistOpts = Object.assign({
39                 arguments: opts.input
40         }, opts.flags);
41
42         minimistOpts = decamelizeKeys(minimistOpts, '-', {exclude: ['stopEarly', '--']});
43
44         if (opts.inferType) {
45                 delete minimistOpts.arguments;
46         }
47
48         minimistOpts = buildMinimistOptions(minimistOpts);
49
50         const pkg = opts.pkg;
51         const argv = minimist(opts.argv, minimistOpts);
52         let help = redent(trimNewlines((opts.help || '').replace(/\t+\n*$/, '')), 2);
53
54         normalizePackageData(pkg);
55
56         process.title = pkg.bin ? Object.keys(pkg.bin)[0] : pkg.name;
57
58         let description = opts.description;
59         if (!description && description !== false) {
60                 description = pkg.description;
61         }
62
63         help = (description ? `\n  ${description}\n` : '') + (help ? `\n${help}\n` : '\n');
64
65         const showHelp = code => {
66                 console.log(help);
67                 process.exit(typeof code === 'number' ? code : 2);
68         };
69
70         const showVersion = () => {
71                 console.log(typeof opts.version === 'string' ? opts.version : pkg.version);
72                 process.exit();
73         };
74
75         if (argv.version && opts.autoVersion) {
76                 showVersion();
77         }
78
79         if (argv.help && opts.autoHelp) {
80                 showHelp(0);
81         }
82
83         const input = argv._;
84         delete argv._;
85
86         const flags = camelcaseKeys(argv, {exclude: ['--', /^\w$/]});
87
88         return {
89                 input,
90                 flags,
91                 pkg,
92                 help,
93                 showHelp,
94                 showVersion
95         };
96 };