2017-03-04 20:10:46 +00:00
|
|
|
import * as plugins from './smartfile.plugins'
|
|
|
|
|
|
|
|
export interface ISmartfileConstructorOptions {
|
|
|
|
path?: string
|
2017-04-29 15:20:09 +00:00
|
|
|
contentString?: string
|
2017-03-04 20:10:46 +00:00
|
|
|
contentBuffer?: Buffer
|
2017-04-30 16:13:17 +00:00
|
|
|
base?: string
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2016-09-20 15:56:49 +00:00
|
|
|
|
2017-04-27 14:48:08 +00:00
|
|
|
/**
|
|
|
|
* class Smartfile
|
|
|
|
* -> is vinyl file compatible
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export class Smartfile {
|
2017-04-27 14:48:08 +00:00
|
|
|
/**
|
|
|
|
* the full path of the file on disk
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
path: string
|
2017-04-27 14:48:08 +00:00
|
|
|
|
2017-05-07 21:00:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
parsedPath: plugins.path.ParsedPath
|
|
|
|
|
2017-04-29 15:20:09 +00:00
|
|
|
/**
|
|
|
|
* the content of the file as Buffer
|
|
|
|
*/
|
|
|
|
contentBuffer: Buffer
|
|
|
|
|
2017-04-27 14:48:08 +00:00
|
|
|
/**
|
|
|
|
* The current working directory of the file
|
2017-05-07 21:00:56 +00:00
|
|
|
* Note:this is similar to gulp and different from native node path base
|
2017-04-27 14:48:08 +00:00
|
|
|
*/
|
2017-04-30 16:13:17 +00:00
|
|
|
base: string
|
2017-04-27 14:48:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sync the file with disk
|
|
|
|
*/
|
|
|
|
sync: boolean
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the constructor of Smartfile
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
2017-05-07 21:00:56 +00:00
|
|
|
|
|
|
|
|
2017-04-27 14:48:08 +00:00
|
|
|
constructor (optionsArg: ISmartfileConstructorOptions) {
|
2017-03-04 20:10:46 +00:00
|
|
|
if (optionsArg.contentBuffer) {
|
2017-04-29 15:20:09 +00:00
|
|
|
this.contentBuffer = optionsArg.contentBuffer
|
|
|
|
} else if (optionsArg.contentString) {
|
2017-04-30 16:13:17 +00:00
|
|
|
this.setContentsFromString(optionsArg.contentString)
|
2017-04-29 15:20:09 +00:00
|
|
|
} else {
|
|
|
|
console.log('created empty Smartfile?')
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2017-03-04 20:10:46 +00:00
|
|
|
this.path = optionsArg.path
|
2017-05-07 21:00:56 +00:00
|
|
|
this.parsedPath = plugins.path.parse(this.path)
|
2017-04-30 16:13:17 +00:00
|
|
|
this.base = optionsArg.base
|
2017-04-27 14:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-04 20:10:46 +00:00
|
|
|
/**
|
|
|
|
* set contents from string
|
|
|
|
* @param contentString
|
|
|
|
*/
|
2017-04-27 14:48:08 +00:00
|
|
|
setContentsFromString(contentString: string) {
|
2017-03-04 20:10:46 +00:00
|
|
|
this.contents = new Buffer(contentString)
|
|
|
|
}
|
2017-04-27 14:48:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* write file to disk
|
|
|
|
*/
|
|
|
|
async write () {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read file from disk
|
|
|
|
*/
|
|
|
|
async read () {
|
|
|
|
}
|
2017-04-30 16:13:17 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------
|
|
|
|
// vinyl compatibility
|
|
|
|
// -----------------------------------------------
|
|
|
|
/**
|
|
|
|
* vinyl-compatibility: alias of this.contentBuffer
|
|
|
|
*/
|
|
|
|
get contents (): Buffer {
|
|
|
|
return this.contentBuffer
|
|
|
|
}
|
|
|
|
set contents (contentsArg) {
|
|
|
|
this.contentBuffer = contentsArg
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vinyl-compatibility
|
|
|
|
*/
|
|
|
|
get cwd () {
|
2017-05-01 20:27:15 +00:00
|
|
|
return process.cwd()
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return relative path of file
|
|
|
|
*/
|
|
|
|
get relative (): string {
|
2017-05-01 20:07:25 +00:00
|
|
|
return plugins.path.relative(this.base, this.path)
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
2017-05-01 20:07:25 +00:00
|
|
|
|
|
|
|
isSymbolic () {
|
|
|
|
return false
|
|
|
|
}
|
2017-05-27 21:47:39 +00:00
|
|
|
|
|
|
|
// update things
|
|
|
|
updateFileName (fileNameArg: string) {
|
|
|
|
let oldFileName = this.parsedPath.base
|
2017-05-27 22:12:02 +00:00
|
|
|
this.path = this.path.replace(new RegExp(oldFileName + '$'),fileNameArg)
|
2017-05-27 21:47:39 +00:00
|
|
|
}
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|