Skip to content

Commit 691f3e0

Browse files
authored
Merge pull request #65 from microservices-suite/repo-engineering/universal-cli
Repo engineering/universal cli
2 parents 96cba86 + 83e1b46 commit 691f3e0

18 files changed

+497
-231
lines changed

.suite-cli/cli/cli.js

Lines changed: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,80 @@
11
#!/usr/bin/env node
22

3-
const figlet = require('figlet')
4-
const { program } = require("commander");
5-
console.log(figlet.textSync('Microservices-Suite'))
6-
const {
7-
addDepsAtWorkspace,
8-
installDepsAtWorkspace,
9-
changeDirectory,
10-
isMatch,
11-
logSuccess,
12-
logError,
13-
logInfoMessage,
14-
pathExists,
15-
generateDirectoryPath,
16-
checkDocker,
17-
getPlatform,
18-
startDocker
19-
} = require("./scripts");
3+
const actionHandlers = require('./scripts')
4+
const { Command } = require('commander');
5+
const program = new Command()
206

21-
const [, , command, ...args] = process.argv;
22-
console.log({ command, args })
23-
switch (command) {
24-
case add:
25-
addDepsAtWorkspace(args)
26-
case add:
27-
installDepsAtWorkspace(args)
28-
case add:
29-
logInfoMessage(args)
30-
case add:
31-
logError(args)
32-
case add:
33-
logSuccess(args)
34-
case add:
35-
changeDirectory(args)
36-
case add:
37-
isMatch(args)
38-
case add:
39-
generateDirectoryPath(args)
40-
case add:
41-
checkDocker(args)
42-
case add:
43-
getPlatform(args)
44-
case add:
45-
pathExists(args)
46-
case add:
47-
startDocker(args)
48-
}
7+
program
8+
.command('err')
9+
.description('Prints Error message to screen')
10+
.argument('<error>', 'Error to display to console')
11+
.action((error) => {
12+
actionHandlers.logErrorMessage({ error });
13+
})
14+
15+
program.command('info')
16+
.description('Prints informational message to screen')
17+
.argument('<message>', 'Info to display to console')
18+
.action((message) => {
19+
actionHandlers.logInfoMessage({ message })
20+
})
21+
22+
program.command('succ')
23+
.description('Prints success message to screen')
24+
.argument('<message>', 'Message to display to console')
25+
.action((message) => {
26+
actionHandlers.logSuccessMessage({ message });
27+
});
28+
29+
program
30+
.command('docker:check')
31+
.alias('do')
32+
.description('Checks if docker is running')
33+
.action(async () => await actionHandlers.checkDocker());
34+
program
35+
.command('chdir')
36+
.description('The function cds into given directory_path')
37+
.argument('<directory_path>', 'The path to the file')
38+
.action((directory_path) => actionHandlers.changeDirectory({ directory_path }));
39+
program
40+
.command('host')
41+
.description('Gets the host platorm Os the app is running on')
42+
.action(() => actionHandlers.getPlatform);
43+
program
44+
.command('add')
45+
.description('Adds dependencies at given workspace and updates package.json')
46+
.requiredOption('-n, --workspace-name <name>', 'Name of the workspace where to add dependencies')
47+
.option('-d, --workspace-directory <directory>', 'Directory where to look for the workspace. Defaults to "microservices"', 'microservices')
48+
.argument('<packages...>', 'Space-separated list of packages to add')
49+
.action(async (packages, options) => await actionHandlers.addDepsAtWorkspace({ packages, options }));
50+
program
51+
.command('install')
52+
.description('Installs dependencies at given workspace. If not specified workspace defaults to "microservices"')
53+
.requiredOption('-n, --workspace-name <name>', 'Name of the workspace where to install dependencies')
54+
.option('-d, --workspace-directory <directory>', 'Name of the workspace where to install dependencies', 'microservices')
55+
.argument('<packages...>', 'Space-separated list of packages to install')
56+
.action(async (packages, options) => await actionHandlers.installDepsAtWorkspace({ packages, options }));
57+
program
58+
.command('equal')
59+
.description('Compares if 2 values match')
60+
.argument('<a>', 'first value')
61+
.argument('<b>', 'second value')
62+
.action((a, b) => actionHandlers.isMatch({ a, b }));
63+
program
64+
.command('path:exist')
65+
.description('Checks if the file exists at the given path')
66+
.argument('<file_path>', 'The path to the file to check')
67+
.action((file_path) => actionHandlers.pathExists({ file_path }));
68+
69+
program
70+
.command('docker:start')
71+
.description('Starts the docker daemon')
72+
.action(async () => await actionHandlers.startDocker());
73+
program
74+
.command('path:gen')
75+
.description('Dynamically generate directory path given workspace_name')
76+
.argument('<workspace-name>', 'Name of the workspace to cd into')
77+
.option('-d, --workspace-directory <directory>', 'Directory where to look for the workspace. Defaults to "microservices"', 'microservices')
78+
.action((workspace_name, options) => actionHandlers.generateDirectoryPath({ workspace_name, options }));
79+
program.parse(process.argv);
80+
module.exports = program
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11

22
const { addDepsAtWorkspace, 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 addDepsAtWorkspace({
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 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+
}
Lines changed: 16 additions & 23 deletions
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+
}
Lines changed: 11 additions & 10 deletions
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+
};
Lines changed: 7 additions & 16 deletions
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+
}
Lines changed: 3 additions & 8 deletions
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+
}

.suite-cli/cli/scripts/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
module.exports.generateDirectoryPath=require('./generateDirectoryPath.cmd'),
2-
module.exports.changeDirectory=require('./changeDirectory.cmd'),
3-
module.exports.pathExists=require('./pathExists.cmd'),
4-
module.exports.logInfoMessage=require('./logInfoMessage.cmd'),
5-
module.exports.logErrorMessage=require('./logErrorMessage.cmd'),
6-
module.exports.logSuccessMessage=require('./logSuccessMessage.cmd'),
7-
module.exports.isMatch=require('./isMatch.cmd'),
8-
module.exports.getPlatform=require('./getPlatform.cmd'),
9-
module.exports.checkDocker=require('./checkDocker.cmd'),
10-
module.exports.startDocker=require('./startDocker.cmd'),
11-
module.exports.installDepsAtWorkspace=require('./installDepsAtWorkspace.cmd'),
12-
module.exports.addDepsAtWorkspace=require('./addDepsAtWorkspace.cmd')
1+
module.exports.installDepsAtWorkspace = require('./installDepsAtWorkspace.cmd');
2+
module.exports.addDepsAtWorkspace = require('./addDepsAtWorkspace.cmd');
3+
module.exports.generateDirectoryPath = require('./generateDirectoryPath.cmd');
4+
module.exports.changeDirectory = require('./changeDirectory.cmd');
5+
module.exports.pathExists = require('./pathExists.cmd');
6+
module.exports.logInfoMessage = require('./logInfoMessage.cmd');
7+
module.exports.logErrorMessage = require('./logErrorMessage.cmd');
8+
module.exports.logSuccessMessage = require('./logSuccessMessage.cmd');
9+
module.exports.isMatch = require('./isMatch.cmd');
10+
module.exports.getPlatform = require('./getPlatform.cmd');
11+
module.exports.checkDocker = require('./checkDocker.cmd');
12+
module.exports.startDocker = require('./startDocker.cmd');
Lines changed: 12 additions & 22 deletions
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

Lines changed: 4 additions & 12 deletions
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+
}
Lines changed: 3 additions & 10 deletions
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+
}

0 commit comments

Comments
 (0)