1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var ArgumentParser = require('argparse').ArgumentParser;
|
10 | var cardinal = require("cardinal");
|
11 | var chalk = require("chalk");
|
12 | var fs = require("fs");
|
13 | var path = require("path");
|
14 | var _ = require("underscore");
|
15 | var Index = require("../index");
|
16 | var extend = require("extend");
|
17 |
|
18 | var argumentParser = new ArgumentParser({
|
19 | version: require("../package.json").version,
|
20 | addHelp: true,
|
21 | description: "command line tool for cmd-nice"
|
22 | });
|
23 | argumentParser.addArgument(["--action"], {
|
24 | help: "action type: transport, debug, concat",
|
25 | type: "string",
|
26 | required: false,
|
27 | dest: "action",
|
28 | defaultValue: "transport"
|
29 | });
|
30 |
|
31 | argumentParser.addArgument(["--config"], {
|
32 | help: "config for transport/debug/concat",
|
33 | type: "string",
|
34 | required: false,
|
35 | dest: "config"
|
36 | });
|
37 |
|
38 | argumentParser.addArgument(["--configFile"], {
|
39 | help: "config file",
|
40 | type: "string",
|
41 | required: false,
|
42 | dest: "configFile"
|
43 | });
|
44 |
|
45 | argumentParser.addArgument(["--input"], {
|
46 | help: "input files",
|
47 | type: "string",
|
48 | required: true,
|
49 | dest: "inputFiles",
|
50 | nargs: "*"
|
51 | });
|
52 |
|
53 | var args = argumentParser.parseArgs();
|
54 |
|
55 | var alias = {};
|
56 | var aliasPaths = {};
|
57 | if (fs.existsSync(args.configFile)) {
|
58 | var configContent = fs.readFileSync(args.configFile, "utf-8");
|
59 | configContent = eval(configContent);
|
60 | alias = configContent.alias;
|
61 | aliasPaths = configContent.paths;
|
62 | }
|
63 |
|
64 | var parsers = {
|
65 | ".handlebars": Index.HandlebarsTemplate,
|
66 | ".json": Index.Json,
|
67 | ".less": Index.LessStyle,
|
68 | ".scss": Index.SassStyle,
|
69 | ".js": Index.Script,
|
70 | ".jsx": Index.React,
|
71 | ".css": Index.Style,
|
72 | ".html": Index.Text,
|
73 | ".tpl": Index.UnderscoreTemplate
|
74 | };
|
75 | var transportConfig = {
|
76 | useCache: true,
|
77 | rootPath: process.cwd(),
|
78 | paths: [
|
79 | process.cwd()
|
80 | ],
|
81 | parsers: parsers,
|
82 | alias: alias,
|
83 | aliasPaths: aliasPaths,
|
84 | handlebars: {
|
85 | id: 'alinw/handlebars/1.3.0/runtime',
|
86 | knownHelpers: [
|
87 | ],
|
88 | knownHelpersOnly: false
|
89 | },
|
90 | sassOptions: {},
|
91 | lessOptions: {},
|
92 | cssOptions: {}
|
93 | };
|
94 |
|
95 | var concatConfig = {
|
96 | separator: ";",
|
97 | useCache: false,
|
98 | paths: [
|
99 | process.cwd()
|
100 | ],
|
101 | parsers: parsers,
|
102 | transportConfig: transportConfig
|
103 | };
|
104 |
|
105 | var debugConfig = {
|
106 | postfix: "-debug"
|
107 | };
|
108 |
|
109 | if (args.config && fs.existsSync(args.config)) {
|
110 | var userConfig = require(args.config);
|
111 | if (args.action === "transport") {
|
112 | extend(true, transportConfig, userConfig);
|
113 | } else if (args.action === "concat") {
|
114 | extend(true, concatConfig, userConfig);
|
115 | } else if (args.action === "debug") {
|
116 | extend(true, debugConfig, userConfig);
|
117 | }
|
118 | }
|
119 |
|
120 | if (args.action === "transport") {
|
121 | transport();
|
122 | } else if (args.action === "concat") {
|
123 | concat();
|
124 | } else if (args.action === "debug") {
|
125 | debug();
|
126 | }
|
127 |
|
128 | function transport() {
|
129 | _.each(args.inputFiles, function(fileName) {
|
130 | var extName = path.extname(fileName);
|
131 |
|
132 | if (!transportConfig.parsers.hasOwnProperty(extName)) return;
|
133 |
|
134 | var Parser = transportConfig.parsers[extName];
|
135 | var parser = new Parser(transportConfig);
|
136 | parser.execute({
|
137 | content: fs.readFileSync(fileName, "utf-8"),
|
138 | src: fs.realpathSync(fileName)
|
139 | }).then(function(code) {
|
140 | console.log(chalk.cyan("\n" + fileName + " after transported:\n"));
|
141 | console.log(cardinal.highlight(code));
|
142 | }).fail(function(error){
|
143 | console.log(error);
|
144 | });
|
145 | });
|
146 | }
|
147 |
|
148 | function concat() {
|
149 | _.each(args.inputFiles, function(fileName) {
|
150 | var concat = new Index.Concat(concatConfig);
|
151 |
|
152 | concat.execute({
|
153 | content: fs.readFileSync(fileName, "utf-8"),
|
154 | src: fs.realpathSync(fileName)
|
155 | }).then(function(code) {
|
156 | console.log(chalk.cyan("\n" + fileName + " after concatted:\n"));
|
157 | console.log(cardinal.highlight(code));
|
158 | });
|
159 | });
|
160 | }
|
161 |
|
162 | function debug() {
|
163 | _.each(args.inputFiles, function(fileName) {
|
164 | var debug = new Index.Debug(debugConfig);
|
165 |
|
166 | debug.execute({
|
167 | content: fs.readFileSync(fileName, "utf-8"),
|
168 | src: fs.realpathSync(fileName)
|
169 | }).then(function(code) {
|
170 | console.log(chalk.cyan("\n" + fileName + "'s debug code:\n"));
|
171 | console.log(cardinal.highlight(code));
|
172 | });
|
173 | });
|
174 | } |
\ | No newline at end of file |