Skip to content

Commit 2322cb8

Browse files
committed
Move args parsing to seperate file
1 parent adb1acd commit 2322cb8

File tree

2 files changed

+127
-124
lines changed

2 files changed

+127
-124
lines changed

index.js

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -15,132 +15,9 @@ const postcss = require('postcss')
1515
const postcssrc = require('postcss-load-config')
1616
const reporter = require('postcss-reporter/lib/formatter')()
1717

18+
const argv = require('./lib/args')
1819
const depGraph = require('./lib/depGraph')
1920

20-
const logo = `
21-
/|\\
22-
// //
23-
// //
24-
//___*___*___//
25-
//--*---------*--//
26-
/|| * * ||/
27-
// ||* *|| //
28-
// || * * || //
29-
//_____||___*_________*___||_____//
30-
`
31-
32-
const version = () => {
33-
const cli = require('./package.json').version
34-
35-
return chalk.bold.red(`
36-
/|\\
37-
// //
38-
// //
39-
//___*___*___//
40-
//--*---------*--//
41-
/|| * * ||/
42-
// ||* v${cli} *|| //
43-
// || * * || //
44-
//_____||___*_________*___||_____//
45-
`)
46-
}
47-
48-
const argv = require('yargs')
49-
.usage(
50-
`${chalk.bold.red(logo)}
51-
Usage:
52-
53-
$0 [input.css] [OPTIONS] [--output|-o output.css] [--watch]`
54-
)
55-
.option('o', {
56-
alias: 'output',
57-
desc: 'Output file',
58-
type: 'string'
59-
})
60-
.option('d', {
61-
alias: 'dir',
62-
desc: 'Output directory',
63-
type: 'string'
64-
})
65-
.option('r', {
66-
alias: 'replace',
67-
desc: 'Replace (overwrite) the input file',
68-
type: 'boolean'
69-
})
70-
.option('u', {
71-
alias: 'use',
72-
desc: 'List of postcss plugins to use',
73-
type: 'array'
74-
})
75-
.option('p', {
76-
alias: 'parser',
77-
desc: 'Custom postcss parser',
78-
type: 'string'
79-
})
80-
.option('t', {
81-
alias: 'stringifier',
82-
desc: 'Custom postcss stringifier',
83-
type: 'string'
84-
})
85-
.option('s', {
86-
alias: 'syntax',
87-
desc: 'Custom postcss syntax',
88-
type: 'string'
89-
})
90-
.option('w', {
91-
alias: 'watch',
92-
desc: 'Watch files for changes and recompile as needed',
93-
type: 'boolean'
94-
})
95-
.option('poll', {
96-
desc:
97-
'Use polling for file watching. Can optionally pass polling interval; default 100 ms'
98-
})
99-
.option('x', {
100-
alias: 'ext',
101-
desc: 'Override the output file extension',
102-
type: 'string',
103-
coerce(ext) {
104-
if (ext.indexOf('.') !== 0) return `.${ext}`
105-
return ext
106-
}
107-
})
108-
.option('e', {
109-
alias: 'env',
110-
desc: 'A shortcut for setting NODE_ENV',
111-
type: 'string'
112-
})
113-
.option('b', {
114-
alias: 'base',
115-
desc:
116-
'Mirror the directory structure relative to this path in the output directory, this only works together with --dir',
117-
type: 'string'
118-
})
119-
.option('c', {
120-
alias: 'config',
121-
desc: 'Set a custom path to look for a config file',
122-
type: 'string'
123-
})
124-
.alias('m', 'map')
125-
.describe('m', 'Create an external sourcemap')
126-
.describe('no-map', 'Disable the default inline sourcemaps')
127-
.version(version)
128-
.alias('v', 'version')
129-
.help('h')
130-
.alias('h', 'help')
131-
.example('$0 input.css -o output.css', 'Basic usage')
132-
.example(
133-
'cat input.css | $0 -u autoprefixer > output.css',
134-
'Piping input & output'
135-
)
136-
.epilog(
137-
`If no input files are passed, it reads from stdin. If neither -o, --dir, or --replace is passed, it writes to stdout.
138-
139-
If there are multiple input files, the --dir or --replace option must be passed.
140-
141-
For more details, please see https://github.com/postcss/postcss-cli`
142-
).argv
143-
14421
const dir = argv.dir
14522

