-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathremove.js
68 lines (56 loc) · 2.25 KB
/
remove.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
const { Args } = require('@oclif/core')
const { putProject, getProject } = require('../../../requests/project')
const {
getProjectIdentifierArg,
splitPathParams,
} = require('../../../support/command/parse-input')
const BaseCommand = require('../../../support/command/base-command')
const { getResponseContent } = require('../../../support/command/handle-response')
const { pipeAsync } = require('../../../utils/general')
class ProjectApiRemoveCommand extends BaseCommand {
async run() {
const { args } = await this.parse(ProjectApiRemoveCommand)
const projectPath = getProjectIdentifierArg(args)
const apiToRemove = args['API']
await this.removeApiFromProject(projectPath, apiToRemove)
}
async getProjectDetails(owner, projectName) {
return this.executeHttp({
execute: () => getProject([owner, projectName]),
onResolve: pipeAsync(getResponseContent, JSON.parse),
options: {}
})
}
async removeApiFromProject(projectPath, apiName) {
const [owner, projectName] = splitPathParams(projectPath)
const project = await this.getProjectDetails(owner, projectName)
if (project.apis && !project.apis.includes(apiName)) {
return this.log(`Api \'${apiName}\' does not exist in project \'${projectPath}\'`)
}
project.apis = project.apis.filter(item => item !== apiName)
const updateRequest = {
pathParams: [owner, projectName],
body: JSON.stringify(project)
}
return this.executeHttp({
execute: () => putProject(updateRequest),
onResolve: this.logCommandSuccess({ apiName, projectPath }),
options: {}
})
}
}
ProjectApiRemoveCommand.description = 'Removes an API from a project in SwaggerHub.'
ProjectApiRemoveCommand.examples = [
'swaggerhub project:api:remove organization/project_name my_api',
]
ProjectApiRemoveCommand.args = {
'OWNER/PROJECT_NAME': Args.string({
required: true,
description: 'The project to remove the API from on Swaggerhub'
}),
'API': Args.string({
required: true,
description: 'The name of the API on Swaggerhub to remove'
})
}
module.exports = ProjectApiRemoveCommand