Skip to content

Commit fccd229

Browse files
committed
simplify logic (JS)
1 parent d7d5308 commit fccd229

File tree

3 files changed

+6
-20
lines changed

3 files changed

+6
-20
lines changed

packages/js-sdk/src/template/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export class TemplateBase
359359

360360
for (const src of srcs) {
361361
const args = [
362-
relativizePath(src, this.fileContextPath),
362+
relativizePath(src),
363363
dest.toString(),
364364
options?.user ?? '',
365365
options?.mode ? padOctal(options.mode) : '',

packages/js-sdk/src/template/utils.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ export function padOctal(mode: number): string {
259259
* Create a compressed tar stream of files matching a pattern.
260260
*
261261
* @param filePath Glob pattern for files to include
262-
* @param fileName Name of the file in the tar archive
263262
* @param fileContextPath Base directory for resolving file paths
264263
* @param ignorePatterns Ignore patterns to exclude from the archive
265264
* @param resolveSymlinks Whether to follow symbolic links
@@ -291,7 +290,7 @@ export async function tarFileStream(
291290

292291
for (const file of allFiles) {
293292
const sourcePath = file.fullpathPosix()
294-
const targetPath = relativizePath(sourcePath, fileContextPath)
293+
const targetPath = relativizePath(filePath)
295294

296295
if (file.isDirectory()) {
297296
sources.push({
@@ -323,7 +322,7 @@ export async function tarFileStream(
323322
/**
324323
* Create a tar stream and calculate its compressed size for upload.
325324
*
326-
* @param fileName Glob pattern for files to include
325+
* @param filePath Glob pattern for files to include
327326
* @param fileContextPath Base directory for resolving file paths
328327
* @param resolveSymlinks Whether to follow symbolic links
329328
* @returns Object containing the content length and upload stream
@@ -410,21 +409,9 @@ export function readGCPServiceAccountJSON(
410409
* @param fileContextPath Base directory for resolving relative paths
411410
* @returns Relative path with forward slashes (for tar/cross-platform compatibility)
412411
*/
413-
export function relativizePath(
414-
src: PathLike,
415-
fileContextPath: PathLike
416-
): string {
417-
let rewrittenPath = src.toString()
418-
419-
// Convert absolute paths to relative paths
420-
if (path.isAbsolute(rewrittenPath)) {
421-
const contextPath = path.resolve(fileContextPath.toString())
422-
const relativePath = path.relative(contextPath, rewrittenPath)
423-
rewrittenPath = relativePath
424-
}
425-
412+
export function relativizePath(src: PathLike): string {
426413
// Strip up directories (../ or ..\ on Windows)
427-
rewrittenPath = rewrittenPath.replace(/\.\.(\/|\\)/g, '')
414+
const rewrittenPath = src.toString().replace(/\.\.(\/|\\)/g, '')
428415

429416
// Normalize to forward slashes for cross-platform compatibility (tar archives require forward slashes)
430417
return normalizePath(rewrittenPath)

packages/js-sdk/tests/template/build.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,13 @@ buildTemplateTest(
7777
'build template with absolute paths',
7878
async ({ buildTemplate }) => {
7979
const packageTxt = path.resolve(process.cwd(), folderPath, 'test.txt')
80-
const rootTxt = path.resolve(process.cwd(), '..', '..', 'package.json')
8180

8281
const template = Template()
8382
// using base image to avoid re-building ubuntu:22.04 image
8483
.fromBaseImage()
8584
.skipCache()
8685
.copy(packageTxt, 'text.txt', { forceUpload: true })
87-
.copy(rootTxt, 'package.json', { forceUpload: true })
86+
.copy('../../../../package.json', 'package.json', { forceUpload: true })
8887
.runCmd(['ls -l .', 'cat text.txt', 'cat package.json'])
8988

9089
await buildTemplate(template, {}, defaultBuildLogger())

0 commit comments

Comments
 (0)