|
|
|
@ -65,6 +65,33 @@ export class Smartfile extends plugins.smartjson.Smartjson {
|
|
|
|
|
return new Smartfile(plugins.smartjson.parse(foldedJsonArg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* creates a Smartfile from a ReadableStream
|
|
|
|
|
* @param stream a readable stream that provides file content
|
|
|
|
|
* @param filePath the file path to associate with the content
|
|
|
|
|
* @param baseArg the base path to use for the file
|
|
|
|
|
*/
|
|
|
|
|
public static async fromStream(
|
|
|
|
|
stream: plugins.stream.Readable,
|
|
|
|
|
filePath: string,
|
|
|
|
|
baseArg: string = process.cwd()
|
|
|
|
|
): Promise<Smartfile> {
|
|
|
|
|
return new Promise<Smartfile>((resolve, reject) => {
|
|
|
|
|
const chunks: Buffer[] = [];
|
|
|
|
|
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
|
|
|
|
|
stream.on('error', (error) => reject(error));
|
|
|
|
|
stream.on('end', () => {
|
|
|
|
|
const contentBuffer = Buffer.concat(chunks);
|
|
|
|
|
const smartfile = new Smartfile({
|
|
|
|
|
contentBuffer: contentBuffer,
|
|
|
|
|
base: baseArg,
|
|
|
|
|
path: plugins.path.relative(baseArg, filePath),
|
|
|
|
|
});
|
|
|
|
|
resolve(smartfile);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ========
|
|
|
|
|
// INSTANCE
|
|
|
|
|
// ========
|
|
|
|
@ -128,7 +155,7 @@ export class Smartfile extends plugins.smartjson.Smartjson {
|
|
|
|
|
* @param contentString
|
|
|
|
|
*/
|
|
|
|
|
public setContentsFromString(contentString: string, encodingArg: 'utf8' | 'binary' = 'utf8') {
|
|
|
|
|
this.contents = new Buffer(contentString, encodingArg);
|
|
|
|
|
this.contents = Buffer.from(contentString, encodingArg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -262,4 +289,14 @@ export class Smartfile extends plugins.smartjson.Smartjson {
|
|
|
|
|
const newFileString = await editFuncArg(this.contentBuffer.toString());
|
|
|
|
|
this.contentBuffer = Buffer.from(newFileString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a ReadableStream from the file's content buffer
|
|
|
|
|
*/
|
|
|
|
|
public getStream(): plugins.stream.Readable {
|
|
|
|
|
const stream = new plugins.stream.Readable();
|
|
|
|
|
stream.push(this.contentBuffer); // Push the content buffer to the stream
|
|
|
|
|
stream.push(null); // Push null to signify the end of the stream (EOF)
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|