-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupload-api-specification.js
More file actions
76 lines (69 loc) · 2.15 KB
/
Copy pathupload-api-specification.js
File metadata and controls
76 lines (69 loc) · 2.15 KB
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
69
70
71
72
73
74
75
76
const getSpecVersion = require('./get-spec-version');
const updateExistingSpec = require('./update-existing-spec');
const createNewSpec = require('./create-new-spec');
const fs = require('fs');
const deleteSpec = require('./delete-spec');
module.exports = async function uploadAPISpecification(filePath) {
// Skip non-yaml files
if (filePath.slice(-5) !== '.yaml') {
console.log('[INFO] skipping non-yaml file', filePath);
return;
}
//get the api key from the arguments passed to the script.
let apiKey = process.argv.slice(-1)[0];
let versionNumber = null;
let deleteFile = false;
let specKeyId = null;
try {
if (!fs.readFileSync(filePath)) {
deleteFile = true;
}
[versionNumber] = filePath.split('descriptions/')[1].split('/');
if (versionNumber == 'Preview') {
// If the version is preview, set it to 0
versionNumber = '0';
}
} catch (err) {
console.error('[ERROR] ', err);
throw err;
}
//fetch existing specifications for the currect version if any.
try {
specKeyId = await getSpecVersion(versionNumber, apiKey);
} catch (err) {
console.error("[ERROR] Couldn't fetch specification version");
}
if (deleteFile && specKeyId) {
try {
console.log(`[INFO] trying to delete API spec ${specKeyId} on version ${versionNumber}`);
return await deleteSpec(specKeyId, apiKey);
} catch (err) {
throw err;
}
}
if (deleteFile && !specKeyId) {
console.log('[INFO] no file to delete');
return;
}
try {
//If a version is found then update the same. If not, create a new version.
if (specKeyId) {
try {
console.log(`[INFO] Updating API spec ${specKeyId} on version ${versionNumber}`);
return await updateExistingSpec(specKeyId, apiKey, filePath);
} catch (err) {
throw err;
}
} else {
try {
console.log(`[INFO] Creating API spec on version ${versionNumber}`);
return await createNewSpec(apiKey, versionNumber, filePath);
} catch (err) {
throw err;
}
}
} catch (err) {
console.error('[ERROR] Error occurred during API spec Create/Update ', err);
throw err;
}
};