From cf8636aa3bb199f589ea777c30117d1eb72a8f17 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Tue, 5 Dec 2023 02:56:42 +0200 Subject: [PATCH 1/7] Add strict mode --- tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 98d0126..04f0e5c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,8 @@ "moduleResolution": "node", "esModuleInterop": true, "declaration": true, - "composite": true + "composite": true, + "strict": true }, "include": ["src/**/*.ts"], "references": [ From 7f951185682e3423b8f3414ccb11eba0c014ae60 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Tue, 5 Dec 2023 03:09:46 +0200 Subject: [PATCH 2/7] Add configuration file / Include and exclude patterns --- mddb.config.js | 4 ++ package-lock.json | 23 +++++++--- package.json | 4 +- src/bin/index.js | 22 +++++++++- src/lib/CustomConfig.ts | 8 ++++ src/lib/indexFolder.ts | 69 ++++++++++++++++++++++++------ src/lib/markdowndb.ts | 34 +++++++++++++-- src/lib/parseFile.ts | 1 + src/lib/process.ts | 18 ++++++-- src/tests/extractWikiLinks.spec.ts | 4 +- src/tests/process.spec.ts | 11 +++-- tsconfig.lib.json | 2 +- 12 files changed, 163 insertions(+), 37 deletions(-) create mode 100644 mddb.config.js create mode 100644 src/lib/CustomConfig.ts diff --git a/mddb.config.js b/mddb.config.js new file mode 100644 index 0000000..0e3ba6e --- /dev/null +++ b/mddb.config.js @@ -0,0 +1,4 @@ +export default { + customFields: [ + ] +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c97855c..3e716bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@portaljs/remark-wiki-link": "^1.0.4", "gray-matter": "^4.0.3", "knex": "^2.4.2", + "micromatch": "^4.0.5", "react-markdown": "^9.0.1", "remark-gfm": "^3.0.1", "remark-parse": "^10.0.1", @@ -25,6 +26,7 @@ "@changesets/changelog-github": "^0.4.8", "@changesets/cli": "^2.26.1", "@types/jest": "^29.5.1", + "@types/micromatch": "^4.0.6", "@typescript-eslint/eslint-plugin": "^5.59.5", "@typescript-eslint/parser": "^5.59.5", "eslint": "^8.40.0", @@ -2154,6 +2156,12 @@ "@babel/types": "^7.3.0" } }, + "node_modules/@types/braces": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/braces/-/braces-3.0.4.tgz", + "integrity": "sha512-0WR3b8eaISjEW7RpZnclONaLFDf7buaowRHdqLp4vLj54AsSAYWfh3DRbfiYJY9XDxMgx1B4sE1Afw2PGpuHOA==", + "dev": true + }, "node_modules/@types/debug": { "version": "4.1.7", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.7.tgz", @@ -2236,6 +2244,15 @@ "@types/unist": "*" } }, + "node_modules/@types/micromatch": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/@types/micromatch/-/micromatch-4.0.6.tgz", + "integrity": "sha512-2eulCHWqjEpk9/vyic4tBhI8a9qQEl6DaK2n/sF7TweX9YESlypgKyhXMDGt4DAOy/jhLPvVrZc8pTDAMsplJA==", + "dev": true, + "dependencies": { + "@types/braces": "*" + } + }, "node_modules/@types/minimist": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz", @@ -2930,7 +2947,6 @@ "version": "3.0.2", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", - "dev": true, "dependencies": { "fill-range": "^7.0.1" }, @@ -4210,7 +4226,6 @@ "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", - "dev": true, "dependencies": { "to-regex-range": "^5.0.1" }, @@ -5182,7 +5197,6 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", - "dev": true, "engines": { "node": ">=0.12.0" } @@ -7634,7 +7648,6 @@ "version": "4.0.5", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", - "dev": true, "dependencies": { "braces": "^3.0.2", "picomatch": "^2.3.1" @@ -8307,7 +8320,6 @@ "version": "2.3.1", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", - "dev": true, "engines": { "node": ">=8.6" }, @@ -10537,7 +10549,6 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", - "dev": true, "dependencies": { "is-number": "^7.0.0" }, diff --git a/package.json b/package.json index 110c8a7..7f157e5 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "@portaljs/remark-wiki-link": "^1.0.4", "gray-matter": "^4.0.3", "knex": "^2.4.2", + "micromatch": "^4.0.5", "react-markdown": "^9.0.1", "remark-gfm": "^3.0.1", "remark-parse": "^10.0.1", @@ -57,6 +58,7 @@ "@changesets/changelog-github": "^0.4.8", "@changesets/cli": "^2.26.1", "@types/jest": "^29.5.1", + "@types/micromatch": "^4.0.6", "@typescript-eslint/eslint-plugin": "^5.59.5", "@typescript-eslint/parser": "^5.59.5", "eslint": "^8.40.0", @@ -69,4 +71,4 @@ "ts-node": "^10.9.1", "typescript": "^5.0.4" } -} \ No newline at end of file +} diff --git a/src/bin/index.js b/src/bin/index.js index 6459ccc..e45e20f 100755 --- a/src/bin/index.js +++ b/src/bin/index.js @@ -1,21 +1,39 @@ #!/usr/bin/env node - +import * as path from 'path'; import { MarkdownDB } from "../lib/markdowndb.js"; +async function loadConfig(configFilePath) { + const normalizedPath = path.resolve(configFilePath); + const fileUrl = new URL(`file://${normalizedPath}`); + + try { + // Import the module using the file URL + const configModule = await import(fileUrl.href); + return configModule.default; + } catch (error) { + throw new Error(`Error loading configuration file from ${normalizedPath}: ${error.message}`); + } +} + // TODO get these from markdowndb.config.js or something const dbPath = "markdown.db"; const ignorePatterns = [/Excalidraw/, /\.obsidian/, /DS_Store/]; -const [contentPath] = process.argv.slice(2); +const [contentPath, configFilePath] = process.argv.slice(2); if (!contentPath) { throw new Error("Invalid/Missing path to markdown content folder"); } +// Load the configuration file dynamically +const config = await loadConfig(configFilePath || 'mddb.config.js'); + const client = new MarkdownDB({ client: "sqlite3", connection: { filename: dbPath, }, +}, { + customConfig: config }); await client.init(); diff --git a/src/lib/CustomConfig.ts b/src/lib/CustomConfig.ts new file mode 100644 index 0000000..af52289 --- /dev/null +++ b/src/lib/CustomConfig.ts @@ -0,0 +1,8 @@ +import { FileInfo } from "./process.js"; +import { Root } from "remark-parse/lib/index.js"; + +export interface CustomConfig { + customFields?: ((fileInfo: FileInfo, ast: Root) => any)[]; + include?: string[]; + exclude?: string[]; +} diff --git a/src/lib/indexFolder.ts b/src/lib/indexFolder.ts index 6b28241..ae445a9 100644 --- a/src/lib/indexFolder.ts +++ b/src/lib/indexFolder.ts @@ -1,33 +1,76 @@ import { FileInfo, processFile } from "./process.js"; import { recursiveWalkDir } from "./recursiveWalkDir.js"; +import { CustomConfig } from "./CustomConfig.js"; +import micromatch from "micromatch"; export function indexFolder( folderPath: string, pathToUrlResolver: (filePath: string) => string, + config: CustomConfig, ignorePatterns?: RegExp[] ) { const filePathsToIndex = recursiveWalkDir(folderPath); const filteredFilePathsToIndex = filePathsToIndex.filter((filePath) => - shouldIncludeFile(filePath, ignorePatterns) + shouldIncludeFile({ + filePath, + ignorePatterns, + includeGlob: config.include, + excludeGlob: config.exclude, + }) ); const files: FileInfo[] = []; + + const customFields = config.customFields || []; for (const filePath of filteredFilePathsToIndex) { - const fileObject = processFile( - folderPath, - filePath, + const fileObject = processFile(folderPath, filePath, { pathToUrlResolver, - filePathsToIndex - ); + filePathsToIndex, + customFields, + }); files.push(fileObject); } return files; } -function shouldIncludeFile( - filePath: string, - ignorePatterns?: RegExp[] -): boolean { - return !( - ignorePatterns && ignorePatterns.some((pattern) => pattern.test(filePath)) - ); +function shouldIncludeFile({ + filePath, + ignorePatterns, + includeGlob, + excludeGlob, +}: { + filePath: string; + ignorePatterns?: RegExp[]; + includeGlob?: string[]; + excludeGlob?: string[]; +}): boolean { + const normalizedFilePath = filePath.replace(/\\/g, "/"); + + if ( + ignorePatterns && + ignorePatterns.some((pattern) => pattern.test(normalizedFilePath)) + ) { + return false; + } + + // Check if the file should be included based on includeGlob + if ( + includeGlob && + !includeGlob.some((pattern) => + micromatch.isMatch(normalizedFilePath, pattern) + ) + ) { + return false; + } + + // Check if the file should be excluded based on excludeGlob + if ( + excludeGlob && + excludeGlob.some((pattern) => + micromatch.isMatch(normalizedFilePath, pattern) + ) + ) { + return false; + } + + return true; } diff --git a/src/lib/markdowndb.ts b/src/lib/markdowndb.ts index a759467..bdf3b1e 100644 --- a/src/lib/markdowndb.ts +++ b/src/lib/markdowndb.ts @@ -12,6 +12,7 @@ import { getUniqueValues, } from "./databaseUtils.js"; import fs from "fs"; +import { CustomConfig } from "./CustomConfig.js"; const defaultFilePathToUrl = (filePath: string) => { let url = filePath @@ -40,14 +41,40 @@ const resolveLinkToUrlPath = (link: string, sourceFilePath?: string) => { */ export class MarkdownDB { config: Knex.Config; + customConfig: CustomConfig = {}; + //@ts-ignore db: Knex; /** * Constructs a new MarkdownDB instance. - * @param {Knex.Config} config - Knex configuration object. + * @param {Knex.Config} knexConfig - Knex configuration object. + * @param {CustomConfig} [customConfig] - Custom configuration object. */ - constructor(config: Knex.Config) { - this.config = config; + constructor( + knexConfig: Knex.Config, + options?: { customConfig?: CustomConfig; configFilePath?: string } + ) { + this.config = knexConfig; + if (options?.customConfig) { + this.customConfig = options.customConfig; + } else { + this.loadConfiguration(options?.configFilePath || "mddb.config.js"); + } + } + + private async loadConfiguration(configFilePath: string) { + const normalizedPath = path.resolve(configFilePath); + const fileUrl = new URL(`file://${normalizedPath}`); + + try { + // Import the module using the file URL + const configModule = await import(fileUrl.href); + this.customConfig = configModule.default; + } catch (error: any) { + throw new Error( + `Error loading configuration file from ${normalizedPath}: ${error.message}` + ); + } } /** @@ -82,6 +109,7 @@ export class MarkdownDB { const fileObjects = indexFolder( folderPath, pathToUrlResolver, + this.customConfig, ignorePatterns ); const filesToInsert = fileObjects.map(mapFileToInsert); diff --git a/src/lib/parseFile.ts b/src/lib/parseFile.ts index 953f7e8..fe207b3 100644 --- a/src/lib/parseFile.ts +++ b/src/lib/parseFile.ts @@ -28,6 +28,7 @@ export function parseFile(source: string, options?: ParsingOptions) { metadata.tasks = tasks; return { + ast, metadata, links, }; diff --git a/src/lib/process.ts b/src/lib/process.ts index 6a8f208..1e9a988 100644 --- a/src/lib/process.ts +++ b/src/lib/process.ts @@ -4,6 +4,7 @@ import path from "path"; import { File } from "./schema.js"; import { WikiLink, parseFile } from "./parseFile.js"; +import { Root } from "remark-parse/lib/index.js"; export interface FileInfo extends File { tags: string[]; @@ -15,8 +16,15 @@ export interface FileInfo extends File { export function processFile( rootFolder: string, filePath: string, - pathToUrlResolver: (filePath: string) => string, - filePathsToIndex: string[] + { + pathToUrlResolver = (filePath) => filePath, + filePathsToIndex = [], + customFields = [(fileInfo: FileInfo, ast: Root) => {}], + }: { + pathToUrlResolver: (filePath: string) => string; + filePathsToIndex: string[]; + customFields: ((fileInfo: FileInfo, ast: Root) => any)[]; + } ) { // Remove rootFolder from filePath const relativePath = path.relative(rootFolder, filePath); @@ -52,7 +60,7 @@ export function processFile( flag: "r", }); - const { metadata, links } = parseFile(source, { + const { ast, metadata, links } = parseFile(source, { from: relativePath, permalinks: filePathsToIndex, }); @@ -66,6 +74,10 @@ export function processFile( const tags = metadata?.tags || []; fileInfo.tags = tags; + for (let index = 0; index < customFields.length; index++) { + const customFieldFunction = customFields[index]; + customFieldFunction(fileInfo, ast); + } return fileInfo; } diff --git a/src/tests/extractWikiLinks.spec.ts b/src/tests/extractWikiLinks.spec.ts index ac9951f..703f7ce 100644 --- a/src/tests/extractWikiLinks.spec.ts +++ b/src/tests/extractWikiLinks.spec.ts @@ -1,4 +1,4 @@ -import { extractWikiLinks, processAST } from "../lib/parseFile"; +import { ParsingOptions, extractWikiLinks, processAST } from "../lib/parseFile"; // TODO test for links with headings and aliases ? // TODO test pdf embeds @@ -7,7 +7,7 @@ import { extractWikiLinks, processAST } from "../lib/parseFile"; // TODO test custom extractors // TODO test with other remark plugins e.g. original wiki links -const getLinksFromSource = (source: string, options?) => { +const getLinksFromSource = (source: string, options?: ParsingOptions) => { const from = "abc/foobar.md"; const ast = processAST(source, options); const links = extractWikiLinks(ast, { from: from, ...options }); diff --git a/src/tests/process.spec.ts b/src/tests/process.spec.ts index 43fd910..2bd7632 100644 --- a/src/tests/process.spec.ts +++ b/src/tests/process.spec.ts @@ -7,12 +7,11 @@ describe("Can parse a file and get file info", () => { test("can parse a file", async () => { const filePath = "index.mdx"; const fullPath = Path.join(pathToContentFixture, filePath); - const fileInfo = processFile( - pathToContentFixture, - fullPath, - (filePath) => filePath, - [] - ); + const fileInfo = processFile(pathToContentFixture, fullPath, { + pathToUrlResolver: (filePath) => filePath, + filePathsToIndex: [], + customFields: [], + }); expect(fileInfo.file_path).toBe(fullPath); expect(fileInfo.url_path).toBe("index.mdx"); diff --git a/tsconfig.lib.json b/tsconfig.lib.json index e578407..f4cbd48 100644 --- a/tsconfig.lib.json +++ b/tsconfig.lib.json @@ -18,5 +18,5 @@ "src/**/*.spec.ts", "src/**/*.test.ts" ], - "include": ["**/*.js", "**/*.ts"] + "include": ["**/*.js", "**/*.ts", "mddb.config.js"] } From 7e42ae252038813657974d73f6584072bddb0cde Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Tue, 5 Dec 2023 03:14:26 +0200 Subject: [PATCH 3/7] add npx markdowndb init CLI script that will bootstrap the config file --- mddb.config.js | 5 ++--- src/bin/index.js | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/mddb.config.js b/mddb.config.js index 0e3ba6e..8984816 100644 --- a/mddb.config.js +++ b/mddb.config.js @@ -1,4 +1,3 @@ export default { - customFields: [ - ] -} \ No newline at end of file + customFields: [] +}; diff --git a/src/bin/index.js b/src/bin/index.js index e45e20f..ae34bb8 100755 --- a/src/bin/index.js +++ b/src/bin/index.js @@ -1,5 +1,6 @@ #!/usr/bin/env node import * as path from 'path'; +import { promises as fs } from 'fs'; import { MarkdownDB } from "../lib/markdowndb.js"; async function loadConfig(configFilePath) { @@ -15,10 +16,27 @@ async function loadConfig(configFilePath) { } } +async function initConfigFile(configFilePath = "mddb.config.js") { + const normalizedPath = path.resolve(configFilePath); + const configFileContent = `export default {\n customFields: []\n};\n`; + + try { + await fs.writeFile(normalizedPath, configFileContent); + console.log(`Configuration file created at ${normalizedPath}`); + } catch (error) { + throw new Error(`Error creating configuration file at ${normalizedPath}: ${error.message}`); + } +} + // TODO get these from markdowndb.config.js or something const dbPath = "markdown.db"; const ignorePatterns = [/Excalidraw/, /\.obsidian/, /DS_Store/]; -const [contentPath, configFilePath] = process.argv.slice(2); +const [command, contentPath, configFilePath] = process.argv.slice(2); + +if (command === 'init') { + await initConfigFile(configFilePath); + process.exit(); +} if (!contentPath) { throw new Error("Invalid/Missing path to markdown content folder"); From 156f79fd61c2ba95dd670642cabb9c0624acf696 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Tue, 5 Dec 2023 03:26:47 +0200 Subject: [PATCH 4/7] fix tests --- src/bin/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bin/index.js b/src/bin/index.js index ae34bb8..5fb9c0a 100755 --- a/src/bin/index.js +++ b/src/bin/index.js @@ -31,7 +31,13 @@ async function initConfigFile(configFilePath = "mddb.config.js") { // TODO get these from markdowndb.config.js or something const dbPath = "markdown.db"; const ignorePatterns = [/Excalidraw/, /\.obsidian/, /DS_Store/]; -const [command, contentPath, configFilePath] = process.argv.slice(2); +let command, contentPath, configFilePath; + +if (process.argv[2] === 'init') { + [command, contentPath, configFilePath] = process.argv.slice(2); +} else { + [contentPath, configFilePath] = process.argv.slice(2); +} if (command === 'init') { await initConfigFile(configFilePath); From 48b38c4d510eebf59391017100131edddcd5ba5d Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Wed, 6 Dec 2023 19:07:13 +0200 Subject: [PATCH 5/7] Remove the init command --- src/bin/index.js | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/src/bin/index.js b/src/bin/index.js index 5fb9c0a..20292e4 100755 --- a/src/bin/index.js +++ b/src/bin/index.js @@ -16,33 +16,10 @@ async function loadConfig(configFilePath) { } } -async function initConfigFile(configFilePath = "mddb.config.js") { - const normalizedPath = path.resolve(configFilePath); - const configFileContent = `export default {\n customFields: []\n};\n`; - - try { - await fs.writeFile(normalizedPath, configFileContent); - console.log(`Configuration file created at ${normalizedPath}`); - } catch (error) { - throw new Error(`Error creating configuration file at ${normalizedPath}: ${error.message}`); - } -} - // TODO get these from markdowndb.config.js or something const dbPath = "markdown.db"; const ignorePatterns = [/Excalidraw/, /\.obsidian/, /DS_Store/]; -let command, contentPath, configFilePath; - -if (process.argv[2] === 'init') { - [command, contentPath, configFilePath] = process.argv.slice(2); -} else { - [contentPath, configFilePath] = process.argv.slice(2); -} - -if (command === 'init') { - await initConfigFile(configFilePath); - process.exit(); -} +const [contentPath, configFilePath] = process.argv.slice(2); if (!contentPath) { throw new Error("Invalid/Missing path to markdown content folder"); From a27ff6f90b4266e697d909bdad3e8a10d24eba4a Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Thu, 7 Dec 2023 01:08:19 +0200 Subject: [PATCH 6/7] Add default for configuration files --- src/bin/index.js | 11 ++++++----- src/lib/markdowndb.ts | 14 ++++++++------ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/bin/index.js b/src/bin/index.js index 20292e4..9df9587 100755 --- a/src/bin/index.js +++ b/src/bin/index.js @@ -1,18 +1,19 @@ #!/usr/bin/env node import * as path from 'path'; -import { promises as fs } from 'fs'; import { MarkdownDB } from "../lib/markdowndb.js"; async function loadConfig(configFilePath) { - const normalizedPath = path.resolve(configFilePath); + const normalizedPath = path.resolve(configFilePath || 'mddb.config.js'); const fileUrl = new URL(`file://${normalizedPath}`); try { - // Import the module using the file URL + // Import the module using the file URL const configModule = await import(fileUrl.href); return configModule.default; } catch (error) { - throw new Error(`Error loading configuration file from ${normalizedPath}: ${error.message}`); + if (configFilePath) { + throw new Error(`Error loading configuration file from ${normalizedPath}: ${error.message}`); + } } } @@ -26,7 +27,7 @@ if (!contentPath) { } // Load the configuration file dynamically -const config = await loadConfig(configFilePath || 'mddb.config.js'); +const config = await loadConfig(configFilePath); const client = new MarkdownDB({ client: "sqlite3", diff --git a/src/lib/markdowndb.ts b/src/lib/markdowndb.ts index bdf3b1e..7b6d09a 100644 --- a/src/lib/markdowndb.ts +++ b/src/lib/markdowndb.ts @@ -58,12 +58,12 @@ export class MarkdownDB { if (options?.customConfig) { this.customConfig = options.customConfig; } else { - this.loadConfiguration(options?.configFilePath || "mddb.config.js"); + this.loadConfiguration(options?.configFilePath); } } - private async loadConfiguration(configFilePath: string) { - const normalizedPath = path.resolve(configFilePath); + private async loadConfiguration(configFilePath?: string) { + const normalizedPath = path.resolve(configFilePath || "mddb.config.js"); const fileUrl = new URL(`file://${normalizedPath}`); try { @@ -71,9 +71,11 @@ export class MarkdownDB { const configModule = await import(fileUrl.href); this.customConfig = configModule.default; } catch (error: any) { - throw new Error( - `Error loading configuration file from ${normalizedPath}: ${error.message}` - ); + if (configFilePath) { + throw new Error( + `Error loading configuration file from ${normalizedPath}: ${error.message}` + ); + } } } From b15bf099589aac6dd959903e3826fb83e4ce4005 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Thu, 7 Dec 2023 01:11:07 +0200 Subject: [PATCH 7/7] Update to markdowndb.config.js --- mddb.config.js => markdowndb.config.js | 0 src/bin/index.js | 2 +- src/lib/markdowndb.ts | 2 +- tsconfig.lib.json | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename mddb.config.js => markdowndb.config.js (100%) diff --git a/mddb.config.js b/markdowndb.config.js similarity index 100% rename from mddb.config.js rename to markdowndb.config.js diff --git a/src/bin/index.js b/src/bin/index.js index 9df9587..b3afc77 100755 --- a/src/bin/index.js +++ b/src/bin/index.js @@ -3,7 +3,7 @@ import * as path from 'path'; import { MarkdownDB } from "../lib/markdowndb.js"; async function loadConfig(configFilePath) { - const normalizedPath = path.resolve(configFilePath || 'mddb.config.js'); + const normalizedPath = path.resolve(configFilePath || 'markdowndb.config.js'); const fileUrl = new URL(`file://${normalizedPath}`); try { diff --git a/src/lib/markdowndb.ts b/src/lib/markdowndb.ts index 7b6d09a..64437c1 100644 --- a/src/lib/markdowndb.ts +++ b/src/lib/markdowndb.ts @@ -63,7 +63,7 @@ export class MarkdownDB { } private async loadConfiguration(configFilePath?: string) { - const normalizedPath = path.resolve(configFilePath || "mddb.config.js"); + const normalizedPath = path.resolve(configFilePath || "markdowndb.config.js"); const fileUrl = new URL(`file://${normalizedPath}`); try { diff --git a/tsconfig.lib.json b/tsconfig.lib.json index f4cbd48..ffa03ee 100644 --- a/tsconfig.lib.json +++ b/tsconfig.lib.json @@ -18,5 +18,5 @@ "src/**/*.spec.ts", "src/**/*.test.ts" ], - "include": ["**/*.js", "**/*.ts", "mddb.config.js"] + "include": ["**/*.js", "**/*.ts", "markdowndb.config.js"] }