forked from hummingbirdtech/hodor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js2hd.js
45 lines (35 loc) · 1.28 KB
/
js2hd.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
#!/usr/bin/env node
/**
* Module dependencies.
*/
var program = require('commander'),
pkginfo = require('pkginfo')(module, 'version'),
colors = require("colors"),
fs = require('fs');
var legend = require('./legend'); // ./ means current directory, and don't need .js b/c all require files are js
program
.version(module.exports.version, '-v, --version')
.description('Hodor hodor hodor')
.parse(process.argv);
var hodor = program.args[0];
if( typeof(hodor) === 'undefined') {
console.log('HODOR:'.bold.red + ' hodor hodor hodor!'.red);
} else {
if (hodor.search(".js") > 0) { // user entered a .js file
console.log('HODOR: '.bold.cyan + hodor.bold.white + ' => '.yellow + hodor.replace('.js', '.hd').bold.white);
var text = fs.readFileSync(hodor).toString(); // the contents of the file
convertCode(text);
} else { // user entered something apart from a js file
console.log('HODOR:'.bold.red + ' hodor hodor!'.red);
}
}
function convertCode (text) {
var outputFileName = hodor.replace(".js", ".hd");
var hodorText = text;
for (i = 0; i < legend.length; i++){
var query = legend[i];
var re = new RegExp(query.search, 'g');
hodorText = hodorText.replace(re, query.replace);
}
fs.writeFileSync(outputFileName, hodorText);
}