Skip to content

Commit

Permalink
feat(pv-styleguide, assemble-lite): read and write to memory fs
Browse files Browse the repository at this point in the history
when in memory file system is provided by webpack, lsg and assemble read and write the output to it.
compared to reading and writing to the actuall file system, this is slightly faster.
  • Loading branch information
mbehzad committed Jul 2, 2024
1 parent 90a3a7d commit a55eb7f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
19 changes: 12 additions & 7 deletions packages/assemble-lite/Assemble.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
const { basename, relative, dirname, extname } = require("path");
const { promisify } = require("util");
const { readJson, readFile } = require("fs-extra");
const fsExtra = require("fs-extra");
const pvHandlebars = require("handlebars").create();
const { loadFront } = require("yaml-front-matter");
const handlebarsHelpers = require("handlebars-helpers/lib/index");
const { load } = require("js-yaml");

const Timer = require("./Timer");
const Visitor = require("./Visitor");
const {
getPaths,
asyncReadFile,
asyncWriteFile,
getName,
} = require("./helper/io-helper");
const { getPaths, asyncReadFile, getName } = require("./helper/io-helper");

/**
* provides an instance which build html pages from handlebars templates, helpers and data
Expand Down Expand Up @@ -40,6 +37,7 @@ module.exports = class Assemble {
// it helps the user to not forget to fix stuff,
// and sometimes a simple re-run might help. e.g. when there was a i/o issues
this.failedPaths = [];
this.fs = fsExtra;
}

// console logs in verbose mode
Expand Down Expand Up @@ -443,7 +441,7 @@ module.exports = class Assemble {

// only write to disc when the value changes
if (html !== tpl.output)
await asyncWriteFile(targetDir, reldir, filename, html);
this._asyncWriteFile(targetDir, reldir, filename, html);
tpl.output = html;
}

Expand Down Expand Up @@ -622,4 +620,11 @@ module.exports = class Assemble {
delete obj[key];
});
}

// writes to the content to the file system. depending on the setup this might be the real file system or one in memory.
async _asyncWriteFile(target, reldir, filename, markup) {
await promisify(this.fs.mkdir)(`${target}/${reldir}`, { recursive: true });
// eslint-disable-next-line prettier/prettier
await promisify(this.fs.writeFile)(`${target}/${reldir}/${filename}.html`, markup);
}
};
22 changes: 16 additions & 6 deletions packages/pv-stylemark/helper/io-helper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
const { ensureDir, writeFile: fsWriteFile, watchFile } = require("fs-extra");
let fs = require("fs-extra");
const { promisify } = require("util");
const { glob } = require("glob");
const { resolve, normalize } = require("path");

// can override the filesystem api used to read and write files, e.g. using memfs via webpack.
function updateFileSystemConnector(fileSystem) {
fs = fileSystem;
}

function readFile(...args) {
return promisify(fs.readFile)(...args);
}

const writeFile = async (target, reldir, filename, markup) => {
await ensureDir(`${target}/${reldir}`);
return await fsWriteFile(`${target}/${reldir}/${filename}.html`, markup, {
encoding: "utf8",
});
await promisify(fs.mkdir)(`${target}/${reldir}`, { recursive: true });
return promisify(fs.writeFile)(`${target}/${reldir}/${filename}.html`, markup);
};

const watchGlob = async (curGlob, callback) => {
Expand All @@ -15,11 +23,13 @@ const watchGlob = async (curGlob, callback) => {
});
const normalizedPaths = paths.map(filePath => normalize(resolve(process.cwd(), filePath)));
normalizedPaths.forEach(path => {
watchFile(path, () => callback());
fs.watchFile(path, () => callback());
});
};

module.exports = {
writeFile,
watchGlob,
updateFileSystemConnector,
readFile,
};
6 changes: 3 additions & 3 deletions packages/pv-stylemark/tasks/lsg/buildLsgExamples.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { resolve } = require("path");
const { readFile } = require("fs-extra");
const { readFile: fsReadFile } = require("fs-extra");
const hbsInstance = require("handlebars").create();

const { writeFile } = require("../../helper/io-helper");
const { writeFile, readFile } = require("../../helper/io-helper");
const { resolveApp, getAppConfig, join } = require("../../helper/paths");

const loadTemplate = async hbsInst => {
const templateContent = await readFile(resolve(__dirname, "../templates/lsg-example.hbs"), {
const templateContent = await fsReadFile(resolve(__dirname, "../templates/lsg-example.hbs"), {
encoding: "utf-8",
});
return hbsInst.compile(templateContent);
Expand Down
3 changes: 3 additions & 0 deletions packages/pv-stylemark/webpack-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Assemble = require("@pro-vision/assemble-lite/Assemble");
const buildStylemark = require("../scripts/buildStylemarkLsg");
const { getFilesToWatch, fileGlobes } = require("./getFilesToWatch");
const { resolveApp, getAppConfig, join } = require("../helper/paths");
const { updateFileSystemConnector } = require("../helper/io-helper");

const { destPath, componentsSrc } = getAppConfig();
class PvStylemarkPlugin {
Expand Down Expand Up @@ -43,6 +44,7 @@ class PvStylemarkPlugin {
const buildLsg = buildAssemble || copyStylemarkFiles;

if (buildAssemble) {
this.assemble.fs = compiler.outputFileSystem;
await this.assemble.build(
{
baseDir: resolveApp(componentsSrc),
Expand All @@ -63,6 +65,7 @@ class PvStylemarkPlugin {
}

if (buildLsg) {
updateFileSystemConnector(compiler.outputFileSystem);
await buildStylemark({
// unless files were changed but none was a static stylemark file
shouldCopyStyleguideFiles: copyStylemarkFiles,
Expand Down

0 comments on commit a55eb7f

Please sign in to comment.