Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dev-script failing in windows issue #969

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions packages/dev-scripts/examples/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,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.
*
Expand All @@ -17,16 +18,20 @@ 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);
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", "")
path
.basename(templateFile)
.replace(".template.tsx", "")
.replace(".template.tsx", "")
.replace("\\", "/")
);

let stringOutput: string | undefined = undefined;
Expand Down Expand Up @@ -57,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) {
Expand All @@ -73,6 +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, "/");

// add as any after deps, otherwise const type inference will be too complex for TS
code = code.replace(/("dependencies":\s*{[^}]*})/g, "$1 as any");
Expand Down
4 changes: 3 additions & 1 deletion packages/dev-scripts/examples/genDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 5 additions & 3 deletions packages/dev-scripts/examples/util.ts
Original file line number Diff line number Diff line change
@@ -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 = {
/**
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand Down