Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"fs.service" was added to manage file-system indexing more efficient + fix #21 #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/tools/fs.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import readDirectory from './readDirectory';
import findRootDirectory from './findRootDirectory';

// noinspection JSUnusedGlobalSymbols
export default (() => {
let kojiConfigFiles;

function refresh() {
kojiConfigFiles = readDirectory(findRootDirectory())
.filter((path) => (path.endsWith('koji.json') || path.includes('.koji')) && !path.includes('.koji-resources'));
}

refresh();

// noinspection JSUnusedAssignment, JSUnusedGlobalSymbols
return {
kojiConfigFiles,
refresh,
};
})();
48 changes: 21 additions & 27 deletions src/tools/writeConfig.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
import fs from 'fs';
import readDirectory from './readDirectory';
import findRootDirectory from './findRootDirectory';
import fsService from './fs.service';

const writeConfig = () => {
export default function writeConfig() {
const projectConfig = {};
const root = findRootDirectory();

// Add config items from koji json files
readDirectory(root)
.filter((path) => (path.endsWith('koji.json') || path.includes('.koji')) && !path.includes('.koji-resources'))
.forEach((path) => {
try {
const file = JSON.parse(fs.readFileSync(path, 'utf8'));
fsService.kojiConfigFiles.forEach((path) => {
try {
const file = JSON.parse(fs.readFileSync(path, 'utf8'));

Object.keys(file).forEach((key) => {
// If the key already exists in the project config, use it
if (projectConfig[key]) {
if (Array.isArray(projectConfig[key]) && Array.isArray(file[key])) {
projectConfig[key] = projectConfig[key].concat(file[key]);
} else {
projectConfig[key] = Object.assign(projectConfig[key], file[key]);
}
Object.keys(file).forEach((key) => {
// If the key already exists in the project config, use it
if (projectConfig[key]) {
if (Array.isArray(projectConfig[key]) && Array.isArray(file[key])) {
projectConfig[key] = projectConfig[key].concat(file[key]);
} else {
// Otherwise, set it
projectConfig[key] = file[key];
projectConfig[key] = Object.assign(projectConfig[key], file[key]);
}
});
} catch (err) {
//
}
});
} else {
// Otherwise, set it
projectConfig[key] = file[key];
}
});
} catch (err) {
//
}
});

// Expose the serviceMap based on environment variables
projectConfig.serviceMap = Object.keys(process.env).reduce((acc, cur) => {
Expand All @@ -48,6 +44,4 @@ const writeConfig = () => {
} catch (err) {
//
}
};

export default writeConfig;
}
16 changes: 6 additions & 10 deletions src/watch.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import chokidar from 'chokidar';

import readDirectory from './tools/readDirectory';
import findRootDirectory from './tools/findRootDirectory';

import * as chokidar from 'chokidar';
import fsService from './tools/fs.service';
import writeConfig from './tools/writeConfig';

const watch = () => {
// noinspection JSUnusedGlobalSymbols
export default function watch() {
// Generate a base config
writeConfig();

// Watch the .koji directory from a node_modules directory
const files = readDirectory(findRootDirectory())
const files = fsService.kojiConfigFiles
.filter((path) => (path.endsWith('koji.json') || path.includes('.koji')) && !path.includes('.koji-resources'));

// Note: Polling is used by default in the container via
Expand All @@ -26,6 +24,4 @@ const watch = () => {
const watched = watcher.getWatched();
Object.keys(watched).map((path) => watched[path].map((file) => console.log(`[@withkoji/vcc] Watching ${path}/${file}`)));
});
};

export default watch;
}