This repository was archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathwriter.js
231 lines (205 loc) · 7.87 KB
/
writer.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* apidoc
* https://apidocjs.com
*
* Copyright (c) 2013 inveris OHG
* Authors: Peter Rottmann <rottmann@inveris.de>
* Nicolas CARPi @ Deltablot
* Licensed under the MIT license.
*/
/**
* Write output files
*/
class Writer {
constructor (api, app, cacheBustingQueryParam = `v=${Date.now()}`) {
this.api = api;
this.log = app.log;
this.opt = app.options;
this.cacheBustingQueryParam = String(cacheBustingQueryParam);
this.fs = require('fs-extra');
this.path = require('path');
}
// The public method
write () {
if (this.opt.dryRun) {
this.log.info('Dry run mode enabled: no files created.');
return new Promise((resolve, reject) => { return resolve(); });
}
this.log.verbose('Writing files...');
if (this.opt.single) {
return this.createSingleFile();
}
return this.createOutputFiles();
}
/**
* Find assets from node_modules folder and return its path
* Argument is the path relative to node_modules folder
*/
findAsset (assetPath) {
try {
const path = require.resolve(assetPath);
return path;
} catch {
this.log.error('Could not find where dependencies of apidoc live!');
}
}
createOutputFiles () {
this.createDir(this.opt.dest);
// create index.html
this.log.verbose('Copying template index.html to: ' + this.opt.dest);
this.fs.writeFileSync(this.path.join(this.opt.dest, 'index.html'), this.getIndexContent());
// create assets folder
const assetsPath = this.path.resolve(this.path.join(this.opt.dest + 'assets'));
this.createDir(assetsPath);
// add the fonts
this.log.verbose('Copying fonts to: ' + assetsPath);
this.fs.copySync(this.path.join(this.opt.template, 'fonts'), assetsPath);
// save the parsed api file
if (this.opt.writeJson) {
const jsonFile = this.path.join(assetsPath, 'api-data.json');
this.log.verbose('Saving parsed API to: ' + jsonFile);
this.fs.writeFileSync(jsonFile, this.api.data);
}
// CSS from dependencies
this.log.verbose('Copying bootstrap css to: ' + assetsPath);
this.fs.copySync(this.findAsset('bootstrap/dist/css/bootstrap.min.css'), this.path.join(assetsPath, 'bootstrap.min.css'));
this.fs.copySync(this.findAsset('bootstrap/dist/css/bootstrap.min.css.map'), this.path.join(assetsPath, 'bootstrap.min.css.map'));
this.log.verbose('Copying prism css to: ' + assetsPath);
this.fs.copySync(this.findAsset('prismjs/themes/prism-tomorrow.css'), this.path.join(assetsPath, 'prism.css'));
this.fs.copySync(this.findAsset('prismjs/plugins/toolbar/prism-toolbar.css'), this.path.join(assetsPath, 'prism-toolbar.css'));
this.fs.copySync(this.findAsset('prismjs/plugins/diff-highlight/prism-diff-highlight.css'), this.path.join(assetsPath, 'prism-diff-highlight.css'));
this.log.verbose('Copying main css to: ' + assetsPath);
// main.css
this.fs.copySync(this.path.join(this.opt.template, 'src', 'css', 'main.css'), this.path.join(assetsPath, 'main.css'));
// images
this.fs.copySync(this.path.join(this.opt.template, 'img'), assetsPath);
return this.runWebpack(this.path.resolve(assetsPath));
}
/**
* Run webpack in a promise
*/
runWebpack (outputPath) {
this.log.verbose('Running webpack bundler');
return new Promise((resolve, reject) => {
// run webpack to create the bundle file in assets
const webpackConfig = require(this.path.resolve(this.path.join(this.opt.template, 'src', 'webpack.config.js')));
const webpack = require('webpack');
// set output
webpackConfig.output.path = outputPath;
this.log.debug('webpack output folder: ' + webpackConfig.output.path);
// set data
const plugins = [
new webpack.DefinePlugin({
API_DATA: this.api.data,
API_PROJECT: this.api.project,
}),
];
webpackConfig.plugins = plugins;
// if the --debug flag is passed, produce unminified bundle with inline map
let mode = 'production';
// https://webpack.js.org/configuration/devtool/ - constistent type
let devtool = '';
if (this.opt.debug) {
mode = 'development';
devtool = 'inline-source-map';
}
webpackConfig.mode = mode;
webpackConfig.devtool = devtool || false;
const compiler = webpack(webpackConfig);
compiler.run((err, stats) => {
if (err) {
this.log.error('Webpack failure:', err);
return reject(err);
}
this.log.debug('Generated bundle with hash: ' + stats.hash);
return resolve(outputPath);
});
});
}
/**
* Get index.html content as string with placeholder values replaced
*/
getIndexContent () {
const projectInfo = JSON.parse(this.api.project);
const title = projectInfo.title || projectInfo.name || 'Loading...';
const description = projectInfo.description || projectInfo.name || 'API Documentation';
const indexHtml = this.fs.readFileSync(this.path.join(this.opt.template, 'index.html'), 'utf8');
return indexHtml.toString()
// replace titles, descriptions and cache busting query params
.replace(/__API_NAME__/g, title)
.replace(/__API_DESCRIPTION__/g, description)
.replace(/__API_CACHE_BUSTING_QUERY_PARAM__/g, this.cacheBustingQueryParam);
}
createSingleFile () {
// dest is a file path, so get the folder with dirname
this.createDir(this.path.dirname(this.opt.dest));
// get all css content
const bootstrapCss = this.fs.readFileSync(this.findAsset('bootstrap/dist/css/bootstrap.min.css'), 'utf8');
const prismCss = this.fs.readFileSync(this.findAsset('prismjs/themes/prism-tomorrow.css'), 'utf8');
const mainCss = this.fs.readFileSync(this.path.join(this.opt.template, 'src', 'css', 'main.css'), 'utf8');
const tmpPath = '/tmp/apidoc-tmp';
// TODO add favicons in base64 in the html
this.createDir(tmpPath);
return this.runWebpack(tmpPath).then(tmpPath => {
const mainBundle = this.fs.readFileSync(this.path.join(tmpPath, 'main.bundle.js'), 'utf8');
// modify index html for single page use
const indexContent = this.getIndexContent()
// remove link to css normally in assets
.replace(/<link href="assets[^>]*>/g, '')
// remove call to main bundle in assets
.replace(/<script src="assets[^>]*><\/script>/, '');
// concatenate all the content (html + css + javascript bundle)
const finalContent = `${indexContent}
<style>${bootstrapCss} ${prismCss} ${mainCss}</style>
<script>${mainBundle}</script>`;
// create target file
const finalPath = this.path.join(this.opt.dest, 'index.html');
// make sure destination exists
this.createDir(this.opt.dest);
this.log.verbose(`Generating self-contained single file: ${finalPath}`);
this.fs.writeFileSync(finalPath, finalContent);
});
}
/**
* Write a JSON file
*
* @param {string} dest Destination path
* @param {string} data Content of the file
*/
writeJsonFile (dest, data) {
this.log.verbose(`Writing json file: ${dest}`);
this.fs.writeFileSync(dest, data + '\n');
}
/**
* Write js file
*
* @param {string} dest Destination path
* @param {string} data Content of the file
*/
writeJSFile (dest, data) {
this.log.verbose(`Writing js file: ${dest}`);
switch (this.opt.mode) {
case 'amd':
case 'es':
this.fs.writeFileSync(dest, 'export default ' + data + ';\n');
break;
case 'commonJS':
this.fs.writeFileSync(dest, 'module.exports = ' + data + ';\n');
break;
default:
this.fs.writeFileSync(dest, 'define(' + data + ');' + '\n');
}
}
/**
* Create a directory
*
* @param {string} dir Path of the directory to create
*/
createDir (dir) {
if (!this.fs.existsSync(dir)) {
this.log.verbose('Creating dir: ' + dir);
this.fs.mkdirsSync(dir);
}
}
}
module.exports = Writer;