Skip to content

Commit

Permalink
feat: find requested ISML template (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
vysinsky committed May 16, 2022
1 parent 5aa16b8 commit 123327a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/server/Response.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { inspect } = require('util');
const dm = require('deepmerge');
const { locateSingleFileInCartridges } = require('../utils');
const { readFileSync } = require('cosmiconfig/dist/readFile');

class Response {
cachePeriod;
Expand Down Expand Up @@ -45,10 +47,16 @@ class Response {
this._applyViewData(data);
this.view = name;

const templatePath = locateSingleFileInCartridges(
`templates/default/${name}.isml`
);

this._appendRenderings({
type: 'render',
subType: 'isml',
view: name,
templatePath,
templateSource: templatePath ? readFileSync(templatePath) : undefined,
});
}

Expand Down
24 changes: 24 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
const { existsSync, realpathSync } = require('fs');

/**
* Locates single file according to cartridge path. Returns the first found file
* Ignores non-existing cartridges.
*
* @param path {string}
*/
function locateSingleFileInCartridges(path) {
for (const cartridge of playgroundConfig.cartridgePath.split(':')) {
const dir = `${playgroundConfig.cartridgesDir}/${cartridge}`;
if (!existsSync(dir)) {
continue;
}

if (existsSync(`${dir}/cartridge/${path}`)) {
return realpathSync(`${dir}/cartridge/${path}`);
}

if (existsSync(`${dir}/cartridge/${path}.js`)) {
return realpathSync(`${dir}/cartridge/${path}.js`);
}
}
}

/**
* Locates all files according to cartridge path.
* Ignores non-existing cartridges.
Expand All @@ -26,5 +49,6 @@ function locateAllFilesInCartridges(path) {
}

module.exports = {
locateSingleFileInCartridges,
locateAllFilesInCartridges,
};

0 comments on commit 123327a

Please sign in to comment.