@rr0/cms
Version:
RR0 Content Management System (CMS)
30 lines (29 loc) • 1.17 kB
JavaScript
import { StringUtil } from "../../util/string/StringUtil.js";
import path from "path";
import fs from "fs";
import { FileContents } from "@javarome/fileutil";
export class FileSource {
constructor(encoding) {
this.encoding = encoding;
}
/**
* Get the file name to read/write data from an external datasource.
*
* @param context
* @param datasource
* @protected
*/
fileName(context, datasource) {
const authorsStr = datasource.authors.map(author => StringUtil.removeAccents(author).replace(FileSource.specialChars, "")).join("-");
return path.join(path.dirname(context.file.name), authorsStr + "_" + StringUtil.removeAccents(datasource.copyright).replace(FileSource.specialChars, "-"));
}
async read(context, datasource) {
return FileContents.read(this.fileName(context, datasource), this.encoding);
}
writeContents(context, contents, datasource) {
const fileName = this.fileName(context, datasource);
fs.writeFileSync(fileName, contents, { encoding: this.encoding });
return fileName;
}
}
FileSource.specialChars = /[ \-?!&*#().:\/\\;=°',]/g;