14623
let input = argv._

lib/args.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
'use strict'
2+
const chalk = require('chalk')
3+
4+
const logo = `
5+
/|\\
6+
// //
7+
// //
8+
//___*___*___//
9+
//--*---------*--//
10+
/|| * * ||/
11+
// ||* *|| //
12+
// || * * || //
13+
//_____||___*_________*___||_____//
14+
`
15+
16+
const version = () => {
17+
const cli = require('../package.json').version
18+
19+
return chalk.bold.red(`
20+
/|\\
21+
// //
22+
// //
23+
//___*___*___//
24+
//--*---------*--//
25+
/|| * * ||/
26+
// ||* v${cli} *|| //
27+
// || * * || //
28+
//_____||___*_________*___||_____//
29+
`)
30+
}
31+
32+
module.exports = require('yargs')
33+
.usage(
34+
`${chalk.bold.red(logo)}
35+
Usage:
36+
37+
$0 [input.css] [OPTIONS] [--output|-o output.css] [--watch]`
38+
)
39+
.option('o', {
40+
alias: 'output',
41+
desc: 'Output file',
42+
type: 'string'
43+
})
44+
.option('d', {
45+
alias: 'dir',
46+
desc: 'Output directory',
47+
type: 'string'
48+
})
49+
.option('r', {
50+
alias: 'replace',
51+
desc: 'Replace (overwrite) the input file',
52+
type: 'boolean'
53+
})
54+
.option('u', {
55+
alias: 'use',
56+
desc: 'List of postcss plugins to use',
57+
type: 'array'
58+
})
59+
.option('p', {
60+
alias: 'parser',
61+
desc: 'Custom postcss parser',
62+
type: 'string'
63+
})
64+
.option('t', {
65+
alias: 'stringifier',
66+
desc: 'Custom postcss stringifier',
67+
type: 'string'
68+
})
69+
.option('s', {
70+
alias: 'syntax',
71+
desc: 'Custom postcss syntax',
72+
type: 'string'
73+
})
74+
.option('w', {
75+
alias: 'watch',
76+
desc: 'Watch files for changes and recompile as needed',
77+
type: 'boolean'
78+
})
79+
.option('poll', {
80+
desc:
81+
'Use polling for file watching. Can optionally pass polling interval; default 100 ms'
82+
})
83+
.option('x', {
84+
alias: 'ext',
85+
desc: 'Override the output file extension',
86+
type: 'string',
87+
coerce(ext) {
88+
if (ext.indexOf('.') !== 0) return `.${ext}`
89+
return ext
90+
}
91+
})
92+
.option('e', {
93+
alias: 'env',
94+
desc: 'A shortcut for setting NODE_ENV',
95+
type: 'string'
96+
})
97+
.option('b', {
98+
alias: 'base',
99+
desc:
100+
'Mirror the directory structure relative to this path in the output directory, this only works together with --dir',
101+
type: 'string'
102+
})
103+
.option('c', {
104+
alias: 'config',
105+
desc: 'Set a custom path to look for a config file',
106+
type: 'string'
107+
})
108+
.alias('m', 'map')
109+
.describe('m', 'Create an external sourcemap')
110+
.describe('no-map', 'Disable the default inline sourcemaps')
111+
.version(version)
112+
.alias('v', 'version')
113+
.help('h')
114+
.alias('h', 'help')
115+
.example('$0 input.css -o output.css', 'Basic usage')
116+
.example(
117+
'cat input.css | $0 -u autoprefixer > output.css',
118+
'Piping input & output'
119+
)
120+
.epilog(
121+
`If no input files are passed, it reads from stdin. If neither -o, --dir, or --replace is passed, it writes to stdout.
122+
123+
If there are multiple input files, the --dir or --replace option must be passed.
124+
125+
For more details, please see https://github.com/postcss/postcss-cli`
126+
).argv

0 commit comments

Comments
 (0)