From 844a26e2a1354960f0c0f6df500bc1bedab8273d Mon Sep 17 00:00:00 2001 From: dtookey Date: Wed, 16 Dec 2020 11:59:48 -0500 Subject: [PATCH] file-specific configs implemented --- src/assets/resources/coming-soon.pdf.json | 5 +++ src/ts_source/fileutils.ts | 47 ++++++++++++++++++++--- tests/fileutils-test.ts | 22 +++-------- 3 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 src/assets/resources/coming-soon.pdf.json diff --git a/src/assets/resources/coming-soon.pdf.json b/src/assets/resources/coming-soon.pdf.json new file mode 100644 index 0000000..6a3ae3e --- /dev/null +++ b/src/assets/resources/coming-soon.pdf.json @@ -0,0 +1,5 @@ +{ + "description": "Coming soon pdf", + "imagePath": "../resources/coming-soon.pdf", + "show": true +} \ No newline at end of file diff --git a/src/ts_source/fileutils.ts b/src/ts_source/fileutils.ts index 1c00146..6a1c547 100644 --- a/src/ts_source/fileutils.ts +++ b/src/ts_source/fileutils.ts @@ -1,3 +1,5 @@ + + let path = require('path') let fs = require('fs'); const shell = require('electron').shell; @@ -107,7 +109,6 @@ export class DocumentDirectory { getCards(): object { return DocumentDirectory.walkCards(this.root); - } private static walkCards(d: DirectoryNode): Map { @@ -124,7 +125,17 @@ export class DocumentDirectory { this.mergeMaps(cardsByCategory, subCards); } } - let sCards = dir.getDocuments() + let sCards = dir.getDocuments(); + let category = dir.getCategory(); + for (let sCard in sCards) { + let scrd = sCards[sCard]; + let cards = cardsByCategory.get(category); + if (cards === undefined || cards === null) { + cards = []; + } + cards.push(scrd.toCard()); + cardsByCategory.set(category, cards); + } } else { let category = (child.parent as DirectoryNode).getCategory(); let cards = cardsByCategory.get(category); @@ -148,7 +159,7 @@ export class DocumentDirectory { sa = []; } let sb = b.get(key) - if (sb === undefined || sa === null) { + if (sb === undefined) { sb = []; } sa.push(...sb); @@ -168,24 +179,50 @@ export class FileNode { this.parent = parent; } + static loadAltProps(path: String): object { + let props = {}; + let jsonPath = path + ".json" + if (fs.existsSync(jsonPath)) { + let raw = fs.readFileSync(jsonPath); + props = JSON.parse(raw.toString()); + } + return props + } + open() { shell.openItem(this.filePath); } + show() { + shell.showItemInFolder(this.filePath); + } + toCard(): object { + let altProps = FileNode.loadAltProps(this.filePath) let cardObj = { "title": this.getTitle(), "description": "", "imagePath": "", "urlText": this.filePath, - "altText": "" + "altText": "", + "fileCard": true } + + let altKeys = Object.keys(altProps); + for (let kidx in altKeys) { + let key = altKeys[kidx]; + cardObj[key] = altProps[key] + } + return cardObj } getTitle(): String { let name = path.basename(this.filePath); - return name + let ext = path.extname(name); + let cName = name.replace(ext, ""); + cName = cName.replace(/[\-._]/ig, " ") + return `${cName} (${ext.substr(1).toUpperCase()})` } isDescriptor() { diff --git a/tests/fileutils-test.ts b/tests/fileutils-test.ts index f98ba57..d69f074 100644 --- a/tests/fileutils-test.ts +++ b/tests/fileutils-test.ts @@ -2,12 +2,13 @@ import {DocumentDirectory, FileNode} from "../src/ts_source/fileutils"; const path = require('path'); const chai = require('chai'); +const {performance} = require('perf_hooks'); describe('fileutils', () => { it('DocumentDirectory Constructor fail-on-not-exist', testDocumentDirectoryFailNoExist) it('DocumentDirectory Constructor fail-on-file', testDocumentDirectoryFailFile) it('documentDirectoryConstructor - debug', testDocumentDirectoryConstructor); - it('pass by test', passBy); + }); function testDocumentDirectoryFailNoExist() { @@ -26,21 +27,10 @@ function testDocumentDirectoryFailFile() { function testDocumentDirectoryConstructor() { let directoryPath = path.join(__dirname, "../src/assets/resources"); + let start = performance.now() let documents = new DocumentDirectory(directoryPath); - documents.getCards() - -} - -function passBy(){ - let a = ["a", "b"]; - let b = ["c", "d"]; - testCopy(a, b); - for(let c in a){ - console.log(c); - } -} - + let end = performance.now() + console.log(documents.getCards()); + console.log(`Execution time: ${end-start}ms`) -function testCopy(a: String[], b: String[]){ - a.push(...b); }