-
Notifications
You must be signed in to change notification settings - Fork 1
/
caf-command.coffee
163 lines (131 loc) · 4.46 KB
/
caf-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
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
161
162
# enable multi-context type-support (slower, but other wise the same)
global.ArtStandardLibMultipleContextTypeSupport = true
colors = require "colors"
glob = require "glob-promise"
fs = require 'fs-extra'
path = require 'path'
require 'coffee-script/register'
# webpack hack
realRequire = eval 'require'
{version, displayError, CafRepl, CompileCache} = CaffeineMc = eval('require') './index'
{log, dashCase, escapeRegExp, present, isString,
Promise, formattedInspect, each, escapeRegExp
} = Neptune.Art.StandardLib
# Preload pre-compiled art-foundation for dramatically faster load-times...
commander = require "commander"
.version version
.usage('[options] <input files and directories>')
.option '-o, --output <directory>', "where to write output files"
.option '-c, --compile', 'compile files'
.option '-C, --cache', 'DEPRICATED - cache is on by default'
.option '--nocache', 'disable compile cache'
.option '-p, --prettier', 'apply "prettier" to any js output'
.option '-t, --transpile [presets...]', 'transpile with babel'
.option '-d, --debug', 'show debug info'
.option '-v, --verbose', 'show more output'
.option '-r, --reset', 'reset cache'
# .option '-m, --map', 'generate source map and save as .js.map files'
.option '-M, --inlineMap', 'generate source map and include it directly in output'
.option '--versions [compiler-npm-name]', "show caffeine-mc's version OR the specified caffeine-mc-compatible compiler's version"
.on "--help", ->
console.log """
An output directory is required if more than one input file is specified.
Default action, if a file is provided, is to execute it.
"""
.parse process.argv
displayError = (e) ->
CaffeineMc.displayError e, commander
{reset, output, compile, prettier, transpile, verbose, versions, cache, nocache} = commander
cache = !nocache
fileCounts =
read: 0
written: 0
compiled: 0
fromCache: 0
compileFile = (filename, outputDirectory) ->
CaffeineMc.compileFile(filename, {
outputDirectory: outputDirectory || output || path.dirname filename
prettier
transpile
cache
inlineMap: commander.inlineMap
})
.then ({readCount, writeCount, output}) ->
if output.fromCache
fileCounts.fromCache += readCount
else
fileCounts.compiled += readCount
if verbose
if output.fromCache
log "cached: #{filename.grey}"
else
log "compiled: #{filename.green}"
fileCounts.read += readCount
fileCounts.written += writeCount
compileDirectory = (dirname) ->
glob path.join dirname, "**", "*.caf"
.then (list) ->
serializer = new Promise.Serializer
each list, (filename) ->
relative = path.relative dirname, filename
if output
outputDirectory = path.join output, path.dirname relative
serializer
.then ->
Promise.then -> outputDirectory && fs.ensureDir outputDirectory
.then -> compileFile filename, outputDirectory
serializer
if reset
CompileCache.reset()
# process.argv = [] 'caf' # [fs.realpathSync 'caf']
#################
# COMPILE FILES
#################
if compile
files = commander.args
# if !output and files.length == 1
# [filename] = files
# unless fs.statSync(filename).isDirectory()
# output = path.dirname filename
if files.length > 0 #&& output
verbose && log compile:
inputs: if files.length == 1 then files[0] else files
output: output
log "caffeine-mc loaded" if verbose
log {prettier, transpile} if verbose && (transpile || prettier)
serializer = new Promise.Serializer
each files, (filename) ->
serializer.then ->
if fs.statSync(filename).isDirectory()
compileDirectory filename
else
compileFile filename
serializer.then ->
if commander.debug
log DEBUG:
loadedModules: Object.keys realRequire('module')._cache
registeredLoaders: Object.keys realRequire.extensions
log success: {fileCounts}
serializer.catch displayError
else
commander.outputHelp()
#################
# RUN FILE
#################
else if commander.args.length >= 1
[fileToRun, args...] = commander.args
CaffeineMc.register()
CaffeineMc.runFile fileToRun, {color: true, cache, verbose, args}
else if versions
if isString versions
compiler = realRequire dashCase versions
log
"#{versions}": compiler.version || compiler.VERSION
log
Neptune: Neptune.getVersions()
#################
# START REPL
#################
else
CafRepl.start()
# commander.outputHelp()