smartfile/ts/memory.ts

95 lines
2.7 KiB
TypeScript
Raw Permalink Normal View History

2024-04-02 18:53:02 +00:00
import * as plugins from './plugins.js';
2023-11-04 19:07:43 +00:00
import { SmartFile } from './classes.smartfile.js';
import * as smartfileFs from './fs.js';
import * as interpreter from './interpreter.js';
import type { StreamFile } from './classes.streamfile.js';
2016-04-02 21:03:18 +00:00
/**
2016-04-09 20:51:30 +00:00
* converts file to Object
* @param fileStringArg
* @param fileTypeArg
* @returns {any|any}
*/
2020-10-09 15:15:47 +00:00
export let toObject = (fileStringArg: string, fileTypeArg: string) => {
return interpreter.objectFile(fileStringArg, fileTypeArg);
};
2017-05-26 12:47:41 +00:00
export interface IToFsOptions {
respectRelative?: boolean;
}
2016-04-02 21:03:18 +00:00
2016-04-04 13:44:00 +00:00
/**
2017-08-02 11:10:30 +00:00
* writes string or Smartfile to disk.
* @param fileArg
2016-04-04 13:44:00 +00:00
* @param fileNameArg
* @param fileBaseArg
*/
export let toFs = async (
2023-11-04 19:07:43 +00:00
fileContentArg: string | Buffer | SmartFile | StreamFile,
2020-03-15 18:58:46 +00:00
filePathArg: string,
optionsArg: IToFsOptions = {}
) => {
2020-03-15 18:58:46 +00:00
const done = plugins.smartpromise.defer();
2017-04-29 14:50:06 +00:00
2017-05-26 12:47:41 +00:00
// check args
2017-04-29 14:50:06 +00:00
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments');
2017-04-29 14:50:06 +00:00
}
// prepare actual write action
let fileContent: string | Buffer;
let fileEncoding: 'utf8' | 'binary' = 'utf8';
let filePath: string = filePathArg;
2017-05-26 12:47:41 +00:00
// handle Smartfile
2023-11-04 19:07:43 +00:00
if (fileContentArg instanceof SmartFile) {
fileContent = fileContentArg.contentBuffer;
2017-05-26 12:47:41 +00:00
// handle options
if (optionsArg.respectRelative) {
filePath = plugins.path.join(filePath, fileContentArg.path);
}
2020-03-15 18:58:46 +00:00
} else if (Buffer.isBuffer(fileContentArg)) {
fileContent = fileContentArg;
2020-03-15 18:58:46 +00:00
fileEncoding = 'binary';
2017-04-29 14:50:06 +00:00
} else if (typeof fileContentArg === 'string') {
fileContent = fileContentArg;
2017-05-26 12:47:41 +00:00
} else {
throw new Error('fileContent is neither string nor Smartfile');
2017-04-29 14:50:06 +00:00
}
await smartfileFs.ensureDir(plugins.path.parse(filePath).dir);
plugins.fsExtra.writeFile(filePath, fileContent, { encoding: fileEncoding }, done.resolve);
return await done.promise;
};
2016-09-20 15:56:49 +00:00
2017-05-26 12:47:41 +00:00
/**
* writes a string or a Smartfile to disk synchronously, only supports string
* @param fileArg
* @param filePathArg
2017-05-26 12:47:41 +00:00
*/
export const toFsSync = (fileArg: string, filePathArg: string) => {
2017-04-29 14:50:06 +00:00
// function checks to abort if needed
if (!fileArg || !filePathArg) {
throw new Error('expected a valid arguments');
2017-04-29 14:50:06 +00:00
}
// prepare actual write action
let fileString: string;
const filePath: string = filePathArg;
2017-04-29 14:50:06 +00:00
if (typeof fileArg !== 'string') {
throw new Error('fileArg is not of type String.');
2017-04-29 14:50:06 +00:00
} else if (typeof fileArg === 'string') {
fileString = fileArg;
2017-04-29 14:50:06 +00:00
}
plugins.fsExtra.writeFileSync(filePath, fileString, { encoding: 'utf8' });
};
2017-05-26 12:47:41 +00:00
2023-11-04 19:07:43 +00:00
export let smartfileArrayToFs = async (smartfileArrayArg: SmartFile[], dirArg: string) => {
await smartfileFs.ensureDir(dirArg);
for (const smartfile of smartfileArrayArg) {
2017-05-26 12:47:41 +00:00
await toFs(smartfile, dirArg, {
respectRelative: true,
});
2017-05-26 12:47:41 +00:00
}
};