-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·129 lines (102 loc) · 3.4 KB
/
cli.js
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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const process = require('process')
const program = require('commander')
const mkdirp = require('mkdirp')
const transformFile = require('./index').transformFile
const CWD = process.cwd()
const extensions = ['.js', '.es', '.es6']
const head = xs => xs[0]
const compose = (f, g) => h => f(g(h))
const isFile = function isFile (pathname) {
try {
return fs.statSync(pathname).isFile()
} catch (e) {
console.error('Given path:', pathname, 'could not be found')
}
}
const isDirectory = function isDirectory (pathname) {
try {
return fs.statSync(pathname).isDirectory()
} catch (e) {
console.error('Given path:', pathname, 'could not be found')
}
}
const getAllInDirectory = function getAllInDirectory (pathname, cb) {
const files = fs.readdirSync(pathname)
for (let i = 0; i < files.length; ++i) {
const filename = path.basename(files[i])
const file = path.join(pathname, filename)
const ext = path.extname(file)
const shouldProcess = isFile(file) && extensions.indexOf(ext) > -1 || isDirectory(file)
if (shouldProcess) {
cb(filename)
}
}
}
const createDirectoryForFile = function createDirectoryForFile (filename, cb) {
const parts = filename.split('/')
parts.splice(-1)
const dir = path.join(CWD, parts.join('/'))
mkdirp(dir, (err) => {
if (err) throw err
cb()
})
}
const writeFile = function writeFile (filename, content) {
fs.writeFile(filename, content, (err) => {
if (err) throw err
})
}
const transformDirectory = function transformDirectory (inDirectory, outDirectory, sourceMaps) {
const _in = path.isAbsolute(inDirectory) ? inDirectory : path.join(CWD, inDirectory)
const _out = path.isAbsolute(outDirectory) ? outDirectory : path.join(CWD, outDirectory)
mkdirp(_out, function (err) {
if (err) throw err
getAllInDirectory(inDirectory, function (file) {
const _infile = path.join(_in, file)
const _outfile = path.join(_out, file)
if (isDirectory(_infile)) {
transformDirectory(_infile, _outfile, sourceMaps)
}
if (isFile(_infile)) {
const output = transformFile(_infile) // TODO add options
writeFile(_outfile, output.code)
if (sourceMaps) {
writeFile(path.join(_outfile, '.map'), output.map)
}
}
})
})
}
const setupOptions = function (argv) {
return program
.version(require('./package.json').version)
.usage('<file input> [options]')
.option('-o --output [string]', 'Where to write the output')
.option('-s --source-maps', 'Output source-maps')
.parse(argv)
}
const parseOptions = function (program) {
if (!program.args.length) {
console.error('Oops: Must provide an input file or directory')
program.help()
}
if (isDirectory(head(program.args))) {
if (!program.output) {
throw new Error('Must provide an output directory when transforming a directory')
}
return transformDirectory(head(program.args), program.output, program.sourceMap)
}
const output = transformFile(head(program.args))
if (!program.output) return console.log(output.code)
createDirectoryForFile(program.output, function () {
writeFile(program.output, output.code)
if (program.sourceMaps && program.output) {
writeFile(program.output + '.map', output.map)
}
})
}
compose(parseOptions, setupOptions)(process.argv)