swaggerhub-cli 0.9.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,7 +16,7 @@ The SwaggerHub CLI enables teams to build automation and workflows around Swagge
16
16
  * [Contributing](#contributing)
17
17
  <!-- tocstop -->
18
18
  # Requirements
19
- Node.js 18.x or later.
19
+ Node.js 20.x or later.
20
20
 
21
21
  # Installation
22
22
  ```sh-session
@@ -146,6 +146,8 @@ USAGE
146
146
  * [`swaggerhub project:get OWNER/PROJECT_NAME`](#swaggerhub-projectget)
147
147
  * [`swaggerhub project:list [OWNER]`](#swaggerhub-projectlist)
148
148
  * [`swaggerhub project:member:list OWNER/PROJECT_NAME`](#swaggerhub-projectmemberlist)
149
+ * [`swaggerhub spectral:upload OWNER/RULESET_NAME directory`](#swaggerhub-spectralupload)
150
+ * [`swaggerhub spectral:download OWNER/RULESET_NAME directory`](#swaggerhub-spectraldownload)
149
151
 
150
152
  ## `swaggerhub api:create`
151
153
 
@@ -1372,6 +1374,55 @@ EXAMPLES
1372
1374
  ```
1373
1375
 
1374
1376
  _See code: [src/commands/project/member/list.js](https://github.com/SmartBear/swaggerhub-cli/blob/v0.9.1/src/commands/project/member/list.js)_
1377
+
1378
+ ## `swaggerhub spectral:upload`
1379
+
1380
+ Create or update organization's Spectral ruleset
1381
+
1382
+ ```
1383
+ USAGE
1384
+ $ swaggerhub spectral:upload OWNER/RULESET_NAME directory [-h]
1385
+
1386
+ ARGUMENTS
1387
+ OWNER/RULESET_NAME The Spectral ruleset details for SwaggerHub organization
1388
+ directory Relative path to directory with ruleset files
1389
+
1390
+ FLAGS
1391
+ -h, --help Show CLI help.
1392
+
1393
+ DESCRIPTION
1394
+ Create or update organization's Spectral ruleset
1395
+
1396
+ EXAMPLES
1397
+ $ swaggerhub spectral:upload my_organization/my_api_ruleset rules
1398
+ ```
1399
+
1400
+ _See code: [src/commands/spectral/upload.js](https://github.com/SmartBear/swaggerhub-cli/blob/v0.9.1/src/commands/spectral/upload.js)_
1401
+
1402
+
1403
+ ## `swaggerhub spectral:download`
1404
+
1405
+ Fetch organization's Spectral ruleset
1406
+
1407
+ ```
1408
+ USAGE
1409
+ $ swaggerhub spectral:download OWNER/RULESET_NAME directory [-h]
1410
+
1411
+ ARGUMENTS
1412
+ OWNER/RULESET_NAME The Spectral ruleset details for SwaggerHub organization
1413
+ directory Relative path to directory the ruleset files should be saved to
1414
+
1415
+ FLAGS
1416
+ -h, --help Show CLI help.
1417
+
1418
+ DESCRIPTION
1419
+ Fetch organization's Spectral ruleset
1420
+
1421
+ EXAMPLES
1422
+ $ swaggerhub spectral:download my_organization/my_api_ruleset rules
1423
+ ```
1424
+
1425
+ _See code: [src/commands/spectral/download.js](https://github.com/SmartBear/swaggerhub-cli/blob/v0.9.1/src/commands/spectral/download.js)_
1375
1426
  <!-- commandsstop -->
1376
1427
 
1377
1428
  # Plugins
@@ -1219,6 +1219,92 @@
1219
1219
  "list.js"
1220
1220
  ]
1221
1221
  },
1222
+ "spectral:download": {
1223
+ "aliases": [],
1224
+ "args": {
1225
+ "OWNER/RULESET_NAME": {
1226
+ "description": "Organization's Spectral ruleset to create or update on SwaggerHub",
1227
+ "name": "OWNER/RULESET_NAME",
1228
+ "required": true
1229
+ },
1230
+ "directory": {
1231
+ "description": "Relative path to directory the ruleset files should be saved to",
1232
+ "name": "directory",
1233
+ "required": true
1234
+ }
1235
+ },
1236
+ "description": "Fetch organization's Spectral ruleset",
1237
+ "examples": [
1238
+ "swaggerhub spectral:download my_organization/my_api_ruleset rules/"
1239
+ ],
1240
+ "flags": {
1241
+ "help": {
1242
+ "char": "h",
1243
+ "description": "Show CLI help.",
1244
+ "name": "help",
1245
+ "allowNo": false,
1246
+ "type": "boolean"
1247
+ }
1248
+ },
1249
+ "hasDynamicHelp": false,
1250
+ "hiddenAliases": [],
1251
+ "id": "spectral:download",
1252
+ "pluginAlias": "swaggerhub-cli",
1253
+ "pluginName": "swaggerhub-cli",
1254
+ "pluginType": "core",
1255
+ "strict": true,
1256
+ "enableJsonFlag": false,
1257
+ "isESM": false,
1258
+ "relativePath": [
1259
+ "src",
1260
+ "commands",
1261
+ "spectral",
1262
+ "download.js"
1263
+ ]
1264
+ },
1265
+ "spectral:upload": {
1266
+ "aliases": [],
1267
+ "args": {
1268
+ "OWNER/RULESET_NAME": {
1269
+ "description": "Organization's Spectral ruleset to create or update on SwaggerHub",
1270
+ "name": "OWNER/RULESET_NAME",
1271
+ "required": true
1272
+ },
1273
+ "directory": {
1274
+ "description": "Relative path to directory with ruleset files",
1275
+ "name": "directory",
1276
+ "required": true
1277
+ }
1278
+ },
1279
+ "description": "Create or update organization's Spectral ruleset",
1280
+ "examples": [
1281
+ "swaggerhub spectral:upload my_organization/my_api_ruleset rules/"
1282
+ ],
1283
+ "flags": {
1284
+ "help": {
1285
+ "char": "h",
1286
+ "description": "Show CLI help.",
1287
+ "name": "help",
1288
+ "allowNo": false,
1289
+ "type": "boolean"
1290
+ }
1291
+ },
1292
+ "hasDynamicHelp": false,
1293
+ "hiddenAliases": [],
1294
+ "id": "spectral:upload",
1295
+ "pluginAlias": "swaggerhub-cli",
1296
+ "pluginName": "swaggerhub-cli",
1297
+ "pluginType": "core",
1298
+ "strict": true,
1299
+ "enableJsonFlag": false,
1300
+ "isESM": false,
1301
+ "relativePath": [
1302
+ "src",
1303
+ "commands",
1304
+ "spectral",
1305
+ "upload.js"
1306
+ ]
1307
+ },
1222
1308
  "api:validate:download-rules": {
1223
1309
  "aliases": [],
1224
1310
  "args": {
@@ -1612,5 +1698,5 @@
1612
1698
  ]
1613
1699
  }
1614
1700
  },
1615
- "version": "0.9.1"
1701
+ "version": "0.10.0"
1616
1702
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "swaggerhub-cli",
3
3
  "description": "SwaggerHub CLI",
4
- "version": "0.9.1",
4
+ "version": "0.10.0",
5
5
  "author": "SmartBear",
6
6
  "bin": {
7
7
  "swaggerhub": "./bin/run"
@@ -11,18 +11,19 @@
11
11
  "@oclif/core": "^3.26.3",
12
12
  "@oclif/plugin-help": "^6.0.21",
13
13
  "@oclif/plugin-plugins": "^5.0.21",
14
+ "archiver": "^7.0.1",
14
15
  "deep-extend": "^0.6.0",
15
16
  "fs-extra": "^11.1.1",
16
17
  "inquirer": "^8.2.5",
17
18
  "js-yaml": "^4.1.0",
18
19
  "json-templates": "^5.0.0",
19
20
  "node-fetch": "^2.6.12",
20
- "semver": "^7.6.0"
21
+ "semver": "^7.6.0",
22
+ "unzipper": "^0.12.3"
21
23
  },
22
24
  "devDependencies": {
23
25
  "@oclif/test": "^3.2.10",
24
26
  "chai": "^4.3.10",
25
- "sinon": "^18.0.0",
26
27
  "eslint": "^9.0.0",
27
28
  "eslint-plugin-chai-friendly": "^1.0.0",
28
29
  "eslint-plugin-immutable": "^1.0.0",
@@ -30,14 +31,15 @@
30
31
  "eslint-plugin-node": "^11.1.0",
31
32
  "globby": "^14.0.0",
32
33
  "husky": "^9.0.1",
33
- "mocha": "^10.2.0",
34
- "nock": "^13.3.2",
34
+ "mocha": "^11.1.0",
35
+ "nock": "^14.0.1",
35
36
  "nyc": "^17.0.0",
36
37
  "rewire": "^7.0.0",
38
+ "sinon": "^19.0.2",
37
39
  "tmp": "^0.2.1"
38
40
  },
39
41
  "engines": {
40
- "node": ">=18.0.0"
42
+ "node": ">=20.0.0"
41
43
  },
42
44
  "files": [
43
45
  "/bin",
@@ -76,6 +78,9 @@
76
78
  },
77
79
  "projects": {
78
80
  "description": "this topic supports the management of Projects"
81
+ },
82
+ "spectral": {
83
+ "description": "this topic supports the management of Spectral rulesets"
79
84
  }
80
85
  },
