Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80680 views
1
/*
2
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
3
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4
*/
5
6
var util = require('util'),
7
EventEmitter = require('events').EventEmitter;
8
9
function extend(cons, proto) {
10
Object.keys(proto).forEach(function (k) {
11
cons.prototype[k] = proto[k];
12
});
13
}
14
15
/**
16
* abstract interfaces for writing content
17
* @class ContentWriter
18
* @module io
19
* @main io
20
* @constructor
21
*/
22
//abstract interface for writing content
23
function ContentWriter() {
24
}
25
26
ContentWriter.prototype = {
27
/**
28
* writes the specified string as-is
29
* @method write
30
* @param {String} str the string to write
31
*/
32
write: /* istanbul ignore next: abstract method */ function (/* str */) {
33
throw new Error('write: must be overridden');
34
},
35
/**
36
* writes the specified string with a newline at the end
37
* @method println
38
* @param {String} str the string to write
39
*/
40
println: function (str) { this.write(str + '\n'); }
41
};
42
43
/**
44
* abstract interface for writing files and assets. The caller is expected to
45
* call `done` on the writer after it has finished writing all the required
46
* files. The writer is an event-emitter that emits a `done` event when `done`
47
* is called on it *and* all files have successfully been written.
48
*
49
* @class Writer
50
* @constructor
51
*/
52
function Writer() {
53
EventEmitter.call(this);
54
}
55
56
util.inherits(Writer, EventEmitter);
57
58
extend(Writer, {
59
/**
60
* allows writing content to a file using a callback that is passed a content writer
61
* @method writeFile
62
* @param {String} file the name of the file to write
63
* @param {Function} callback the callback that is called as `callback(contentWriter)`
64
*/
65
writeFile: /* istanbul ignore next: abstract method */ function (/* file, callback */) {
66
throw new Error('writeFile: must be overridden');
67
},
68
/**
69
* copies a file from source to destination
70
* @method copyFile
71
* @param {String} source the file to copy, found on the file system
72
* @param {String} dest the destination path
73
*/
74
copyFile: /* istanbul ignore next: abstract method */ function (/* source, dest */) {
75
throw new Error('copyFile: must be overridden');
76
},
77
/**
78
* marker method to indicate that the caller is done with this writer object
79
* The writer is expected to emit a `done` event only after this method is called
80
* and it is truly done.
81
* @method done
82
*/
83
done: /* istanbul ignore next: abstract method */ function () {
84
throw new Error('done: must be overridden');
85
}
86
});
87
88
module.exports = {
89
Writer: Writer,
90
ContentWriter: ContentWriter
91
};
92
93
94