Comparing version 1.0.0 to 1.0.4
@@ -1,29 +0,27 @@ | ||
import VirtualFile from "./file"; | ||
declare class VirtualDirectory { | ||
static builder(): VirtualDirectory.Builder; | ||
import { ImmutableFile } from "./file"; | ||
export declare type MutableDirectory = { | ||
[key: string]: string | MutableDirectory; | ||
}; | ||
export declare class ImmutableDirectory { | ||
static builder(): ImmutableDirectoryBuilder; | ||
private _children; | ||
constructor(children: { | ||
[key: string]: VirtualFile | VirtualDirectory; | ||
}); | ||
list(): { | ||
[key: string]: VirtualFile | VirtualDirectory; | ||
}; | ||
merged(other: VirtualDirectory): VirtualDirectory; | ||
constructor(children: Map<string, ImmutableFile | ImmutableDirectory>); | ||
static from(mutableDirectory: MutableDirectory): ImmutableDirectory; | ||
toMutable(): MutableDirectory; | ||
list(): Map<string, ImmutableFile | ImmutableDirectory>; | ||
merged(other: ImmutableDirectory): ImmutableDirectory; | ||
toString(): string; | ||
inspect(depth: number, opts?: {}): string; | ||
private inspectInternal(textBuilder, depth, opts); | ||
static wrap(path: string, terminalNode: VirtualDirectory | VirtualFile): VirtualDirectory; | ||
static unwrap(path: string, rootDirectory: VirtualDirectory): VirtualDirectory | VirtualFile; | ||
static merged(...directories: VirtualDirectory[]): VirtualDirectory; | ||
static wrap(path: string, terminalNode: ImmutableDirectory | ImmutableFile): ImmutableDirectory; | ||
static unwrap(path: string, rootDirectory: ImmutableDirectory): ImmutableDirectory | ImmutableFile; | ||
static merged(...directories: ImmutableDirectory[]): ImmutableDirectory; | ||
} | ||
declare namespace VirtualDirectory { | ||
class Builder { | ||
private _children; | ||
constructor(); | ||
addFile(name: string, f: VirtualFile | string): VirtualDirectory.Builder; | ||
addDirectory(name: string, d: VirtualDirectory | VirtualDirectory.Builder): VirtualDirectory.Builder; | ||
addChild(name: string, child: VirtualFile | VirtualDirectory): VirtualDirectory.Builder; | ||
build(): VirtualDirectory; | ||
} | ||
export declare class ImmutableDirectoryBuilder { | ||
private _children; | ||
constructor(); | ||
addFile(name: string, f: ImmutableFile | string): ImmutableDirectoryBuilder; | ||
addDirectory(name: string, d: ImmutableDirectory | ImmutableDirectoryBuilder): ImmutableDirectoryBuilder; | ||
addChild(name: string, child: ImmutableFile | ImmutableDirectory): ImmutableDirectoryBuilder; | ||
build(): ImmutableDirectory; | ||
} | ||
export default VirtualDirectory; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const file_1 = require("./file"); | ||
const textbuilder_1 = require("textbuilder"); | ||
const file_1 = require("./file"); | ||
class VirtualDirectory { | ||
class ImmutableDirectory { | ||
static builder() { | ||
return new VirtualDirectory.Builder(); | ||
return new ImmutableDirectoryBuilder(); | ||
} | ||
constructor(children) { | ||
this._children = Object.assign({}, children); | ||
this._children = new Map(children); | ||
} | ||
static from(mutableDirectory) { | ||
let builder = ImmutableDirectory.builder(); | ||
for (let key of Object.keys(mutableDirectory)) { | ||
let entry = mutableDirectory[key]; | ||
if (typeof entry == "string") { | ||
builder.addFile(key, entry); | ||
} | ||
else { | ||
builder.addDirectory(key, this.from(entry)); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
toMutable() { | ||
let mutable = {}; | ||
for (let [name, child] of this._children.entries()) { | ||
if (child instanceof file_1.ImmutableFile) { | ||
mutable[name] = child.getBuffer().toString("utf8"); | ||
} | ||
else { | ||
mutable[name] = child.toMutable(); | ||
} | ||
} | ||
return mutable; | ||
} | ||
list() { | ||
return Object.assign({}, this._children); | ||
return new Map(this._children); | ||
} | ||
merged(other) { | ||
return VirtualDirectory.merged(this, other); | ||
return ImmutableDirectory.merged(this, other); | ||
} | ||
@@ -27,5 +52,4 @@ toString() { | ||
inspectInternal(textBuilder, depth, opts) { | ||
for (let childName of Object.keys(this._children).sort()) { | ||
let child = this._children[childName]; | ||
if (child instanceof file_1.default) { | ||
for (let [childName, child] of this._children.entries()) { | ||
if (child instanceof file_1.ImmutableFile) { | ||
textBuilder.append(`${childName}\n`); | ||
@@ -52,3 +76,3 @@ } | ||
let remainingPath = path.substr(slashPosition + 1); | ||
return VirtualDirectory.builder() | ||
return ImmutableDirectory.builder() | ||
.addDirectory(rootName, this.wrap(remainingPath, terminalNode)) | ||
@@ -58,3 +82,3 @@ .build(); | ||
else { | ||
return VirtualDirectory.builder().addChild(path, terminalNode).build(); | ||
return ImmutableDirectory.builder().addChild(path, terminalNode).build(); | ||
} | ||
@@ -67,4 +91,4 @@ } | ||
let remainingPath = path.substr(slashPosition + 1); | ||
let child = rootDirectory.list()[rootName]; | ||
if (child instanceof VirtualDirectory) { | ||
let child = rootDirectory.list().get(rootName); | ||
if (child instanceof ImmutableDirectory) { | ||
return this.unwrap(remainingPath, child); | ||
@@ -80,3 +104,3 @@ } | ||
else { | ||
let child = rootDirectory.list()[path]; | ||
let child = rootDirectory.list().get(path); | ||
if (!child) { | ||
@@ -90,21 +114,20 @@ throw new Error(`No such file or directory: '${path}'`); | ||
// TODO: Consider failing on conflict. | ||
let mergedChildren = {}; | ||
let mergedChildren = new Map(); | ||
for (let directory of directories) { | ||
for (let childName of Object.keys(directory._children)) { | ||
let child = directory._children[childName]; | ||
if (child instanceof file_1.default) { | ||
if (mergedChildren[childName] instanceof VirtualDirectory) { | ||
for (let [childName, child] of directory._children.entries()) { | ||
if (child instanceof file_1.ImmutableFile) { | ||
if (mergedChildren.get(childName) instanceof ImmutableDirectory) { | ||
throw new Error("Cannot merge file into directory"); | ||
} | ||
mergedChildren[childName] = child; | ||
mergedChildren.set(childName, child); | ||
} | ||
else if (child instanceof VirtualDirectory) { | ||
if (mergedChildren[childName] instanceof file_1.default) { | ||
else if (child instanceof ImmutableDirectory) { | ||
if (mergedChildren.get(childName) instanceof file_1.ImmutableFile) { | ||
throw new Error("Cannot merge directory into file"); | ||
} | ||
else if (mergedChildren[childName] instanceof VirtualDirectory) { | ||
mergedChildren[childName] = mergedChildren[childName].merged(child); | ||
else if (mergedChildren.get(childName) instanceof ImmutableDirectory) { | ||
mergedChildren.set(childName, mergedChildren.get(childName).merged(child)); | ||
} | ||
else { | ||
mergedChildren[childName] = child; | ||
mergedChildren.set(childName, child); | ||
} | ||
@@ -117,31 +140,31 @@ } | ||
} | ||
return new VirtualDirectory(mergedChildren); | ||
return new ImmutableDirectory(mergedChildren); | ||
} | ||
} | ||
(function (VirtualDirectory) { | ||
class Builder { | ||
constructor() { | ||
this._children = {}; | ||
exports.ImmutableDirectory = ImmutableDirectory; | ||
class ImmutableDirectoryBuilder { | ||
constructor() { | ||
this._children = new Map(); | ||
} | ||
addFile(name, f) { | ||
let file = f instanceof file_1.ImmutableFile | ||
? f | ||
: new file_1.ImmutableFile(Buffer.from(f, "utf8")); | ||
return this.addChild(name, file); | ||
} | ||
addDirectory(name, d) { | ||
let dir = d instanceof ImmutableDirectory ? d : d.build(); | ||
return this.addChild(name, dir); | ||
} | ||
addChild(name, child) { | ||
if (this._children.has(name)) { | ||
throw new Error("Conflicting names: " + name); | ||
} | ||
addFile(name, f) { | ||
let file = f instanceof file_1.default ? f : new file_1.default(Buffer.from(f, "utf8")); | ||
return this.addChild(name, file); | ||
} | ||
addDirectory(name, d) { | ||
let dir = d instanceof VirtualDirectory ? d : d.build(); | ||
return this.addChild(name, dir); | ||
} | ||
addChild(name, child) { | ||
if (name in this._children) { | ||
throw new Error("Conflicting names: " + name); | ||
} | ||
this._children[name] = child; | ||
return this; | ||
} | ||
build() { | ||
return new VirtualDirectory(this._children); | ||
} | ||
this._children.set(name, child); | ||
return this; | ||
} | ||
VirtualDirectory.Builder = Builder; | ||
})(VirtualDirectory || (VirtualDirectory = {})); | ||
exports.default = VirtualDirectory; | ||
build() { | ||
return new ImmutableDirectory(this._children); | ||
} | ||
} | ||
exports.ImmutableDirectoryBuilder = ImmutableDirectoryBuilder; |
/// <reference types="node" /> | ||
export default class VirtualFile { | ||
export declare class ImmutableFile { | ||
private _buffer; | ||
@@ -4,0 +4,0 @@ constructor(text: string | Buffer); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class VirtualFile { | ||
class ImmutableFile { | ||
constructor(text) { | ||
@@ -11,2 +11,2 @@ this._buffer = typeof text == "string" ? Buffer.from(text, "utf8") : text; | ||
} | ||
exports.default = VirtualFile; | ||
exports.ImmutableFile = ImmutableFile; |
import * as io from "./io"; | ||
import VirtualDirectory from "./directory"; | ||
import VirtualFile from "./file"; | ||
export { VirtualDirectory, VirtualFile, io }; | ||
import { ImmutableDirectory, ImmutableDirectoryBuilder, MutableDirectory } from "./directory"; | ||
import { ImmutableFile } from "./file"; | ||
export { ImmutableDirectory, ImmutableDirectoryBuilder, MutableDirectory, ImmutableFile, io }; |
@@ -6,4 +6,5 @@ "use strict"; | ||
const directory_1 = require("./directory"); | ||
exports.VirtualDirectory = directory_1.default; | ||
exports.ImmutableDirectory = directory_1.ImmutableDirectory; | ||
exports.ImmutableDirectoryBuilder = directory_1.ImmutableDirectoryBuilder; | ||
const file_1 = require("./file"); | ||
exports.VirtualFile = file_1.default; | ||
exports.ImmutableFile = file_1.ImmutableFile; |
@@ -1,5 +0,4 @@ | ||
import VirtualDirectory from "./directory"; | ||
import VirtualFile from "./file"; | ||
export declare function generate(directory: VirtualDirectory, destinationPath: string, replace?: boolean): void; | ||
export declare function read(sourcePath: string): VirtualDirectory | VirtualFile; | ||
export declare function deleteRecursively(p: string): void; | ||
import { ImmutableDirectory } from "./directory"; | ||
import { ImmutableFile } from "./file"; | ||
export declare function generate(directory: ImmutableDirectory, destinationPath: string, replace?: boolean): void; | ||
export declare function read(sourcePath: string): ImmutableDirectory | ImmutableFile | null; |
@@ -23,6 +23,5 @@ "use strict"; | ||
let list = directory.list(); | ||
for (let name of Object.keys(list)) { | ||
let child = list[name]; | ||
for (let [name, child] of list.entries()) { | ||
let childDestinationPath = path.join(destinationPath, name); | ||
if (child instanceof directory_1.default) { | ||
if (child instanceof directory_1.ImmutableDirectory) { | ||
if (fs.existsSync(childDestinationPath)) { | ||
@@ -64,6 +63,9 @@ if (!fs.lstatSync(childDestinationPath).isDirectory()) { | ||
if (lstat.isDirectory()) { | ||
let directory = directory_1.default.builder(); | ||
for (let child of fs.readdirSync(sourcePath)) { | ||
let childDestinationPath = path.join(sourcePath, child); | ||
directory.addChild(child, read(childDestinationPath)); | ||
let directory = directory_1.ImmutableDirectory.builder(); | ||
for (let childName of fs.readdirSync(sourcePath)) { | ||
let childDestinationPath = path.join(sourcePath, childName); | ||
let child = read(childDestinationPath); | ||
if (child) { | ||
directory.addChild(childName, child); | ||
} | ||
} | ||
@@ -73,4 +75,8 @@ return directory.build(); | ||
else if (lstat.isFile()) { | ||
return new file_1.default(fs.readFileSync(sourcePath)); | ||
return new file_1.ImmutableFile(fs.readFileSync(sourcePath)); | ||
} | ||
else if (lstat.isSymbolicLink()) { | ||
// Symbolic links are ignored. | ||
return null; | ||
} | ||
else { | ||
@@ -93,2 +99,1 @@ throw new Error(`Unsupported path: ${sourcePath}`); | ||
} | ||
exports.deleteRecursively = deleteRecursively; |
{ | ||
"name": "nofiles", | ||
"version": "1.0.0", | ||
"version": "1.0.4", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"files": ["dist"], | ||
"scripts": { | ||
@@ -10,0 +8,0 @@ "build": "rimraf dist && tsc", |
# Nofiles | ||
[![Circle CI status](https://circleci.com/gh/fwouts/nofiles.svg?&style=shield)](https://circleci.com/gh/fwouts/nofiles) | ||
Utilities to handles files and directories as pure JavaScript objects. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12823
319
6