From 264bbdbc923bb234eea2a0ce11d9c4c913a2e33e Mon Sep 17 00:00:00 2001 From: willliu Date: Tue, 30 Jul 2024 10:33:48 +0800 Subject: [PATCH 1/4] fix: windows gen file --- packages/dev-scripts/examples/gen.ts | 3 ++- packages/dev-scripts/examples/genDocs.ts | 4 ++-- packages/dev-scripts/examples/util.ts | 19 ++++++++++++------- packages/dev-scripts/package.json | 1 + 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/dev-scripts/examples/gen.ts b/packages/dev-scripts/examples/gen.ts index 71f538e59f..5f6d31ec40 100644 --- a/packages/dev-scripts/examples/gen.ts +++ b/packages/dev-scripts/examples/gen.ts @@ -1,3 +1,4 @@ +import { fileURLToPath } from "node:url"; import * as glob from "glob"; import * as fs from "node:fs"; import * as path from "node:path"; @@ -17,7 +18,7 @@ import { Project, getExampleProjects, groupProjects } from "./util"; * (The downside of this is that we have some almost duplicate, generated files in the repo, * but the upside is anyone can run npm start in any of the examples (and that we can point a codesandbox / repl to the examples directory)) */ -const dir = path.parse(import.meta.url.replace("file://", "")).dir; +const dir = path.parse(fileURLToPath(import.meta.url)).dir; async function writeTemplate(project: Project, templateFile: string) { const template = await import(templateFile); diff --git a/packages/dev-scripts/examples/genDocs.ts b/packages/dev-scripts/examples/genDocs.ts index 22f791afdd..34063d4328 100644 --- a/packages/dev-scripts/examples/genDocs.ts +++ b/packages/dev-scripts/examples/genDocs.ts @@ -1,3 +1,4 @@ +import { fileURLToPath } from "node:url"; import * as fs from "node:fs"; import * as path from "node:path"; import { @@ -13,8 +14,7 @@ import { `genDocs` generates the nextjs example blocks for the website docs. Note that these files are not checked in to the repo, so this command should always be run before running / building the site */ - -const dir = path.parse(import.meta.url.replace("file://", "")).dir; +const dir = path.parse(fileURLToPath(import.meta.url)).dir; const getLanguageFromFileName = (fileName: string) => fileName.split(".").pop(); diff --git a/packages/dev-scripts/examples/util.ts b/packages/dev-scripts/examples/util.ts index f3b500a7c8..657b07e0c2 100644 --- a/packages/dev-scripts/examples/util.ts +++ b/packages/dev-scripts/examples/util.ts @@ -1,8 +1,10 @@ import glob from "fast-glob"; import * as fs from "node:fs"; import * as path from "node:path"; +import slash from "slash"; +import { fileURLToPath } from "node:url"; -const dir = path.parse(import.meta.url.replace("file://", "")).dir; +const dir = path.parse(fileURLToPath(import.meta.url)).dir; export type Project = { /** @@ -114,7 +116,7 @@ export type Files = Record< export function getProjectFiles(project: Project): Files { const dir = path.resolve("../../", project.pathFromRoot); - const files = glob.globSync(dir + "/**/*", { + const files = glob.globSync(glob.convertPathToPattern(dir + "/**/*"), { ignore: ["**/node_modules/**/*", "**/dist/**/*"], }); const passedFiles = Object.fromEntries( @@ -140,7 +142,11 @@ export function getProjectFiles(project: Project): Files { */ export function getExampleProjects(): Project[] { const examples: Project[] = glob - .globSync(path.join(dir, "../../../examples/**/*/.bnexample.json")) + .globSync( + glob.convertPathToPattern( + path.join(dir, "../../../examples/**/*/.bnexample.json") + ) + ) .map((configPath) => { const config = JSON.parse(fs.readFileSync(configPath, "utf-8")); const directory = path.dirname(configPath); @@ -162,9 +168,8 @@ export function getExampleProjects(): Project[] { .split(path.sep); const group = { - pathFromRoot: path.relative( - path.resolve("../../"), - path.join(directory, "..") + pathFromRoot: slash( + path.relative(path.resolve("../../"), path.join(directory, "..")) ), // remove optional 01- prefix slug: groupDir.replace(/^\d{2}-/, ""), @@ -174,7 +179,7 @@ export function getExampleProjects(): Project[] { const project = { projectSlug, fullSlug: `${group.slug}/${projectSlug}`, - pathFromRoot: path.relative(path.resolve("../../"), directory), + pathFromRoot: slash(path.relative(path.resolve("../../"), directory)), config, title, group, diff --git a/packages/dev-scripts/package.json b/packages/dev-scripts/package.json index 1d490b8295..6b680d2395 100644 --- a/packages/dev-scripts/package.json +++ b/packages/dev-scripts/package.json @@ -22,6 +22,7 @@ "prettier": "^2.7.1", "react": "^18", "react-dom": "^18", + "slash": "^5.1.0", "tsx": "^4.6.2", "typescript": "^5.3.3" }, From 5b33e18e94fbb37dd50e4cfe40f8e32764556cf1 Mon Sep 17 00:00:00 2001 From: willliu Date: Tue, 30 Jul 2024 10:57:36 +0800 Subject: [PATCH 2/4] fix: win32 --- packages/dev-scripts/examples/util.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/dev-scripts/examples/util.ts b/packages/dev-scripts/examples/util.ts index 657b07e0c2..7f20a1dab2 100644 --- a/packages/dev-scripts/examples/util.ts +++ b/packages/dev-scripts/examples/util.ts @@ -116,7 +116,7 @@ export type Files = Record< export function getProjectFiles(project: Project): Files { const dir = path.resolve("../../", project.pathFromRoot); - const files = glob.globSync(glob.convertPathToPattern(dir + "/**/*"), { + const files = glob.globSync(glob.win32.convertPathToPattern(dir + "/**/*"), { ignore: ["**/node_modules/**/*", "**/dist/**/*"], }); const passedFiles = Object.fromEntries( @@ -143,7 +143,7 @@ export function getProjectFiles(project: Project): Files { export function getExampleProjects(): Project[] { const examples: Project[] = glob .globSync( - glob.convertPathToPattern( + glob.win32.convertPathToPattern( path.join(dir, "../../../examples/**/*/.bnexample.json") ) ) From e7cd01514f3713eef0d23051ec3c0ca5bed517f1 Mon Sep 17 00:00:00 2001 From: jkcs <1778768609@qq.com> Date: Tue, 30 Jul 2024 12:01:26 +0800 Subject: [PATCH 3/4] fix: template --- packages/dev-scripts/examples/gen.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/dev-scripts/examples/gen.ts b/packages/dev-scripts/examples/gen.ts index 5f6d31ec40..60c1d700e1 100644 --- a/packages/dev-scripts/examples/gen.ts +++ b/packages/dev-scripts/examples/gen.ts @@ -1,4 +1,4 @@ -import { fileURLToPath } from "node:url"; +import { fileURLToPath, pathToFileURL } from "node:url"; import * as glob from "glob"; import * as fs from "node:fs"; import * as path from "node:path"; @@ -21,7 +21,7 @@ import { Project, getExampleProjects, groupProjects } from "./util"; const dir = path.parse(fileURLToPath(import.meta.url)).dir; async function writeTemplate(project: Project, templateFile: string) { - const template = await import(templateFile); + const template = await import(pathToFileURL(templateFile).toString()); const ret = await template.default(project); const targetFilePath = path.join( @@ -58,7 +58,7 @@ async function writeTemplate(project: Project, templateFile: string) { async function generateCodeForExample(project: Project) { const templates = glob.sync( - path.resolve(dir, "./template-react/*.template.tsx") + path.resolve(dir, "./template-react/*.template.tsx").replace(/\\/g, "/") ); for (const template of templates) { From 192e79d5da2b3ded7e91cb39d13a698477c6390a Mon Sep 17 00:00:00 2001 From: jkcs <1778768609@qq.com> Date: Thu, 1 Aug 2024 16:04:38 +0800 Subject: [PATCH 4/4] add util replacePathSepToSlash --- packages/dev-scripts/examples/gen.ts | 9 +++++++-- packages/dev-scripts/examples/util.ts | 21 ++++++++++++++++----- packages/dev-scripts/package.json | 1 - 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/dev-scripts/examples/gen.ts b/packages/dev-scripts/examples/gen.ts index 60c1d700e1..41226b6994 100644 --- a/packages/dev-scripts/examples/gen.ts +++ b/packages/dev-scripts/examples/gen.ts @@ -5,7 +5,12 @@ import * as path from "node:path"; import prettier from "prettier"; import React from "react"; import ReactDOM from "react-dom/server"; -import { Project, getExampleProjects, groupProjects } from "./util"; +import { + Project, + getExampleProjects, + groupProjects, + replacePathSepToSlash, +} from "./util"; /** * This script reads the examples in the /examples folder. These folders initially only need an App.tsx, .bnexample.json and README.md file. @@ -58,7 +63,7 @@ async function writeTemplate(project: Project, templateFile: string) { async function generateCodeForExample(project: Project) { const templates = glob.sync( - path.resolve(dir, "./template-react/*.template.tsx").replace(/\\/g, "/") + replacePathSepToSlash(path.resolve(dir, "./template-react/*.template.tsx")) ); for (const template of templates) { diff --git a/packages/dev-scripts/examples/util.ts b/packages/dev-scripts/examples/util.ts index 7f20a1dab2..78e1040a23 100644 --- a/packages/dev-scripts/examples/util.ts +++ b/packages/dev-scripts/examples/util.ts @@ -1,7 +1,6 @@ import glob from "fast-glob"; import * as fs from "node:fs"; import * as path from "node:path"; -import slash from "slash"; import { fileURLToPath } from "node:url"; const dir = path.parse(fileURLToPath(import.meta.url)).dir; @@ -116,7 +115,7 @@ export type Files = Record< export function getProjectFiles(project: Project): Files { const dir = path.resolve("../../", project.pathFromRoot); - const files = glob.globSync(glob.win32.convertPathToPattern(dir + "/**/*"), { + const files = glob.globSync(replacePathSepToSlash(dir + "/**/*"), { ignore: ["**/node_modules/**/*", "**/dist/**/*"], }); const passedFiles = Object.fromEntries( @@ -143,7 +142,7 @@ export function getProjectFiles(project: Project): Files { export function getExampleProjects(): Project[] { const examples: Project[] = glob .globSync( - glob.win32.convertPathToPattern( + replacePathSepToSlash( path.join(dir, "../../../examples/**/*/.bnexample.json") ) ) @@ -168,7 +167,7 @@ export function getExampleProjects(): Project[] { .split(path.sep); const group = { - pathFromRoot: slash( + pathFromRoot: replacePathSepToSlash( path.relative(path.resolve("../../"), path.join(directory, "..")) ), // remove optional 01- prefix @@ -179,7 +178,9 @@ export function getExampleProjects(): Project[] { const project = { projectSlug, fullSlug: `${group.slug}/${projectSlug}`, - pathFromRoot: slash(path.relative(path.resolve("../../"), directory)), + pathFromRoot: replacePathSepToSlash( + path.relative(path.resolve("../../"), directory) + ), config, title, group, @@ -201,3 +202,13 @@ export function getExampleProjects(): Project[] { // }); return examples; } + +export function replacePathSepToSlash(path: string) { + const isExtendedLengthPath = path.startsWith("\\\\?\\"); + + if (isExtendedLengthPath) { + return path; + } + + return path.replace(/\\/g, "/"); +} diff --git a/packages/dev-scripts/package.json b/packages/dev-scripts/package.json index 6b680d2395..1d490b8295 100644 --- a/packages/dev-scripts/package.json +++ b/packages/dev-scripts/package.json @@ -22,7 +22,6 @@ "prettier": "^2.7.1", "react": "^18", "react-dom": "^18", - "slash": "^5.1.0", "tsx": "^4.6.2", "typescript": "^5.3.3" },