1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const
|
19 | Check = require('./tasks/Check'),
|
20 | New = require('./tasks/New'),
|
21 | Make = require('./tasks/Make'),
|
22 | Watch = require('./tasks/Watch'),
|
23 | helpers = require('./helpers'),
|
24 | options = require('./options'),
|
25 | path = require('path'),
|
26 | chalk = require('chalk'),
|
27 | co = require('co'),
|
28 | optionator = require('optionator')(options),
|
29 | API =
|
30 | {
|
31 | help() {
|
32 | var h = optionator.generateHelp()
|
33 | helpers.log(h)
|
34 | return h
|
35 | },
|
36 | version() {
|
37 | var v = helpers.getVersion()
|
38 | helpers.log(v)
|
39 | return v
|
40 | },
|
41 | new(opt) { return new New(opt) },
|
42 | check(opt) { return new Check(opt) },
|
43 | make(opt) { return new Make(opt) },
|
44 | watch(opt) { return new Watch(opt) },
|
45 |
|
46 |
|
47 | _cli: cli
|
48 | }
|
49 |
|
50 |
|
51 | function cli(ar) {
|
52 |
|
53 |
|
54 |
|
55 | var args
|
56 |
|
57 |
|
58 | try {
|
59 | args = optionator.parse(
|
60 | ar ? ['node', path.resolve('lib')].concat(ar) : process.argv,
|
61 | options
|
62 | )
|
63 | } catch (e) {
|
64 | helpers.err(e)
|
65 | return e
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 | return co(function*() {
|
71 | if (args.config)
|
72 | return yield helpers.loadConfigFile(args.config)
|
73 | else
|
74 | return {}
|
75 |
|
76 | }).then(function(opt) {
|
77 |
|
78 |
|
79 | helpers.extend(args, opt)
|
80 | opt = {
|
81 | compiler: {
|
82 | compact: args.compact,
|
83 | template: args.template,
|
84 | style: args.style,
|
85 | type: args.type,
|
86 | brackets: args.brackets,
|
87 | entities: !!args.export,
|
88 | exclude: args.exclude,
|
89 | expr: args.expr,
|
90 | modular: args.modular,
|
91 | silent: args.silent,
|
92 | whitespace: args.whitespace
|
93 | },
|
94 | ext: args.ext,
|
95 | css: args.css,
|
96 | new: args.new,
|
97 | export: args.export,
|
98 | colors: args.colors,
|
99 | parsers: args.parsers,
|
100 | from: args.from || args._.shift(),
|
101 | to: args.to || args._.shift()
|
102 | }
|
103 |
|
104 |
|
105 | var method = Object
|
106 | .keys(API)
|
107 | .filter((v) => args[v])[0] || (opt.from ? 'make' : 'help')
|
108 |
|
109 |
|
110 | chalk.constructor({enabled: !!opt.colors })
|
111 |
|
112 |
|
113 | global.isSilent = args.silent
|
114 |
|
115 |
|
116 | opt.isCli = true
|
117 |
|
118 | return API[method](opt)
|
119 |
|
120 | }).catch((e) => helpers.err(e))
|
121 |
|
122 | }
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | if (module.parent) {
|
128 | module.exports = API
|
129 | global.isSilent = true
|
130 |
|
131 | } else cli()
|
132 |
|
133 |
|