-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from czprz/#7_add_support_for_fix_command
#7 Added initial implementation of 'dever fix' to support fixing comm…
- Loading branch information
Showing
13 changed files
with
775 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.