-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
30 lines (28 loc) · 1.18 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
#! /usr/bin/env node
const fs = require('fs');
const glob = require('glob');
const colors = require('colors');
const readArgv = require('read-argv');
const { join, resolve, parse } = require('path');
const { replace } = require('./options');
const isString = require('./common/isString');
const node = require('.');
const argv = readArgv(process.argv);
const files = argv._.length > 1 ? `{${argv._.join(',')}}` : argv._[0] || '*';
const context = isString(argv.dir) ? argv.dir : '';
const options = replace(argv.replace);
const outputDir = argv.outputdir;
glob.sync(resolve(join(context, files))).forEach(file => {
options.file = file;
const source = fs.readFileSync(file, 'utf8').toString();
const output = node(source, options);
let outputPath = parse(file.replace(options.pattern, options.replacement));
outputPath = join(isString(outputDir) ? outputDir : outputPath.dir, outputPath.base);
if (/test/i.test(process.env.NODE_ENV)) return console.log(output);
const writeStream = fs.createWriteStream(outputPath);
writeStream.write(output, 'utf-8');
writeStream.on('finish', () => {
console.info(colors.underline('unkn:'), file, colors.green(' -> '), outputPath);
});
writeStream.end();
});