1 #!/usr/bin/env node
2
3 import { Octokit } from "@octokit/core"
4 import fs from "fs"
5 import path from "path"
6 import { getDirname } from "utils"
7
8 const octokit = new Octokit({
9 auth: process.env.GH_TOKEN,
10 })
11
12 type DocsConfig = {
13 version: {
14 number: string
15 releaseUrl: string
16 releaseDate: string
17 }
18 }
19
20 function getConfigPath() {
21 const __dirname = getDirname(import.meta.url)
22 return path.join(
23 __dirname,
24 "..",
25 "..",
26 "..",
27 "..",
28 "packages",
29 "docs-utils",
30 "src",
31 "global-config.ts"
32 )
33 }
34
35 const configFilePrefix = `/* eslint-disable comma-dangle */
36 /* eslint-disable prettier/prettier */
37 import { DocsConfig } from "types"
38
39 export const globalConfig: Pick<DocsConfig, "version"> = `
40
41 async function main() {
42 //retrieve the latest release
43 const response = await octokit.request(
44 "GET /repos/{owner}/{repo}/releases/latest",
45 {
46 owner: "medusajs",
47 repo: "medusa",
48 sha: "develop",
49 }
50 )
51
52 const version = response.data.tag_name.replace(/\.0$/, "").replace(/^v/, "")
53
54 // get the existing version
55 const configPath = getConfigPath()
56 const configContent = fs
57 .readFileSync(configPath, "utf-8")
58 .replace(configFilePrefix, "")
59 const oldConfig: DocsConfig = JSON.parse(configContent)
60
61 if (oldConfig.version.number === version) {
62 return
63 }
64
65 //add new announcement
66 const config: DocsConfig = {
67 version: {
68 number: version,
69 releaseUrl: response.data.html_url,
70 releaseDate: response.data.published_at || new Date().toISOString(),
71 },
72 }
73
74 //write new config file
75 fs.writeFileSync(
76 configPath,
77 `${configFilePrefix}${JSON.stringify(config, undefined, 2)}`
78 )
79 console.log(`Version in config has been updated.`)
80 }
81
82 void main()