From 368b7cceee2185c4518cc0df9b6ef2018b1a0e56 Mon Sep 17 00:00:00 2001 From: ascorbic Date: Thu, 22 Feb 2024 09:40:49 +0000 Subject: [PATCH] chore: refactor to move lambda-specific files into init script --- README.md | 2 +- package.json | 8 ++-- remix.init/edge/README.md | 8 +--- remix.init/{ => functions}/README.md | 0 .../functions/vite.config.ts | 0 remix.init/index.js | 40 ++++++++++--------- 6 files changed, 29 insertions(+), 29 deletions(-) rename remix.init/{ => functions}/README.md (100%) rename vite.config.ts => remix.init/functions/vite.config.ts (100%) diff --git a/README.md b/README.md index 16f0b70..3737744 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Netlify Remix Template -Welcome to the Netlify Remix Template project. +Welcome to the Netlify Remix Template project. If you were expecting this to be your site, run `remix init` in the root of this project to get started. To use the template, run diff --git a/package.json b/package.json index 89b0191..ef7d150 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,10 @@ "sideEffects": false, "type": "module", "scripts": { - "build": "remix vite:build", - "dev": "remix vite:dev", + "build": "remix init && remix vite:build", + "dev": "remix init && remix vite:dev", "lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .", - "start": "netlify serve", + "start": "remix init && netlify serve", "typecheck": "tsc" }, "dependencies": { @@ -42,4 +42,4 @@ "engines": { "node": ">=18.0.0" } -} +} \ No newline at end of file diff --git a/remix.init/edge/README.md b/remix.init/edge/README.md index 52a6a52..8b950e2 100644 --- a/remix.init/edge/README.md +++ b/remix.init/edge/README.md @@ -43,18 +43,14 @@ Run netlify dev ``` -Open up [http://localhost:3000](http://localhost:3000), and you're ready to go! - -### Adding Redirects and Rewrites - -To add redirects and rewrites, add them to the `netlify.toml` file. For more information about redirects and rewrites, see the [Netlify docs](https://docs.netlify.com/routing/redirects/). +Open up [http://localhost:8888](http://localhost:8888), and you're ready to go! ### Serve your site locally To serve your site locally in a production-like environment, run ```sh -npm run start +netlify serve ``` Your site will be available at [http://localhost:8888](http://localhost:8888). Note that it will not auto-reload when you make changes. diff --git a/remix.init/README.md b/remix.init/functions/README.md similarity index 100% rename from remix.init/README.md rename to remix.init/functions/README.md diff --git a/vite.config.ts b/remix.init/functions/vite.config.ts similarity index 100% rename from vite.config.ts rename to remix.init/functions/vite.config.ts diff --git a/remix.init/index.js b/remix.init/index.js index c9ae6ed..c58d16f 100644 --- a/remix.init/index.js +++ b/remix.init/index.js @@ -41,15 +41,25 @@ async function mergeDirs(src, dest) { } } -async function copyTemplateFiles(rootDirectory) { - const source = join(rootDirectory, "remix.init", "edge"); +async function copyTemplateFiles(rootDirectory, useEdge) { + const source = join( + rootDirectory, + "remix.init", + useEdge ? "edge" : "functions" + ); await mergeDirs(source, rootDirectory); } -async function removeDependencies(directory, dependencies) { +async function removeUpdatePackageJson(directory, dependencies) { const packageJson = await PackageJson.load(directory); - const { dependencies: currentDependencies = {} } = packageJson.content; + const { dependencies: currentDependencies = {}, scripts } = + packageJson.content; + + // Remove the auto-init command from the scripts + for (const script of ["build", "start", "dev"]) { + scripts[script] = scripts[script]?.replace("remix init && ", ""); + } packageJson.update({ dependencies: Object.fromEntries( @@ -94,7 +104,7 @@ async function installAdditionalDependencies({ } catch (e) { return false; } - // return true; + return true; } async function shouldUseEdge(rootDirectory) { @@ -120,10 +130,9 @@ async function shouldUseEdge(rootDirectory) { }); if (isCancel(projectType)) { cancel( - `Project setup cancelled. Run remix init inside "${relative( - process.cwd(), - rootDirectory - )}" to complete setup.` + `Project setup cancelled. Run remix init inside ${ + relative(process.cwd(), rootDirectory) || "this folder" + } to complete setup.` ); process.exit(1); } @@ -134,25 +143,20 @@ async function shouldUseEdge(rootDirectory) { async function main({ rootDirectory, packageManager }) { intro(`Welcome to Remix on Netlify`); - + console.log("rootDirectory", rootDirectory); const useEdge = await shouldUseEdge(rootDirectory); const spin = spinner(); spin.start("Setting up your project"); - if (useEdge) { - await copyTemplateFiles(rootDirectory); - } + await copyTemplateFiles(rootDirectory, useEdge); await removeNonTemplateFiles({ rootDirectory, folders: foldersToRemove, }); - await fs.copyFile( - join(rootDirectory, "remix.init", "README.md"), - join(rootDirectory, "README.md") - ); + spin.stop("Setup complete"); spin.start("Updating dependencies"); - await removeDependencies( + await removeUpdatePackageJson( rootDirectory, packagesToRemove[useEdge ? "edge" : "functions"] );