1 |
|
2 |
|
3 |
|
4 |
|
5 | var util = require('util'),
|
6 | path = require('path'),
|
7 | fs = require('fs'),
|
8 | mkdirp = require('make-dir'),
|
9 | supportsColor = require('supports-color'),
|
10 | isAbsolute = path.isAbsolute || function (p) {
|
11 | return path.resolve(p) === path.normalize(p);
|
12 | };
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function ContentWriter() {
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | ContentWriter.prototype.write = function () {
|
29 | throw new Error('write: must be overridden');
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | ContentWriter.prototype.colorize = function (str /*, clazz*/) {
|
42 | return str;
|
43 | };
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | ContentWriter.prototype.println = function (str) {
|
50 | this.write(str + '\n');
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | ContentWriter.prototype.close = function () {
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function FileContentWriter(fd) {
|
66 | this.fd = fd;
|
67 | }
|
68 | util.inherits(FileContentWriter, ContentWriter);
|
69 |
|
70 | FileContentWriter.prototype.write = function (str) {
|
71 | fs.writeSync(this.fd, str);
|
72 | };
|
73 |
|
74 | FileContentWriter.prototype.close = function () {
|
75 | fs.closeSync(this.fd);
|
76 | };
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function ConsoleWriter() {
|
84 | }
|
85 | util.inherits(ConsoleWriter, ContentWriter);
|
86 |
|
87 |
|
88 | var capture = false;
|
89 | var output = '';
|
90 | ConsoleWriter.prototype.write = function (str) {
|
91 | if (capture) {
|
92 | output += str;
|
93 | } else {
|
94 | process.stdout.write(str);
|
95 | }
|
96 | };
|
97 |
|
98 | ConsoleWriter.prototype.colorize = function (str, clazz) {
|
99 | var colors = {
|
100 | low: '31;1',
|
101 | medium: '33;1',
|
102 | high: '32;1'
|
103 | };
|
104 |
|
105 |
|
106 | if (supportsColor.stdout && colors[clazz]) {
|
107 | return '\u001b[' + colors[clazz] + 'm' + str + '\u001b[0m';
|
108 | }
|
109 | return str;
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | function FileWriter(baseDir) {
|
119 | if (!baseDir) {
|
120 | throw new Error('baseDir must be specified');
|
121 | }
|
122 | this.baseDir = baseDir;
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | FileWriter.startCapture = function () {
|
130 | capture = true;
|
131 | };
|
132 | FileWriter.stopCapture = function () {
|
133 | capture = false;
|
134 | };
|
135 | FileWriter.getOutput = function () {
|
136 | return output;
|
137 | };
|
138 | FileWriter.resetOutput = function () {
|
139 | output = '';
|
140 | };
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | FileWriter.prototype.writerForDir = function (subdir) {
|
149 | if (isAbsolute(subdir)) {
|
150 | throw new Error('Cannot create subdir writer for absolute path: ' + subdir);
|
151 | }
|
152 | return new FileWriter(this.baseDir + '/' + subdir);
|
153 | };
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | FileWriter.prototype.copyFile = function (source, dest) {
|
160 | if (isAbsolute(dest)) {
|
161 | throw new Error('Cannot write to absolute path: ' + dest);
|
162 | }
|
163 | dest = path.resolve(this.baseDir, dest);
|
164 | mkdirp.sync(path.dirname(dest));
|
165 | fs.writeFileSync(dest, fs.readFileSync(source));
|
166 | };
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | FileWriter.prototype.writeFile = function (file) {
|
174 | if (file === null || file === '-') {
|
175 | return new ConsoleWriter();
|
176 | }
|
177 | if (isAbsolute(file)) {
|
178 | throw new Error('Cannot write to absolute path: ' + file);
|
179 | }
|
180 | file = path.resolve(this.baseDir, file);
|
181 | mkdirp.sync(path.dirname(file));
|
182 | return new FileContentWriter(fs.openSync(file, 'w'));
|
183 | };
|
184 |
|
185 | module.exports = FileWriter;
|