Skip to content

Commit

Permalink
#27 Added support for not adding projects to dever_config.json when k…
Browse files Browse the repository at this point in the history
…eywords conflict with pre-defined keys
  • Loading branch information
czprz committed Jan 31, 2022
1 parent 933eb12 commit 902a21b
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 119 deletions.
6 changes: 3 additions & 3 deletions bin/common/default-yargs-generator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const config_handler = require("../configuration/handleConfigFile");
const components_handler = require("../configuration/handleComponents");
const projectsConfig = require("../configuration/projects-config");
const versionChecker = require('../common/helper/version-checker');

const init = require("../init");
Expand Down Expand Up @@ -80,7 +80,7 @@ module.exports = new class {
* Shows a list of found components in the console
*/
#listAllComponents() {
const projects = components_handler.getAllComponents();
const projects = projectsConfig.getAll();
if (projects == null || projects.length === 0) {
console.error(`Could not find any projects. Please try running ${chalk.green('dever init')}`);
return;
Expand All @@ -94,7 +94,7 @@ module.exports = new class {
}

#listAllUnsupportedProjects() {
const projects = components_handler.getAllComponents();
const projects = projectsConfig.getAll();
if (projects == null || projects.length === 0) {
console.error(`Could not find any projects. Please try running ${chalk.green('dever init')}`);
return;
Expand Down
106 changes: 0 additions & 106 deletions bin/configuration/handleComponents.js

This file was deleted.

118 changes: 118 additions & 0 deletions bin/configuration/projects-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const config_handler = require('./handleConfigFile');

module.exports = new class {
/**
* Gets only one component specifically looking through component keywords found in dever.json
* @param keyword
* @returns Config
*/
get(keyword) {
let config = this.#getProjects();
if (config == null) {
return null;
}

let components = [];

config.components.forEach(x => {
if (x == null) {
return;
}

if (x.keywords.includes(keyword)) {
components.push(x);
}
})

if (components.length > 1) {
console.error('Components are not allowed to share keywords. Please fix this.');
return null;
}

if (components.length === 0) {
return null;
}

return components[0];
}

/**
* Gets all projects from dever_config.json
* @returns {Config[] | null}
*/
getAll() {
const config = this.#getProjects();
if (config == null) {
return null;
}

const components = [];

config.components.forEach(x => {
if (x == null) {
return;
}

components.push(x);
});

return components;
}

/**
* Removes all projects from dever_config.json
* @return void
*/
clear() {
const config = config_handler.get();

config.components = [];

config_handler.write(config);
}

/**
* Adds project to dever_config.json
* @param file string
*/
add(file) {
const config = config_handler.get();

config.components.push(file);

config_handler.write(config);
}

/**
* Removes project from dever_config.json
* @param file {string}
*/
remove(file) {
const config = config_handler.get();

const index = config.components.indexOf(file);
if (index === -1) {
return;
}

config.components.splice(index, 1);

config_handler.write(config);
}

/**
* Gets all components and their dever.json configuration
* @returns LocalConfig
*/
#getProjects() {
const config = config_handler.get();
if (config.components == null || config.components.length === 0) {
console.error("No components found. Please run 'dever init'");
return null;
}

return {
components: config.components.map(x => config_handler.getComponentConfig(x))
}
}
}
4 changes: 2 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const defaultYargsGenerator = require('./common/default-yargs-generator');
const projectYargsGenerator = require('./common/project-yargs-generator');
const versionChecker = require('./common/helper/version-checker');
const projectConfig = require('./configuration/handleComponents');
const projectConfig = require('./configuration/projects-config');

let argv = process.argv.slice(2);

Expand Down Expand Up @@ -31,7 +31,7 @@ function projectYargs(keyword, config) {

if (argv.length !== 0 && !['init', 'list', 'config'].some(x => x === argv[0])) {
const keyword = argv[0];
const config = projectConfig.getComponent(keyword);
const config = projectConfig.get(keyword);

if (!versionChecker.supported(config)) {
console.error(`dever does not support this projects dever.json version`);
Expand Down
45 changes: 37 additions & 8 deletions bin/init.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const powershell = require('./common/helper/powershell');
const componentHandler = require('./configuration/handleComponents');
const projectsConfig = require('./configuration/projects-config');
const versionChecker = require('./common/helper/version-checker');

const chalk = require("chalk");
const path = require("path");
const fs = require("fs");

module.exports = new class {
#preDefinedKeys = ['init', 'list', 'config', 'validate'];

async init() {
const file = path.join(path.dirname(fs.realpathSync(__filename)), 'common/find_all_dever_json_files.ps1');

console.log('Initialization has started.. Please wait..');

componentHandler.clearComponents();
projectsConfig.clear();

const raw = await powershell.executeFileSync(file);
const paths = raw.trim().split('\n');
Expand All @@ -20,15 +23,25 @@ module.exports = new class {
this.#getConfigFiles(path);
}

const allConfigs = componentHandler.getAllComponents();
if (!versionChecker.supported(allConfigs)) {
console.warn(`One or more of the found projects is not supported due to dever.json version not being supported by the installed version of dever`);
console.warn(`Check 'dever list --not-supported' to get a list of the unsupported projects`);
}
const configs = projectsConfig.getAll();

this.#checkForSupportedVersion(configs);
this.#checkForKeywordViolations(configs);

console.log('Initialization has been completed!');
}

/**
* Check if there is any dever.json which has an unsupported version
* @param configs {Config[]}
*/
#checkForSupportedVersion(configs) {
if (!versionChecker.supported(configs)) {
console.warn(chalk.yellow(`One or more of the found projects is not supported due to dever.json version not being supported by the installed version of dever`));
console.warn(chalk.yellow(`Check 'dever list --not-supported' to get a list of the unsupported projects`));
}
}

#getConfigFiles(filePath) {
const file = filePath.trim();

Expand All @@ -40,6 +53,22 @@ module.exports = new class {
return;
}

componentHandler.addComponent(file);
projectsConfig.add(file);
}

/**
* Check if any projects has keywords which violate pre-defined keys ('init', 'list', 'config', 'validate')
* @param configs {Config[]}
*/
#checkForKeywordViolations(configs) {
for (const config of configs) {
if (config.keywords.every(x => !this.#preDefinedKeys.includes(x))) {
continue;
}

console.error(chalk.red(`Could not add the project '${config.name}' due to having keywords which are conflicting with pre-defined keys`));

projectsConfig.remove(config.location);
}
}
}

0 comments on commit 902a21b

Please sign in to comment.