1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const fs = require("fs");
|
13 | const path = require("path");
|
14 | const slam = require("./index");
|
15 | const cliArgs = require("command-line-args");
|
16 | const getCliUsage = require("command-line-usage");
|
17 | const options = [
|
18 | {
|
19 | name: 'source',
|
20 | alias: 's',
|
21 | type: String,
|
22 | description: 'File to clean',
|
23 | defaultOption: true
|
24 | },
|
25 | {
|
26 | name: 'type',
|
27 | alias: 't',
|
28 | type: function (t) {
|
29 | return t.toLowerCase();
|
30 | },
|
31 | description: 'Type of file to clean. Choices are "html" and "css"'
|
32 | },
|
33 | {
|
34 | name: 'help',
|
35 | alias: 'h',
|
36 | type: Boolean,
|
37 | description: 'Print this message'
|
38 | }
|
39 | ];
|
40 | const args = cliArgs(options);
|
41 | function getUsage() {
|
42 | return getCliUsage([
|
43 | {
|
44 | header: 'css-slam',
|
45 | content: 'Minimal CSS, fast'
|
46 | },
|
47 | {
|
48 | header: 'Options',
|
49 | optionList: options
|
50 | }
|
51 | ]);
|
52 | }
|
53 | if (args.help) {
|
54 | console.log(getUsage());
|
55 | process.exit(0);
|
56 | }
|
57 | if (!args.type) {
|
58 | console.log('Must pick a type with --type|-t!');
|
59 | console.log(getUsage());
|
60 | process.exit(1);
|
61 | }
|
62 | const source = args.source;
|
63 | function processSource(text) {
|
64 | const type = args.type;
|
65 | if (type === 'html') {
|
66 | return slam.html(text);
|
67 | }
|
68 | else if (type === 'css') {
|
69 | return slam.css(text);
|
70 | }
|
71 | else {
|
72 | console.error('Only supported types are "html" and "css"!');
|
73 | console.error(getUsage());
|
74 | process.exit(1);
|
75 | }
|
76 | }
|
77 | if (source) {
|
78 | const clean = processSource(fs.readFileSync(path.resolve(source), 'utf-8'));
|
79 | console.log(clean);
|
80 | }
|
81 | else if (!process.stdin.isTTY) {
|
82 | const chunks = [];
|
83 | process.stdin.setEncoding('utf-8');
|
84 | process.stdin.on('readable', function () {
|
85 | const chunk = process.stdin.read();
|
86 | if (chunk !== null) {
|
87 | chunks.push(chunk);
|
88 | }
|
89 | });
|
90 | process.stdin.on('end', function () {
|
91 | console.log(processSource(chunks.join('')));
|
92 | });
|
93 | }
|
94 | else {
|
95 | console.error('Missing source file!');
|
96 | console.error('Supply file with STDIN or --source flag');
|
97 | console.error(getUsage());
|
98 | process.exit(1);
|
99 | }
|
100 |
|
\ | No newline at end of file |