From 2c6eae29e15650a02384dc28cc3b9d2725ffafe1 Mon Sep 17 00:00:00 2001 From: Gefei Hou Date: Thu, 12 Sep 2024 10:39:52 +0800 Subject: [PATCH] fix: windows support for cca --- packages/create-cosmos-app/src/git-cca-template.ts | 13 +++++++------ packages/create-cosmos-app/src/utils.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/create-cosmos-app/src/git-cca-template.ts b/packages/create-cosmos-app/src/git-cca-template.ts index 0381811d8..cf0d3a5f3 100644 --- a/packages/create-cosmos-app/src/git-cca-template.ts +++ b/packages/create-cosmos-app/src/git-cca-template.ts @@ -4,7 +4,7 @@ import * as c from 'ansi-colors'; import { prompt } from './prompt'; import { join, dirname, basename, sep, relative } from 'path'; import { sync as mkdirp } from 'mkdirp'; -import { sync as glob } from 'glob'; +import { crossGlob as glob, toPosixPath } from './utils'; import * as fs from 'fs'; import { cloneRepo, @@ -14,14 +14,15 @@ import { } from './utils'; import { CCA_URL } from './constants'; +const posixPath = require('path').posix; const requiredTools = ['git', 'yarn']; const motd = (cmd: string, printCmd: boolean) => { const commandSection = printCmd ? `Now, run this command:\n\n${c.bold.whiteBright(cmd)}` : ''; return ` | _ _ - === |.===. '\\-//\` - (o o) {}o o{} (o o) + === |.===. '\\-//\` + (o o) {}o o{} (o o) ooO--(_)--Ooo-ooO--(_)--Ooo-ooO--(_)--Ooo- ✨ Have fun! Now you can start on your project ⚛️ @@ -93,7 +94,7 @@ export const createGitApp = (repo: string, version: string) => { // check version await warnIfOutdated(repo, clonedRepoDir, version); - // get template + // get template const list = shell.ls(`./${folderName}`); const { template } = await prompt([ { @@ -166,7 +167,7 @@ export const createGitApp = (repo: string, version: string) => { // Construct the file path - const relativeFilePath = templateFile.split(join(folderName, template) + sep)[1]; + const relativeFilePath = templateFile.split(toPosixPath(join(folderName, template) + '/'))[1]; // Replace keys in the entire file path const replacedFilePath = Object.keys(results).reduce((filePath, key) => { @@ -196,7 +197,7 @@ export const createGitApp = (repo: string, version: string) => { const closestPkgJson = [] .concat(glob(join(currentDirectory, name, '**', 'package.json'))) .reduce((shortest, current) => { - return current.split(sep).length < shortest.split(sep).length ? current : shortest; + return current.split(posixPath.sep).length < shortest.split(posixPath.sep).length ? current : shortest; }, fakeLongPath); // long string for kicks if (closestPkgJson === fakeLongPath) { diff --git a/packages/create-cosmos-app/src/utils.ts b/packages/create-cosmos-app/src/utils.ts index 55b6ceda5..7fa6f0e1c 100644 --- a/packages/create-cosmos-app/src/utils.ts +++ b/packages/create-cosmos-app/src/utils.ts @@ -3,7 +3,10 @@ import { prompt } from './prompt'; import { join } from 'path'; import { sync as mkdirp } from 'mkdirp'; import { tmpdir } from 'os' +import { sync as globSync } from 'glob'; import * as fs from 'fs'; + +const posixPath = require('path').posix; const dargs = require('dargs'); @@ -130,3 +133,12 @@ export const getPackageLicAndAccessInfo = async (results) => { }; } +// Replace all \\ to / for windows support purpose +export const crossGlob = (input: string, options?: object) => { + return globSync(toPosixPath(input), options); +} + +// Unify all the path to posixPath for windows support purpose +export const toPosixPath = (mixedPath): string => { + return mixedPath.replace(/\\/g, posixPath.sep); +}