forked from PastVu/pastvu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.js
70 lines (63 loc) · 2.06 KB
/
transform.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
#! /usr/bin/env node
'use strict';
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const colors = require('ansi-colors');
const babel = require('@babel/core');
const desc = `
Tranforms script with Babel using application's config.
Usage: $0 <file> [options]
By default transpiles script for server.
Use -c option to specify another config`;
const argv = require('yargs')
.usage(desc)
.options({
'c': {
describe: 'Config for transformation. Default is config for server',
default: path.join(__dirname, 'server.config.js'),
alias: 'config',
type: 'string',
},
'f': {
describe: 'File to transform',
demand: true,
alias: 'file',
type: 'string',
},
'o': {
describe: 'File to save transformed script into',
alias: 'out',
type: 'string',
},
})
.check(argv => {
if (!fs.existsSync(argv.file)) {
throw new Error(`File "${argv.file}" doesn't exist`);
}
return true;
})
.help('help')
.showHelpOnFail(false, 'Run with `--help` to see usage example')
.argv;
const input = fs.readFileSync(argv.file, 'utf8');
const config = require(path.resolve(argv.config));
const start = Date.now();
const output = babel.transform(input, config);
const end = Date.now() - start;
const inputSize = Buffer.byteLength(input, 'utf8');
const outputSize = Buffer.byteLength(output.code, 'utf8');
/* eslint no-console:0 */
if (argv.out) {
fs.writeFileSync(argv.out, output.code);
console.log(colors.green(`File "${argv.file}" transpiled into "${argv.out}"`));
} else {
console.log(output.code);
console.log('---------');
}
console.log(
`time: ${colors.yellow(`${end}ms`)},`,
`inputSize: ${colors.yellow(inputSize)}, outputSize: ${colors.yellow(outputSize)}\n`,
`config: ${colors.yellow(argv.config)}\n`,
`usedHelpers: ${_.map(output.metadata.usedHelpers, helper => colors.yellow(helper)).join(', ') || '—'}`
);