2018-07-03 06:55:09 +00:00
|
|
|
import * as plugins from './smartfile.plugins';
|
|
|
|
import * as fs from './smartfile.fs';
|
|
|
|
import * as memory from './smartfile.memory';
|
2017-03-04 20:10:46 +00:00
|
|
|
|
|
|
|
export interface ISmartfileConstructorOptions {
|
2018-07-03 06:55:09 +00:00
|
|
|
path?: string;
|
|
|
|
contentString?: string;
|
|
|
|
contentBuffer?: Buffer;
|
|
|
|
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 {
|
2019-01-27 02:11:10 +00:00
|
|
|
// ======
|
|
|
|
// STATIC
|
|
|
|
// ======
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a Smartfile from a filePath
|
|
|
|
* @param filePath
|
|
|
|
*/
|
2019-04-08 14:39:41 +00:00
|
|
|
public static async fromFilePath(filePath: string) {
|
2019-01-27 02:11:10 +00:00
|
|
|
filePath = plugins.path.resolve(filePath);
|
|
|
|
const fileString = fs.toStringSync(filePath);
|
|
|
|
const smartfile = new Smartfile({
|
|
|
|
path: filePath,
|
|
|
|
contentString: fileString
|
|
|
|
});
|
|
|
|
return smartfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ========
|
|
|
|
// INSTANCE
|
|
|
|
// ========
|
2017-04-27 14:48:08 +00:00
|
|
|
/**
|
|
|
|
* the full path of the file on disk
|
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
path: string;
|
2017-04-27 14:48:08 +00:00
|
|
|
|
2017-05-07 21:00:56 +00:00
|
|
|
/**
|
2018-07-03 06:55:09 +00:00
|
|
|
*
|
2017-05-07 21:00:56 +00:00
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
parsedPath: plugins.path.ParsedPath;
|
2017-05-07 21:00:56 +00:00
|
|
|
|
2017-04-29 15:20:09 +00:00
|
|
|
/**
|
|
|
|
* the content of the file as Buffer
|
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
contentBuffer: Buffer;
|
2017-04-29 15:20:09 +00:00
|
|
|
|
2017-04-27 14:48:08 +00:00
|
|
|
/**
|
|
|
|
* The current working directory of the file
|
2018-07-03 06:55:09 +00:00
|
|
|
* Note:this is similar to gulp and different from native node path base
|
2017-04-27 14:48:08 +00:00
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
base: string;
|
2017-04-27 14:48:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sync the file with disk
|
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
sync: boolean;
|
2017-04-27 14:48:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the constructor of Smartfile
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
2017-05-07 21:00:56 +00:00
|
|
|
|
2018-07-03 06:55:09 +00:00
|
|
|
constructor(optionsArg: ISmartfileConstructorOptions) {
|
2017-03-04 20:10:46 +00:00
|
|
|
if (optionsArg.contentBuffer) {
|
2018-07-03 06:55:09 +00:00
|
|
|
this.contentBuffer = optionsArg.contentBuffer;
|
2017-04-29 15:20:09 +00:00
|
|
|
} else if (optionsArg.contentString) {
|
2018-07-03 06:55:09 +00:00
|
|
|
this.setContentsFromString(optionsArg.contentString);
|
2017-04-29 15:20:09 +00:00
|
|
|
} else {
|
2018-07-03 06:55:09 +00:00
|
|
|
console.log('created empty Smartfile?');
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
this.path = optionsArg.path;
|
|
|
|
this.parsedPath = plugins.path.parse(this.path);
|
|
|
|
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) {
|
2018-07-03 06:55:09 +00:00
|
|
|
this.contents = new Buffer(contentString);
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
2017-04-27 14:48:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* write file to disk
|
2018-02-16 20:57:44 +00:00
|
|
|
* Behaviours:
|
|
|
|
* - no argument write to exactly where the file was picked up
|
2017-04-27 14:48:08 +00:00
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
async write(pathArg?: string) {
|
|
|
|
const stringToWrite = this.contentBuffer.toString();
|
|
|
|
await memory.toFs(stringToWrite, this.path);
|
2017-04-27 14:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read file from disk
|
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
async read() {}
|
2017-04-30 16:13:17 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------
|
|
|
|
// vinyl compatibility
|
|
|
|
// -----------------------------------------------
|
|
|
|
/**
|
|
|
|
* vinyl-compatibility: alias of this.contentBuffer
|
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
get contents(): Buffer {
|
|
|
|
return this.contentBuffer;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
set contents(contentsArg) {
|
|
|
|
this.contentBuffer = contentsArg;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vinyl-compatibility
|
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
get cwd() {
|
|
|
|
return process.cwd();
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return relative path of file
|
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
get relative(): string {
|
|
|
|
return plugins.path.relative(this.base, this.path);
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return truw when the file has content
|
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
isNull(): boolean {
|
2017-04-30 16:13:17 +00:00
|
|
|
if (!this.contentBuffer) {
|
2018-07-03 06:55:09 +00:00
|
|
|
return true;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
return false;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return true if contents are Buffer
|
|
|
|
*/
|
2018-07-03 06:55:09 +00:00
|
|
|
isBuffer(): boolean {
|
2017-04-30 16:13:17 +00:00
|
|
|
if (this.contents instanceof Buffer) {
|
2018-07-03 06:55:09 +00:00
|
|
|
return true;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
return false;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 06:55:09 +00:00
|
|
|
isDirectory() {
|
|
|
|
return false;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 06:55:09 +00:00
|
|
|
isStream() {
|
|
|
|
return false;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
2017-05-01 20:07:25 +00:00
|
|
|
|
2018-07-03 06:55:09 +00:00
|
|
|
isSymbolic() {
|
|
|
|
return false;
|
2017-05-01 20:07:25 +00:00
|
|
|
}
|
2017-05-27 21:47:39 +00:00
|
|
|
|
|
|
|
// update things
|
2018-07-03 06:55:09 +00:00
|
|
|
updateFileName(fileNameArg: string) {
|
|
|
|
let oldFileName = this.parsedPath.base;
|
|
|
|
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
|
|
|
}
|