mushroom-cli 1.2.8 → 2.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
2
  import open from 'open';
3
- import { minify } from 'minify';
3
+ import { minify } from 'html-minifier-terser';
4
4
  import { fileURLToPath } from 'url';
5
5
  import path from 'path';
6
6
  import * as common from '../common.js';
@@ -227,42 +227,58 @@ function filterEntityFields(entity) {
227
227
  async function writeDocs(filePath, strTemplate, strData) {
228
228
  const placeholder = '<script id="INJECT-JAVASCRIPT-HERE"></script>';
229
229
  const idx = strTemplate.indexOf(placeholder);
230
- fs.writeFileSync(filePath, strTemplate.substring(0, idx));
231
- fs.appendFileSync(filePath, '<script>window.docs = ');
232
- fs.appendFileSync(filePath, strData);
233
- fs.appendFileSync(filePath, ';\n');
234
- fs.appendFileSync(filePath, fs.readFileSync(path.join(__dirname, '../docs/docs.v2.js')));
235
- fs.appendFileSync(filePath, '</script>');
236
- fs.appendFileSync(filePath, strTemplate.substring(idx + placeholder.length));
237
- fs.writeFileSync(filePath, await minify(filePath, {
238
- html: {
239
- removeComments: true,
240
- collapseWhitespace: true,
241
- collapseBooleanAttributes: true,
242
- removeAttributeQuotes: true,
243
- removeRedundantAttributes: true,
244
- useShortDoctype: true,
245
- removeEmptyAttributes: true,
246
- removeEmptyElements: false,
247
- removeOptionalTags: true,
248
- removeScriptTypeAttributes: false,
249
- removeStyleLinkTypeAttributes: true,
250
- minifyJS: true,
251
- minifyCSS: true,
252
- html5: true,
253
- },
254
- }));
255
- copyStaticFiles(path.dirname(filePath), [
256
- 'bootstrap-theme.min.css',
257
- 'bootstrap.min.css',
258
- 'bootstrap.min.js',
259
- 'jquery.min.js',
260
- ]);
230
+ if (idx === -1) {
231
+ throw new Error(`Docs template does not contain the placeholder ${placeholder}`);
232
+ }
233
+ const docsScript = fs.readFileSync(path.join(__dirname, '../docs/docs.v2.js')).toString();
234
+ let html = strTemplate.substring(0, idx) +
235
+ '<script>window.docs = ' +
236
+ strData +
237
+ ';\n' +
238
+ docsScript +
239
+ '</script>' +
240
+ strTemplate.substring(idx + placeholder.length);
241
+ html = await minify(html, {
242
+ removeComments: true,
243
+ collapseWhitespace: true,
244
+ collapseBooleanAttributes: true,
245
+ removeAttributeQuotes: true,
246
+ removeRedundantAttributes: true,
247
+ useShortDoctype: true,
248
+ removeEmptyAttributes: true,
249
+ removeEmptyElements: false,
250
+ removeOptionalTags: true,
251
+ removeScriptTypeAttributes: false,
252
+ removeStyleLinkTypeAttributes: true,
253
+ minifyJS: true,
254
+ minifyCSS: true,
255
+ html5: true,
256
+ });
257
+ html = injectStaticFiles(html, ['bootstrap-theme.min.css', 'bootstrap.min.css', 'bootstrap.min.js', 'jquery.min.js']);
258
+ fs.writeFileSync(filePath, html);
261
259
  }
262
- const copyStaticFiles = (folderPath, files) => {
260
+ const injectStaticFiles = (content, files) => {
261
+ let html = content;
263
262
  files.forEach((file) => {
264
- fs.copyFileSync(path.join(__dirname, '../docs/', file), path.join(folderPath, file));
263
+ const sourcePath = path.join(__dirname, '../docs/', file);
264
+ if (!fs.existsSync(sourcePath)) {
265
+ common.logWarning(`Static file ${file} does not exist, skipping...`);
266
+ return;
267
+ }
268
+ const escapedFile = file.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
269
+ const isCss = path.extname(file).toLowerCase() === '.css';
270
+ const tagPattern = isCss
271
+ ? new RegExp(`<link[^>]*href=["']?${escapedFile}["']?[^>]*>`, 'gi')
272
+ : new RegExp(`<script[^>]*src=["']?${escapedFile}["']?[^>]*>\\s*</script>`, 'gi');
273
+ if (!tagPattern.test(html)) {
274
+ return;
275
+ }
276
+ tagPattern.lastIndex = 0;
277
+ const assetContent = fs.readFileSync(sourcePath).toString();
278
+ const inlined = isCss ? `<style>${assetContent}</style>` : `<script>${assetContent}</script>`;
279
+ html = html.replace(tagPattern, () => inlined);
265
280
  });
281
+ return html;
266
282
  };
267
283
  export default async function genDocs(args, projectName, project, alias, source) {
268
284
  const aliasProject = ((project.docs || {}).alias || {})[alias];
@@ -300,6 +316,7 @@ export default async function genDocs(args, projectName, project, alias, source)
300
316
  environmentVariables: source.environmentVariables,
301
317
  entities: entities,
302
318
  });
319
+ common.ensureFolder(aliasProject.output);
303
320
  const html = fs.readFileSync(__dirname + '/../docs/docs.v2.html').toString();
304
321
  if (args[3] === 'tmp') {
305
322
  const htmlFilePath = aliasProject.output + '_tmp.html';
@@ -320,7 +337,6 @@ export default async function genDocs(args, projectName, project, alias, source)
320
337
  }
321
338
  return;
322
339
  }
323
- common.ensureFolder(aliasProject.output);
324
340
  common.logInfo('Writing to', aliasProject.output);
325
341
  await writeDocs(aliasProject.output, html, data);
326
342
  common.logInfo(`Generated API docs for ${projectName}@${alias} to ${aliasProject.output}`);