Skip to content

Commit

Permalink
parse recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
m-scherpi committed Nov 16, 2018
1 parent f1f563f commit 3fe6d60
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/FolderParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const path = require('path')
const fs = require('fs')

function recFindByName(base, name, files, result) {
files = files || fs.readdirSync(base);
result = result || [];

files.forEach((file) => {
const newbase = path.join(base, file);
if (fs.statSync(newbase).isDirectory()) {
result = recFindByName(newbase, name, fs.readdirSync(newbase), result)
}else {
if (file === name) {
result.push(newbase)
}
}
}
);
return result
}

function FolderParser() {

const findManifestFiles = (path) => {
return recFindByName(path, "manifest.json")
};

return {
findManifestFiles
}
}

module.exports = FolderParser;
22 changes: 22 additions & 0 deletions test/FolderParser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const {assert} = require("chai");
const FolderParser = require("../src/FolderParser");

describe('FolderParser', () => {
it('should find simple path', () => {
const parser = FolderParser();
let manifestPaths = parser.findManifestFiles("./test/data/simple-component");

assert.equal(manifestPaths.length, 1);
assert.equal(manifestPaths[0],"test\\data\\simple-component\\manifest.json");
});

it('should find recursive path', () => {
const parser = FolderParser();
let manifestPaths = parser.findManifestFiles("./test/data/dir");

assert.equal(manifestPaths.length, 3);
assert(manifestPaths.indexOf("test\\data\\dir\\subdir\\manifest.json") > -1);
assert(manifestPaths.indexOf("test\\data\\dir\\subdir2\\manifest.json") > -1);
assert(manifestPaths.indexOf("test\\data\\dir\\subdir2\\subdir3\\manifest.json") > -1);
});
});
8 changes: 8 additions & 0 deletions test/data/dir/subdir/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Bundle",
"components": [
{
"name": "Component"
}
]
}
1 change: 1 addition & 0 deletions test/data/dir/subdir/subdir4/Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => {};
8 changes: 8 additions & 0 deletions test/data/dir/subdir2/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Bundle",
"components": [
{
"name": "Component"
}
]
}
8 changes: 8 additions & 0 deletions test/data/dir/subdir2/subdir3/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Bundle",
"components": [
{
"name": "Component"
}
]
}

0 comments on commit 3fe6d60

Please sign in to comment.