UNPKG

3.47 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const fs = require('fs');
18const mkdirp = require('mkdirp');
19const path = require('path');
20const Writer = require('./writer');
21
22/**
23 * FileWriter creates text files under a directory tree. It can be used
24 * by code generators to create source files for example.
25 * Basic usage is: openFile(fileName), writeLine(...), closeFile().
26 *
27 * @private
28 * @extends Writer
29 * @see See {@link Writer}
30 * @class
31 * @memberof module:concerto-tools
32 */
33class FileWriter extends Writer {
34
35 /**
36 * Create a FileWriter.
37 *
38 * @param {string} outputDirectory - the path to an output directory
39 * that will be used to store generated files.
40 */
41 constructor(outputDirectory) {
42 super();
43 this.outputDirectory = outputDirectory;
44 this.relativeDir = null;
45 this.fileName = null;
46 mkdirp.sync(outputDirectory);
47 }
48
49 /**
50 * Opens a file for writing. The file will be created in the
51 * root directory of this FileWriter.
52 *
53 * @param {string} fileName - the name of the file to open
54 */
55 openFile(fileName) {
56 this.fileName = fileName;
57 this.relativeDir = null;
58 }
59
60 /**
61 * Opens a file for writing, with a location relative to the
62 * root directory of this FileWriter.
63 *
64 * @param {string} relativeDir - the relative directory to use
65 * @param {string} fileName - the name of the file to open
66 */
67 openRelativeFile(relativeDir, fileName) {
68 this.relativeDir = relativeDir;
69 this.fileName = fileName;
70 }
71
72 /**
73 * Writes text to the current open file
74 * @param {int} tabs - the number of tabs to use
75 * @param {string} text - the text to write
76 */
77 writeLine(tabs,text) {
78 if (this.fileName) {
79 super.writeLine(tabs,text);
80 } else {
81 throw Error('File has not been opened!');
82 }
83 }
84
85 /**
86 * Writes text to the start of the current open file
87 * @param {int} tabs - the number of tabs to use
88 * @param {string} text - the text to write
89 */
90 writeBeforeLine(tabs,text) {
91 if (this.fileName) {
92 super.writeBeforeLine(tabs,text);
93 } else {
94 throw Error('File has not been opened!');
95 }
96 }
97
98 /**
99 * Closes the current open file
100 */
101 closeFile() {
102 if (!this.fileName) {
103 throw new Error('No file open');
104 }
105
106 let filePath = this.outputDirectory;
107 if (this.relativeDir) {
108 filePath = path.resolve(filePath, this.relativeDir);
109 }
110 filePath = path.resolve(filePath, this.fileName);
111
112 //console.log('Writing to ' + filePath );
113 mkdirp.sync(path.dirname(filePath));
114 fs.writeFileSync(filePath, this.getBuffer());
115
116 this.fileName = null;
117 this.relativeDir = null;
118 this.clearBuffer();
119 }
120}
121
122module.exports = FileWriter;