UNPKG

1.69 kBPlain TextView Raw
1#!/usr/bin/env node
2
3/**
4 * 命令行工具
5 *
6 * @author Zongmin Lei<leizongmin@gmail.com>
7 */
8
9var fs = require('fs');
10var path = require('path');
11var program = require('commander');
12var xss = require('../');
13var packageInfo = require('../package.json');
14
15program
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
24program.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
37program.parse(process.argv);
38
39if (program.test) {
40 require('../lib/cli');
41 return;
42}
43
44var config = {};
45if (program.config) {
46 config = require(path.resolve(program.config));
47}
48if (program.input) {
49 var input = fs.readFileSync(program.input, 'utf8');
50} else {
51 program.help();
52}
53
54if (program['strip-ignore-tag']) {
55 config.stripIgnoreTag = true;
56}
57if (program['strip-ignore-tag-body']) {
58 config.stripIgnoreTagBody = true;
59}
60
61var output = xss(input, config);
62
63if (program.output) {
64 fs.writeFileSync(program.output, output);
65} else {
66 console.log(output);
67}