1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | (function () {
|
34 | 'use strict';
|
35 |
|
36 | var core = require('./core');
|
37 | var mkdirp = require('mkdirp');
|
38 | var path = require('path');
|
39 | var fs = require('fs');
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function stream(nodeStream, linebreak) {
|
45 | linebreak = (typeof linebreak !== 'undefined' ? linebreak : '\n');
|
46 |
|
47 | var mw = core.base();
|
48 | mw.enabled = true;
|
49 | mw.stream = nodeStream;
|
50 | mw.linebreak = linebreak;
|
51 | mw.writeln = function (line) {
|
52 | if (mw.enabled) {
|
53 | mw.stream.write(line + linebreak);
|
54 | }
|
55 | };
|
56 | mw.toString = function () {
|
57 | return '<miniwrite-stream>';
|
58 | };
|
59 | return mw;
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function disk(file, linebreak) {
|
68 | var mw = core.base();
|
69 | mw.linebreak = (typeof linebreak !== 'undefined' ? linebreak : '\n');
|
70 | mw.enabled = true;
|
71 | mw.file = file;
|
72 | var stream;
|
73 | var start = 0;
|
74 | var buffer = core.buffer();
|
75 | var streaming = {
|
76 | writeln: function (line) {
|
77 | var buff = new Buffer(line + mw.linebreak, 'utf8');
|
78 | start += buff.length;
|
79 | stream.write(buff);
|
80 | }
|
81 | };
|
82 | var active = buffer;
|
83 | var splitter = core.splitter({
|
84 | writeln: function (line) {
|
85 | active.writeln(line);
|
86 | }
|
87 | });
|
88 |
|
89 | var opening = false;
|
90 | var initing = false;
|
91 | (function () {
|
92 | initing = true;
|
93 | function finalKill(err) {
|
94 | initing = false;
|
95 | if (err) {
|
96 | console.log(err);
|
97 | return;
|
98 | }
|
99 |
|
100 | start = 0;
|
101 | if (buffer.lines.length > 0) {
|
102 | open(mw.file);
|
103 | return;
|
104 | }
|
105 |
|
106 | if (flushCallback.length > 0) {
|
107 | doFlush();
|
108 | }
|
109 | }
|
110 |
|
111 | fs.exists(mw.file, function (exists) {
|
112 | if (!exists) {
|
113 | finalKill();
|
114 | return;
|
115 | }
|
116 | fs.truncate(mw.file, 0, finalKill);
|
117 | });
|
118 | }());
|
119 |
|
120 | function open(dest) {
|
121 | if (!stream && !opening) {
|
122 | opening = true;
|
123 | mkdirp(path.dirname(dest), function (err) {
|
124 |
|
125 | opening = false;
|
126 | if (err) {
|
127 | console.log(err);
|
128 | return;
|
129 | }
|
130 | stream = fs.createWriteStream(dest, {
|
131 | start: start,
|
132 | flags: (start === 0 ? 'w' : 'a'),
|
133 | encoding: 'utf8',
|
134 | mode: '0644'
|
135 | });
|
136 | active = streaming;
|
137 |
|
138 | buffer.lines.forEach(function (line) {
|
139 | active.writeln(line);
|
140 | });
|
141 | buffer.clear();
|
142 |
|
143 |
|
144 | if (flushCallback.length > 0) {
|
145 | doFlush();
|
146 | }
|
147 | });
|
148 | }
|
149 | }
|
150 |
|
151 | mw.writeln = function (line) {
|
152 |
|
153 | if (mw.enabled) {
|
154 | if (!stream) {
|
155 | open(mw.file);
|
156 | }
|
157 | splitter.writeln(line);
|
158 | }
|
159 | };
|
160 |
|
161 | var flushCallback = [];
|
162 |
|
163 | function doFlush() {
|
164 | if (initing || opening) {
|
165 | return;
|
166 | }
|
167 | active = buffer;
|
168 | var callbackList = flushCallback;
|
169 | flushCallback = [];
|
170 | if (stream) {
|
171 | stream.on('finish', function () {
|
172 | callbackList.forEach(function (callback) {
|
173 | callback(mw);
|
174 | });
|
175 | });
|
176 | stream.end();
|
177 | stream = null;
|
178 | return;
|
179 | }
|
180 | process.nextTick(function () {
|
181 | callbackList.forEach(function (callback) {
|
182 | callback(mw);
|
183 | });
|
184 | });
|
185 | }
|
186 |
|
187 | mw.flush = function (callback) {
|
188 | flushCallback.push(callback);
|
189 | doFlush();
|
190 | };
|
191 | mw.toString = function () {
|
192 | return '<miniwrite-stream>';
|
193 | };
|
194 | return mw;
|
195 | }
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | var io = {
|
201 | stream: stream,
|
202 | disk: disk
|
203 | };
|
204 | module.exports = io;
|
205 |
|
206 | }).call();
|