This repository was archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.coffee
78 lines (60 loc) · 1.89 KB
/
command.coffee
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
fs = require 'fs'
argumentum = require 'argumentum'
helpers = require './helpers'
compiler = require './compiler'
optConfig =
script: 'leco'
commandRequired: no
options:
'no-include-helpers':
abbr: 'n'
flag: true
help: 'do not include helpers in template'
'wrap':
abbr: 'w'
default: 'amd'
help: 'wrapper type (AMD, CommonJS, global)'
'helpers-name':
help: 'name of object containing helper functions'
default: 'helpers'
'helpers':
flag: true
help: 'only output helpers'
wrappers =
amd:
begin: 'define(function() { return '
end: '});'
commonjs:
begin: 'module.exports = '
end: ''
global:
begin: '''(function() {
(this.templates || (this.templates = {}))["%name%"] = '''
end: '}).call(this);'
wrap = (name, template, path) ->
wrapper = wrappers[name.toLowerCase()]
if not wrapper
return "Unknown wrapper: #{wrapper}"
begin = wrapper.begin
if begin.indexOf('%name%') != -1
tname = path.slice(path.lastIndexOf('/') + 1, path.lastIndexOf('.'))
begin = begin.replace('%name%', name)
return begin + template.trim() + wrapper.end
run = ->
parser = argumentum.load(optConfig)
options = parser.parse()
if options.helpers
return compiler.printHelpers(helpers, options['helpers-name'])
if not options[0]
return parser.getUsage()
replaceName = (s) ->
source = fs.readFileSync(options[0]).toString()
if options['no-include-helpers']
template = compiler.compile(source)
else
template = compiler.compile(source,
{helpers, helpersName: options['helpers-name']})
return wrap(options.wrap, template, options[0])
exports.wrap = wrap
exports.wrappers = wrappers
exports.run = run