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