|
|
|
|
@ -6,8 +6,6 @@ let fileExtensionToImage: object;
|
|
|
|
|
|
|
|
|
|
export class Configurator {
|
|
|
|
|
|
|
|
|
|
fileutils: FileUtils = new FileUtils();
|
|
|
|
|
|
|
|
|
|
getFileExtensionToImageMap(): Object {
|
|
|
|
|
if (!fileExtensionToImage) {
|
|
|
|
|
fileExtensionToImage = this.loadAppConfig("file-extensions.json");
|
|
|
|
|
@ -16,14 +14,14 @@ export class Configurator {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loadAppConfig(fileName: string): object {
|
|
|
|
|
let filePath = this.fileutils.getPathToConfig(fileName);
|
|
|
|
|
let filePath = FileUtils.getPathToConfig(fileName);
|
|
|
|
|
let fileBuffer = fs.readFileSync(filePath);
|
|
|
|
|
let content = fileBuffer.toString('utf8')
|
|
|
|
|
return JSON.parse(content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadUserConfig(fileName: string): object {
|
|
|
|
|
let userConfigPath = this.fileutils.getPathToUserDir(fileName);
|
|
|
|
|
let userConfigPath = FileUtils.getPathToUserDir(fileName);
|
|
|
|
|
if (!fs.existsSync(userConfigPath)) {
|
|
|
|
|
return {};
|
|
|
|
|
} else {
|
|
|
|
|
@ -33,7 +31,7 @@ export class Configurator {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveUserConfig(fileName: string, obj: any) {
|
|
|
|
|
let userConfigPath = this.fileutils.getPathToUserDir(fileName);
|
|
|
|
|
let userConfigPath = FileUtils.getPathToUserDir(fileName);
|
|
|
|
|
let content = JSON.stringify(obj);
|
|
|
|
|
fs.writeFile(userConfigPath, content, (err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
@ -45,81 +43,104 @@ export class Configurator {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class FileUtils {
|
|
|
|
|
getPathToView(templateName: string): string {
|
|
|
|
|
export module FileUtils {
|
|
|
|
|
export function getPathToView(templateName: string): string {
|
|
|
|
|
return path.join(this.getPathToAssets(), "views", templateName + ".mustache")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPathToImage(imageName: string): string {
|
|
|
|
|
export function getPathToImage(imageName: string): string {
|
|
|
|
|
return path.join(this.getPathToAssets(), "images", imageName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPathToDocument(documentName: string): string {
|
|
|
|
|
export function getPathToDocument(documentName: string): string {
|
|
|
|
|
return path.join(this.getPathToAssets(), "documents", documentName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPathToConfig(documentName: string): string {
|
|
|
|
|
export function getPathToConfig(documentName: string): string {
|
|
|
|
|
return path.join(this.getPathToAssets(), "conf", documentName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPathToAssets(): string {
|
|
|
|
|
export function getPathToAssets(): string {
|
|
|
|
|
return path.join(__dirname, "..", "assets");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getFileExtension(fileName: string): string {
|
|
|
|
|
export function getFileExtension(fileName: string): string {
|
|
|
|
|
return path.extname(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPathToUserDir(fileName: string): string {
|
|
|
|
|
export function getPathToUserDir(fileName: string): string {
|
|
|
|
|
let dirPath = path.join(__dirname, "..", "user");
|
|
|
|
|
let filePath = path.join(dirPath, fileName);
|
|
|
|
|
this.touchDir(dirPath);
|
|
|
|
|
return filePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
touchDir(dirPath: string) {
|
|
|
|
|
export function touchDir(dirPath: string) {
|
|
|
|
|
if (!fs.existsSync(dirPath)) {
|
|
|
|
|
fs.mkdirSync(dirPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class DocumentDirectory {
|
|
|
|
|
constructor(filepath: string) {
|
|
|
|
|
if (fs.existsSync(filepath)) {
|
|
|
|
|
let stats = fs.lstatSync(filepath);
|
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
|
let contents = fs.readdirSync(filepath);
|
|
|
|
|
for (let i = 0, l = contents.length; i < l; i++) {
|
|
|
|
|
console.log(contents[i]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw Error("attempted to scan non-directory");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw Error("attempted to load document directory for non-existing directory");
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* you *must* provide an absolute file path here
|
|
|
|
|
*/
|
|
|
|
|
constructor(filePath: string) {
|
|
|
|
|
this.root = new DirectoryNode(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
children: FileNode[];
|
|
|
|
|
root: DirectoryNode;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class FileNode {
|
|
|
|
|
filePath: string;
|
|
|
|
|
|
|
|
|
|
constructor(filePath: string) {
|
|
|
|
|
this.filePath = filePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
open() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class DirectoryNode extends FileNode {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
children: FileNode[] = [];
|
|
|
|
|
|
|
|
|
|
children: FileNode[];
|
|
|
|
|
constructor(filePath: string) {
|
|
|
|
|
super(filePath);
|
|
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
|
|
let stats = fs.lstatSync(filePath);
|
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
|
let contents = fs.readdirSync(filePath);
|
|
|
|
|
for (let i = 0, l = contents.length; i < l; i++) {
|
|
|
|
|
let childPath = path.join(filePath, contents[i]);
|
|
|
|
|
let childStats = fs.lstatSync(childPath);
|
|
|
|
|
if (childStats.isDirectory()) {
|
|
|
|
|
this.children.push(new DirectoryNode(childPath));
|
|
|
|
|
} else {
|
|
|
|
|
this.children.push(new DocumentNode(childPath));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw Error(`attempted to scan non-directory: ${filePath}`);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw Error(`attempted to load document directory for non-existing directory: ${filePath}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class DocumentNode extends FileNode {
|
|
|
|
|
|
|
|
|
|
constructor(filePath: string) {
|
|
|
|
|
super(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|