Skip to content

Commit 56f122f

Browse files
committed
Use resolveBasePath
1 parent 2395f39 commit 56f122f

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ function isResponseOk(response: IncomingMessage): boolean {
218218
)
219219
}
220220

221+
function resolveBasePath(pathsRelativeTo = '.'): string {
222+
// Node's path.resolve will process path segments from right to left until
223+
// it creates a valid absolute path. So if `pathsRelativeTo` is an absolute
224+
// path, process.cwd() is not used, which is the common expectation. If none
225+
// of the paths resolve then it defaults to process.cwd().
226+
return path.resolve(process.cwd(), pathsRelativeTo)
227+
}
228+
221229
/**
222230
* Package.json data to base the User-Agent on
223231
*/
@@ -445,7 +453,7 @@ export class SocketSdk {
445453
filepaths: string[],
446454
pathsRelativeTo = '.'
447455
): Promise<SocketSdkResultType<'createDependenciesSnapshot'>> {
448-
const basePath = path.join(process.cwd(), pathsRelativeTo)
456+
const basePath = resolveBasePath(pathsRelativeTo)
449457
const absFilepaths = filepaths.map(p => path.join(basePath, p))
450458
try {
451459
const data = await getResponseJson(
@@ -468,11 +476,7 @@ export class SocketSdk {
468476
filepaths: string[],
469477
pathsRelativeTo: string = '.'
470478
): Promise<SocketSdkResultType<'CreateOrgFullScan'>> {
471-
// Resolve will process path segments until it creates a valid absolute
472-
// path. So if the given pathsRelativeTo is an absolute path, the cwd()
473-
// is not used, which is the common expectation. If none of the paths
474-
// resolve then it defaults to process.cwd()
475-
const basePath = path.resolve(process.cwd(), pathsRelativeTo)
479+
const basePath = resolveBasePath(pathsRelativeTo)
476480
const absFilepaths = filepaths.map(p => path.join(basePath, p))
477481
try {
478482
const data = await getResponseJson(
@@ -513,7 +517,7 @@ export class SocketSdk {
513517
pathsRelativeTo: string = '.',
514518
issueRules?: Record<string, boolean>
515519
): Promise<SocketSdkResultType<'createReport'>> {
516-
const basePath = path.join(process.cwd(), pathsRelativeTo)
520+
const basePath = resolveBasePath(pathsRelativeTo)
517521
const absFilepaths = filepaths.map(p => path.join(basePath, p))
518522
try {
519523
const data = await createUploadRequest(

0 commit comments

Comments
 (0)