Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
81 lines (70 loc) · 2.48 KB
/
Copy pathcli.js
File metadata and controls
81 lines (70 loc) · 2.48 KB
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
import fs from 'fs-extra'
import sade from 'sade'
import { green, yellow, red, gray } from 'ansi-colors'
import { loadConfig } from './config'
import * as pkg from './package.json'
import { refresh } from './tools'
import { setWatch } from './watcher'
const prog = sade('trim').version(pkg.version)
const potential_settings = ['input', 'outputSvelte', 'outputStatic']
const white_list = ['postcss', 'svgo', 'remark', 'lilypond', 'json']
for (let command of ['watch', 'build']) {
const command_name = command.replace(/./, ch => ch.toUpperCase())
prog
.command(command)
.describe(`${command_name} specified Sapper "trimming" compilers`)
.option('-e, --env', 'Use .env file')
.option('-p, --postcss', 'Use PostCSS for preprocessing', false)
.option('-s, --svgo', 'Use SVGO compiler for svgs', false)
.option('-r, --remark', 'Use Remark compiler for markdown', false)
.option('-l, --lilypond', 'Use LilyPond compiler for sheet music', false)
.option('-j, --json', 'Export generated *.json files from *.js files', false)
.action(async(opts) => {
console.log()
if (opts.env) {
require('dotenv').config({ path: opts.env })
console.log('~>', yellow('Preloading environment variables from:'), opts.env)
}
const config = loadConfig()
try {
if (command === 'watch') {
await refresh()
}
// here we:
// 1. check if the named option is set and white listed (i.e., postcss, svgo, etc.)
for (let name of white_list.filter(item => opts[item])) {
for (let potentialDir in config[name]) {
// 2. check if `mkdir` needs to be employed (for different dirs)
if (potential_settings.indexOf(potentialDir) > -1) {
// 3. if so, do it...
const dir = config[name][potentialDir]
if (dir) {
await fs.ensureDir(dir)
}
}
}
if (command === 'watch') {
console.log('~>', green('Watching Sapper trimming:'), name)
setWatch(name, config)
} else if (command === 'build') {
console.log('\n~>', green('Building Sapper trimming:'), name)
const { change } = await require(`../${name}`)
if (name === 'postcss') {
change({ config })
} else {
console.log('\n~>', yellow(
`There is no build available yet for the ${name} option, \
but that will come in the future.`
))
}
}
}
} catch (err) {
console.log(red(`> ${err.message}`))
console.log(gray(err.stack))
process.exit(1)
}
console.log()
})
}
prog.parse(process.argv)