added tests for Smartfile instance

This commit is contained in:
2017-04-30 18:13:17 +02:00
parent 9d02fccc01
commit 4899d454eb
6 changed files with 172 additions and 60 deletions

View File

@ -4,7 +4,7 @@ export interface ISmartfileConstructorOptions {
path?: string
contentString?: string
contentBuffer?: Buffer
cwd?: string
base?: string
}
/**
@ -17,11 +17,6 @@ export class Smartfile {
*/
path: string
/**
* gulp-compatibility: alias of this.contentBuffer
*/
contents: Buffer
/**
* the content of the file as Buffer
*/
@ -30,7 +25,7 @@ export class Smartfile {
/**
* The current working directory of the file
*/
cwd: string
base: string
/**
* sync the file with disk
@ -44,23 +39,13 @@ export class Smartfile {
constructor (optionsArg: ISmartfileConstructorOptions) {
if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer
this.contents = optionsArg.contentBuffer
} else if (optionsArg.contentString) {
this.contentBuffer = optionsArg.contentBuffer
this.contents = Buffer.from(optionsArg.contentString)
this.setContentsFromString(optionsArg.contentString)
} else {
console.log('created empty Smartfile?')
}
this.path = optionsArg.path
this.cwd = optionsArg.cwd
}
/**
* return relative path of file
* ->
*/
get relative () {
return ''
this.base = optionsArg.base
}
@ -84,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

@ -221,10 +221,11 @@ export let fileTreeToObject = async (dirPathArg: string, miniMatchFilter: string
let smartfileArray: Smartfile[] = []
for (let filePath of fileTree) {
smartfileArray.push(new Smartfile({
path: filePath,
contentBuffer: new Buffer(toStringSync(
plugins.path.join(dirPath, filePath)
))
)),
base: dirPath,
path: filePath
}))
}
return smartfileArray