Skip to content

Commit

Permalink
Merge pull request #40 from czprz/18-add-support-for-starting-multipl…
Browse files Browse the repository at this point in the history
…e-environments-at-once

#18 Moved dependencies to environment keyword in dever.json
  • Loading branch information
czprz authored Mar 20, 2022
2 parents 9702a6a + 133bdf6 commit 53a5753
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 155 deletions.
2 changes: 1 addition & 1 deletion bin/common/project-yargs-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = new class {
* @param yargs
*/
#createEnvironment(keyword, config, yargs) {
if (config.dependencies == null) {
if (config.environment == null) {
return;
}

Expand Down
11 changes: 8 additions & 3 deletions bin/configuration/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class Config {
fix;

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

/**
* @return {Install[]}
Expand All @@ -42,7 +42,7 @@ class Config {
location;
}

class Dependency {
class Execution {
/**
* Define which handler you're using ('docker-container','powershell-command','powershell-script','docker-compose','mssql')
* @return {string}
Expand All @@ -54,6 +54,11 @@ class Dependency {
*/
name;

/**
* @return {boolean}
*/
runtime;

/**
* @return {string}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ module.exports = new class {
/**
* Handle starting and stopping of docker-compose
* @param component {Config} Component configuration
* @param dependency {Dependency} Dependency options
* @param args {EnvArgs} shell arguments
* @param name {string} Name of docker-compose sequence
* @param execution {Execution} Dependency options
* @param runtime {Runtime} shell arguments
*/
handle(component, dependency, args, name) {
handle(component, execution, runtime) {
switch (true) {
case args.start:
this.#start(component, dependency.file, args, name);
case runtime.start:
this.#start(component, execution, runtime);
break;
case args.stop:
this.#stop(component, dependency.file, name);
case runtime.stop:
this.#stop(component, execution);
break;
}
}
Expand All @@ -41,34 +40,33 @@ module.exports = new class {
/**
* Start docker-compose
* @param component {Config} Component configuration
* @param file {string} FilePath to docker-compose
* @param args {EnvArgs}
* @param name {string} Name of docker-compose sequence
* @param execution {Execution} FilePath to docker-compose
* @param runtime {Runtime}
*/
#start(component, file, args, name) {
#start(component, execution, runtime) {
const state = this.#run_state();
switch (state) {
case states.NotRunning: {
if (this.#recreate(component, file, name, args.clean)) {
if (this.#recreate(component, execution.file, execution.name, runtime.clean)) {
return;
}

const filePath = path.join(component.location, file);
const filePath = path.join(component.location, execution.file);
shell.executeSync(`docker-compose --file ${filePath} --project-name dever up -d`);
break;
}
case states.Running: {
if (this.#recreate(component, file, name, args.clean)) {
if (this.#recreate(component, execution.file, execution.name, runtime.clean)) {
return;
}

console.log(`docker-compose: '${name}' already running!`);
console.log(`docker-compose: '${execution.name}' already running!`);
break;
}
case states.NotFound: {
const filePath = path.join(component.location, file);
const filePath = path.join(component.location, execution.file);
shell.executeSync(`docker-compose --file ${filePath} --project-name dever up -d`);
console.log(`docker-compose: '${name}' created successfully`);
console.log(`docker-compose: '${execution.name}' created successfully`);
break;
}
}
Expand Down Expand Up @@ -97,14 +95,13 @@ module.exports = new class {
/**
* Stop docker-compose
* @param component {Config} Component configuration
* @param file {string} FilePath to docker-compose
* @param name {string} Name of docker-compose sequence
* @param execution {Execution} FilePath to docker-compose
*/
#stop(component, file, name) {
const filePath = path.join(component.location, file);
#stop(component, execution) {
const filePath = path.join(component.location, execution.file);
shell.executeSync(`docker-compose --file ${filePath} --project-name dever down`);

console.log(`docker-compose: '${name}' stopped successfully`);
console.log(`docker-compose: '${execution.name}' stopped successfully`);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ const docker = require('../../../common/helper/docker');
module.exports = new class {
/**
* Handle starting and stopping of docker containers
* @param dependency {Dependency}
* @param args {EnvArgs}
* @param dependency {Execution}
* @param runtime {Runtime}
*/
handle(dependency, args) {
handle(dependency, runtime) {
switch(true) {
case args.start:
this.#start(dependency.container, args);
case runtime.start:
this.#start(dependency.container, runtime);
break;
case args.stop:
case runtime.stop:
this.#stop(dependency.container);
break;
}
Expand All @@ -33,13 +33,13 @@ module.exports = new class {
/**
* Start docker container
* @param container {Container}
* @param args {EnvArgsv}
* @param runtime {Runtime}
*/
#start(container, args) {
#start(container, runtime) {
const state = docker.container.getRunState(container.name);
switch (state) {
case docker.states.NotRunning: {
if (this.#recreate(container, args.clean)) {
if (this.#recreate(container, runtime.clean)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,91 @@ const mssql = require('../../../common/helper/mssql');
module.exports = new class {
/**
* Handler for mssql dependencies
* @param dependency {Dependency}
* @param args {EnvArgs}
* @param name {string}
* @param execution {Execution}
* @param runtime {Runtime}
*/
async handle(dependency, args, name) {
if (args.stop) {
async handle(execution, runtime) {
if (runtime.stop) {
return;
}

switch (dependency.option) {
switch (execution.option) {
case "create-database":
await this.#createDatabase(dependency, name);
await this.#createDatabase(execution);
break;
case "create-table":
await this.#createTable(dependency, name);
await this.#createTable(execution);
break;
case "insert-into":
await this.#insertInto(dependency, name);
await this.#insertInto(execution);
break;
default:
console.error(`mssql: option '${dependency.option}' not supported`);
console.error(`mssql: option '${execution.option}' not supported`);
}
}

/**
* Create database
* @param dependency {Dependency}
* @param name {string}
* @param execution {Execution}
* @returns {Promise<void>}
*/
async #createDatabase(dependency, name) {
if (await mssql.databaseExists(dependency.command)) {
console.log(`mssql: '${name}' :: database has already been created`);
async #createDatabase(execution) {
if (await mssql.databaseExists(execution.command)) {
console.log(`mssql: '${execution.name}' :: database has already been created`);
return;
}

try {
await mssql.createDatabase(dependency.command);
console.log(`mssql: '${name}' :: database has been created`);
await mssql.createDatabase(execution.command);
console.log(`mssql: '${execution.name}' :: database has been created`);
} catch (e) {
console.error(`mssql: '${name}' :: database has not been created`);
console.error(`mssql: '${execution.name}' :: database has not been created`);
throw e;
}
}

/**
* Create table
* @param dependency {Dependency}
* @param name {string}
* @param execution {Execution}
*/
async #createTable(dependency, name) {
const names = this.#getDatabaseAndTableNames(dependency.command);
async #createTable(execution) {
const names = this.#getDatabaseAndTableNames(execution.command);
if (names == null) {
console.log(`mssql: '${name}' could not find database or table names`);
console.log(`mssql: '${execution.name}' could not find database or table names`);
return;
}

if (!await mssql.databaseExists(names.databaseName)) {
console.log(`mssql: '${name}' :: could not create table due to the database '${names.databaseName}' not existing`);
console.log(`mssql: '${execution.name}' :: could not create table due to the database '${names.databaseName}' not existing`);
return;
}

if (await mssql.tableExists(names.databaseName, names.tableName)) {
console.log(`mssql: '${name}' :: table has already been created'`);
console.log(`mssql: '${execution.name}' :: table has already been created'`);
return;
}

try {
await mssql.createTable(dependency.command);
console.log(`mssql: '${name}' :: table has been created`);
await mssql.createTable(execution.command);
console.log(`mssql: '${execution.name}' :: table has been created`);
} catch (e) {
console.error(`mssql: '${name}' :: table has not been created`);
console.error(`mssql: '${execution.name}' :: table has not been created`);
throw e;
}
}

/**
* Checks and runs 'insert into' once or multiple times depending on whether it's a string or array
* @param dependency {Dependency}
* @param name {string}
* @param execution {Execution}
* @returns {Promise<void>}
*/
async #insertInto(dependency, name) {
if (Array.isArray(dependency.command)) {
for (const command of dependency.command) {
await this.#insertIntoRunner(command, name);
async #insertInto(execution) {
if (Array.isArray(execution.command)) {
for (const command of execution.command) {
await this.#insertIntoRunner(command, execution.name);
}
} else {
await this.#insertIntoRunner(dependency.command, name);
await this.#insertIntoRunner(execution.command, execution.name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ const customOptions = require('../../../common/helper/custom_options');

/**
*
* @param dependency {Dependency}
* @param args {EnvArgs}
* @param name {string}
* @param execution {Execution}
* @param runtime {Runtime}
*/
async function handle(dependency, args, name) {
async function handle(execution, runtime) {
switch(true) {
case args.start: {
case runtime.start: {
try {
const command = customOptions.addOptionsToCommand(dependency.command, dependency.options, args);
await powershell.executeSync(command, dependency.runAsElevated);
const command = customOptions.addOptionsToCommand(execution.command, execution.options, runtime.args);
await powershell.executeSync(command, execution.runAsElevated);

console.log(`powershell-command: '${name}' completed successfully`);
console.log(`powershell-command: '${execution.name}' completed successfully`);
} catch (e) {
console.error(e);
}

break;
}
case args.stop:
case runtime.stop:
// Todo: Any reason for having this? / How can this be implemented?
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ const path = require("path");
/**
*
* @param component {Config}
* @param dependency {Dependency}
* @param args {EnvArgs}
* @param name {string}
* @param execution {Execution}
* @param runtime {Runtime}
* @return {Promise<void>}
*/
async function handle(component, dependency, args, name) {
async function handle(component, execution, runtime) {
switch(true) {
case args.start: {
const file = path.join(component.location, dependency.file);
const fileWithParameters = customOptions.addOptionsToFile(file, dependency.options, args);
await powershell.executeFileSync(fileWithParameters, dependency.runAsElevated);
case runtime.start: {
const file = path.join(component.location, execution.file);
const fileWithParameters = customOptions.addOptionsToFile(file, execution.options, runtime.args);
await powershell.executeFileSync(fileWithParameters, execution.runAsElevated);

console.log(`powershell-script: '${name}' completed successfully`);
console.log(`powershell-script: '${execution.name}' completed successfully`);

break;
}
case args.stop:
case runtime.stop:
// Todo: Any reason for having this? / How can this be implemented?
break;
}
Expand Down
Loading

0 comments on commit 53a5753

Please sign in to comment.