Skip to content

Commit

Permalink
fix: ensure the directories are actually created.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChecksumDev committed Jul 18, 2024
1 parent 0845172 commit c07ef2c
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,32 @@ async function extractTarball(
tarballPath: string,
extractPath: string
): Promise<void> {
await fs.mkdir(extractPath, { recursive: true })

await tar.x({
file: tarballPath,
cwd: extractPath
})
}

async function moveContents(srcDir: string, destDir: string): Promise<void> {
const files = await fs.readdir(srcDir, { withFileTypes: true })

for (const file of files) {
const srcPath = join(srcDir, file.name)
const destPath = join(destDir, file.name)

if (file.isDirectory()) {
await fs.mkdir(destPath, { recursive: true })
await moveContents(srcPath, destPath)
} else {
await fs.copyFile(srcPath, destPath)
try {
await fs.mkdir(destDir, { recursive: true })
const files = await fs.readdir(srcDir, { withFileTypes: true })

for (const file of files) {
const srcPath = join(srcDir, file.name)
const destPath = join(destDir, file.name)

if (file.isDirectory()) {
await moveContents(srcPath, destPath)
} else {
await fs.copyFile(srcPath, destPath)
}
}
} catch (error) {
core.error(`Failed to move contents from ${srcDir} to ${destDir} - ${error}`)
throw error
}
}

Expand All @@ -87,18 +94,20 @@ async function run(): Promise<void> {
const archiveUrl = `https://${host}/${owner}/${repoName}/archive/refs/heads/${branch}.tar.gz`
const tarballPath = `./${repoName}-${branch}.tar.gz`
const extractPath = `./${repoName}-${branch}`
const refsPath = resolve(referencesPath)

core.info(`Creating directory ${extractPath}...`)
await fs.mkdir(extractPath, { recursive: true })

core.info(`Downloading ${archiveUrl}...`)
core.info(`Downloading ${archiveUrl} to ${tarballPath}...`)
await downloadFile(archiveUrl, tarballPath, token)

core.info(`Extracting ${tarballPath}...`)
await extractTarball(tarballPath, './')
core.info(`Extracting ${tarballPath} to ${extractPath}...`)
await extractTarball(tarballPath, extractPath)

core.info(`Moving contents from ${extractPath} to ${refsPath}...`)
await moveContents(extractPath, refsPath)

const dataPath = join(extractPath, 'data')
await moveContents(dataPath, referencesPath)
core.info('Cleaning up...')
await fs.rm(tarballPath)
await fs.rm(extractPath, { recursive: true })
} catch (error) {
core.setFailed(
error instanceof Error ? error.message : 'An unknown error occurred'
Expand Down

0 comments on commit c07ef2c

Please sign in to comment.