Compare commits

..

7 Commits

Author SHA1 Message Date
dcc85a56b8 4.2.4 2017-04-30 18:13:20 +02:00
4899d454eb added tests for Smartfile instance 2017-04-30 18:13:17 +02:00
9d02fccc01 4.2.3 2017-04-30 15:37:36 +02:00
a5b24a7c33 update fileTreeToObject method 2017-04-30 15:37:34 +02:00
06fb0acd52 4.2.2 2017-04-30 15:12:37 +02:00
97bf5ff74b improve creation of Smartfiles 2017-04-30 15:12:35 +02:00
531f169c11 update yarn lock 2017-04-29 23:07:13 +02:00
8 changed files with 202 additions and 77 deletions

View File

@ -3,6 +3,7 @@ export interface ISmartfileConstructorOptions {
path?: string;
contentString?: string;
contentBuffer?: Buffer;
base?: string;
}
/**
* class Smartfile
@ -13,10 +14,6 @@ export declare class Smartfile {
* the full path of the file on disk
*/
path: string;
/**
* gulp-compatibility: alias of this.contentBuffer
*/
contents: Buffer;
/**
* the content of the file as Buffer
*/
@ -24,7 +21,7 @@ export declare class Smartfile {
/**
* The current working directory of the file
*/
cwd: string;
base: string;
/**
* sync the file with disk
*/
@ -34,11 +31,6 @@ export declare class Smartfile {
* @param optionsArg
*/
constructor(optionsArg: ISmartfileConstructorOptions);
/**
* return relative path of file
* ->
*/
readonly relative: string;
/**
* set contents from string
* @param contentString
@ -52,4 +44,26 @@ export declare class Smartfile {
* read file from disk
*/
read(): Promise<void>;
/**
* vinyl-compatibility: alias of this.contentBuffer
*/
contents: Buffer;
/**
* vinyl-compatibility
*/
readonly cwd: string;
/**
* return relative path of file
*/
readonly relative: string;
/**
* return truw when the file has content
*/
isNull(): boolean;
/**
* return true if contents are Buffer
*/
isBuffer(): boolean;
isDirectory(): boolean;
isStream(): boolean;
}

View File

@ -20,22 +20,15 @@ class Smartfile {
constructor(optionsArg) {
if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer;
this.contents = optionsArg.contentBuffer;
}
else if (optionsArg.contentString) {
this.contents = Buffer.from(optionsArg.contentString);
this.setContentsFromString(optionsArg.contentString);
}
else {
console.log('created empty Smartfile?');
}
this.path = optionsArg.path;
}
/**
* return relative path of file
* ->
*/
get relative() {
return '';
this.base = optionsArg.base;
}
/**
* set contents from string
@ -58,6 +51,54 @@ class Smartfile {
return __awaiter(this, void 0, void 0, function* () {
});
}
// -----------------------------------------------
// vinyl compatibility
// -----------------------------------------------
/**
* vinyl-compatibility: alias of this.contentBuffer
*/
get contents() {
return this.contentBuffer;
}
set contents(contentsArg) {
this.contentBuffer = contentsArg;
}
/**
* vinyl-compatibility
*/
get cwd() {
return this.base;
}
/**
* return relative path of file
*/
get relative() {
return this.path;
}
/**
* return truw when the file has content
*/
isNull() {
if (!this.contentBuffer) {
return true;
}
return false;
}
/**
* return true if contents are Buffer
*/
isBuffer() {
if (this.contents instanceof Buffer) {
return true;
}
return false;
}
isDirectory() {
return false;
}
isStream() {
return false;
}
}
exports.Smartfile = Smartfile;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLmNsYXNzZXMuc21hcnRmaWxlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRmaWxlLmNsYXNzZXMuc21hcnRmaWxlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFRQTs7O0dBR0c7QUFDSDtJQTBCRTs7O09BR0c7SUFDSCxZQUFhLFVBQXdDO1FBQ25ELEVBQUUsQ0FBQyxDQUFDLFVBQVUsQ0FBQyxhQUFhLENBQUMsQ0FBQyxDQUFDO1lBQzdCLElBQUksQ0FBQyxhQUFhLEdBQUcsVUFBVSxDQUFDLGFBQWEsQ0FBQTtZQUM3QyxJQUFJLENBQUMsUUFBUSxHQUFHLFVBQVUsQ0FBQyxhQUFhLENBQUE7UUFDMUMsQ0FBQztRQUFDLElBQUksQ0FBQyxFQUFFLENBQUMsQ0FBQyxVQUFVLENBQUMsYUFBYSxDQUFDLENBQUMsQ0FBQztZQUNwQyxJQUFJ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BQyxJQUFJLENBQUE7SUFDbEIsQ0FBQztJQUVEOztPQUVHO0lBQ0gsTUFBTTtRQUNKLEVBQUUsQ0FBQyxDQUFDLENBQUMsSUFBSSxDQUFDLGFBQWEsQ0FBQyxDQUFDLENBQUM7WUFDeEIsTUFBTSxDQUFDLElBQUksQ0FBQTtRQUNiLENBQUM7UUFDRCxNQUFNLENBQUMsS0FBSyxDQUFBO0lBQ2QsQ0FBQztJQUVEOztPQUVHO0lBQ0gsUUFBUTtRQUNOLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxRQUFRLFlBQVksTUFBTSxDQUFDLENBQUMsQ0FBQztZQUNwQyxNQUFNLENBQUMsSUFBSSxDQUFBO1FBQ2IsQ0FBQztRQUNELE1BQU0sQ0FBQyxLQUFLLENBQUE7SUFDZCxDQUFDO0lBRUQsV0FBVztRQUNULE1BQU0sQ0FBQyxLQUFLLENBQUE7SUFDZCxDQUFDO0lBRUQsUUFBUTtRQUNOLE1BQU0sQ0FBQyxLQUFLLENBQUE7SUFDZCxDQUFDO0NBQ0Y7QUFqSEQsOEJBaUhDIn0=

