Skip to content

Commit

Permalink
feat: add default in config
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Apr 28, 2024
1 parent d683e0b commit 977c5c4
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 10 additions & 4 deletions src/__tests__/createGeneralTOC.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { statSync } from 'fs';
import { readFileSync, statSync } from 'fs';

Check failure on line 1 in src/__tests__/createGeneralTOC.test.js

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

'readFileSync' is defined but never used
import { join } from 'path';

import readdir from 'recursive-readdir';
Expand All @@ -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,]);
});
10 changes: 9 additions & 1 deletion src/__tests__/general/Group1/Data1/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@
]
}
},
"version": 6
"version": 6,
"settings": {
"display": {
"general": {
"hideGeneralSettings": true
}
},
"version": 2
}
}
10 changes: 9 additions & 1 deletion src/__tests__/general/Group1/Data2/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,13 @@
]
}
},
"version": 6
"version": 6,
"settings": {
"display": {
"general": {
"hideGeneralSettings": true
}
},
"version": 2
}
}
3 changes: 3 additions & 0 deletions src/__tests__/general/Group1/index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
menuLabel: Menu label
default:
settingsFilename: settings.json
8 changes: 8 additions & 0 deletions src/__tests__/general/Group1/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"display": {
"general": {
"hideGeneralSettings": true
}
},
"version": 2
}
2 changes: 1 addition & 1 deletion src/__tests__/general/toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"selected": true
},
{
"groupName": "Group1",
"groupName": "Menu label",
"folderName": "Group1",
"children": [
{
Expand Down
11 changes: 8 additions & 3 deletions src/commands/toc/createToc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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(
Expand All @@ -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]*_/, ''),
Expand All @@ -72,7 +77,7 @@ async function processFolder(
join(folder, subfolder),
subTOC.children,
folderProcessor,
{ ...options },
{ ...defaultFolderConfig, ...options },
);
}
}
Expand Down
23 changes: 18 additions & 5 deletions src/commands/toc/getFolderConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 4 additions & 1 deletion src/commands/toc/processGeneralFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 977c5c4

Please sign in to comment.