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 {
|
2021-12-01 00:14:07 +00:00
|
|
|
path: 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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2021-12-01 00:14:07 +00:00
|
|
|
public static async fromFilePath(filePath: string, baseArg: string = process.cwd()) {
|
2019-01-27 02:11:10 +00:00
|
|
|
filePath = plugins.path.resolve(filePath);
|
2020-08-10 20:58:22 +00:00
|
|
|
const fileBuffer = fs.toBufferSync(filePath);
|
2019-01-27 02:11:10 +00:00
|
|
|
const smartfile = new Smartfile({
|
2020-08-10 20:58:22 +00:00
|
|
|
contentBuffer: fileBuffer,
|
2021-12-01 00:14:07 +00:00
|
|
|
base: baseArg,
|
|
|
|
path: plugins.path.relative(baseArg, filePath),
|
2019-01-27 02:11:10 +00:00
|
|
|
});
|
|
|
|
return smartfile;
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:14:07 +00:00
|
|
|
public static async fromBuffer(
|
|
|
|
filePath: string,
|
|
|
|
contentBufferArg: Buffer,
|
|
|
|
baseArg: string = process.cwd()
|
|
|
|
) {
|
2020-08-10 20:58:22 +00:00
|
|
|
const smartfile = new Smartfile({
|
|
|
|
contentBuffer: contentBufferArg,
|
2021-12-01 00:14:07 +00:00
|
|
|
base: baseArg,
|
|
|
|
path: plugins.path.relative(baseArg, filePath),
|
2020-08-10 20:58:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return smartfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async fromString(
|
|
|
|
filePath: string,
|
|
|
|
contentStringArg: string,
|
2021-12-01 00:14:07 +00:00
|
|
|
encodingArg: 'utf8' | 'binary',
|
|
|
|
baseArg = process.cwd()
|
2020-08-10 20:58:22 +00:00
|
|
|
) {
|
|
|
|
const smartfile = new Smartfile({
|
|
|
|
contentBuffer: Buffer.from(contentStringArg, encodingArg),
|
2021-12-01 00:14:07 +00:00
|
|
|
base: baseArg,
|
|
|
|
path: plugins.path.relative(baseArg, filePath),
|
2020-08-10 20:58:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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()
|
2020-08-10 20:58:22 +00:00
|
|
|
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()
|
2020-08-10 20:58:22 +00:00
|
|
|
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()
|
2020-08-10 20:58:22 +00:00
|
|
|
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
|
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
|
|
|
*/
|
2020-10-06 00:57:47 +00:00
|
|
|
@plugins.smartjson.foldDec()
|
2020-08-10 20:58:22 +00:00
|
|
|
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()
|
2020-08-10 20:58:22 +00:00
|
|
|
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
|
|
|
|
2018-07-03 06:55:09 +00:00
|
|
|
constructor(optionsArg: ISmartfileConstructorOptions) {
|
2020-10-05 16:20:57 +00:00
|
|
|
super();
|
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 {
|
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
|
|
|
|
*/
|
2020-08-10 20:58:22 +00:00
|
|
|
public setContentsFromString(contentString: string, encodingArg: 'utf8' | 'binary' = 'utf8') {
|
|
|
|
this.contents = new Buffer(contentString, encodingArg);
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
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() {
|
2021-12-01 00:14:07 +00:00
|
|
|
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
|
2021-12-01 00:14:07 +00:00
|
|
|
* 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() {
|
2021-12-01 00:14:07 +00:00
|
|
|
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() {
|
2021-12-01 00:14:07 +00:00
|
|
|
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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2020-08-10 20:58:22 +00:00
|
|
|
public get cwd() {
|
2018-07-03 06:55:09 +00:00
|
|
|
return process.cwd();
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return relative path of file
|
|
|
|
*/
|
2020-08-10 20:58:22 +00:00
|
|
|
public get relative(): string {
|
2021-12-01 00:14:07 +00:00
|
|
|
return this.path;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return truw when the file has content
|
|
|
|
*/
|
2020-08-10 20:58:22 +00:00
|
|
|
public 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
|
|
|
|
*/
|
2020-08-10 20:58:22 +00:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2020-08-10 20:58:22 +00:00
|
|
|
public isDirectory() {
|
2018-07-03 06:55:09 +00:00
|
|
|
return false;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 20:58:22 +00:00
|
|
|
public isStream() {
|
2018-07-03 06:55:09 +00:00
|
|
|
return false;
|
2017-04-30 16:13:17 +00:00
|
|
|
}
|
2017-05-01 20:07:25 +00:00
|
|
|
|
2020-08-10 20:58:22 +00:00
|
|
|
public isSymbolic() {
|
2018-07-03 06:55:09 +00:00
|
|
|
return false;
|
2017-05-01 20:07:25 +00:00
|
|
|
}
|
2017-05-27 21:47:39 +00:00
|
|
|
|
|
|
|
// update things
|
2020-08-10 20:58:22 +00:00
|
|
|
public updateFileName(fileNameArg: string) {
|
|
|
|
const oldFileName = this.parsedPath.base;
|
2018-07-03 06:55:09 +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
|
|
|
}
|