Skip to content

Commit faf9a62

Browse files
committed
refactor: adopt action handler command approach
1 parent 897687f commit faf9a62

12 files changed

+88
-171
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11

2-
const { addDepsAtWorkspace, logSuccess, logError, logInfo } = require('./scripts.module')
3-
const { program } = require('commander');
2+
const { addDepsAtWorkspace, logSuccess, logError } = require('./scripts.module')
43

5-
6-
program
7-
.command('add')
8-
.description('Adds dependencies at given workspace and updates package.json')
9-
.requiredOption('-n, --workspace-name <name>', 'Name of the workspace where to add dependencies')
10-
.option('-d, --workspace-directory <directory>', 'Directory where to look for the workspace. Defaults to "microservices"', 'microservices')
11-
.argument('<packages...>', 'Space-separated list of packages to add')
12-
.action(async (packages, options) => {
13-
try {
14-
const message = await addDepsAtWorkspace({
15-
workspace_name: options.workspaceName,
16-
workspace_directory: options.workspaceDirectory,
17-
packages: packages.join(' ')
18-
});
19-
logSuccess({ message })
20-
} catch (error) {
21-
const CODE = error.split(':')[1]
22-
if(![' EEXIST',' ENOENT',' EINVAL',' ENOTDIR'].includes(CODE))logError({ error })
23-
}
24-
});
25-
program.parse(process.argv);
26-
module.exports = program
4+
module.exports = async ({ packages, options }) => {
5+
try {
6+
const message = await addDepsAtWorkspace({
7+
workspace_name: options.workspaceName,
8+
workspace_directory: options.workspaceDirectory,
9+
packages: packages.join(' ')
10+
});
11+
logSuccess({ message })
12+
} catch (error) {
13+
const CODE = error.split(':')[1]
14+
if (![' EEXIST', ' ENOENT', ' EINVAL', ' ENOTDIR'].includes(CODE)) logError({ error })
15+
}
16+
}
+16-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11

22
const { exit } = require('node:process')
33
const { changeDirectory, logError, logSuccess } = require('./scripts.module')
4-
const { program } = require('commander');
54

