From 52cb3e7353b22df99d709500988f5be89fa31cfb Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Thu, 3 Mar 2022 12:25:39 +0200 Subject: [PATCH] fixed more windows paths, updated tests --- src/utils.ts | 20 +++++++++++--------- tests/utils.test.ts | 13 +++++++++++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 5ea2d80..2d2a287 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -145,26 +145,28 @@ export async function isDir(path: string): Promise { } export function removeGlob(template: string) { - return template.replace(/\*/g, "").replace(/\/\//g, "/") + return template.replace(/\*/g, "").replace(/(\/\/|\\\\)/g, path.sep) } export function makeRelativePath(str: string): string { - return str.startsWith("/") ? str.slice(1) : str + return str.startsWith(path.sep) ? str.slice(1) : str } export function getBasePath(relPath: string) { return path .resolve(process.cwd(), relPath) - .replace(process.cwd() + "/", "") + .replace(process.cwd() + path.sep, "") .replace(process.cwd(), "") } export async function getFileList(options: ScaffoldConfig, template: string) { - return await promisify(glob)(template, { - dot: true, - debug: options.verbose === LogLevel.Debug, - nodir: true, - }) + return ( + await promisify(glob)(template, { + dot: true, + debug: options.verbose === LogLevel.Debug, + nodir: true, + }) + ).map((f) => f.replace(/\//g, path.sep)) } export interface GlobInfo { @@ -185,7 +187,7 @@ export async function getTemplateGlobInfo(options: ScaffoldConfig, template: str const _shouldAddGlob = !isGlob && isDirOrGlob const origTemplate = template if (_shouldAddGlob) { - _template = template + "/**/*" + _template = path.join(template, "**", "*") } return { nonGlobTemplate, origTemplate, isDirOrGlob, isGlob, template: _template } } diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 142afb8..ecc820d 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -27,8 +27,17 @@ describe("Utils", () => { ) }) }) - test("should work for non-windows paths", async () => { - expect(handlebarsParse(blankConf, "/home/test/{{name}}.txt", { isPath: true })).toEqual("/home/test/test.txt") + describe("non-windows paths", () => { + beforeAll(() => { + origSep = path.sep + Object.defineProperty(path, "sep", { value: "/" }) + }) + afterAll(() => { + Object.defineProperty(path, "sep", { value: origSep }) + }) + test("should work for non-windows paths", async () => { + expect(handlebarsParse(blankConf, "/home/test/{{name}}.txt", { isPath: true })).toEqual("/home/test/test.txt") + }) }) test("should not do path escaping on non-path compiles", async () => { expect(handlebarsParse(blankConf, "/home/test/{{name}} \\{{escaped}}.txt", { isPath: false })).toEqual(