Skip to content

Commit c3a1746

Browse files
jkcswillliu
and
willliu
authored
fix: windows gen file (#970)
* fix: windows gen file * fix: win32 * fix: template * add util replacePathSepToSlash --------- Co-authored-by: willliu <[email protected]>
1 parent 0b65530 commit c3a1746

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

Diff for: packages/dev-scripts/examples/gen.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import { fileURLToPath, pathToFileURL } from "node:url";
12
import * as glob from "glob";
23
import * as fs from "node:fs";
34
import * as path from "node:path";
45
import prettier from "prettier";
56
import React from "react";
67
import ReactDOM from "react-dom/server";
7-
import { Project, getExampleProjects, groupProjects } from "./util";
8+
import {
9+
Project,
10+
getExampleProjects,
11+
groupProjects,
12+
replacePathSepToSlash,
13+
} from "./util";
814

915
/**
1016
* This script reads the examples in the /examples folder. These folders initially only need an App.tsx, .bnexample.json and README.md file.
@@ -17,10 +23,10 @@ import { Project, getExampleProjects, groupProjects } from "./util";
1723
* (The downside of this is that we have some almost duplicate, generated files in the repo,
1824
* 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))
1925
*/
20-
const dir = path.parse(import.meta.url.replace("file://", "")).dir;
26+
const dir = path.parse(fileURLToPath(import.meta.url)).dir;
2127

2228
async function writeTemplate(project: Project, templateFile: string) {
23-
const template = await import(templateFile);
29+
const template = await import(pathToFileURL(templateFile).toString());
2430
const ret = await template.default(project);
2531

2632
const targetFilePath = path.join(
@@ -57,7 +63,7 @@ async function writeTemplate(project: Project, templateFile: string) {
5763

5864
async function generateCodeForExample(project: Project) {
5965
const templates = glob.sync(
60-
path.resolve(dir, "./template-react/*.template.tsx")
66+
replacePathSepToSlash(path.resolve(dir, "./template-react/*.template.tsx"))
6167
);
6268

6369
for (const template of templates) {

Diff for: packages/dev-scripts/examples/genDocs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fileURLToPath } from "node:url";
12
import * as fs from "node:fs";
23
import * as path from "node:path";
34
import {
@@ -13,8 +14,7 @@ import {
1314
`genDocs` generates the nextjs example blocks for the website docs.
1415
Note that these files are not checked in to the repo, so this command should always be run before running / building the site
1516
*/
16-
17-
const dir = path.parse(import.meta.url.replace("file://", "")).dir;
17+
const dir = path.parse(fileURLToPath(import.meta.url)).dir;
1818

1919
const getLanguageFromFileName = (fileName: string) => fileName.split(".").pop();
2020

Diff for: packages/dev-scripts/examples/util.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import glob from "fast-glob";
22
import * as fs from "node:fs";
33
import * as path from "node:path";
4+
import { fileURLToPath } from "node:url";
45

5-
const dir = path.parse(import.meta.url.replace("file://", "")).dir;
6+
const dir = path.parse(fileURLToPath(import.meta.url)).dir;
67

78
export type Project = {
89
/**
@@ -114,7 +115,7 @@ export type Files = Record<
114115

115116
export function getProjectFiles(project: Project): Files {
116117
const dir = path.resolve("../../", project.pathFromRoot);
117-
const files = glob.globSync(dir + "/**/*", {
118+
const files = glob.globSync(replacePathSepToSlash(dir + "/**/*"), {
118119
ignore: ["**/node_modules/**/*", "**/dist/**/*"],
119120
});
120121
const passedFiles = Object.fromEntries(
@@ -140,7 +141,11 @@ export function getProjectFiles(project: Project): Files {
140141
*/
141142
export function getExampleProjects(): Project[] {
142143
const examples: Project[] = glob
143-
.globSync(path.join(dir, "../../../examples/**/*/.bnexample.json"))
144+
.globSync(
145+
replacePathSepToSlash(
146+
path.join(dir, "../../../examples/**/*/.bnexample.json")
147+
)
148+
)
144149
.map((configPath) => {
145150
const config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
146151
const directory = path.dirname(configPath);
@@ -162,9 +167,8 @@ export function getExampleProjects(): Project[] {
162167
.split(path.sep);
163168

164169
const group = {
165-
pathFromRoot: path.relative(
166-
path.resolve("../../"),
167-
path.join(directory, "..")
170+
pathFromRoot: replacePathSepToSlash(
171+
path.relative(path.resolve("../../"), path.join(directory, ".."))
168172
),
169173
// remove optional 01- prefix
170174
slug: groupDir.replace(/^\d{2}-/, ""),
@@ -174,7 +178,9 @@ export function getExampleProjects(): Project[] {
174178
const project = {
175179
projectSlug,
176180
fullSlug: `${group.slug}/${projectSlug}`,
177-
pathFromRoot: path.relative(path.resolve("../../"), directory),
181+
pathFromRoot: replacePathSepToSlash(
182+
path.relative(path.resolve("../../"), directory)
183+
),
178184
config,
179185
title,
180186
group,
@@ -196,3 +202,13 @@ export function getExampleProjects(): Project[] {
196202
// });
197203
return examples;
198204
}
205+
206+
export function replacePathSepToSlash(path: string) {
207+
const isExtendedLengthPath = path.startsWith("\\\\?\\");
208+
209+
if (isExtendedLengthPath) {
210+
return path;
211+
}
212+
213+
return path.replace(/\\/g, "/");
214+
}

0 commit comments

Comments
 (0)