smartfile/ts/smartfile.classes.smartfile.ts

239 lines
5.4 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
*/
public static async fromFilePath(filePath: string, baseArg: string = process.cwd()) {
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({
contentBuffer: fileBuffer,
base: baseArg,
path: plugins.path.relative(baseArg, filePath),
2019-01-27 02:11:10 +00:00
});
return smartfile;
}
public static async fromBuffer(
filePath: string,
contentBufferArg: Buffer,
baseArg: string = process.cwd()
) {
const smartfile = new Smartfile({
contentBuffer: contentBufferArg,
base: baseArg,
path: plugins.path.relative(baseArg, filePath),
});
return smartfile;
}
public static async fromString(
filePath: string,
contentStringArg: string,
encodingArg: 'utf8' | 'binary',
baseArg = process.cwd()
) {
const smartfile = new Smartfile({
contentBuffer: Buffer.from(contentStringArg, encodingArg),
base: baseArg,
path: plugins.path.relative(baseArg, 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, plugins.path.join(this.base, this.path));
2020-10-09 15:15:47 +00:00
}
/**
* writes the file to path given as argument
* note: if the path is not absolute, takes process.cwd() as base
2020-10-09 15:15:47 +00:00
* @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);
}
2021-11-30 15:34:35 +00:00
/**
* writes the file to a directory combined with the relative path portion
* @param dirPathArg
* @returns
*/
2020-10-09 15:15:47 +00:00
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
*/
2021-11-30 15:34:35 +00:00
public async read() {
this.contentBuffer = await fs.toBuffer(plugins.path.join(this.base, this.path));
2021-11-30 15:34:35 +00:00
}
/**
* deletes the file from disk at its original location
*/
public async delete() {
await fs.remove(plugins.path.join(this.base, this.path));
2021-11-30 15:34:35 +00:00
}
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 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
}