UNPKG

2.8 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8 * Code distributed by Google as part of the polymer project is also
9 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10 */
11Object.defineProperty(exports, "__esModule", { value: true });
12const fs = require("fs");
13const path = require("path");
14const slam = require("./index");
15const cliArgs = require("command-line-args");
16const getCliUsage = require("command-line-usage");
17const 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];
40const args = cliArgs(options);
41function 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}
53if (args.help) {
54 console.log(getUsage());
55 process.exit(0);
56}
57if (!args.type) {
58 console.log('Must pick a type with --type|-t!');
59 console.log(getUsage());
60 process.exit(1);
61}
62const source = args.source;
63function 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}
77if (source) {
78 const clean = processSource(fs.readFileSync(path.resolve(source), 'utf-8'));
79 console.log(clean);
80}
81else 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}
94else {
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//# sourceMappingURL=cli.js.map
\No newline at end of file