1 | #!/usr/bin/env node
|
2 |
|
3 | /**
|
4 | * 命令行工具
|
5 | *
|
6 | * @author Zongmin Lei<leizongmin@gmail.com>
|
7 | */
|
8 |
|
9 | var fs = require('fs');
|
10 | var path = require('path');
|
11 | var program = require('commander');
|
12 | var xss = require('../');
|
13 | var packageInfo = require('../package.json');
|
14 |
|
15 | program
|
16 | .version(packageInfo.version)
|
17 | .option('-t, --test', 'active test')
|
18 | .option('-i, --input <input_file>', 'input file name')
|
19 | .option('-o, --output <output_file>', 'output filename')
|
20 | .option('-c, --config <config_file>', 'load custom config')
|
21 | .option('-s, --strip-ignore-tag', 'set stripIgnoreTag=true')
|
22 | .option('-b, --strip-ignore-tag-body', 'set stripIgnoreTagBody=true');
|
23 |
|
24 | program.on('--help', function () {
|
25 | console.log(' Examples:');
|
26 | console.log('');
|
27 | console.log(' $ xss -t');
|
28 | console.log(' $ xss -i origin.html');
|
29 | console.log(' $ xss -i origin.html -o targer.html');
|
30 | console.log(' $ xss -i origin.html -c config.js');
|
31 | console.log(' $ xss -i origin.html -s');
|
32 | console.log(' $ xss -i origin.html -s -b');
|
33 | console.log('');
|
34 | console.log(' For more details, please see: https://npmjs.org/package/xss')
|
35 | });
|
36 |
|
37 | program.parse(process.argv);
|
38 |
|
39 | if (program.test) {
|
40 | require('../lib/cli');
|
41 | return;
|
42 | }
|
43 |
|
44 | var config = {};
|
45 | if (program.config) {
|
46 | config = require(path.resolve(program.config));
|
47 | }
|
48 | if (program.input) {
|
49 | var input = fs.readFileSync(program.input, 'utf8');
|
50 | } else {
|
51 | program.help();
|
52 | }
|
53 |
|
54 | if (program['strip-ignore-tag']) {
|
55 | config.stripIgnoreTag = true;
|
56 | }
|
57 | if (program['strip-ignore-tag-body']) {
|
58 | config.stripIgnoreTagBody = true;
|
59 | }
|
60 |
|
61 | var output = xss(input, config);
|
62 |
|
63 | if (program.output) {
|
64 | fs.writeFileSync(program.output, output);
|
65 | } else {
|
66 | console.log(output);
|
67 | }
|