diff --git a/package.json b/package.json index fc6f9cb..2c4e5ab 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "homepage": "https://github.com/zakodium/nmrium-cli#readme", "devDependencies": { "@types/jest": "^29.5.12", + "@types/node": "^20.12.7", "eslint": "^8.56.0", "eslint-config-cheminfo": "^9.1.1", "esm": "^3.2.25", diff --git a/src/__tests__/createGeneralTOC.test.js b/src/__tests__/createGeneralTOC.test.js index 9901aa7..8073366 100644 --- a/src/__tests__/createGeneralTOC.test.js +++ b/src/__tests__/createGeneralTOC.test.js @@ -1,4 +1,4 @@ -import { statSync } from 'fs'; +import { readFileSync, statSync } from 'fs'; import { join } from 'path'; import readdir from 'recursive-readdir'; @@ -11,11 +11,17 @@ const dataDir = join(__dirname, 'general'); test('createGeneralTOC', async () => { await createGeneralTOC(__dirname, { dataDir }); - const files = (await readdir(dataDir)).sort(); - const sizes = files + const files = (await readdir(dataDir)).sort() .filter((file) => file.match(/\.json$/)) + + const sizes = files .map((file) => { return statSync(file).size; }); - expect(sizes).toStrictEqual([1656, 707, 1002, 483, 250]); + expect(sizes).toStrictEqual([1656, + 832, + 1127, + 96, + 487, + 250,]); }); diff --git a/src/__tests__/general/Group1/Data1/index.json b/src/__tests__/general/Group1/Data1/index.json index 4d9cb8c..9341585 100644 --- a/src/__tests__/general/Group1/Data1/index.json +++ b/src/__tests__/general/Group1/Data1/index.json @@ -28,5 +28,13 @@ ] } }, - "version": 6 + "version": 6, + "settings": { + "display": { + "general": { + "hideGeneralSettings": true + } + }, + "version": 2 + } } \ No newline at end of file diff --git a/src/__tests__/general/Group1/Data2/index.json b/src/__tests__/general/Group1/Data2/index.json index 844c38e..f683676 100644 --- a/src/__tests__/general/Group1/Data2/index.json +++ b/src/__tests__/general/Group1/Data2/index.json @@ -44,5 +44,13 @@ ] } }, - "version": 6 + "version": 6, + "settings": { + "display": { + "general": { + "hideGeneralSettings": true + } + }, + "version": 2 + } } \ No newline at end of file diff --git a/src/__tests__/general/Group1/index.yml b/src/__tests__/general/Group1/index.yml new file mode 100644 index 0000000..74ff5da --- /dev/null +++ b/src/__tests__/general/Group1/index.yml @@ -0,0 +1,3 @@ +menuLabel: Menu label +default: + settingsFilename: settings.json diff --git a/src/__tests__/general/Group1/settings.json b/src/__tests__/general/Group1/settings.json new file mode 100644 index 0000000..f15155c --- /dev/null +++ b/src/__tests__/general/Group1/settings.json @@ -0,0 +1,8 @@ +{ + "display": { + "general": { + "hideGeneralSettings": true + } + }, + "version": 2 +} diff --git a/src/__tests__/general/toc.json b/src/__tests__/general/toc.json index 522e4ad..4ba0b97 100644 --- a/src/__tests__/general/toc.json +++ b/src/__tests__/general/toc.json @@ -6,7 +6,7 @@ "selected": true }, { - "groupName": "Group1", + "groupName": "Menu label", "folderName": "Group1", "children": [ { diff --git a/src/commands/toc/createToc.js b/src/commands/toc/createToc.js index 90589e4..bb9ca50 100644 --- a/src/commands/toc/createToc.js +++ b/src/commands/toc/createToc.js @@ -18,7 +18,6 @@ const debug = debugFct('nmrium.toc'); */ export async function createToc(commandDir, folderProcessor, options = {}) { const { dataDir = commandDir } = options; - debug(`Creating TOC of: ${dataDir}`); let toc = []; @@ -46,6 +45,10 @@ async function processFolder( lstatSync(join(currentFolder, file)).isDirectory(), ); + // top level configuration ? + const { defaultFolderConfig } = getFolderConfig(currentFolder); + options = { ...defaultFolderConfig, ...options }; + for (let subfolder of folders) { // is there any molfile ? let isDataFolder = readdirSync(join(currentFolder, subfolder)).some( @@ -59,7 +62,9 @@ async function processFolder( recursive: true, }).some((file) => file.match(/(?:.mol|.dx|.jdx)$/i)); if (containsData) { - const folderConfig = getFolderConfig(join(currentFolder, subfolder)); + const { folderConfig, defaultFolderConfig } = getFolderConfig( + join(currentFolder, subfolder), + ); const subTOC = { groupName: folderConfig.menuLabel || subfolder.replace(/^[0-9]*_/, ''), @@ -72,7 +77,7 @@ async function processFolder( join(folder, subfolder), subTOC.children, folderProcessor, - { ...options }, + { ...defaultFolderConfig, ...options }, ); } } diff --git a/src/commands/toc/getFolderConfig.js b/src/commands/toc/getFolderConfig.js index a237b23..8250bda 100644 --- a/src/commands/toc/getFolderConfig.js +++ b/src/commands/toc/getFolderConfig.js @@ -8,13 +8,26 @@ export function getFolderConfig(folder) { const folderConfig = existsSync(folderConfigFilename) ? YAML.parse(readFileSync(folderConfigFilename, 'utf8')) : {}; - if (folderConfig.settingsFilename) { - const settingsFilename = join(folder, folderConfig.settingsFilename); + + const defaultFolderConfig = folderConfig.default || {}; + delete folderConfig.default; + + loadSettings(folderConfig, folder); + loadSettings(defaultFolderConfig, folder); + + + return { + folderConfig: { ...defaultFolderConfig, ...folderConfig }, defaultFolderConfig + }; +} + +function loadSettings(config, folder) { + if (config.settingsFilename) { + const settingsFilename = join(folder, config.settingsFilename); if (existsSync(settingsFilename)) { - folderConfig.settings = JSON.parse( + config.settings = JSON.parse( readFileSync(settingsFilename, 'utf8'), ); } } - return folderConfig; -} +} \ No newline at end of file diff --git a/src/commands/toc/processGeneralFolder.js b/src/commands/toc/processGeneralFolder.js index b62ac94..1e5c6a4 100644 --- a/src/commands/toc/processGeneralFolder.js +++ b/src/commands/toc/processGeneralFolder.js @@ -15,7 +15,10 @@ let dataCount = 0; export async function processGeneralFolder(basename, folder, toc, options) { const currentFolder = join(basename, folder); - options = { ...options, ...getFolderConfig(currentFolder) }; + + const { folderConfig, defaultFolderConfig } = getFolderConfig(currentFolder); + + options = { ...options, ...defaultFolderConfig, ...folderConfig }; const { id, nmrium } = await loadData(currentFolder, options);