Skip to content

Commit 33c0f06

Browse files
authored
chore: updated eslint configuration (#487)
1 parent e8d7c23 commit 33c0f06

14 files changed

Lines changed: 68971 additions & 83894 deletions

File tree

.github/utils/update_metadata.js

Lines changed: 0 additions & 122 deletions
This file was deleted.

.github/utils/update_metadata.ts

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import path from 'path'
2+
import {promises as fs} from 'fs'
3+
import * as core from '@actions/core'
4+
import {exec, getExecOutput} from '@actions/exec'
5+
import yaml from 'js-yaml'
6+
import semver from 'semver'
7+
import https from 'https'
8+
9+
interface SwiftRelease {
10+
name: string
11+
date: string
12+
tag: string
13+
}
14+
15+
interface SnapshotEntry {
16+
date: string
17+
dir: string
18+
}
19+
20+
interface SwiftorgMetadata {
21+
commit: string
22+
release: SwiftRelease
23+
dev: SwiftRelease
24+
snapshot: {date: string; tag: string}
25+
}
26+
27+
const SWIFT_ORG = 'swiftorg'
28+
const SWIFT_ORG_BUILD = path.join(SWIFT_ORG, '_data', 'builds')
29+
const SWIFT_ORG_CWD = {cwd: SWIFT_ORG}
30+
31+
async function swiftorgCommit(): Promise<string> {
32+
const {stdout} = await getExecOutput(
33+
'git',
34+
['rev-parse', '--verify', 'HEAD'],
35+
SWIFT_ORG_CWD
36+
)
37+
return stdout.trim()
38+
}
39+
40+
async function latestRelease(): Promise<SwiftRelease> {
41+
const swiftRelease = path.join(SWIFT_ORG_BUILD, 'swift_releases.yml')
42+
const releaseData = await fs.readFile(swiftRelease, 'utf-8')
43+
const releases = yaml.load(releaseData) as SwiftRelease[]
44+
return releases[releases.length - 1]
45+
}
46+
47+
async function latestDevRelease(): Promise<SwiftRelease> {
48+
const buildEntries = await fs.readdir(SWIFT_ORG_BUILD, {withFileTypes: true})
49+
const devBranchRegex = /swift-([^-]*)-branch/
50+
const devDirs = buildEntries
51+
.flatMap(entry => {
52+
if (!entry.isDirectory() || !devBranchRegex.exec(entry.name)) {
53+
return []
54+
}
55+
return entry.name
56+
})
57+
.sort((dir1, dir2) => {
58+
const ver1 = devBranchRegex.exec(dir1)?.[1]?.replace('_', '.') ?? '0'
59+
const ver2 = devBranchRegex.exec(dir2)?.[1]?.replace('_', '.') ?? '0'
60+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61+
return semver.gt(semver.coerce(ver2)!, semver.coerce(ver1)!) ? 1 : -1
62+
})
63+
64+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
65+
const devVer = devBranchRegex.exec(devDirs[0])![1].replace('_', '.')
66+
const xcodeSnapshot = path.join(SWIFT_ORG_BUILD, devDirs[0], 'xcode.yml')
67+
const devReleaseData = await fs.readFile(xcodeSnapshot, 'utf-8')
68+
const devReleases = yaml.load(devReleaseData) as SnapshotEntry[]
69+
return {name: devVer, date: devReleases[0].date, tag: devReleases[0].dir}
70+
}
71+
72+
async function latestSnapshot(): Promise<{date: string; tag: string}> {
73+
const xcodeSnapshot = path.join(SWIFT_ORG_BUILD, 'development', 'xcode.yml')
74+
const devSnapshotsData = await fs.readFile(xcodeSnapshot, 'utf-8')
75+
const snapshots = yaml.load(devSnapshotsData) as SnapshotEntry[]
76+
return {date: snapshots[0].date, tag: snapshots[0].dir}
77+
}
78+
79+
export async function update(): Promise<string> {
80+
const commit = await swiftorgCommit()
81+
const release = await latestRelease()
82+
const dev = await latestDevRelease()
83+
const snapshot = await latestSnapshot()
84+
85+
const swiftorg: SwiftorgMetadata = {commit, release, dev, snapshot}
86+
const data = JSON.stringify(swiftorg)
87+
core.info(`Updating swiftorg metadata to "${data}"`)
88+
const metadata = path.join('pages', 'metadata.json')
89+
await fs.mkdir(path.dirname(metadata), {recursive: true})
90+
await fs.writeFile(metadata, data, 'utf-8')
91+
return data
92+
}
93+
94+
export async function currentData(): Promise<SwiftorgMetadata> {
95+
return new Promise((resolve, reject) => {
96+
https.get('https://swiftylab.github.io/setup-swift/metadata.json', res => {
97+
const {statusCode} = res
98+
const contentType = res.headers['content-type']
99+
100+
let error: Error | undefined
101+
if (statusCode !== 200) {
102+
error = new Error(`Request Failed Status Code: '${statusCode}'`)
103+
} else if (!contentType?.startsWith('application/json')) {
104+
error = new Error(`Invalid content-type: ${contentType}`)
105+
}
106+
107+
if (error) {
108+
core.error(error.message)
109+
res.resume()
110+
reject(error)
111+
return
112+
}
113+
114+
let rawData = ''
115+
res.setEncoding('utf8')
116+
res.on('data', (chunk: string) => {
117+
rawData += chunk
118+
})
119+
res.on('end', () => {
120+
try {
121+
const parsedData = JSON.parse(rawData) as SwiftorgMetadata
122+
core.debug(`Recieved swift.org metadata: "${rawData}"`)
123+
resolve(parsedData)
124+
} catch (e) {
125+
core.error(`Parsing swift.org metadata error: '${e}'`)
126+
reject(e)
127+
}
128+
})
129+
})
130+
})
131+
}
132+
133+
export async function fetch(): Promise<void> {
134+
let checkoutData: SwiftorgMetadata | undefined
135+
if (process.env.SETUPSWIFT_SWIFTORG_METADATA) {
136+
checkoutData = JSON.parse(
137+
process.env.SETUPSWIFT_SWIFTORG_METADATA
138+
) as SwiftorgMetadata
139+
}
140+
if (!checkoutData || !checkoutData.commit) {
141+
checkoutData = await currentData()
142+
}
143+
const origin = 'https://github.com/apple/swift-org-website.git'
144+
const ref = checkoutData.commit
145+
await exec('git', ['init', SWIFT_ORG])
146+
await exec(
147+
'git',
148+
['fetch', origin, ref, '--depth=1', '--no-tags'],
149+
SWIFT_ORG_CWD
150+
)
151+
await exec('git', ['checkout', 'FETCH_HEAD', '--detach'], SWIFT_ORG_CWD)
152+
}

.github/workflows/main.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ jobs:
7272
uses: actions/github-script@v8
7373
with:
7474
script: |
75-
const hook = await import('${{ github.workspace }}/.github/utils/update_metadata.js');
75+
const path = require('path');
76+
const {createJiti} = require(path.join('${{ github.workspace }}', 'node_modules', 'jiti'));
77+
const jiti = createJiti('${{ github.workspace }}', {interopDefault: true});
78+
const hook = jiti('./.github/utils/update_metadata.ts');
7679
const {stdout} = await exec.getExecOutput('git', ['rev-parse', 'HEAD'], {cwd: 'swiftorg'});
7780
const oldCommit = (await hook.currentData()).commit;
7881
const newCommit = stdout.trim();
@@ -508,7 +511,10 @@ jobs:
508511
uses: actions/github-script@v8
509512
with:
510513
script: |
511-
const hook = await import('${{ github.workspace }}/.github/utils/update_metadata.js');
514+
const path = require('path');
515+
const {createJiti} = require(path.join('${{ github.workspace }}', 'node_modules', 'jiti'));
516+
const jiti = createJiti('${{ github.workspace }}', {interopDefault: true});
517+
const hook = jiti('./.github/utils/update_metadata.ts');
512518
await hook.update();
513519
return true;
514520

__tests__/version.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as exec from '@actions/exec'
1616
import {describe, expect, it, vi} from 'vitest'
1717

1818
vi.mock('@actions/exec', {spy: true})
19+
vi.mock('@actions/tool-cache', {spy: true})
1920
vi.mock('os', {spy: true})
2021

2122
describe('parse version from provided string', () => {

0 commit comments

Comments
 (0)