Skip to content

Commit

Permalink
Added the support of a string and a buffer as value in the 'addSubTre…
Browse files Browse the repository at this point in the history
…e' method
  • Loading branch information
AdrienCastex committed Sep 4, 2017
1 parent 0f2c1d9 commit 3380374
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/manager/v2/fileSystem/CommonTypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="node" />
import { XMLElement } from 'xml-js-builder';
export declare type SimpleCallback = (error?: Error) => void;
export declare type ReturnCallback<T> = (error?: Error, data?: T) => void;
Expand All @@ -17,5 +18,5 @@ export declare class ResourceType {
}
export declare type OpenWriteStreamMode = 'mustCreate' | 'canCreate' | 'mustExist' | 'canCreateIntermediates' | 'mustCreateIntermediates';
export interface SubTree {
[name: string]: ResourceType | SubTree;
[name: string]: ResourceType | SubTree | string | Buffer;
}
4 changes: 2 additions & 2 deletions lib/manager/v2/fileSystem/FileSystem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ export declare abstract class FileSystem implements ISerializableFileSystem {
* @param resourceType Type of the resource to add.
* @param callback Returns an error if one occured.
*/
addSubTree(ctx: RequestContext, resourceType: ResourceType, callback: SimpleCallback): any;
addSubTree(ctx: RequestContext, resourceType: ResourceType | string | Buffer, callback: SimpleCallback): any;
/**
* Add a sub-tree to the file system.
*
Expand All @@ -506,7 +506,7 @@ export declare abstract class FileSystem implements ISerializableFileSystem {
* @param resourceType Type of the resource to add.
* @param callback Returns an error if one occured.
*/
addSubTree(ctx: RequestContext, rootPath: Path | string, resourceType: ResourceType, callback: SimpleCallback): any;
addSubTree(ctx: RequestContext, rootPath: Path | string, resourceType: ResourceType | string | Buffer, callback: SimpleCallback): any;
/**
* Search for locks in the parents, starting at the 'startPath' path.
*
Expand Down
16 changes: 15 additions & 1 deletion lib/manager/v2/fileSystem/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,12 +942,26 @@ var FileSystem = (function () {
if (tree.constructor === CommonTypes_1.ResourceType) {
this.create(ctx, rootPath, tree, callback);
}
else if (tree.constructor === String || tree.constructor === Buffer) {
var data_1 = tree;
this.openWriteStream(ctx, rootPath, 'mustCreate', true, data_1.length, function (e, w, created) {
if (e)
return callback(e);
w.end(data_1);
w.on('error', function (e) {
callback(e);
});
w.on('finish', function () {
callback();
});
});
}
else {
new Workflow_1.Workflow()
.each(Object.keys(tree), function (name, cb) {
var value = tree[name];
var childPath = rootPath.getChildPath(name);
if (value.constructor === CommonTypes_1.ResourceType)
if (value.constructor === CommonTypes_1.ResourceType || value.constructor === String || value.constructor === Buffer)
_this.addSubTree(ctx, childPath, value, cb);
else
_this.addSubTree(ctx, childPath, CommonTypes_1.ResourceType.Directory, function (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/manager/v2/fileSystem/CommonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ export type OpenWriteStreamMode = 'mustCreate' | 'canCreate' | 'mustExist' | 'ca

export interface SubTree
{
[name : string] : ResourceType | SubTree
[name : string] : ResourceType | SubTree | string | Buffer
}
26 changes: 21 additions & 5 deletions src/manager/v2/fileSystem/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ export abstract class FileSystem implements ISerializableFileSystem
* @param resourceType Type of the resource to add.
* @param callback Returns an error if one occured.
*/
addSubTree(ctx : RequestContext, resourceType : ResourceType, callback : SimpleCallback)
addSubTree(ctx : RequestContext, resourceType : ResourceType | string | Buffer, callback : SimpleCallback)
/**
* Add a sub-tree to the file system.
*
Expand All @@ -1450,8 +1450,8 @@ export abstract class FileSystem implements ISerializableFileSystem
* @param resourceType Type of the resource to add.
* @param callback Returns an error if one occured.
*/
addSubTree(ctx : RequestContext, rootPath : Path | string, resourceType : ResourceType, callback : SimpleCallback)
addSubTree(ctx : RequestContext, _rootPath : Path | string | SubTree | ResourceType | SimpleCallback, _tree : SubTree | ResourceType | SimpleCallback, _callback ?: SimpleCallback)
addSubTree(ctx : RequestContext, rootPath : Path | string, resourceType : ResourceType | string | Buffer, callback : SimpleCallback)
addSubTree(ctx : RequestContext, _rootPath : Path | string | SubTree | ResourceType | SimpleCallback | string | Buffer, _tree : SubTree | ResourceType | SimpleCallback | string | Buffer, _callback ?: SimpleCallback)
{
const tree = _callback ? _tree as SubTree | ResourceType : _rootPath as SubTree | ResourceType;
const rootPath = _callback ? new Path(_rootPath as Path | string) : new Path('/');
Expand All @@ -1462,14 +1462,30 @@ export abstract class FileSystem implements ISerializableFileSystem
{
this.create(ctx, rootPath, tree as ResourceType, callback);
}
else if(tree.constructor === String || tree.constructor === Buffer)
{
const data : String | Buffer = tree as any;
this.openWriteStream(ctx, rootPath, 'mustCreate', true, data.length, (e, w, created) => {
if(e)
return callback(e);

w.end(data);
w.on('error', (e) => {
callback(e);
})
w.on('finish', () => {
callback();
})
})
}
else
{
new Workflow()
.each(Object.keys(tree), (name, cb) => {
const value = tree[name];
const childPath = rootPath.getChildPath(name);
if(value.constructor === ResourceType)
this.addSubTree(ctx, childPath, value as ResourceType, cb)
if(value.constructor === ResourceType || value.constructor === String || value.constructor === Buffer)
this.addSubTree(ctx, childPath, value, cb)
else
this.addSubTree(ctx, childPath, ResourceType.Directory, (e) => {
if(e)
Expand Down

0 comments on commit 3380374

Please sign in to comment.