81
86
  "helpClass": "./src/help"
@@ -0,0 +1,69 @@
1
+ const { Args , Errors } = require('@oclif/core')
2
+ const { getSpectralRuleset } = require('../../requests/spectral')
3
+ const { getSpectralIdentifierArg, splitPathParams } = require('../../support/command/parse-input')
4
+ const { errorMsg } = require('../../template-strings')
5
+ const BaseCommand = require('../../support/command/base-command')
6
+ const unzipper = require('unzipper');
7
+ const fs = require('fs');
8
+
9
+ class DownloadSpectralRulesetCommand extends BaseCommand {
10
+ async run() {
11
+ const { args } = await this.parse(DownloadSpectralRulesetCommand)
12
+ const rulesetPath = getSpectralIdentifierArg(args)
13
+
14
+ await this.getSpectralRuleset(rulesetPath, args['directory'])
15
+ }
16
+
17
+ getSpectralRuleset(pathParams, outputDir) {
18
+ return this.executeHttp({
19
+ execute: () => getSpectralRuleset(pathParams),
20
+ onResolve: response => this.extractZipResponse(response, outputDir),
21
+ options: {}
22
+ })
23
+ }
24
+
25
+ async extractZipResponse(response, outputDir) {
26
+ const buffer = Buffer.from(response.content, 'binary');
27
+ await this.createDirectory(outputDir)
28
+ const directory = await unzipper.Open.buffer(buffer);
29
+ await directory.extract({ path: outputDir });
30
+ }
31
+
32
+ async createDirectory(outputDir) {
33
+ try {
34
+ await fs.promises.mkdir(outputDir)
35
+ } catch (err) {
36
+ if (err.code === 'EEXIST') {
37
+ throw new Errors.CLIError(errorMsg.directoryExists({ directory: outputDir }))
38
+ }
39
+ if (err.message) {
40
+ throw new Errors.CLIError(err.message)
41
+ }
42
+
43
+ throw new Errors.CLIError(errorMsg.unknown())
44
+ }
45
+ }
46
+ }
47
+
48
+ DownloadSpectralRulesetCommand.description = `Fetch organization's Spectral ruleset`
49
+
50
+ DownloadSpectralRulesetCommand.examples = [
51
+ 'swaggerhub spectral:download my_organization/my_api_ruleset rules/',
52
+ ]
53
+
54
+ DownloadSpectralRulesetCommand.args = {
55
+ 'OWNER/RULESET_NAME': Args.string({
56
+ required: true,
57
+ description: 'Organization\'s Spectral ruleset to create or update on SwaggerHub'
58
+ }),
59
+ 'directory': Args.string({
60
+ required: true,
61
+ description: 'Relative path to directory the ruleset files should be saved to'
62
+ })
63
+ }
64
+
65
+ DownloadSpectralRulesetCommand.flags = {
66
+ ...BaseCommand.flags
67
+ }
68
+
69
+ module.exports = DownloadSpectralRulesetCommand
@@ -0,0 +1,59 @@
1
+ const { Args } = require('@oclif/core')
2
+ const {saveSpectralRuleset} = require('../../requests/spectral')
3
+ const { getSpectralIdentifierArg, splitPathParams } = require('../../support/command/parse-input')
4
+ const BaseCommand = require('../../support/command/base-command')
5
+ const { PassThrough } = require('stream')
6
+ const { pipeAsync } = require('../../utils/general')
7
+ const archiver = require('archiver')
8
+ const { getResponseContent } = require('../../support/command/handle-response')
9
+
10
+ class UploadSpectralRulesetCommand extends BaseCommand {
11
+ async run() {
12
+ const { args } = await this.parse(UploadSpectralRulesetCommand)
13
+ const rulesetPath = getSpectralIdentifierArg(args)
14
+
15
+ const zippedDirectory = await this.zipTheDirectory(args.directory)
16
+ await this.saveSpectralRuleset(rulesetPath, zippedDirectory)
17
+ }
18
+
19
+ async zipTheDirectory(directoryPath) {
20
+ const archive = archiver('zip', { zlib: { level: 9 } });
21
+ const passthrough = new PassThrough();
22
+
23
+ archive.directory(directoryPath, false);
24
+ archive.finalize();
25
+ archive.pipe(passthrough);
26
+ return passthrough;
27
+ }
28
+
29
+ saveSpectralRuleset(pathParams, zippedDirectory) {
30
+ return this.executeHttp({
31
+ execute: () => saveSpectralRuleset(pathParams, zippedDirectory),
32
+ onResolve: pipeAsync(getResponseContent, JSON.parse),
33
+ options: {}
34
+ })
35
+ }
36
+ }
37
+
38
+ UploadSpectralRulesetCommand.description = `Create or update organization's Spectral ruleset`
39
+
40
+ UploadSpectralRulesetCommand.examples = [
41
+ 'swaggerhub spectral:upload my_organization/my_api_ruleset rules/',
42
+ ]
43
+
44
+ UploadSpectralRulesetCommand.args = {
45
+ 'OWNER/RULESET_NAME': Args.string({
46
+ required: true,
47
+ description: 'Organization\'s Spectral ruleset to create or update on SwaggerHub'
48
+ }),
49
+ 'directory': Args.string({
50
+ required: true,
51
+ description: 'Relative path to directory with ruleset files'
52
+ })
53
+ }
54
+
55
+ UploadSpectralRulesetCommand.flags = {
56
+ ...BaseCommand.flags
57
+ }
58
+
59
+ module.exports = UploadSpectralRulesetCommand
@@ -0,0 +1,30 @@
1
+ const config = require('../config')
2
+ const http = require('../support/http')
3
+
4
+ const saveSpectralRuleset = (pathParams, body) => {
5
+ const { SWAGGERHUB_URL, SWAGGERHUB_API_KEY } = config.getConfig()
6
+
7
+ return http({
8
+ url: [SWAGGERHUB_URL, 'standardization', 'spectral-rulesets', pathParams, 'zip'],
9
+ auth: SWAGGERHUB_API_KEY,
10
+ contentType: 'zip',
11
+ method: 'PUT',
12
+ body
13
+ })
14
+ }
15
+
16
+ const getSpectralRuleset = (pathParams) => {
17
+ const { SWAGGERHUB_URL, SWAGGERHUB_API_KEY } = config.getConfig()
18
+
19
+ return http({
20
+ url: [SWAGGERHUB_URL, 'standardization', 'spectral-rulesets', pathParams, 'zip'],
21
+ auth: SWAGGERHUB_API_KEY,
22
+ contentType: 'zip',
23
+ method: 'GET'
24
+ })
25
+ }
26
+
27
+ module.exports = {
28
+ saveSpectralRuleset,
29
+ getSpectralRuleset
30
+ }
@@ -2,12 +2,21 @@ const { CLIError } = require('@oclif/core').Errors
2
2
  const { hasJsonStructure, isError } = require('../../utils/general')
