nocobase/packages/core/server/src/read-config.ts
chenos f5dbb04a9f
fix: create-nocobase-app (#306)
* chore: create-nocobase-app

* chore: change create-nocobase-app lib to src

* chore(versions): 😊 publish v0.6.2-alpha.9

* fix: publish

* chore(versions): 😊 publish v0.6.2-alpha.10

* fix: read-config

* chore(versions): 😊 publish v0.6.2-alpha.11

* fix: create-nocobase-app publish

* chore: create-nocobase-app package.json

* chore(versions): 😊 publish v0.6.2-alpha.12

Co-authored-by: Chareice <chareice@live.com>
2022-04-21 00:15:38 +08:00

59 lines
1.5 KiB
TypeScript

import lodash from 'lodash';
import fs from 'fs';
import path from 'path';
export async function readConfig(dir: string) {
const repository = new ConfigurationRepository();
await loadConfiguration(dir, repository);
return repository.toObject();
}
export class ConfigurationRepository {
protected items = new Map<string, any>();
get(key: string, defaultValue = undefined) {
if (this.items.has(key)) {
return this.items.get(key);
}
return defaultValue;
}
set(key: string, value: any) {
return this.items.set(key, value);
}
toObject() {
const result = {};
for (const [key, value] of this.items.entries()) {
lodash.set(result, key, value);
}
return result;
}
}
export async function loadConfiguration(configurationDir: string, repository: ConfigurationRepository) {
const getConfigurationFiles = async (dir: string, prefix = []) => {
const files = await fs.promises.readdir(dir, { withFileTypes: true });
for (const file of files) {
if (file.isDirectory()) {
await getConfigurationFiles(path.join(dir, file.name), [...prefix, file.name]);
} else {
if (!['ts', 'js'].includes(file.name.split('.').slice(1).join('.'))) {
continue;
}
const filePath = path.join(dir, file.name);
const keyName = path.parse(filePath).name;
const configuration = require(filePath).default;
repository.set([...prefix, keyName].join('.'), configuration);
}
}
};
await getConfigurationFiles(configurationDir);
}