Skip to content

Commit

Permalink
Ensure JSON assets are hashed
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiemh committed Jan 29, 2024
1 parent bec02f4 commit 6558b7c
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/mdfiles/lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Plugin = require('broccoli-caching-writer');
const frontmatter = require('front-matter');
const mkdirp = require('mkdirp');
const crypto = require('crypto');
const path = require('path');
const fs = require('fs');

Expand All @@ -26,16 +27,23 @@ module.exports = class Trees extends Plugin {

if (this.opts.type === 'addon') {

let output = this.opts.folders.reduce((list, folder) => {
let content = this.opts.folders.reduce((list, folder) => {

let text = JSON.stringify(output[folder.name]);
let hash = crypto.createHash('md5').update(text).digest('hex').substring(0,6);

list[folder.name] = path.join(
this.opts.rootURL,
this.opts.outputDir,
folder.name,
'index.json'
`index-${hash}.json`
);

return list;

}, {});
let buffer = `export default ${JSON.stringify(output)}`;

let buffer = `export default ${JSON.stringify(content)}`;
fs.writeFileSync(path.join(this.outputPath, 'files.js'), buffer);

}
Expand All @@ -44,13 +52,16 @@ module.exports = class Trees extends Plugin {

for (const [name, entries] of Object.entries(output)) {

let text = JSON.stringify(entries);
let hash = crypto.createHash('md5').update(text).digest('hex').substring(0,6);

// Write the index.json file contents

let buffer = JSON.stringify(entries.map(v => {
return { name: v.name, path: v.path, attributes: v.attributes };
}));
let folder = path.join(this.outputPath, this.opts.outputDir, name);
let file = path.join(folder, 'index.json');
let file = path.join(folder, `index-${hash}.json`);
mkdirp.sync(folder);
fs.writeFileSync(file, buffer);

Expand Down

0 comments on commit 6558b7c

Please sign in to comment.