5+
module.exports = ({ directory_path }) => {
6+
try {
7+
changeDirectory({ directory_path });
8+
logSuccess({ message: `Directory found:- ${directory_path}` })
9+
} catch (error) {
10+
switch (error.code) {
11+
case ('ENOENT'):
12+
logError({ error: `Direcotry not found:- ${directory_path}` });
13+
exit(1)
14+
case ('ENOTDIR'):
15+
logError({ error: `Path not directory:- ${directory_path}` });
16+
exit(1)
17+
default:
18+
console.log(error)
619

7-
program
8-
.description('The function cds into given directory_path')
9-
.argument('<directory_path>', 'The path to the file')
10-
.action((directory_path) => {
11-
try {
12-
changeDirectory({ directory_path });
13-
logSuccess({ message: `Directory found:- ${directory_path}` })
14-
} catch (error) {
15-
switch (error.code) {
16-
case ('ENOENT'):
17-
logError({ error: `Direcotry not found:- ${directory_path}` });
18-
exit(1)
19-
case ('ENOTDIR'):
20-
logError({ error: `Path not directory:- ${directory_path}` });
21-
exit(1)
22-
default:
23-
console.log(error)
24-
25-
}
2620
}
27-
});
28-
program.parse(process.argv);
29-
module.exports = program
21+
}
22+
}
+11-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11

2-
const { checkDocker, getPlatform, logInfo } = require('./scripts.module')
3-
const { program } = require('commander')
2+
const { checkDocker, getPlatform, logInfo } = require('./scripts.module');
43

4+
module.exports = async () => {
5+
const isDockerRunning = await checkDocker();
6+
const platform = getPlatform();
7+
const platformEmoji = platform === 'MacOS' ? '🍏' : platform === 'Linux' ? '🐧' : '🪟';
8+
const dockerStatusMessage = isDockerRunning ? 'running...' : 'not running. Attempting to start docker...';
9+
const dockerStatusIcon = isDockerRunning ? '✓' : '⚠️';
510

6-
program
7-
.description('Checks if docker is running')
8-
.action(async () => {
9-
const _ = await checkDocker()
10-
logInfo({ message: `Platform : ${`${getPlatform() === 'MacOS' ? '🍏' : 'Linux' ? '🐧' : '🪟'} ${getPlatform()}`} : ${_ ? '✓' : '⚠️'} docker is ${_ ? 'running...' : 'not running. Attempting to start docker...'}` })
11-
})
12-
program.parse(process.argv);
13-
module.exports = program
11+
logInfo({
12+
message: `Platform: ${platformEmoji} ${platform} : ${dockerStatusIcon} Docker is ${dockerStatusMessage}`
13+
});
14+
};
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11

22
const { generateDirectoryPath, logInfo } = require('./scripts.module')
3-
const { program } = require('commander');
43

5-
6-
program
7-
.command('generate-path')
8-
.description('Dynamically generate directory path given workspace_name')
9-
.argument('<workspace-name>', 'Name of the workspace to cd into')
10-
.option('-d, --workspace-directory <directory>', 'Directory where to look for the workspace. Defaults to "microservices"', 'microservices')
11-
.action((workspace_name, options) => {
12-
const path = generateDirectoryPath({
13-
workspace_name,
14-
workspace_directory: options.workspaceDirectory
15-
});
16-
logInfo({ message: path })
17-
});
18-
program.parse(process.argv);
19-
module.exports = program
4+
module.exports = ({ workspace_name, options }) => {
5+
const path = generateDirectoryPath({
6+
workspace_name,
7+
workspace_directory: options.workspaceDirectory
8+
});
9+
logInfo({ message: path })
10+
}
+3-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11

22
const { getPlatform, logInfo } = require('./scripts.module')
3-
const { program } = require('commander');
43

54

6-
program
7-
.description('Gets the platorm Os the app is running on')
8-
.action(() => {
9-
logInfo({ message: `Platform: ${getPlatform() === 'MacOS' ? '🍏' : 'Linux' ? '🐧' : '🪟'} ${getPlatform()}` });
10-
});
11-
program.parse(process.argv);
12-
module.exports = program
5+
module.exports = () => {
6+
logInfo({ message: `Platform: ${getPlatform() === 'MacOS' ? '🍏' : 'Linux' ? '🐧' : '🪟'} ${getPlatform()}` });
7+
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11

22
const { installDepsAtWorkspace, logSuccess, logError } = require('./scripts.module')
3-
const { program } = require('commander');
43

5-
6-
program
7-
.command('add')
8-
.description('Adds dependencies at given workspace and updates package.json')
9-
.requiredOption('-n, --workspace-name <name>', 'Name of the workspace where to add dependencies')
10-
.option('-d, --workspace-directory <directory>', 'Directory where to look for the workspace. Defaults to "microservices"', 'microservices')
11-
.argument('<packages...>', 'Space-separated list of packages to add')
12-
.action(async (packages, options) => {
13-
try {
14-
const message = await installDepsAtWorkspace({
15-
workspace_name: options.workspaceName,
16-
workspace_directory: options.workspaceDirectory,
17-
packages: packages.join(' ')
18-
});
19-
logSuccess({ message })
20-
} catch (error) {
21-
logError({ error })
22-
}
23-
});
24-
program.parse(process.argv);
25-
module.exports = program
4+
module.exports = async ({ packages, options }) => {
5+
try {
6+
const message = await installDepsAtWorkspace({
7+
workspace_name: options.workspaceName,
8+
workspace_directory: options.workspaceDirectory,
9+
packages: packages.join(' ')
10+
});
11+
logSuccess({ message })
12+
} catch (error) {
13+
logError({ error })
14+
}
15+
}

.suite-cli/cli/scripts/isMatch.cmd.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11

22
const { isMatch, logSuccess } = require('./scripts.module')
3-
const { program } = require('commander');
43

5-
6-
program
7-
.description('Compares if 2 values match')
8-
.argument('<a>', 'first value')
9-
.argument('<b>', 'second value')
10-
.action((a, b) => {
11-
isMatch({ a, b }) ? logSuccess({ message: true }) : logError({ error: false })
12-
isMatch({ a, b });
13-
});
14-
program.parse(process.argv);
15-
module.exports = program
4+
module.exports = ({ a, b }) => {
5+
isMatch({ a, b }) ? logSuccess({ message: true }) : logError({ error: false })
6+
isMatch({ a, b });
7+
}
+3-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11

22
const { logError } = require('./scripts.module')
3-
const { program } = require('commander');
43

5-
6-
program
7-
.description('Prints Error message to screen')
8-
.argument('<error>', 'Error to display to console')
9-
.action((error) => {
10-
logError({ error });
11-
});
12-
program.parse(process.argv);
13-
module.exports = program
4+
module.exports = ({ error }) => {
5+
logError({ error });
6+
}
+3-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11

22
const { logInfo } = require('./scripts.module')
3-
const { program } = require('commander');
43

5-
6-
program
7-
.description('Prints informational message to screen')
8-
.argument('<message>', 'Info to display to console')
9-
.action((message) => {
10-
logInfo({ message });
11-
});
12-
program.parse(process.argv);
13-
module.exports = program
4+
module.exports = ({ message }) => {
5+
logInfo({ message });
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11

22
const { logSuccess } = require('./scripts.module')
3-
const { program } = require('commander');
43

5-
6-
program
7-
.description('Prints success message to screen')
8-
.argument('<message>', 'Message to display to console')
9-
.action((message) => {
10-
logSuccess({ message });
11-
});
12-
program.parse(process.argv);
13-
module.exports = program
4+
module.exports = ({ message }) => {
5+
logSuccess({ message });
6+
}
+3-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11

22
const { pathExists, logInfo, logError } = require('./scripts.module')
3-
const { program } = require('commander');
43
const { statSync } = require('fs')
54

6-
7-
program
8-
.description('Checks if the file exists at the given path')
9-
.argument('<file_path>', 'The path to the file to check')
10-
.action((file_path) => {
11-
pathExists({ file_path }) ? logInfo({ message: `${statSync(file_path).isFile() ? 'File' : 'Directory'} found:- ${file_path}` }) : logError({ error: `Path not found:- ${file_path}` })
12-
});
13-
program.parse(process.argv);
14-
module.exports = program
5+
module.exports = ({ file_path }) => {
6+
pathExists({ file_path }) ? logInfo({ message: `${statSync(file_path).isFile() ? 'File' : 'Directory'} found:- ${file_path}` }) : logError({ error: `Path not found:- ${file_path}` })
7+
}
+9-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11

22
const { startDocker, logError, logInfo, logSuccess } = require('./scripts.module')
3-
const { program } = require('commander')
43

5-
6-
7-
program
8-
.description('Checks if docker is running')
9-
.action(async () => {
10-
try {
11-
const message = await startDocker();
12-
if (message.split(' ').includes('⏳')) logSuccess({ message })
13-
else logInfo({ message })
14-
} catch (error) {
15-
logError({ error })
16-
}
17-
});
18-
program.parse(process.argv);
19-
module.exports = program
4+
module.exports = async () => {
5+
try {
6+
const message = await startDocker();
7+
if (message.split(' ').includes('⏳')) logSuccess({ message })
8+
else logInfo({ message })
9+
} catch (error) {
10+
logError({ error })
11+
}
12+
}

0 commit comments

Comments
 (0)