Skip to content

Commit

Permalink
Match process.arch on artifact names
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Sep 21, 2022
1 parent 4263ab3 commit 493d4c8
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 14 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ jobs:
npm install
- run: |
npm run all
test: # make sure the action works on a clean machine without building
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm ci
- run: npm test
smoketest: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
36 changes: 29 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ async function run(): Promise<void> {
core.debug(`Running stylua with arguments: ${args}`)

await exec(`stylua ${args}`)
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
core.setFailed(error.message)
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/stylua.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {getFilenameMatcher} from './stylua'

const ARTIFACT_NAMES = [
'stylua-linux-aarch64.zip',
'stylua-linux-x86_64.zip',
'stylua-linux.zip',
'stylua-macos-aarch64.zip',
'stylua-macos-x86_64.zip',
'stylua-macos.zip',
'stylua-win64.zip',
'stylua-windows-x86_64.zip'
]

const selectArtifact = (
platform: NodeJS.Platform,
arch: string
): string | undefined => {
const matcher = getFilenameMatcher(platform, arch)
return ARTIFACT_NAMES.find(name => matcher(name))
}

test('matches windows x64 platform', () => {
expect(selectArtifact('win32', 'x64')).toBe('stylua-windows-x86_64.zip')
})

test('matches macos x64 platform', () => {
expect(selectArtifact('darwin', 'x64')).toBe('stylua-macos-x86_64.zip')
})

test('matches macos aarch64 platform', () => {
expect(selectArtifact('darwin', 'arm64')).toBe('stylua-macos-aarch64.zip')
})

test('matches linux x64 platform', () => {
expect(selectArtifact('linux', 'x64')).toBe('stylua-linux-x86_64.zip')
})

test('matches linux aarch64 platform', () => {
expect(selectArtifact('linux', 'arm64')).toBe('stylua-linux-aarch64.zip')
})
28 changes: 24 additions & 4 deletions src/stylua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ function chooseRelease(
return releases.find(release => semver.satisfies(release.tag_name, version))
}

type Matcher = (name: string) => boolean
export type Matcher = (name: string) => boolean

const getFilenameMatcher: () => Matcher = () => {
switch (process.platform) {
const getPlatformMatcher = (platform: NodeJS.Platform): Matcher => {
switch (platform) {
case 'win32':
return name => name.includes('win64') || name.includes('windows')
case 'linux':
Expand All @@ -49,8 +49,28 @@ const getFilenameMatcher: () => Matcher = () => {
}
}

const getArchMatcher = (arch: string): Matcher => {
switch (arch) {
case 'x64':
return name => name.includes('x86_64')
case 'arm64':
return name => name.includes('aarch64')
default:
throw new Error('Arch not supported')
}
}

export const getFilenameMatcher = (
platform: NodeJS.Platform,
arch: string
): Matcher => {
const matchPlatform = getPlatformMatcher(platform)
const matchArch = getArchMatcher(arch)
return name => matchPlatform(name) && matchArch(name)
}

function chooseAsset(release: GitHubRelease): GitHubAsset | undefined {
const matcher = getFilenameMatcher()
const matcher = getFilenameMatcher(process.platform, process.arch)
return release.assets.find(asset => matcher(asset.name))
}

Expand Down

0 comments on commit 493d4c8

Please sign in to comment.