Skip to content

Commit

Permalink
fix(fs.service.js): fs.service was added to manage file-system inde…
Browse files Browse the repository at this point in the history
…xing more efficient

`fs.service` was added to manage file-system indexing more efficient. It also fixes
"twice-event-report" problem caused by `git` command (#21).
This service (that is a singleton) can cache the result of requests and so improves the performance.
It exposes an extra layer on top of `readDirectory` that consumers (`buildConfig.js`, `refresh.js`,
and `watch.js` for now) should use (instead of direct access to `readDirectory`).

fix #21
  • Loading branch information
mirismaili committed Aug 30, 2019
1 parent d309f32 commit f559e76
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
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;
}

0 comments on commit f559e76

Please sign in to comment.