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

Add simple dever json validation #55

Merged
merged 25 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
902a21b
#27 Added support for not adding projects to dever_config.json when k…
czprz Jan 31, 2022
d22135c
#27 Added initial structure for validate command
czprz Feb 1, 2022
7c9b15f
Merge branch 'main' into 27_add_keywords_check
czprz Feb 6, 2022
08c061f
#27 Removed unused file
czprz Feb 6, 2022
0fe3dad
#27 Incremented version
czprz Feb 6, 2022
66beae8
#27 Added helper for config validation
czprz Feb 6, 2022
fe35559
#27 Fixed issue with projects with conflicting keywords would not be …
czprz Feb 6, 2022
8d3864a
#27 Fixed issue with keywords check not being case-sensitive
czprz Feb 6, 2022
a17a573
#27 Moved the message for not supported version of dever.json
czprz Feb 6, 2022
752115a
#27 Added validate commands to README
czprz Feb 6, 2022
b18f1bf
Merge pull request #30 from czprz/27_add_keywords_check
czprz Feb 6, 2022
73763fb
#5 Added confirmation dialog to dever init when dever_config.json com…
czprz Feb 6, 2022
34afe8f
Merge pull request #37 from czprz/5_add_confirmation_to_dever_init
czprz Feb 6, 2022
fc36708
#5 Removed dever_config.json and added handling if config file has no…
czprz Feb 6, 2022
430ceca
#5 Removed dever_config.json and added handling if config file has no…
czprz Feb 6, 2022
4373550
#5 Added dever_config.json to .gitignore
czprz Feb 6, 2022
a0573d8
Merge pull request #38 from czprz/5_add_confirmation_to_dever_init
czprz Feb 6, 2022
665ec73
#4 Moved location config file to user folder to avoid it being cleare…
czprz Feb 7, 2022
747ebfd
Merge pull request #39 from czprz/4_improve_handling_of_dever_config
czprz Feb 7, 2022
71f3fca
Merge branch 'main' into 8.0
czprz Apr 12, 2022
be57752
Improved error messages for 'dever validate'
czprz Apr 12, 2022
b3354ba
Changed log level to error for 'dever validate' message
czprz Apr 12, 2022
dadf7d2
Minor adjustments to comments on functions
czprz Apr 12, 2022
74bb57e
Minor adjustments to comments on functions
czprz Apr 12, 2022
7173b6a
Merge remote-tracking branch 'origin/8.0' into 8.0
czprz Apr 12, 2022
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
.idea/
.idea/
/bin/dever_config.json
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Or you could also check the list of commands below in the [Available Commands](#
| dever list --not-supported | Lists all found not supported projects |
| dever config | Show content of dever_config.json |
| dever config --location, -l | Show location of dever_config.json |
| dever validate | Validate any dever.json which is at the same location as the console |
| dever validate -f, --file | Validate any dever.json using a filepath |
| dever [keyword] install | Installs all available packages for specified project keyword if available or shows help |
| dever [keyword] install -l, --list | List all options under install section in the projects dever.json |
| dever [keyword] install -lgs, --list-groups | List of all installation groups under install section in the projects dever.json |
Expand Down
4 changes: 4 additions & 0 deletions bin/common/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = new class {
predefinedKeys = ['init', 'list', 'config', 'validate'];
notAllowedKeywords = ['init', 'list', 'config', 'validate', '--help', '--version'];
}
45 changes: 40 additions & 5 deletions bin/common/default-yargs-generator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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 configValidator = require('../common/helper/config-validator');

const path = require("path");
const init = require("../init");
const chalk = require("chalk");

Expand Down Expand Up @@ -57,7 +59,31 @@ module.exports = new class {
default:
this.#showConfig();
}

}
})
.command({
command: 'validate',
desc: `Validate dever.json config file before running 'dever init'`,
builder: (yargs) => {
yargs
.option('f', {
alias: 'file',
describe: 'Filepath for dever.json that needs to be validated'
});
},
handler: (argv) => {
switch (true) {
case argv.file != null:
{
this.#validate(argv.file);
break;
}
default:
{
const file = path.join(process.cwd(), 'dever.json');
this.#validate(file);
}
}
}
})
.command({
Expand All @@ -80,7 +106,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 +120,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 Expand Up @@ -127,12 +153,21 @@ module.exports = new class {
*/
#showConfig() {
const config = config_handler.get();

if (config == null) {
console.error(chalk.redBright('Could not find dever configuration'));
return;
}

console.log(config);
}

#validate(file) {
const result = configValidator.validateFile(file);
if (!result.status) {
console.error(chalk.redBright(result.message));
return;
}

console.log('No problems with dever.json');
}
}
80 changes: 80 additions & 0 deletions bin/common/helper/config-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const constants = require('../constants');
const fs = require("fs");

module.exports = new class {
/**
* Validate configuration
* @param config {Config}
* @return {boolean}
*/
validate(config) {
return config.keywords.every(x => !constants.predefinedKeys.includes(x.toLowerCase()));
}

/**
* Validate json
* @param json {string}
*/
validateJson(json) {
// Todo: Missing implementation
}

/**
* Validate json file
* @param file {string}
* @return {ConfigValidation}
*/
validateFile(file) {
try {
const config = this.#getJson(file);
if (this.validate(config)) {
return {status: true};
}

return {status: false, message: `One or more keywords are conflicting with the pre-defined keys. ('${constants.predefinedKeys.join("','")}')` };
} catch (e) {
switch(e.code) {
case "ENOENT":
case "EISDIR":
return {status: false, message: `Could not find '${file}'. Please check if file path is correct.`};
default:
return {status: false, message: "Something went wrong. Most likely due to JSON being wrongly formatted!"};
}
}
}

/**
* Get json from file
* @param file {string}
* @returns {Config}
*/
#getJson(file) {
const content = fs.readFileSync(file);
return this.#convertToJson(content);
}

/**
*
* @param content {null|Buffer}
* @return {Config}
*/
#convertToJson(content) {
try {
return JSON.parse(content);
} catch (e) {
throw e;
}
}
}

class ConfigValidation {
/**
* @return {boolean}
*/
status;

/**
* @return {string}
*/
message;
}
105 changes: 0 additions & 105 deletions bin/configuration/handleComponents.js

This file was deleted.

8 changes: 3 additions & 5 deletions bin/configuration/handleConfigFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const fs = require("fs");
const chalk = require('chalk');

module.exports = new class {
#fileName = 'dever_config.json';
#fileName = '.dever';

#root;
#filePath;

constructor() {
this.#root = path.join(path.dirname(fs.realpathSync(__filename)), '../');
this.#root = require('os').homedir();
this.#filePath = path.join(this.#root, this.#fileName);
}

Expand Down Expand Up @@ -45,9 +45,8 @@ module.exports = new class {
*/
get() {
const config = this.#readJson(this.#filePath);

if (config == null) {
throw 'Could not find configuration';
return null;
}

return config;
Expand Down Expand Up @@ -89,7 +88,6 @@ module.exports = new class {
} catch (e) {
switch (e.code) {
case "ENOENT":
console.error(`Could not find '${filePath}' please run 'dever init' again.`);
return null;
default:
console.error(chalk.redBright(`Could not parse '${filePath}' due to json formatting.`));
Expand Down
Loading