generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.ts
55 lines (46 loc) · 1.77 KB
/
exec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {Args} from '@oclif/core'
import get from 'lodash.get'
import BaseCommand from '../base-command.js'
import execSync from '../exec-sync.js'
import {Repos} from '../repos.js'
import {parseRepoNameFromPath} from '../util.js'
export default class Exec extends BaseCommand {
public static aliases = ['x']
public static args = {
repo: Args.string({
description: 'Name of repository to execute in. Use "." to specify the current working directory.',
required: true,
}),
}
public static description = 'Execute a command or script in a repository.'
public static examples = [
{
command: '<%= config.bin %> <%= command.id %> my-repo -- yarn compile',
description: 'Execute a script in a different repository',
},
{
command: '<%= config.bin %> <%= command.id %> . -- yarn compile',
description: 'Execute a script in the current working directory',
},
{
command:
'<%= config.bin %> <%= command.id %> . -- open https://app.circleci.com/pipelines/github/{repo.fullName}',
description: 'Interpolate values into command execution',
},
]
public static flags = {}
public static strict = false
public async run(): Promise<void> {
const {args, argv} = await this.parse(Exec)
const repoName = args.repo === '.' ? parseRepoNameFromPath() : args.repo
let executable = argv.splice(argv.indexOf(args.repo) + 1).join(' ')
const repo = (await new Repos().init()).getOne(repoName)
const tokensRegex = /{(.*?)}/g
const tokens = executable.match(tokensRegex) ?? []
for (const t of tokens) {
const value = get(repo, t.replaceAll(/{|}/g, '').replace('repo.', '')) as string
executable = executable.replace(t, value)
}
execSync(`(cd ${repo.location} && ${executable})`)
}
}