Skip to content

Commit

Permalink
Added the static method 'loadFromPath(...)' in the 'PhysicalFolder' c…
Browse files Browse the repository at this point in the history
…lass to allow easy resource tree loading based on a url
  • Loading branch information
AdrienCastex committed Jun 16, 2017
1 parent 7ed6c7a commit b8c5ca0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/resource/physical/PhysicalFolder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export declare class PhysicalFolder extends PhysicalResource {
addChild(resource: IResource, callback: SimpleCallback): void;
removeChild(resource: IResource, callback: SimpleCallback): void;
getChildren(callback: ReturnCallback<IResource[]>): void;
static loadFromPath(path: string, callback: ReturnCallback<PhysicalFolder>): void;
}
47 changes: 47 additions & 0 deletions lib/resource/physical/PhysicalFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,42 @@ Object.defineProperty(exports, "__esModule", { value: true });
var IResource_1 = require("../IResource");
var ResourceChildren_1 = require("../std/ResourceChildren");
var PhysicalResource_1 = require("./PhysicalResource");
var PhysicalFile_1 = require("./PhysicalFile");
var Workflow_1 = require("../../helper/Workflow");
var Errors_1 = require("../../Errors");
var path = require("path");
var fs = require("fs");
function loader(fpath, callback) {
fs.readdir(fpath, function (e, files) {
if (e)
throw e;
new Workflow_1.Workflow()
.each(files, function (file, cb) {
var fullPath = path.join(fpath, file);
fs.stat(fullPath, function (e, stat) {
if (e)
cb(e);
else if (stat.isFile())
cb(null, new PhysicalFile_1.PhysicalFile(fullPath));
else {
var folder_1 = new PhysicalFolder(fullPath);
loader(fullPath, function (e, resources) {
if (e)
cb(e);
else {
new Workflow_1.Workflow()
.each(resources, function (r, cb) { return folder_1.addChild(r, cb); })
.error(cb)
.done(function () { return cb(null, folder_1); });
}
});
}
});
})
.error(callback)
.done(function (resources) { return callback(null, resources); });
});
}
var PhysicalFolder = (function (_super) {
__extends(PhysicalFolder, _super);
function PhysicalFolder(realPath, parent, fsManager) {
Expand Down Expand Up @@ -98,6 +132,19 @@ var PhysicalFolder = (function (_super) {
PhysicalFolder.prototype.getChildren = function (callback) {
callback(null, this.children.children);
};
PhysicalFolder.loadFromPath = function (path, callback) {
loader(path, function (e, resources) {
if (!e) {
var folder_2 = new PhysicalFolder(path);
new Workflow_1.Workflow()
.each(resources, function (r, cb) { return folder_2.addChild(r, cb); })
.error(function (e) { return callback(e, null); })
.done(function () { return callback(null, folder_2); });
}
else
callback(e, null);
});
};
return PhysicalFolder;
}(PhysicalResource_1.PhysicalResource));
exports.PhysicalFolder = PhysicalFolder;
55 changes: 55 additions & 0 deletions src/resource/physical/PhysicalFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,49 @@ import { Readable, Writable } from 'stream'
import { ResourceChildren } from '../std/ResourceChildren'
import { StandardResource } from '../std/StandardResource'
import { PhysicalResource } from './PhysicalResource'
import { PhysicalFile } from './PhysicalFile'
import { FSManager } from '../../manager/FSManager'
import { Workflow } from '../../helper/Workflow'
import { Errors } from '../../Errors'
import * as path from 'path'
import * as fs from 'fs'

function loader(fpath : string, callback : (error : Error, resources ?: IResource[]) => void)
{
fs.readdir(fpath, (e, files) => {
if(e) throw e;

new Workflow()
.each(files, (file, cb) => {
const fullPath = path.join(fpath, file);

fs.stat(fullPath, (e, stat) => {
if(e)
cb(e);
else if(stat.isFile())
cb(null, new PhysicalFile(fullPath));
else
{
const folder = new PhysicalFolder(fullPath);
loader(fullPath, (e, resources) => {
if(e)
cb(e);
else
{
new Workflow()
.each(resources, (r, cb) => folder.addChild(r, cb))
.error(cb)
.done(() => cb(null, folder));
}
})
}
})
})
.error(callback)
.done((resources) => callback(null, resources));
});
}

export class PhysicalFolder extends PhysicalResource
{
children : ResourceChildren
Expand Down Expand Up @@ -120,4 +159,20 @@ export class PhysicalFolder extends PhysicalResource
{
callback(null, this.children.children);
}

static loadFromPath(path : string, callback : ReturnCallback<PhysicalFolder>)
{
loader(path, (e, resources) => {
if(!e)
{
const folder = new PhysicalFolder(path);
new Workflow()
.each(resources, (r, cb) => folder.addChild(r, cb))
.error((e) => callback(e, null))
.done(() => callback(null, folder));
}
else
callback(e, null);
})
}
}

0 comments on commit b8c5ca0

Please sign in to comment.