diff --git a/src/ts_source/fileutils.ts b/src/ts_source/fileutils.ts index b94105e..b65b7a2 100644 --- a/src/ts_source/fileutils.ts +++ b/src/ts_source/fileutils.ts @@ -1,7 +1,7 @@ let path = require('path') let fs = require('fs'); - +let shell = require('electron').shell; let fileExtensionToImage: object; export class Configurator { @@ -88,7 +88,7 @@ export class DocumentDirectory { * you *must* provide an absolute file path here */ constructor(filePath: string) { - this.root = new DirectoryNode(filePath); + this.root = new DirectoryNode(filePath, null); } root: DirectoryNode; @@ -97,21 +97,23 @@ export class DocumentDirectory { export class FileNode { filePath: string; + parent: FileNode; - constructor(filePath: string) { + constructor(filePath: string, parent: FileNode) { this.filePath = filePath; + this.parent = parent; } open() { - + shell.open(this.filePath); } } export class DirectoryNode extends FileNode { children: FileNode[] = []; - constructor(filePath: string) { - super(filePath); + constructor(filePath: string, parent: FileNode) { + super(filePath, parent); if (fs.existsSync(filePath)) { let stats = fs.lstatSync(filePath); if (stats.isDirectory()) { @@ -120,27 +122,37 @@ export class DirectoryNode extends FileNode { let childPath = path.join(filePath, contents[i]); let childStats = fs.lstatSync(childPath); if (childStats.isDirectory()) { - this.children.push(new DirectoryNode(childPath)); + this.children.push(new DirectoryNode(childPath, this)); } else { - this.children.push(new DocumentNode(childPath)); + this.children.push(new DocumentNode(childPath, this)); } } } else { - throw Error(`attempted to scan non-directory: ${filePath}`); + throw new DirectoryIsActuallyFileError(filePath); } } else { - throw Error(`attempted to load document directory for non-existing directory: ${filePath}`); + throw new DirectoryDoesNotExistError(filePath); } } } export class DocumentNode extends FileNode { + constructor(filePath: string, parent: FileNode) { + super(filePath, parent); + } +} + +export class DirectoryIsActuallyFileError extends Error { constructor(filePath: string) { - super(filePath); + super(`attempted to scan non-directory: ${filePath}`); } - } +export class DirectoryDoesNotExistError extends Error { + constructor(filePath: string) { + super(`attempted to load document directory for non-existing directory: ${filePath}`); + } +} diff --git a/tests/fileutils-test.ts b/tests/fileutils-test.ts index f652681..04bcfa2 100644 --- a/tests/fileutils-test.ts +++ b/tests/fileutils-test.ts @@ -1,17 +1,29 @@ -import {DocumentDirectory} from "../src/ts_source/fileutils"; +import {DirectoryIsActuallyFileError, DocumentDirectory} from "../src/ts_source/fileutils"; const path = require('path'); +const chai = require('chai'); -describe('fileutils', ()=>{ +describe('fileutils', () => { + it('DocumentDirectory Constructor fail-on-not-exist', testDocumentDirectoryFailNoExist) it('DocumentDirectory Constructor fail-on-file', testDocumentDirectoryFailFile) it('documentDirectoryConstructor', testDocumentDirectoryConstructor); }); -function testDocumentDirectoryFailFile(){ - let directoryPath = path.join(__dirname, "../src/assets/documents"); +function testDocumentDirectoryFailNoExist() { + let directoryPath = path.join(__dirname, "../src/assets/documents/madeup"); + chai.expect(() => { + new DocumentDirectory(directoryPath) + }).to.throw(Error) +} + +function testDocumentDirectoryFailFile() { + let directoryPath = path.join(__dirname, "../src/assets/documents/Test.Word.Dock.docx"); + chai.expect(() => { + new DocumentDirectory(directoryPath) + }).to.throw(Error) } -function testDocumentDirectoryConstructor(){ +function testDocumentDirectoryConstructor() { let directoryPath = path.join(__dirname, "../src/assets/documents"); let documents = new DocumentDirectory(directoryPath); let firstChild = documents.root.children[0];