Skip to content

Commit 8ee9ec4

Browse files
committed
implement feedback
1 parent 9d07267 commit 8ee9ec4

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/cli.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22

3+
import { realpathSync } from 'node:fs'
34
import path from 'node:path'
45
import process from 'node:process'
56
import { fileURLToPath } from 'node:url'
@@ -129,11 +130,29 @@ export async function main(args = process.argv.slice(2)): Promise<void> {
129130
}
130131
}
131132

132-
const currentFile = path.resolve(fileURLToPath(import.meta.url))
133-
const invokedFile = typeof process.argv[1] === 'string' ? path.resolve(process.argv[1]) : ''
133+
const currentFile = realpathSync(fileURLToPath(import.meta.url))
134+
135+
function resolveInvokedPath(invoked?: string): string | null {
136+
if (invoked == null) return null
137+
try {
138+
return realpathSync(invoked)
139+
} catch {
140+
return path.resolve(invoked)
141+
}
142+
}
143+
144+
export function shouldRunCli(invoked?: string): boolean {
145+
const resolved = resolveInvokedPath(invoked)
146+
if (resolved == null) return false
147+
return resolved === currentFile
148+
}
149+
150+
export function runCliWhenExecuted(): void {
151+
if (!shouldRunCli(process.argv[1])) return
134152

135-
if (currentFile === invokedFile) {
136153
void main().catch((error) => {
137154
fail((error as Error).message)
138155
})
139156
}
157+
158+
runCliWhenExecuted()

test/unit/test-cli.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { mkdtempSync, rmSync, symlinkSync } from 'node:fs'
2+
import path from 'node:path'
3+
import { tmpdir } from 'node:os'
4+
import { fileURLToPath } from 'node:url'
15
import { afterEach, describe, expect, it, vi } from 'vitest'
2-
import { main, runSmartSig } from '../../src/cli.ts'
6+
import { main, runSmartSig, shouldRunCli } from '../../src/cli.ts'
37
import { Transloadit } from '../../src/Transloadit.ts'
48

59
const mockedExpiresDate = '2025-01-01T00:00:00.000Z'
@@ -19,6 +23,20 @@ afterEach(() => {
1923
})
2024

2125
describe('cli smart_sig', () => {
26+
it('recognizes symlinked invocation paths', () => {
27+
const tmpDir = mkdtempSync(path.join(tmpdir(), 'transloadit-cli-'))
28+
const symlinkTarget = fileURLToPath(new URL('../../src/cli.ts', import.meta.url))
29+
const symlinkPath = path.join(tmpDir, 'transloadit')
30+
31+
symlinkSync(symlinkTarget, symlinkPath)
32+
try {
33+
expect(shouldRunCli(symlinkPath)).toBe(true)
34+
} finally {
35+
rmSync(symlinkPath)
36+
rmSync(tmpDir, { recursive: true, force: true })
37+
}
38+
})
39+
2240
it('overwrites auth key with env credentials', async () => {
2341
mockExpires()
2442
vi.stubEnv('TRANSLOADIT_KEY', 'key')

0 commit comments

Comments
 (0)