|
1
|
#!/usr/bin/env node |
|
2
|
|
|
3
|
const fs = require("fs/promises") |
|
4
|
const os = require("os") |
|
5
|
const path = require("path") |
|
6
|
const execa = require("execa") |
|
7
|
|
|
8
|
const isDryRun = process.argv.indexOf("--dry-run") !== -1 |
|
9
|
const withFullFile = process.argv.indexOf("--with-full-file") !== -1 |
|
10
|
const archiveVersionArg = process.argv.find((a) => |
|
11
|
a.startsWith("--archive-version=") |
|
12
|
) |
|
13
|
const archiveVersion = archiveVersionArg ? archiveVersionArg.split("=")[1] : undefined |
|
14
|
const basePath = path.resolve(__dirname, `../`) |
|
15
|
const repoRootPath = path.resolve(basePath, `../../../../`) |
|
16
|
const docsApiPath = path.resolve(repoRootPath, "www/apps/api-reference/specs") |
|
17
|
|
|
18
|
const FULL_SPEC_FILE_NAME = "openapi.full.yaml" |
|
19
|
|
|
20
|
const run = async () => { |
|
21
|
const oasOutDir = isDryRun ? await getTmpDirectory() : docsApiPath |
|
22
|
for (const apiType of ["store", "admin"]) { |
|
23
|
await archiveCurrentFullSpec(apiType) |
|
24
|
await generateOASSource(oasOutDir, apiType) |
|
25
|
const oasSrcFile = path.resolve(oasOutDir, `${apiType}.oas.json`) |
|
26
|
const docsOutDir = path.resolve(oasOutDir, apiType) |
|
27
|
await generateDocs(oasSrcFile, docsOutDir, apiType, isDryRun) |
|
28
|
} |
|
29
|
} |
|
30
|
|
|
31
|
/** |
|
32
|
* Copies the full OAS of the currently released version to |
|
33
|
* `specs/versions/{archiveVersion}` before it's overwritten by the generated |
|
34
|
* OAS of the new release. Must run before `generateDocs`, which cleans the |
|
35
|
* output directory. |
|
36
|
*/ |
|
37
|
const archiveCurrentFullSpec = async (apiType) => { |
|
38
|
if (!archiveVersion || isDryRun) { |
|
39
|
return |
|
40
|
} |
|
41
|
|
|
42
|
const currentFile = path.resolve(docsApiPath, apiType, FULL_SPEC_FILE_NAME) |
|
43
|
if (!(await fileExists(currentFile))) { |
|
44
|
console.log(`Skipped archiving ${apiType}: ${currentFile} doesn't exist`) |
|
45
|
return |
|
46
|
} |
|
47
|
|
|
48
|
const archiveOutFile = path.resolve( |
|
49
|
docsApiPath, |
|
50
|
"versions", |
|
51
|
archiveVersion, |
|
52
|
apiType, |
|
53
|
FULL_SPEC_FILE_NAME |
|
54
|
) |
|
55
|
await fs.mkdir(path.dirname(archiveOutFile), { recursive: true }) |
|
56
|
await fs.copyFile(currentFile, archiveOutFile) |
|
57
|
console.log(`Archived version ${archiveVersion} to ${archiveOutFile}`) |
|
58
|
} |
|
59
|
|
|
60
|
const fileExists = async (filePath) => { |
|
61
|
try { |
|
62
|
await fs.access(filePath) |
|
63
|
return true |
|
64
|
} catch { |
|
65
|
return false |
|
66
|
} |
|
67
|
} |
|
68
|
|
|
69
|
const generateOASSource = async (outDir, apiType) => { |
|
70
|
const commandParams = ["oas", `--type=${apiType}`, `--out-dir=${outDir}`, "--local"] |
|
71
|
const { all: logs } = await execa( |
|
72
|
"medusa-oas", |
|
73
|
commandParams, |
|
74
|
{ cwd: basePath, all: true } |
|
75
|
) |
|
76
|
console.log(logs) |
|
77
|
} |
|
78
|
|
|
79
|
const generateDocs = async (srcFile, outDir, apiType, isDryRun) => { |
|
80
|
let params = [ |
|
81
|
"docs", |
|
82
|
`--src-file=${srcFile}`, |
|
83
|
`--out-dir=${outDir}`, |
|
84
|
`--clean`, |
|
85
|
`--split`, |
|
86
|
] |
|
87
|
if (isDryRun) { |
|
88
|
params.push("--dry-run") |
|
89
|
} |
|
90
|
await runMedusaOasCommand(params) |
|
91
|
if (withFullFile && !isDryRun) { |
|
92
|
console.log("Generating full file...") |
|
93
|
params = [ |
|
94
|
"docs", |
|
95
|
`--src-file=${srcFile}`, |
|
96
|
`--out-dir=${outDir}`, |
|
97
|
`--main-file-name=${FULL_SPEC_FILE_NAME}`, |
|
98
|
] |
|
99
|
await runMedusaOasCommand(params) |
|
100
|
console.log("Finished generating full file.") |
|
101
|
} |
|
102
|
} |
|
103
|
|
|
104
|
const runMedusaOasCommand = async (params) => { |
|
105
|
const { all: logs } = await execa("medusa-oas", params, { |
|
106
|
cwd: basePath, |
|
107
|
all: true, |
|
108
|
}) |
|
109
|
console.log(logs) |
|
110
|
} |
|
111
|
|
|
112
|
const getTmpDirectory = async () => { |
|
113
|
/** |
|
114
|
* RUNNER_TEMP: GitHub action, the path to a temporary directory on the runner. |
|
115
|
*/ |
|
116
|
const tmpDir = process.env["RUNNER_TEMP"] ?? os.tmpdir() |
|
117
|
return await fs.mkdtemp(tmpDir) |
|
118
|
} |
|
119
|
|
|
120
|
void (async () => { |
|
121
|
try { |
|
122
|
await run() |
|
123
|
} catch (err) { |
|
124
|
console.log(err) |
|
125
|
process.exit(1) |
|
126
|
} |
|
127
|
})() |