UNPKG

3.19 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict'
4
5//
6// Use in CLI:
7//
8// Type "riot" for help.
9//
10// Use in Node:
11//
12// var riot = require('riot/compiler')
13// riot.make({ from: 'foo', to: 'bar', compact: true })
14// riot.watch({ from: 'foo.tag', to: 'bar.js' })
15//
16
17// Include the available tasks
18const
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 // this could be handy to someone who wants to have
46 // also access to the private cli parser function
47 _cli: cli
48 }
49
50/* istanbul ignore next */
51function cli(ar) {
52
53 // Get CLI arguments
54
55 var args
56
57 // was an error thrown parsing the options?
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 // read the config file
69 // let's go async here
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 // Translate args into options hash
78 // extending args with the options loaded via config file
79 helpers.extend(args, opt)
80 opt = {
81 compiler: {
82 compact: args.compact,
83 template: args.template, // html preprocessor
84 style: args.style, // css preprocessor
85 type: args.type, // javascript preprocessor
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, // to extend the default compiler parsers
100 from: args.from || args._.shift(),
101 to: args.to || args._.shift()
102 }
103
104 // Call matching method
105 var method = Object
106 .keys(API)
107 .filter((v) => args[v])[0] || (opt.from ? 'make' : 'help')
108
109 // check whether the output should be colorized
110 chalk.constructor({enabled: !!opt.colors })
111
112 // create isSilent as global variable
113 global.isSilent = args.silent
114
115 // flag used to detect wheter a task is triggered via command line or not
116 opt.isCli = true
117
118 return API[method](opt)
119
120 }).catch((e) => helpers.err(e))
121
122}
123
124
125// Run from CLI or as Node module
126
127if (module.parent) {
128 module.exports = API
129 global.isSilent = true
130/* istanbul ignore next */
131} else cli()
132
133