refactored path calculation

This commit is contained in:
Edwin Sirko
2024-01-26 16:35:51 -05:00
parent 814845b943
commit 89c429cf42
3 changed files with 33 additions and 29 deletions
+2 -2
View File
@@ -226,7 +226,7 @@ describe('bundleFilesintoDirectory', () => {
fs.writeFileSync(file3, fileContent)
// Bundle the files and folders into the targetDir
fsHelper.bundleFilesintoDirectory([file1, folder1], targetDir)
fsHelper.bundleFilesintoDirectory([file1, folder1])
// Check that the files and folders were copied
expect(fs.existsSync(file1)).toEqual(true)
@@ -244,7 +244,7 @@ describe('bundleFilesintoDirectory', () => {
it('throws an error if a file or directory does not exist', () => {
expect(() => {
fsHelper.bundleFilesintoDirectory(['/does/not/exist'], targetDir)
fsHelper.bundleFilesintoDirectory(['/does/not/exist'])
}).toThrow('File /does/not/exist does not exist')
})
})
+28 -13
View File
@@ -29,6 +29,23 @@ export interface FileMetadata {
sha256: string
}
export function getConsolidatedDirectory(filePathSpec: string): { path: string, needToCleanUpDir: boolean } {
const paths: string[] = filePathSpec.split(' ') // TODO: handle files with spaces
// TODO: do check on paths to make sure they're valid and not reaching outside the space
let path = ''
let needToCleanUpDir = false
if (paths.length === 1 && isDirectory(paths[0])) {
// If the path is a single directory, we can skip the bundling step
path = paths[0]
} else {
// Otherwise, we need to bundle the files & folders into a temporary directory
path = bundleFilesintoDirectory(paths)
needToCleanUpDir = true
}
return { path, needToCleanUpDir }
}
// Creates both a tar.gz and zip archive of the given directory and returns the paths to both archives (stored in the provided target directory)
// as well as the size/sha256 hash of each file.
export async function createArchives(
@@ -55,7 +72,7 @@ export async function createArchives(
})
archive.pipe(output)
archive.directory(distPath, false)
archive.directory(distPath, false) // TODO: make sure this doesn't include dirs that start with ., same with below
archive.finalize()
})
@@ -102,21 +119,19 @@ export function readFileContents(filePath: string): Buffer {
return fs.readFileSync(filePath)
}
export function bundleFilesintoDirectory(
files: string[],
targetDir: string = createTempDir()
): string {
for (const file of files) {
if (!fs.existsSync(file)) {
throw new Error(`File ${file} does not exist`)
export function bundleFilesintoDirectory(filePaths: string[]): string {
const targetDir: string = createTempDir()
for (const filePath of filePaths) {
if (!fs.existsSync(filePath)) {
throw new Error(`filePath ${filePath} does not exist`)
}
if (isDirectory(file)) {
const targetFolder = path.join(targetDir, path.basename(file))
fsExtra.copySync(file, targetFolder)
if (isDirectory(filePath)) {
const targetFolder = path.join(targetDir, path.basename(filePath)) // TODO: basename is probably not what we actually want here. Or is it? Maybe conflicts between dir1/dir2 and dir1/dir3/dir2 are just user error or ??
fsExtra.copySync(filePath, targetFolder) // TODO: ignore files preceded by .
} else {
const targetFile = path.join(targetDir, path.basename(file))
fs.copyFileSync(file, targetFile)
const targetFile = path.join(targetDir, path.basename(filePath))
fs.copyFileSync(filePath, targetFile)
}
}
+3 -14
View File
@@ -41,20 +41,9 @@ export async function run(pathInput: string): Promise<void> {
const token: string = process.env.TOKEN!
// Gather & validate user input
// Paths to be included in the OCI image
// const paths: string[] = core.getInput('path').split(' ')
const paths: string[] = pathInput.split(' ')
let path = ''
if (paths.length === 1 && fsHelper.isDirectory(paths[0])) {
// If the path is a single directory, we can skip the bundling step
path = paths[0]
} else {
// Otherwise, we need to bundle the files & folders into a temporary directory
const bundleDir = fsHelper.createTempDir()
tmpDirs.push(bundleDir)
path = fsHelper.bundleFilesintoDirectory(paths, bundleDir)
const { path, needToCleanUpDir } = fsHelper.getConsolidatedDirectory(pathInput)
if (needToCleanUpDir) {
tmpDirs.push(path)
}
if (!fsHelper.isActionRepo(path)) {