-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathindex.ls
More file actions
160 lines (151 loc) · 5.25 KB
/
Copy pathindex.ls
File metadata and controls
160 lines (151 loc) · 5.25 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# REQUIRES ########################
require! {
bach
fs
optionator
'prelude-ls': { each, map }
}
# VARS ############################
options =
* option: \browser
alias: \b
type: \Boolean
description: 'Compile livescript.js and livescript.min.js'
* option: \clean
alias: \c
type: \Boolean
description: 'Remove lib, coverage and browser directories'
* option: \coverage
alias: \i
type: \Boolean
description: 'Run the coverage (with istanbul)'
* option: \lib
alias: \l
type: \Boolean
description: 'Compile the lib'
* option: \package
alias: \p
type: \Boolean
description: 'Generate the package.json'
# FUNCTIONS #######################
create-dir = (dir, cb) !-->
console.log "creating the directory [ #dir ]"
er <-! fs.mkdir dir
if er? and er.code isnt \EEXIST then cb e, void
else
if er? and er.code is \EEXIST
console.log "Directory [ #dir ] already exists"
else console.log "Directory [ #dir ] CREATED"
cb void 2
generic-cb = (err, ok) !-> if err? then console.log err
preroll = ->
require! './package': {version}
"""// Generated by LiveScript #version\n
// LiveScript #version
// Copyright (c) Jeremy Ashkenas, Satoshi Murakami, George Zahariev
// Released under the MIT License
// https://raw.githubusercontent.com/gkz/LiveScript/master/LICENSE\n
"""
# CORE ############################
try
op = optionator {options}
opts = op.parseArgv process.argv
switch
# compiling into browser directory #########################################
| opts.browser
require! {browserify, 'uglify-js':{minify}}
# function to compile into livescript.js
compile-ls = (cb) !->
console.log 'Compiling for livescript.js ...'
b = browserify \./lib/browser.js, {require: \./lib/browser.js}
(err, buf) <- b.bundle!
if err? then cb e, void
else
try
fs.writeFileSync \./browser/livescript.js, (preroll! ++ buf)
console.log '==> livescript.js COMPILED'
cb void 3
catch
cb e, void
# uglifying livescript.js into livescript.min.js
uglifying = (cb) !->
console.log 'Uglifying into livescript-min.js...'
try
code = fs.readFileSync \./browser/livescript.js, \utf-8
res = minify code, output: {comments: yes}
if res.error? then cb e, void
else
if res.warnings?
console.log '====> UGLIFY WARNINGS <====='
JSON.stringify res.warnings |> console.log
fs.writeFileSync \./browser/livescript-min.js, res.code
console.log '==> livescript-min.js UGLIFIED'
cb void 4
catch
cb e, void
# list of all actions done to compile into browser
actions =
create-dir \browser
compile-ls
uglifying
# compiling and uglifying
(bach.series actions) generic-cb
# cleaning the repository (removing coverage, lib and browser) #############
| opts.clean
rmd = (dir) -> (cb) !->
console.log "removing `#dir`"
fs.rm dir, {force: yes, recursive: yes}, cb
args = <[browser lib coverage]> |> map rmd
(bach.series args) (err, ok) !->
console.log if err? then err else 'dirs removed'
# Executing istanbul #######################################################
| opts.coverage
require! { child_process: {spawn} }
istanbul =
if process.platform is \win32 then 'node_modules\\.bin\\istanbul.cmd'
else 'node_modules/.bin/istanbul'
opts = stdio: [process.stdin, process.stdout, process.stderr]
spawn istanbul, ['cover', './scripts/test'], opts
# compiling the lib ########################################################
| opts.lib
# generating the parser
generate-grammar = (cb) !->
console.log 'Generating parser...'
require! { path: {resolve, dirname}, '.': {compile}, './lib/grammar' }
target = resolve dirname(module.filename), \./lib/parser.js
try
parser = grammar.generate!
fs.writeFileSync target, parser ++ '\n'
console.log '==> parser GENERATED'
cb void 2
catch
cb e, void
# compiling files from src to lib
compile-lib = (cb) !->
require! '.': {compile}
try
mapper = (file) !->
console.log "compiling '#file'..."
code = fs.readFileSync "./src/#file", \utf-8
res = compile code, {bare: yes}
fs.writeFileSync "./lib/#{file.split \. .0}.js", res
fs.readdirSync \./src |> each mapper
catch
cb e, void
# doing all actions relative to lib
actions =
create-dir \lib
generate-grammar
compile-lib
(bach.series actions) generic-cb
# generating the package.json ##############################################
| opts.package
require! '.': {compile}
pkg = fs.readFileSync \./package.json.ls, \utf-8
res = compile pkg, json: yes
fs.writeFileSync \./package.json, res
console.log 'package.json (re)generated'
# Help #####################################################################
| otherwise => console.log op.generateHelp!
catch
console.log e