From 3964605fa8a8f54c3ae62197257bffb2b9e7133c Mon Sep 17 00:00:00 2001 From: rogueloop Date: Tue, 30 Jul 2024 03:41:18 +0530 Subject: [PATCH 1/2] fix: dev-script failing in windows issue --- packages/dev-scripts/examples/gen.ts | 8 +++++--- packages/dev-scripts/examples/genDocs.ts | 4 +++- packages/dev-scripts/examples/util.ts | 8 +++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/dev-scripts/examples/gen.ts b/packages/dev-scripts/examples/gen.ts index 71f538e59f..02316d9458 100644 --- a/packages/dev-scripts/examples/gen.ts +++ b/packages/dev-scripts/examples/gen.ts @@ -5,7 +5,8 @@ import prettier from "prettier"; import React from "react"; import ReactDOM from "react-dom/server"; import { Project, getExampleProjects, groupProjects } from "./util"; - +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; /** * This script reads the examples in the /examples folder. These folders initially only need an App.tsx, .bnexample.json and README.md file. * @@ -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 = dirname(fileURLToPath(import.meta.url)); async function writeTemplate(project: Project, templateFile: string) { const template = await import(templateFile); @@ -26,7 +27,7 @@ async function writeTemplate(project: Project, templateFile: string) { const targetFilePath = path.join( "../../", project.pathFromRoot, - path.basename(templateFile).replace(".template.tsx", "") + path.basename(templateFile).replace(".template.tsx", "").replace(".template.tsx", "").replace("\\", "/") ); let stringOutput: string | undefined = undefined; @@ -73,6 +74,7 @@ async function generateExamplesData(projects: Project[]) { let code = `// generated by dev-scripts/examples/gen.ts export const examples = ${JSON.stringify(examples, undefined, 2)};`; + code = code.replace(/\\\\/g, '/'); // add as any after deps, otherwise const type inference will be too complex for TS code = code.replace(/("dependencies":\s*{[^}]*})/g, "$1 as any"); diff --git a/packages/dev-scripts/examples/genDocs.ts b/packages/dev-scripts/examples/genDocs.ts index 22f791afdd..ee8233527f 100644 --- a/packages/dev-scripts/examples/genDocs.ts +++ b/packages/dev-scripts/examples/genDocs.ts @@ -8,13 +8,15 @@ import { groupProjects, Project, } from "./util"; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; /* `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 = dirname(fileURLToPath(import.meta.url)); 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..f546abaa61 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 { fileURLToPath } from 'url'; +import { dirname } from 'path'; -const dir = path.parse(import.meta.url.replace("file://", "")).dir; +const dir = dirname(fileURLToPath(import.meta.url)); 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((dir + "/**/*").replace(/\\/g, '/'), { ignore: ["**/node_modules/**/*", "**/dist/**/*"], }); const passedFiles = Object.fromEntries( @@ -140,7 +142,7 @@ export function getProjectFiles(project: Project): Files { */ export function getExampleProjects(): Project[] { const examples: Project[] = glob - .globSync(path.join(dir, "../../../examples/**/*/.bnexample.json")) + .globSync(path.join(dir, "../../../examples/**/*/.bnexample.json").replace(/\\/g, '/')) .map((configPath) => { const config = JSON.parse(fs.readFileSync(configPath, "utf-8")); const directory = path.dirname(configPath); From 38385c481c6092c9090bcbcff41dbdc8fe8c7b0b Mon Sep 17 00:00:00 2001 From: rogueloop Date: Wed, 31 Jul 2024 00:10:08 +0530 Subject: [PATCH 2/2] Fix: fixed files not getting generated issue --- packages/dev-scripts/examples/gen.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/dev-scripts/examples/gen.ts b/packages/dev-scripts/examples/gen.ts index 02316d9458..c86ca0aa96 100644 --- a/packages/dev-scripts/examples/gen.ts +++ b/packages/dev-scripts/examples/gen.ts @@ -5,8 +5,8 @@ import prettier from "prettier"; import React from "react"; import ReactDOM from "react-dom/server"; import { Project, getExampleProjects, groupProjects } from "./util"; -import { fileURLToPath } from 'url'; -import { dirname } from 'path'; +import { fileURLToPath,pathToFileURL } from "url"; +import { dirname } from "path"; /** * This script reads the examples in the /examples folder. These folders initially only need an App.tsx, .bnexample.json and README.md file. * @@ -21,13 +21,17 @@ import { dirname } from 'path'; const dir = dirname(fileURLToPath(import.meta.url)); 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( "../../", project.pathFromRoot, - path.basename(templateFile).replace(".template.tsx", "").replace(".template.tsx", "").replace("\\", "/") + path + .basename(templateFile) + .replace(".template.tsx", "") + .replace(".template.tsx", "") + .replace("\\", "/") ); let stringOutput: string | undefined = undefined; @@ -58,7 +62,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.join(dir, "./template-react/*.template.tsx").replace(/\\/g, '/') ); for (const template of templates) { @@ -74,7 +78,7 @@ async function generateExamplesData(projects: Project[]) { let code = `// generated by dev-scripts/examples/gen.ts export const examples = ${JSON.stringify(examples, undefined, 2)};`; - code = code.replace(/\\\\/g, '/'); + code = code.replace(/\\\\/g, "/"); // add as any after deps, otherwise const type inference will be too complex for TS code = code.replace(/("dependencies":\s*{[^}]*})/g, "$1 as any");