-
Notifications
You must be signed in to change notification settings - Fork 10
/
cli.js
executable file
·128 lines (108 loc) · 3.68 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const _ = require('lodash')
const meow = require('meow')
const chalk = require('chalk')
const mkdirp = require('mkdirp')
const dedent = require('dedent')
const isBlank = require('is-blank')
const isPresnet = require('is-present')
const fileExists = require('file-exists')
const cssstats = require('cssstats')
const trailingLines = require('single-trailing-newline')
const authorsToMd = require('authors-to-markdown')
const tachyonsBuildCss = require('tachyons-build-css')
const cli = meow(`
Usage
$ tachyons <input.css>
Options
-m, --minify Minify the output stylesheet
-r, --repeat Repeat class names to increase specificity
-a, --authors Dynamically add authors based on package.json
-n, --new Generate a new Tachyons project
--rtl Generate rtl supported css
--generate-docs Generate documentation for a given module
--package The path to the module package to be documented
--preserve-variables Preserve CSS variables in output
Example
$ tachyons src/tachyons.css > dist/c.css
$ tachyons src/tachyons.css > dist/c.css --minify
$ tachyons src/tachyons.css > dist/c.repeated.css --repeat
$ tachyons src/tachyons-type-scale.css --generate-docs --package=./package.json > readme.md
$ tachyons --new=my-new-project
`, {
alias: {
m: 'minify',
r: 'repeat',
a: 'authors',
n: 'new'
}
})
const inputFile = cli.input[0]
const outputFile = cli.input[1]
if (cli.flags.new) {
console.log('Generating a new Tachyons project')
const projDir = cli.flags.new == true ? 'tachyons-project' : cli.flags.new
mkdirp.sync(projDir)
mkdirp.sync(projDir + '/src')
mkdirp.sync(projDir + '/css')
const index = fs.readFileSync(__dirname + '/templates/new/index.html', 'utf8')
const pkg = fs.readFileSync(__dirname + '/templates/new/package.json', 'utf8')
const readme = fs.readFileSync(__dirname + '/templates/new/readme.md', 'utf8')
const style = fs.readFileSync(__dirname + '/templates/new/src/styles.css', 'utf8')
fs.writeFileSync(projDir + '/index.html', index)
fs.writeFileSync(projDir + '/package.json', pkg)
fs.writeFileSync(projDir + '/readme.md', readme)
fs.writeFileSync(projDir + '/src/styles.css', style)
console.log('New project located in ' + projDir)
process.exit(0)
}
if (isBlank(inputFile)) {
console.error(chalk.red('Please provide an input stylesheet'))
console.log(cli.help)
process.exit(1)
} else if (!fileExists(inputFile)) {
console.error(chalk.red('File does not exist ' + inputFile))
console.log(cli.help)
process.exit(1)
}
const input = fs.readFileSync(inputFile, 'utf8')
tachyonsBuildCss(input, {
from: inputFile,
to: outputFile,
rtl: cli.flags.rtl,
minify: cli.flags.minify,
repeat: cli.flags.repeat,
preserveVariables: cli.flags.preserveVariables
}).then(function (result) {
if (cli.flags.generateDocs) {
const stats = cssstats(result.css)
const pkg = require(cli.flags.package)
const template = fs.readFileSync(__dirname + '/templates/readme.md', 'utf8')
const tpl = _.template(template)
let authors = `* [mrmrs](http://mrmrs.io)
* [johno](http://johnotander.com)
`
if (cli.flags.authors) {
authors = authorsToMd(pkg)
}
const srcMd = /^\/\*!!![\s\S]*?\*\/.*/.exec(input)
const defaultMd = `
# ${pkg.name} ${pkg.version}
${pkg.description}
`
const md = tpl({
stats,
authors,
module: pkg,
srcMd: dedent(srcMd && srcMd[0] || defaultMd).replace(/^\/\*!!!/, '').replace(/\*\/$/, ''),
srcCss: trailingLines(result.css)
})
console.log(trailingLines(md))
process.exit(0)
} else {
console.log(trailingLines(result.css))
process.exit(0)
}
})