Skip to content

Commit

Permalink
chore: refactor to move lambda-specific files into init script
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Feb 22, 2024
1 parent f316daa commit 368b7cc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -42,4 +42,4 @@
"engines": {
"node": ">=18.0.0"
}
}
}
8 changes: 2 additions & 6 deletions remix.init/edge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
File renamed without changes.
File renamed without changes.
40 changes: 22 additions & 18 deletions remix.init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -94,7 +104,7 @@ async function installAdditionalDependencies({
} catch (e) {
return false;
}
// return true;
return true;
}

async function shouldUseEdge(rootDirectory) {
Expand All @@ -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);
}
Expand All @@ -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"]
);
Expand Down

0 comments on commit 368b7cc

Please sign in to comment.