Skip to content

Commit

Permalink
feat: git tag command
Browse files Browse the repository at this point in the history
  • Loading branch information
milosbugarinovic committed Dec 11, 2021
1 parent 97bd8c7 commit ae0b4be
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 57 deletions.
28 changes: 27 additions & 1 deletion dist/dal/shell-dal.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 13 additions & 21 deletions src/controller/yargs-router/cli-router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gitRouter } from 'src/controller/yargs-router/git-router'
import { initRouter } from 'src/controller/yargs-router/init-router'
import { npmRouter } from 'src/controller/yargs-router/npm-router'
import { InitConfig } from 'src/model/command/init-config'
import { terminalWrapperFactory } from 'src/service/terminal-wrapper'
import { config } from 'src/util/config'
import yargs from 'yargs'

Expand Down Expand Up @@ -31,47 +32,38 @@ export class CliRouter {
}

protected _helpVersionAlias(): void {
this._yargs.alias('version', 'v').alias('help', 'h').fail(false)
this._yargs.alias('version', 'v').alias('help', 'h').showHelpOnFail(false, 'Specify --help for available options')
}

protected _demandCommands(): void {
this._yargs.demandCommand(1, 1, 'You need to select one command before moving on').strict()
}

protected _gitCommands(): void {
if (!config().cmd.gitEnabled) return
this._yargs.command({
command: 'git <git-command>',
command: 'git',
describe: 'execute git command',
aliases: ['g'],
builder: (y) =>
y.positional('git-command', {
description: 'GIT Commands',
type: 'string',
choices: ['clone', 'pull', 'fetch', 'status'],
}),
gitRouter.commands(y).demandCommand(1, 1).showHelpOnFail(false, 'Specify --help for available options').version(false),
handler: async (argv: any) => {
if (!config().cmd.gitEnabled) throw new Error('Git commands are not enabled. Check config: CMD_GIT_ENABLED')
const { gitCommand } = argv
await gitRouter.route({ gitCommand })
//return
},
})
}

protected _npmCommands(): void {
if (!config().cmd.npmEnabled) return
this._yargs.command({
command: 'npm <npm-command>',
command: 'npm',
aliases: ['n'],
describe: 'execute npm command',

builder: (y) =>
y.positional('npm-command', {
description: 'NPM Commands',
type: 'string',
choices: ['install', 'i', 'global', 'g'],
}),
npmRouter.commands(y).demandCommand(1, 1).showHelpOnFail(false, 'Specify --help for available options').version(false),
handler: async (argv: any) => {
if (!config().cmd.npmEnabled) throw new Error('Git commands are not enabled. Check config: CMD_NPM_ENABLED')
const { npmCommand } = argv
await npmRouter.route({ npmCommand })
//return
},
})
}
Expand All @@ -82,7 +74,7 @@ export class CliRouter {
aliases: ['i'],
describe: 'Create init file',
handler: async (argv: any) => {
await initRouter.route()
await terminalWrapperFactory({ command: new InitConfig() }).execute()
},
})
}
Expand Down
75 changes: 60 additions & 15 deletions src/controller/yargs-router/git-router.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,80 @@
import { GitCloneProjectCommand } from 'src/model/command/project-command/git-clone-project-command'
import { GitSimpleCommand, GitSimpleProjectCommand } from 'src/model/command/project-command/git-simple-project-command'
import { GitTagProjectCommand } from 'src/model/command/project-command/git-tag-project-command'
import { projectCommandFactory } from 'src/model/command/project-command/project-command'
import { terminalWrapperFactory } from 'src/service/terminal-wrapper'
import yargs from 'yargs'