17
dist/smartfile.fs.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"name": "smartfile",
"version": "4.2.1",
"version": "4.2.4",
"description": "offers smart ways to work with files in nodejs",
"main": "dist/index.js",
"typings": "dist/index.d.ts",

View File

@ -51,7 +51,7 @@ tap.test('.fs.listFileTree() -> should get a file tree', async () => {
expect(folderArrayArg).to.not.deep.include('mytest.json')
})
tap.test('.fstoObjectFromFileTree -> should read a file tree into an Object', async () => {
tap.test('.fs.toObjectFromFileTree -> should read a file tree into an Object', async () => {
let fileArrayArg = await smartfile.fs.fileTreeToObject(path.resolve('./test/'), '**/*.txt')
// expect(fileArrayArg[1]).to.be.instanceof(smartfile.Smartfile)
})
@ -72,36 +72,28 @@ tap.test('.fs.remove() -> should remove an entire directory', async () => {
})
tap.test('smartfile.fs.remove -> should remove single files', async () => {
tap.test('.fs.remove -> should remove single files', async () => {
await expect(smartfile.fs.remove('./test/temp/mytestRenamed.yaml')).to.eventually.be.fulfilled
})
tap.test('smartfile.fs.removeSync -> should remove single files synchronouly', async () => {
tap.test('.fs.removeSync -> should remove single files synchronouly', async () => {
smartfile.fs.removeSync('./test/temp/testfile1.txt')
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
})
tap.test('smartfile.fs.removeMany -> should remove and array of files', async () => {
tap.test('.fs.removeMany -> should remove and array of files', async () => {
smartfile.fs.removeMany([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ]).then(() => {
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false
})
})
tap.test('smartfile.fs.removeManySync -> should remove and array of single files synchronouly', async () => {
tap.test('.fs.removeManySync -> should remove and array of single files synchronouly', async () => {
smartfile.fs.removeManySync([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ])
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false
})
// ---------------------------
// smartfile.interpreter
// ---------------------------
tap.test('.interpreter.filetype() -> should get the file type from a string', async () => {
expect(smartfile.interpreter.filetype('./somefolder/data.json')).equal('json')
})
tap.test('.fs.toObjectSync() -> should read an ' + '.yaml' + ' file to an object', async () => {
let testData = smartfile.fs.toObjectSync('./test/mytest.yaml')
expect(testData).have.property('key1', 'this works')
@ -129,6 +121,18 @@ tap.test('.fs.toVinylSync -> should read an ' + '.json OR .yaml' + ' file to an
expect(vinyl.isVinyl(testData)).to.be.true
})
// ---------------------------
// smartfile.interpreter
// ---------------------------
tap.test('.interpreter.filetype() -> should get the file type from a string', async () => {
expect(smartfile.interpreter.filetype('./somefolder/data.json')).equal('json')
})
// ---------------------------
// smartfile.memory
// ---------------------------
tap.test('.memory.toVinylFileSync() -> should produce a vinylFile', async () => {
let localString = 'myString'
let localOptions = { filename: 'vinylfile2', base: '/someDir' }
@ -183,4 +187,18 @@ tap.test('.remote.toString() -> should reject a Promise when the link is false',
.to.eventually.be.rejected
})
// ---------------------------
// smartfile.Smartfile
// ---------------------------
tap.test('.Smartfile -> should produce vinyl compatible files', async () => {
let smartfileArray = await smartfile.fs.fileTreeToObject(process.cwd(), './test/testfolder/**/*')
let localSmartfile = smartfileArray[0]
expect(localSmartfile).to.be.instanceof(smartfile.Smartfile)
expect(localSmartfile.contents).to.be.instanceof(Buffer)
expect(localSmartfile.isBuffer()).to.be.true
expect(localSmartfile.isDirectory()).to.be.false
expect(localSmartfile.isNull()).to.be.false
})
tap.start()

View File

@ -4,6 +4,7 @@ export interface ISmartfileConstructorOptions {
path?: string
contentString?: string
contentBuffer?: Buffer
base?: string
}
/**
@ -16,11 +17,6 @@ export class Smartfile {
*/
path: string
/**
* gulp-compatibility: alias of this.contentBuffer
*/
contents: Buffer
/**
* the content of the file as Buffer
*/
@ -29,7 +25,7 @@ export class Smartfile {
/**
* The current working directory of the file
*/
cwd: string
base: string
/**
* sync the file with disk
@ -43,21 +39,13 @@ export class Smartfile {
constructor (optionsArg: ISmartfileConstructorOptions) {
if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer
this.contents = optionsArg.contentBuffer
} else if (optionsArg.contentString) {
this.contents = Buffer.from(optionsArg.contentString)
this.setContentsFromString(optionsArg.contentString)
} else {
console.log('created empty Smartfile?')
}
this.path = optionsArg.path
}
/**
* return relative path of file
* ->
*/
get relative () {
return ''
this.base = optionsArg.base
}
@ -81,4 +69,59 @@ export class Smartfile {
*/
async read () {
}
// -----------------------------------------------
// vinyl compatibility
// -----------------------------------------------
/**
* vinyl-compatibility: alias of this.contentBuffer
*/
get contents (): Buffer {
return this.contentBuffer
}
set contents (contentsArg) {
this.contentBuffer = contentsArg
}
/**
* vinyl-compatibility
*/
get cwd () {
return this.base
}
/**
* return relative path of file
*/
get relative (): string {
return this.path
}
/**
* return truw when the file has content
*/
isNull (): boolean {
if (!this.contentBuffer) {
return true
}
return false
}
/**
* return true if contents are Buffer
*/
isBuffer (): boolean {
if (this.contents instanceof Buffer) {
return true
}
return false
}
isDirectory () {
return false
}
isStream () {
return false
}
}

View File

@ -209,14 +209,23 @@ export let toStringSync = function (filePath: string) {
}
export let fileTreeToObject = async (dirPathArg: string, miniMatchFilter: string) => {
let fileTree = await listFileTree(dirPathArg, miniMatchFilter)
// handle absolute miniMatchFilter
let dirPath: string
if (plugins.path.isAbsolute(miniMatchFilter)) {
dirPath = '/'
} else {
dirPath = dirPathArg
}
let fileTree = await listFileTree(dirPath, miniMatchFilter)
let smartfileArray: Smartfile[] = []
for (let filePath of fileTree) {
smartfileArray.push(new Smartfile({
path: filePath,
contentBuffer: new Buffer(toStringSync(
plugins.path.join(dirPathArg, filePath)
))
plugins.path.join(dirPath, filePath)
)),
base: dirPath,
path: filePath
}))
}
return smartfileArray

View File

@ -16,8 +16,8 @@
"@types/chai" "*"
"@types/chai@*", "@types/chai@^3.4.35":
version "3.5.1"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-3.5.1.tgz#9bd77fe12503ae00648b0945b38eab666adffe2e"
version "3.5.2"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-3.5.2.tgz#c11cd2817d3a401b7ba0f5a420f35c56139b1c1e"
"@types/fs-extra@2.x.x":
version "2.1.0"
@ -33,11 +33,7 @@
version "0.0.27"
resolved "https://registry.yarnpkg.com/@types/promises-a-plus/-/promises-a-plus-0.0.27.tgz#c64651134614c84b8f5d7114ce8901d36a609780"
"@types/q@0.0.32":
version "0.0.32"
resolved "https://registry.yarnpkg.com/@types/q/-/q-0.0.32.tgz#bd284e57c84f1325da702babfc82a5328190c0c5"
"@types/through2@^2.0.31":
"@types/through2@^2.0.32":
version "2.0.32"
resolved "https://registry.yarnpkg.com/@types/through2/-/through2-2.0.32.tgz#470024450f1ab7640f19f9ebf42d3da574c26129"
dependencies:
@ -191,14 +187,13 @@ graceful-fs@^4.1.2, graceful-fs@^4.1.6:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
gulp-function@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/gulp-function/-/gulp-function-2.2.3.tgz#8f62de74ce74de3fa91c48ba247472c1f56873bd"
version "2.2.6"
resolved "https://registry.yarnpkg.com/gulp-function/-/gulp-function-2.2.6.tgz#e14c1eefdab4d4ac11d9f4abfdbe94115a12d10d"
dependencies:
"@types/q" "0.0.32"
"@types/through2" "^2.0.31"
q "^1.4.1"
through2 "^2.0.1"
typings-global "^1.0.14"
"@types/through2" "^2.0.32"
smartq "^1.1.1"
through2 "^2.0.3"
typings-global "^1.0.16"
home@^1.0.1:
version "1.0.1"
@ -310,10 +305,6 @@ process-nextick-args@^1.0.6, process-nextick-args@~1.0.6:
version "1.0.7"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3"
q@^1.4.1:
version "1.5.0"
resolved "https://registry.yarnpkg.com/q/-/q-1.5.0.tgz#dd01bac9d06d30e6f219aecb8253ee9ebdc308f1"
readable-stream@^2.0.2, readable-stream@^2.1.5:
version "2.2.9"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.9.tgz#cf78ec6f4a6d1eb43d26488cac97f042e74b7fc8"
@ -440,7 +431,7 @@ tapbundle@^1.0.10:
smartq "^1.1.1"
typings-global "^1.0.16"
through2@^2.0.1:
through2@^2.0.1, through2@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.3.tgz#0004569b37c7c74ba39c43f3ced78d1ad94140be"
dependencies: