-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (100 loc) · 3.39 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const core = require('@actions/core');
const axios = require('axios');
const commandLineArgs = require('command-line-args');
const { default: OASNormalize } = require('oas-normalize');
const swaggerInline = require('swagger-inline');
const getContext = require('./lib/context');
const getPkgVersion = require('./lib/getPkgVersion');
const utils = require('./utils');
require('dotenv').config({ path: path.join(__dirname, '.env') });
async function main(opts) {
const options = commandLineArgs(
[
{ name: 'src', type: String, multiple: true, defaultOption: true },
{ name: 'key', alias: 'k', type: String },
],
{ partial: true }
); // this prevents the script from throwing during unit tests
// This allows us to override the options during unit tests
Object.assign(options, opts);
const src = utils.listOas(options.src);
const context = await getContext();
const out = {
markdown: undefined, // micro.md file
specs: [], // the specs {filename, oas}
...context,
actionVersion: getPkgVersion(), // version of Micro that's running
};
const markdown = path.join(process.cwd(), 'micro.md');
if (fs.existsSync(markdown)) {
out.markdown = fs.readFileSync(markdown, 'utf8');
}
for (let i = 0; i < src.length; i += 1) {
const fileName = src[i];
const file = path.join(process.cwd(), fileName);
if (fs.existsSync(file)) {
let oas = {};
const original = fs.readFileSync(file, 'utf-8');
if (fileName === 'api.config.json') {
/*
const prepare = await import('./api.js/prepare.js');
oas = JSON.stringify((await prepare.default(process.cwd())).oas, undefined, 2);
*/
} else {
/* TODO: I would love Swagger Inline to eventually
* use a glob from the OAS file itself, so hopefully
* eventually we can do that!
*
* Like this:
*
* path: '**\/*.js'
*
* */
// eslint-disable-next-line no-await-in-loop
oas = await swaggerInline(['**/*'], {
base: file,
});
}
const normalized = new OASNormalize(oas);
out.specs.push({
fileName,
// eslint-disable-next-line no-await-in-loop
oas: JSON.stringify(await normalized.bundle()),
original,
});
}
}
// Get the base and remove the trailing slash that comes by default from vercel
const base = (process.env.BASE_URL || 'https://micro.readme.com').replace(/\/$/, '');
return axios
.post(`${base}/api/uploadSpec`, out, {
headers: { 'X-API-KEY': options.key },
})
.then(response => {
const body = response.data;
if (body.lint?.length) {
body.lint.forEach(file => {
core.info(`Linting issues in ${file.fileName}:`);
file.result.forEach(l => {
core.info(` ⚠️ ${l.message} (${l.path.join(' > ')})`);
});
});
return core.setFailed('Your OAS files failed linting!');
}
return core.info('Successfully synced file to ReadMe Micro! 🦉');
})
.catch(error => {
// eslint-disable-next-line no-console
console.log(error);
// TODO not sure what this will do on bitbucket pipelines?
core.setFailed(error.message);
throw error;
});
}
module.exports = main;
if (require.main === module) {
main();
}