-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
85 lines (84 loc) · 2.49 KB
/
index.ts
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
import { Plugin } from '@merryjs/cli/lib/plugin'
import chalk from 'chalk'
import chokidar from 'chokidar'
import glob from 'glob'
import path from 'path'
import { DtsCreator } from './lib/dtsCreator'
/**
* TypedCssModulesAnswers
*/
export interface TypedCssModulesAnswers {
name: string
}
export interface TypedCssModulesOptions {
pattern: string
watch: boolean
silent: string
camelCase: boolean
}
let creator
let args: TypedCssModulesOptions
function writeFile(f) {
creator
.create(f, null, !!args.watch)
.then(content => content.writeFile())
.then(content => {
if (!args.silent) {
// tslint:disable-next-line:no-console
console.log('Wrote ' + chalk.green(content.outputFilePath))
if (content.messageList && content.messageList.length) {
content.messageList.forEach(message => {
// tslint:disable-next-line:no-console
console.warn(chalk.yellow('[Warn] ' + message))
})
}
}
})
// tslint:disable-next-line:no-console
.catch(reason => console.error(chalk.red('[Error] ' + reason)))
}
export default (api: Plugin) => {
api
.command('typed-css-modules [name]')
.option('-C, --camelCase', 'Convert CSS class tokens to camelcase')
.option('-P, --pattern [value]', 'filter path by pattern')
.option('-W, --watch', 'Watch input directory\'s css files or pattern')
.option(
'-S, --silent',
'Silent output. Do not show "files written" or warning messages'
)
.action(async (name: string, options: TypedCssModulesOptions) => {
args = options
if (!options) {
api.outputHelp()
return
}
const rootDir = process.cwd()
const searchDir = api.conf.dist
const filesPattern = path.join(searchDir, options.pattern || '**/*.+(css|scss|less)')
creator = new DtsCreator({
rootDir,
searchDir,
camelCase: options.camelCase
})
if (options.watch) {
// tslint:disable-next-line:no-console
console.log('Watching...')
const watcher = chokidar.watch([filesPattern.replace(/\\/g, '/')])
watcher.on('add', writeFile)
watcher.on('change', writeFile)
} else {
glob(filesPattern, null, (err, files) => {
if (err) {
// tslint:disable-next-line:no-console
console.error(err)
return
}
if (!files || !files.length) {
return
}
files.forEach(writeFile)
})
}
})
}