Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
fix to recursively create and build .abell files with cli
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhdaware committed May 21, 2020
1 parent e03f946 commit 969310f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v0.1.9
- Fix to recursively find and create nested `.abell` files

## v0.1.8
- basePath is set to paths relative to input files.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "abell-renderer",
"version": "0.1.8",
"version": "0.1.9",
"description": "A wrapper arround Mustache that adds some additional features and some syntatic sugar required for abell",
"main": "dist/index.js",
"bin": {
Expand Down
38 changes: 32 additions & 6 deletions src/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ const green = (message) => `\u001b[32m${message}\u001b[39m`;

const cwd = process.cwd();


const recursiveFind = (base, ext, inputFiles, inputResult) => {
const files = inputFiles || fs.readdirSync(base);
let result = inputResult || [];

for (const file of files) {
const newbase = path.join(base, file);
if (fs.statSync(newbase).isDirectory()) {
result = recursiveFind(newbase, ext, fs.readdirSync(newbase), result);
} else {
if (file.substr(-1 * ext.length) == ext) {
result.push(newbase);
}
}
}

return result;
};

const createPathIfAbsent = (pathToCreate) => {
if (!fs.existsSync(pathToCreate)) {
fs.mkdirSync(pathToCreate);
}
};

/**
* @method generateHTMLFromAbell
* @param {string} inputPath - A complete input path of the .abell file
Expand All @@ -19,7 +44,7 @@ const cwd = process.cwd();
*/
function generateHTMLFromAbell(inputPath, outputPath, abellRenderOptions) {
console.log(`${ green('-') } 📜 Rendering ${inputPath.replace(cwd, '')}`);

createPathIfAbsent(path.dirname(outputPath));
const data = fs.readFileSync(
inputPath,
{encoding: 'utf8', flag: 'r'}
Expand Down Expand Up @@ -55,13 +80,14 @@ function build() {
generateHTMLFromAbell(inputPath, outputPath, {basePath});
} else {
// If input is a directory
const filesInDirectory = fs.readdirSync(inputPath);
const relativePaths = recursiveFind(inputPath, '.abell')
.map(absolutePath => path.relative(inputPath, absolutePath));

for (const file of filesInDirectory) {
for (const filepath of relativePaths) {
generateHTMLFromAbell(
path.join(inputPath, file),
path.join(outputPath, file.replace('.abell', '.html')),
{basePath: path.dirname(path.join(inputPath, file))}
path.join(inputPath, filepath),
path.join(outputPath, filepath.replace('.abell', '.html')),
{basePath: path.dirname(path.join(inputPath, filepath))}
);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/example/out/loop_example/components/nice.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Hello</div>
1 change: 1 addition & 0 deletions src/example/templates/loop_example/components/nice.abell
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>{{ 'Hello' }}</div>
2 changes: 2 additions & 0 deletions src/example/templates/loop_example/components/random.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 'bar';
module.exports = foo;

0 comments on commit 969310f

Please sign in to comment.