Skip to content

Commit 7e1ac4a

Browse files
committed
refactoring
1 parent 0e9f882 commit 7e1ac4a

File tree

7 files changed

+43
-29
lines changed

7 files changed

+43
-29
lines changed

command/BaseCommand.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const log = require('../logger');
22

3-
module.exports = class Command {
3+
class BaseCommand {
44
constructor(name, options) {
55
this.name = name;
66
this.options = options;
77
this.dependencies = [];
88
}
99

1010
addDependency(dependency) {
11-
if (dependency instanceof Command) {
11+
if (dependency instanceof BaseCommand) {
1212
this.dependencies.push(dependency);
1313
log.info(`${this.name} -> ${dependency.name}`)
1414
}
@@ -41,4 +41,6 @@ module.exports = class Command {
4141
const message = 'Method "cleanUp()" not defined. Please define "cleanUp()" on the child class';
4242
return Promise.reject(new Error(message));
4343
}
44-
};
44+
}
45+
46+
module.exports = BaseCommand;

command/NetworkCommand.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const BaseCommand = require('./BaseCommand');
55
/**
66
* simulated network creation
77
* */
8-
module.exports = class NetworkCommand extends BaseCommand {
8+
class NetworkCommand extends BaseCommand {
99
constructor(name, options) {
1010
super(name, options)
1111
}
@@ -18,4 +18,6 @@ module.exports = class NetworkCommand extends BaseCommand {
1818
async cleanUp() {
1919
log.info(`network removed: '${this.name}'`);
2020
}
21-
};
21+
}
22+
23+
module.exports = NetworkCommand;

command/ServiceCommand.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const log = require('../logger');
2-
const timer = require('../helpers/timer');
32
const BaseCommand = require('./BaseCommand');
43
const docker = require('../docker/client');
54

65

7-
module.exports = class ServiceCommand extends BaseCommand {
6+
class ServiceCommand extends BaseCommand {
87
constructor(name, options) {
98
super(name, options)
109
}
@@ -44,4 +43,6 @@ module.exports = class ServiceCommand extends BaseCommand {
4443
log.info(`Service removed: '${this.name}'`)
4544
}
4645
}
47-
};
46+
}
47+
48+
module.exports = ServiceCommand;

command/VolumeCommand.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const BaseCommand = require('./BaseCommand');
55
/**
66
* simulated volume creation
77
* */
8-
module.exports = class VolumeCommand extends BaseCommand {
8+
class VolumeCommand extends BaseCommand {
99
constructor(name, options) {
1010
super(name, options)
1111
}
@@ -18,4 +18,6 @@ module.exports = class VolumeCommand extends BaseCommand {
1818
async cleanUp() {
1919
log.info(`network removed: '${this.name}'`);
2020
}
21-
};
21+
}
22+
23+
module.exports = VolumeCommand;

controllers/DockerController.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const log = require('../logger');
21
const ValidationError = require('../errors/ValidationError');
32
const DockerService = require('../services/DockerService');
3+
const extractYaml = require('../helpers/extractYaml');
44

5-
module.exports = class DockerController {
5+
class DockerController {
66
static async applyYaml(req, res, next) {
77
try {
88
const file = extractYaml(req.files);
@@ -17,19 +17,8 @@ module.exports = class DockerController {
1717
next(e);
1818
}
1919
}
20-
};
20+
}
21+
22+
module.exports = DockerController;
2123

22-
const mimetype = 'text/yaml';
2324

24-
/**
25-
* extract first yaml from request
26-
* */
27-
function extractYaml(files) {
28-
for (const name in files) {
29-
if (files.hasOwnProperty(name) && files[name].mimetype === mimetype) {
30-
log.info(`file found: ${name}`);
31-
return files[name];
32-
}
33-
}
34-
return null;
35-
}

helpers/extractYaml.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const mimetype = 'text/yaml';
2+
3+
/**
4+
* extract first yaml from request
5+
* */
6+
function extractYaml(files) {
7+
for (const name in files) {
8+
if (files.hasOwnProperty(name) && files[name].mimetype === mimetype) {
9+
log.info(`file found: ${name}`);
10+
return files[name];
11+
}
12+
}
13+
return null;
14+
}
15+
16+
module.exports = extractYaml;

services/DockerService.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const VolumeCommand = require('../command/VolumeCommand');
88
/**
99
* Service for applying "docker-compose" files.
1010
* */
11-
module.exports = class DockerService {
11+
class DockerService {
1212
static applyYaml(fileContent) {
1313
const directives = yaml.parseSync(fileContent);
1414
return this.applyDirectives(directives);
@@ -73,7 +73,7 @@ module.exports = class DockerService {
7373
timer(5000).then(() => runnable.forEach(c => c.cleanUp()));
7474
return {ok: true}
7575
}
76-
};
76+
}
7777

7878
function getCommand(name, directives, commands, CommandType) {
7979
let command = commands[name];
@@ -82,4 +82,6 @@ function getCommand(name, directives, commands, CommandType) {
8282
commands[name] = command;
8383
}
8484
return command;
85-
}
85+
}
86+
87+
module.exports = DockerService;

0 commit comments

Comments
 (0)