3
3
  const { errorMsg } = require('../../template-strings')
4
4
 
5
- const parseResponse = response => new Promise(resolve => response.text()
6
- .then(content => resolve({
5
+ const parseResponse = response => new Promise(resolve => {
6
+
7
+ const resolveContent = content => resolve({
7
8
  status: response.status,
8
9
  ok: response.ok,
9
- content: content,
10
- })))
10
+ content,
11
+ });
12
+
13
+ const contentType = response.headers?.get('content-type') || '';
14
+ if (contentType.includes('application/zip')) {
15
+ response.buffer().then(resolveContent)
16
+ } else {
17
+ response.text().then(resolveContent)
18
+ }
19
+ });
11
20
 
12
21
  const checkForErrors = ({ resolveStatus = [] } = {}) => response => {
13
22
  if (resolveStatus.includes(response.status) || response.ok) {
@@ -7,6 +7,7 @@ const optionalVersionRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+(\/[\w\-.]+)?(\/?
7
7
  const requiredVersionRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+\/[\w\-.]+(\/?)$/)
8
8
  const integrationIdentifierRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+\/[\w\-.]+\/[\w\-.]+(\/?)$/)
9
9
  const projectIdentifierRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+$/)
10
+ const spectralIdentifierRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+$/)
10
11
 
