smartfile/ts/smartfile.classes.smartfile.ts

216 lines
4.7 KiB
TypeScript
Raw Normal View History

import * as plugins from './smartfile.plugins';
import * as fs from './smartfile.fs';
import * as memory from './smartfile.memory';
export interface ISmartfileConstructorOptions {
path?: string;
contentBuffer?: Buffer;
base?: string;
}
2016-09-20 15:56:49 +00:00
2017-04-27 14:48:08 +00:00
/**
* class Smartfile
* -> is vinyl file compatible
*/
2020-10-05 16:20:57 +00:00
export class Smartfile extends plugins.smartjson.Smartjson {
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 fileBuffer = fs.toBufferSync(filePath);
2019-01-27 02:11:10 +00:00
const smartfile = new Smartfile({
path: filePath,
contentBuffer: fileBuffer,
2019-01-27 02:11:10 +00:00
});
return smartfile;
}
public static async fromBuffer(filePath: string, contentBufferArg: Buffer) {
const smartfile = new Smartfile({
contentBuffer: contentBufferArg,
path: filePath,
});
return smartfile;
}
public static async fromString(
filePath: string,
contentStringArg: string,
encodingArg: 'utf8' | 'binary'
) {
const smartfile = new Smartfile({
contentBuffer: Buffer.from(contentStringArg, encodingArg),
path: filePath,
});
return smartfile;
}
2021-04-26 08:24:36 +00:00
public static async fromFoldedJson(foldedJsonArg: string) {
return new Smartfile(plugins.smartjson.parse(foldedJsonArg));
}
2019-01-27 02:11:10 +00:00
// ========
// INSTANCE
// ========
2017-04-27 14:48:08 +00:00
/**
* the full path of the file on disk
*/
2020-10-05 16:20:57 +00:00
@plugins.smartjson.foldDec()
public path: string;
2017-04-27 14:48:08 +00:00
2017-05-07 21:00:56 +00:00
/**
2020-10-06 00:57:47 +00:00
* a parsed path
2017-05-07 21:00:56 +00:00
*/
2020-10-06 00:57:47 +00:00
@plugins.smartjson.foldDec()
public 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
*/
2020-10-06 00:57:47 +00:00
@plugins.smartjson.foldDec()
public 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
* Note:this is similar to gulp and different from native node path base
2017-04-27 14:48:08 +00:00
*/
2020-10-06 00:57:47 +00:00
@plugins.smartjson.foldDec()
public base: string;
2017-04-27 14:48:08 +00:00
/**
* sync the file with disk
*/
2020-10-06 00:57:47 +00:00
@plugins.smartjson.foldDec()
public sync: boolean;
2017-04-27 14:48:08 +00:00
/**
* the constructor of Smartfile
* @param optionsArg
*/
2017-05-07 21:00:56 +00:00
constructor(optionsArg: ISmartfileConstructorOptions) {
2020-10-05 16:20:57 +00:00
super();
if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer;
2017-04-29 15:20:09 +00:00
} else {
console.log('created empty Smartfile?');
2016-09-20 15:56:49 +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
}
/**
* set contents from string
* @param contentString
*/
public setContentsFromString(contentString: string, encodingArg: 'utf8' | 'binary' = 'utf8') {
this.contents = new Buffer(contentString, encodingArg);
}
2017-04-27 14:48:08 +00:00
/**
2020-10-09 15:15:47 +00:00
* write file to disk at its original location
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
*/
2020-10-09 15:15:47 +00:00
public async write() {
await memory.toFs(this.contentBuffer, this.path);
}
/**
* writes the file to path given as argument
* @param filePathArg
*/
public async writeToDiskAtPath(filePathArg: string) {
if (!plugins.path.isAbsolute(filePathArg)) {
filePathArg = plugins.path.join(process.cwd(), filePathArg);
}
await memory.toFs(this.contentBuffer, filePathArg);
}
public async writeToDir(dirPathArg: string) {
2020-10-11 15:34:24 +00:00
dirPathArg = plugins.smartpath.transform.toAbsolute(dirPathArg);
const filePath = plugins.path.join(dirPathArg, this.path);
2020-10-09 15:15:47 +00:00
await memory.toFs(this.contentBuffer, filePath);
2020-10-11 15:34:24 +00:00
return filePath;
2017-04-27 14:48:08 +00:00
}
/**
* read file from disk
*/
public async read() {}
2017-04-30 16:13:17 +00:00
// -----------------------------------------------
// vinyl compatibility
// -----------------------------------------------
/**
* vinyl-compatibility: alias of this.contentBuffer
*/
get contents(): Buffer {
return this.contentBuffer;
2017-04-30 16:13:17 +00:00
}
set contents(contentsArg) {
this.contentBuffer = contentsArg;
2017-04-30 16:13:17 +00:00
}
/**
* vinyl-compatibility
*/
public get cwd() {
return process.cwd();
2017-04-30 16:13:17 +00:00
}
/**
* return relative path of file
*/
public 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
*/
public isNull(): boolean {
2017-04-30 16:13:17 +00:00
if (!this.contentBuffer) {
return true;
2017-04-30 16:13:17 +00:00
}
return false;
2017-04-30 16:13:17 +00:00
}
/**
* return true if contents are Buffer
*/
public isBuffer(): boolean {
2017-04-30 16:13:17 +00:00
if (this.contents instanceof Buffer) {
return true;
2017-04-30 16:13:17 +00:00
}
return false;
2017-04-30 16:13:17 +00:00
}
public isDirectory() {
return false;
2017-04-30 16:13:17 +00:00
}
public isStream() {
return false;
2017-04-30 16:13:17 +00:00
}
public isSymbolic() {
return false;
}
2017-05-27 21:47:39 +00:00
// update things
public updateFileName(fileNameArg: string) {
const 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
}