forked from naddison36/sol2uml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriterFiles.js
171 lines (171 loc) · 6.46 KB
/
writerFiles.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writePng = exports.writeSVG = exports.writeDot = exports.writeSourceCode = exports.convertDot2Svg = exports.writeOutputFiles = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const sync_1 = __importDefault(require("@aduh95/viz.js/sync"));
const { convert } = require('convert-svg-to-png');
const debug = require('debug')('sol2uml');
/**
* Writes output files to the file system based on the provided input and options.
* @param dot The input string in DOT format.
* @param contractName The name of the contract.
* @param outputFormat The format of the output file. choices: svg, png, dot or all. default: png
* @param outputFilename optional filename of the output file.
*/
const writeOutputFiles = async (dot, contractName, outputFormat = 'svg', outputFilename) => {
// If all output then extension is svg
const outputExt = outputFormat === 'all' ? 'svg' : outputFormat;
if (!outputFilename) {
outputFilename =
path_1.default.join(process.cwd(), contractName) + '.' + outputExt;
}
else {
// check if outputFilename is a folder
try {
const folderOrFile = (0, fs_1.lstatSync)(outputFilename);
if (folderOrFile.isDirectory()) {
outputFilename =
path_1.default.join(process.cwd(), outputFilename, contractName) +
'.' +
outputExt;
}
}
catch (err) { } // we can ignore errors as it just means outputFilename does not exist yet
}
if (outputFormat === 'dot' || outputFormat === 'all') {
writeDot(dot, outputFilename);
// No need to continue if only generating a dot file
if (outputFormat === 'dot') {
return;
}
}
const svg = convertDot2Svg(dot);
if (outputFormat === 'svg' || outputFormat === 'all') {
await writeSVG(svg, outputFilename, outputFormat);
}
if (outputFormat === 'png' || outputFormat === 'all') {
await writePng(svg, outputFilename);
}
};
exports.writeOutputFiles = writeOutputFiles;
function convertDot2Svg(dot) {
debug(`About to convert dot to SVG`);
try {
return (0, sync_1.default)(dot);
}
catch (err) {
console.error(`Failed to convert dot to SVG. ${err.message}`);
console.log(dot);
throw new Error(`Failed to parse dot string`, { cause: err });
}
}
exports.convertDot2Svg = convertDot2Svg;
function writeSourceCode(code, filename = 'source', extension = '.sol') {
const fileExtension = path_1.default.extname(filename);
const outputFile = fileExtension === extension ? filename : filename + extension;
debug(`About to write source code to file ${outputFile}`);
(0, fs_1.writeFile)(outputFile, code, (err) => {
if (err) {
throw new Error(`Failed to write source code to file ${outputFile}`, {
cause: err,
});
}
else {
console.log(`Source code written to ${outputFile}`);
}
});
}
exports.writeSourceCode = writeSourceCode;
function writeDot(dot, filename) {
const dotFilename = changeFileExtension(filename, 'dot');
debug(`About to write Dot file to ${dotFilename}`);
(0, fs_1.writeFile)(dotFilename, dot, (err) => {
if (err) {
throw new Error(`Failed to write Dot file to ${dotFilename}`, {
cause: err,
});
}
else {
console.log(`Dot file written to ${dotFilename}`);
}
});
}
exports.writeDot = writeDot;
/**
* Writes an SVG file to the file system.
* @param svg The SVG input to be written to the file system.
* @param svgFilename The desired file name for the SVG file. default: classDiagram.svg
* @param outputFormats The format of the output file. choices: svg, png, dot or all. default: png
* @throws Error - If there is an error writing the SVG file.
*/
function writeSVG(svg, svgFilename = 'classDiagram.svg', outputFormats = 'png') {
debug(`About to write SVG file to ${svgFilename}`);
if (outputFormats === 'png') {
const parsedFile = path_1.default.parse(svgFilename);
if (!parsedFile.dir) {
svgFilename = process.cwd() + '/' + parsedFile.name + '.svg';
}
else {
svgFilename = parsedFile.dir + '/' + parsedFile.name + '.svg';
}
}
return new Promise((resolve, reject) => {
(0, fs_1.writeFile)(svgFilename, svg, (err) => {
if (err) {
reject(new Error(`Failed to write SVG file to ${svgFilename}`, {
cause: err,
}));
}
else {
console.log(`Generated svg file ${svgFilename}`);
resolve();
}
});
});
}
exports.writeSVG = writeSVG;
/**
* Asynchronously writes a PNG file to the file system from an SVG input.
* @param svg - The SVG input to be converted to a PNG file.
* @param filename - The desired file name for the PNG file.
* @throws Error - If there is an error converting or writing the PNG file.
*/
async function writePng(svg, filename) {
const pngFilename = changeFileExtension(filename, 'png');
debug(`About to write png file ${pngFilename}`);
try {
const png = await convert(svg, {
outputFilePath: pngFilename,
});
return new Promise((resolve, reject) => {
(0, fs_1.writeFile)(pngFilename, png, (err) => {
if (err) {
reject(new Error(`Failed to write PNG file to ${pngFilename}`, {
cause: err,
}));
}
else {
console.log(`Generated png file ${pngFilename}`);
resolve();
}
});
});
}
catch (err) {
throw new Error(`Failed to convert PNG file ${pngFilename}`, {
cause: err,
});
}
}
exports.writePng = writePng;
// put a new file extension on a filename
const changeFileExtension = (filename, extension) => {
const parsedFile = path_1.default.parse(filename);
const dir = parsedFile.dir === '' ? '.' : path_1.default.resolve(parsedFile.dir);
return dir + '/' + parsedFile.name + '.' + extension;
};
//# sourceMappingURL=writerFiles.js.map