export const gitRouter = {
route: async (params: { gitCommand: string }): Promise<void> => {
const { gitCommand } = params
switch (gitCommand) {
case 'clone':
commands: (yargs: yargs.Argv): yargs.Argv => {
gitRouter._clone(yargs)
gitRouter._pull(yargs)
gitRouter._fetch(yargs)
gitRouter._status(yargs)
gitRouter._tag(yargs)
return yargs
},
_clone: (yargs: yargs.Argv): void => {
yargs.command({
command: 'clone',
describe: 'execute git clone',
handler: async (argv: any) => {
await terminalWrapperFactory({
command: projectCommandFactory({ command: new GitCloneProjectCommand() }),
}).execute()

break
case 'pull':
},
})
},
_pull: (yargs: yargs.Argv): void => {
yargs.command({
command: 'pull',
describe: 'execute git pull',
handler: async (argv: any) => {
await terminalWrapperFactory({
command: projectCommandFactory({ command: new GitSimpleProjectCommand({ gitSimpleCommand: GitSimpleCommand.PULL }) }),
}).execute()
break
case 'fetch':
},
})
},
_fetch: (yargs: yargs.Argv): void => {
yargs.command({
command: 'fetch',
describe: 'execute git fetch',
handler: async (argv: any) => {
await terminalWrapperFactory({
command: projectCommandFactory({ command: new GitSimpleProjectCommand({ gitSimpleCommand: GitSimpleCommand.FETCH }) }),
}).execute()
break
case 'status':
},
})
},
_status: (yargs: yargs.Argv): void => {
yargs.command({
command: 'status',
describe: 'execute git status',
handler: async (argv: any) => {
await terminalWrapperFactory({
command: projectCommandFactory({ command: new GitSimpleProjectCommand({ gitSimpleCommand: GitSimpleCommand.STATUS }) }),
}).execute()
break
default:
throw new Error(`Unsupported git command [${gitCommand}]`)
}
},
})
},
_tag: (yargs: yargs.Argv): void => {
yargs.command({
command: 'tag',
describe: 'execute git tag',
builder: (y) => {
y.options('name', {
alias: 'n',
describe: 'filter tag by name (* is allowed)',
type: 'string',
})
return y
},
handler: async (argv: any) => {
await terminalWrapperFactory({
command: projectCommandFactory({ command: new GitTagProjectCommand({ filterByName: argv.name }) }),
}).execute()
},
})
},
}
8 changes: 0 additions & 8 deletions src/controller/yargs-router/init-router.ts

This file was deleted.

35 changes: 23 additions & 12 deletions src/controller/yargs-router/npm-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,36 @@ import { npmGlobalProjectCommandFactory } from 'src/model/command/project-comman
import { NpmInstallProjectCommand } from 'src/model/command/project-command/npm-install-project-command'
import { projectCommandFactory } from 'src/model/command/project-command/project-command'
import { terminalWrapperFactory } from 'src/service/terminal-wrapper'
import yargs from 'yargs'

export const npmRouter = {
route: async (params: { npmCommand: string }): Promise<void> => {
const { npmCommand } = params
switch (npmCommand) {
case 'install':
case 'i':
commands: (yargs: yargs.Argv): yargs.Argv => {
npmRouter._install(yargs)
npmRouter._global(yargs)
return yargs
},
_install: (yargs: yargs.Argv): void => {
yargs.command({
command: 'install',
describe: 'execute npm i for all projects',
aliases: ['i'],
handler: async (argv: any) => {
await terminalWrapperFactory({
command: projectCommandFactory({
command: new NpmInstallProjectCommand(),
}),
}).execute()
break
case 'global':
case 'g':
},
})
},
_global: (yargs: yargs.Argv): void => {
yargs.command({
command: 'global',
aliases: ['g'],
describe: 'gather npm packages to global package.json (project root level)',
handler: async (argv: any) => {
await terminalWrapperFactory({ command: npmGlobalProjectCommandFactory() }).execute()
break
default:
throw new Error(`Unsupported npm command [${npmCommand}]`)
}
},
})
},
}
26 changes: 26 additions & 0 deletions src/dal/shell-dal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ export type ExecResult = {
}

export const shellDal = {
// exec: (cmd: string): Promise<ExecResult> =>
// new Promise((resolve, reject) => {
// logger().debug(shellDal.pwd())
// const child = shell.exec(cmd, { async: true, silent: true })
// const result = { stdout: '', stderr: '', errorOccurred: false }
//
// if (child.stdout) {
// child.stdout.on('data', (data) => {
// result.stdout = (result.stdout ?? '') + data.toString()
// })
// child.stdout.on('end', () => {
// resolve(result)
// })
// } else if (child.stderr) {
// child.stderr.on('end', () => {
// resolve(result)
// })
// }
//
// if (child.stderr) {
// child.stderr.on('data', (data) => {
// result.stderr = (result.stderr ?? '') + data.toString()
// result.errorOccurred = (child.exitCode ?? 0) !== 0
// })
// }
// }),
exec: (cmd: string): Promise<ExecResult> =>
new Promise((resolve) => {
logger().debug(shellDal.pwd())
Expand Down
25 changes: 25 additions & 0 deletions src/model/command/project-command/git-tag-project-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ExecuteResult, ProjectExecutable } from 'src/model/command/interfaces'
import { shellService } from 'src/service/shell-service'
import { config } from 'src/util/config'

export class GitTagProjectCommand implements ProjectExecutable {
protected readonly _rootDir: string
protected readonly _filterByName?: string

constructor(params: { filterByName?: string; rootDir?: string }) {
const { filterByName, rootDir = config().rootDir } = params
this._rootDir = rootDir
this._filterByName = filterByName
}

public async execute(project: string): Promise<ExecuteResult[]> {
try {
const filterLine = this._filterByName ? ` -l ${this._filterByName}` : ''
const cmd = `git -C ${this._rootDir}/${project} tag -n9${filterLine}`
const result = await shellService.exec(cmd)
return [{ name: project, errorMessage: result.stderr, stringResult: result.stdout }]
} catch (err: any) {
return [{ errorMessage: err.message }]
}
}
}

0 comments on commit ae0b4be

Please sign in to comment.