11
12
  const isValidIdentifier = (id, isVersionRequired) => isVersionRequired
12
13
  ? requiredVersionRegex.test(id)
@@ -52,6 +53,15 @@ const getProjectIdentifierArg = args => {
52
53
  return identifier
53
54
  }
54
55
 
56
+ const getSpectralIdentifierArg = args => {
57
+ const format = 'OWNER/RULESET_NAME'
58
+ const identifier = args[format]
59
+ if (!spectralIdentifierRegex.test(identifier)) {
60
+ throw new CLIError(errorMsg.argsMustMatchFormat({ format }))
61
+ }
62
+ return identifier
63
+ }
64
+
55
65
  const readConfigFile = filename => {
56
66
  if (!existsSync(filename)) {
57
67
  throw new CLIError(errorMsg.fileNotFound({ filename }))
@@ -81,6 +91,7 @@ module.exports = {
81
91
  getDomainIdentifierArg,
82
92
  getIntegrationIdentifierArg,
83
93
  getProjectIdentifierArg,
94
+ getSpectralIdentifierArg,
84
95
  readConfigFile,
85
96
  splitPathParams,
86
97
  splitFlagParams,
@@ -23,6 +23,8 @@ const errorMsg = {
23
23
 
24
24
  invalidConfig: 'Invalid configuration file. Please ensure that the file is in JSON format',
25
25
 
26
+ directoryExists: 'Directory already exists: \'{{directory}}\'',
27
+
26
28
  unknown: 'Unknown Error',
27
29
  }
28
30