Skip to content

Commit

Permalink
Merge pull request #17 from czprz/#7_add_support_for_fix_command
Browse files Browse the repository at this point in the history
#7 Added initial implementation of 'dever fix' to support fixing comm…
  • Loading branch information
czprz authored Dec 12, 2021
2 parents 21754be + 49d5235 commit 6e25087
Show file tree
Hide file tree
Showing 13 changed files with 775 additions and 396 deletions.
8 changes: 4 additions & 4 deletions bin/common/helper/delayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class Delayer {

/**
* Message to be included with promise when delay ends
* @var {string | boolean}
* @var {unknown}
*/
#value;

/**
*
* @param ms {number} Milliseconds until delay ends
* @param value {string | boolean} @Optional include with promise when delay is expired
* @param value {unknown} @Optional include with promise when delay is expired
* @returns {Promise<unknown>}
*/
async delay(ms, value) {
Expand All @@ -38,7 +38,7 @@ class Delayer {

/**
* Will stop delay before expiration
* @param value {string | boolean} @Optional Value will be included with promise
* @param value {unknown} @Optional Value will be included with promise
* @return {void}
*/
done(value) {
Expand All @@ -55,7 +55,7 @@ class Delayer {

/**
* Sets message that will be included when delay promise ends
* @param value {string | boolean | null}
* @param value {unknown}
* @return {void}
*/
#setValue(value) {
Expand Down
2 changes: 1 addition & 1 deletion bin/configuration/handleComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function get() {
}

return {
components: config.components.map(x => config_handler.getComponentDeverJsonConfig(x))
components: config.components.map(x => config_handler.getComponentConfig(x))
}
}

Expand Down
295 changes: 64 additions & 231 deletions bin/configuration/handleConfigFile.js
Original file line number Diff line number Diff line change
@@ -1,257 +1,90 @@
module.exports = {
get: get,
getComponentDeverJsonConfig: getComponentDeverJsonConfig,
write: write
}

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

const fileName = 'dever_config.json';

const root = path.join(path.dirname(fs.realpathSync(__filename)), '../');
const filePath = path.join(root, fileName);

function readJson(filePath) {
try {
let rawData = fs.readFileSync(filePath);
return JSON.parse(rawData);
}
catch (e) {
switch (e.code) {
case "ENOENT":
console.error(`Could not find '${filePath}' please run 'dever init' again.`);
return null;
default:
throw e;
}
}
}

/**
*
* @returns {LocalConfig}
*/
function get() {
const config = readJson(filePath);

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

return config;
}

/**
* Save configuration
* @param config {LocalConfig}
*/
function write(config) {
const fs = require('fs');
let data = JSON.stringify(config);

fs.writeFileSync(filePath, data, (err) => {
if (err) {
throw err;
}
module.exports = new class {
#fileName = 'dever_config.json';

console.log(data);
console.log('Configuration updated');
});
}
#root;
#filePath;

/**
* Get component with location
* @param filePath
* @returns {null|Config}
*/
function getComponentDeverJsonConfig(filePath) {
const component = readJson(filePath);
if (component == null) {
return null;
constructor() {
this.#root = path.join(path.dirname(fs.realpathSync(__filename)), '../');
this.#filePath = path.join(this.#root, this.#fileName);
}

component['location'] = path.dirname(filePath);

return component;
}

class LocalConfig {
/**
* @return {Config[]}
* Save configuration
* @param config {LocalConfig}
*/
components;
}

class Config {
/**
* @return {string}
*/
version;
write(config) {
const fs = require('fs');
let data = JSON.stringify(config);

/**
* @return {string}
*/
component;

/**
* @return {string[]}
*/
keywords;

/**
* @return {Dependency[]}
*/
dependencies;

/**
* @return {string}
*/
location;
}

class Dependency {
/**
* Define which handler you're using ('docker-container','powershell-command','powershell-script','docker-compose','mssql')
* @return {string}
*/
type;

/**
* @return {string}
*/
name;

/**
* @return {string}
*/
file;

/**
* @return {string}
*/
command;

/**
* Container object only used when type is 'docker-container'
* @return {Container | null}
*/
container;
fs.writeFileSync(this.#filePath, data, (err) => {
if (err) {
throw err;
}

/**
* Currently only used to select between mssql options
* @return {string | null}
*/
option;

/**
* Custom options that will be passed along to dependency
* @return {CustomOption[] | null}
*/
options;
// Todo: Consider new name for this property

/**
* @return {Wait}
*/
wait;

/**
* Informs whether a dependency needs to be run as elevated user
* @return {boolean}
*/
runAsElevated;
}

class Wait {
/**
* Choose when wait should occur ('before', 'after')
* @return {string}
*/
when;

/**
* Choose for how long it should wait
* @return {number}
*/
time; // in milliseconds
}

class Container {
/**
* Name
* @var {string}
*/
name;

/**
* Port mappings
* @var {string[]}
*/
ports;

/**
* Environment variables
* @var {string[]}
*/
variables;

/**
* Name of docker image
* @var {string}
*/
image;
}

class CustomOption {
/**
* Check if dependency is allowed to execute without option
* @return {boolean}
*/
required;
console.log(data);
console.log('Configuration updated');
});
}

/**
* Option key can be used in console
* @return {string}
* Get dever configuration
* @returns {LocalConfig}
*/
key;
get() {
const config = this.#readJson(this.#filePath);

/**
* Possibility for having an alias for the option
* @Optional
* @return {string}
*/
alias;
if (config == null) {
throw 'Could not find configuration';
}

/**
* Describe what this option will be used for
* @return {string}
*/
describe;
return config;
}

/**
* Replace specific area given in value area e.g. "$0" if e.g. command is "docker run $0 nginx"
* @return {string}
* Get all configuration for all components
* @returns {null|Config[]}
*/
insert;
getAllComponentsConfig() {
const config = this.#readJson(this.#filePath);
return config == null ?
null :
config.components.map(x => this.#readJson(x));
}

/**
* Condition for which this option is allowed to receive a value
* @return {CustomOptionRule}
* Get component configuration
* @param filePath
* @returns {null|Config}
*/
rule;
}
getComponentConfig(filePath) {
const component = this.#readJson(filePath);

class CustomOptionRule {
/**
* Check whether value being passed is as expected using regex match
* @return {string}
*/
match;
return component == null ?
null :
{...component, location: path.dirname(filePath)};
}

/**
* If condition check fails this message will be shown
* @return {string}
*/
message;
* Get and parse file
* @param filePath {string}
* @returns {null|LocalConfig|Config}
*/
#readJson(filePath) {
try {
let rawData = fs.readFileSync(filePath);
return JSON.parse(rawData);
} catch (e) {
switch (e.code) {
case "ENOENT":
console.error(`Could not find '${filePath}' please run 'dever init' again.`);
return null;
default:
throw e;
}
}
}
}
Loading

0 comments on commit 6e25087

Please sign in to comment.