-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·59 lines (49 loc) · 1.42 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
#!/usr/bin/env node
const args = process.argv.slice(2);
let source;
let output;
const resc = {};
const reFlag = /(?:\-\-resc\:|\-r\:)(.*)/;
for (let i = 0; i < args.length; i++) {
let arg = args[i];
if (arg.startsWith('-')) { // flag
let match = reFlag.exec(arg);
if (match) {
let flag = match[1];
let value = args[++i];
if (isUndef(value)) printUsage();
resc[flag] = value;
let nextFlag = args[i+1];
if (nextFlag && !nextFlag.startsWith("-")) {
resc[flag] = {
path: value,
data: nextFlag
};
i++;
}
} else {
// Unrecognized flag
printUsage();
}
} else { // source
if (!isUndef(source)) {
if (!isUndef(output)) printUsage();
output = arg;
} else {
source = arg;
}
}
}
if (isUndef(source)) printUsage();
function printUsage() {
console.log(`inline-resc <source_path> [<output_path>] --resc:<template_name> <template_file> [<template_content>]`);
process.exit(1);
}
function isUndef(x) {
return typeof x === "undefined";
}
const InlineResource = require("./index.js");
const fs = require('fs');
const file = fs.readFileSync(source).toString();
const inlined = (new InlineResource(resc)).inline(file);
fs.writeFileSync(output, inlined);