Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Fix Windows support #101

Merged
merged 1 commit into from
Nov 24, 2020
Merged

Fix Windows support #101

merged 1 commit into from
Nov 24, 2020

Conversation

ehmicky
Copy link
Contributor

@ehmicky ehmicky commented Nov 23, 2020

Fixes #88.

This adds support for Windows. This fixes tests on Windows, now running them in CI.
Many of Netlify CLI users are on Windows (example: #56 (comment)).

@ehmicky ehmicky self-assigned this Nov 23, 2020
@ehmicky ehmicky added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Nov 23, 2020
@ehmicky ehmicky marked this pull request as ready for review November 23, 2020 19:46
@@ -1,10 +1,10 @@
{
"name": "next-on-netlify-test",
"scripts": {
"build": "../../../node_modules/.bin/next build",
Copy link
Contributor Author

@ehmicky ehmicky Nov 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relies on shebangs, which do not work on Windows.
This PR instead relies on execa, which adds shebangs support for Windows.
Note: I co-maintain execa.


// Build the given NextJS project
const buildProject = ({ project }, config) => {
process.stdout.write(`Building project: ${project}...`);

// Build project
spawnSync("npm", ["run", "build"], {
execa.sync("npm", ["run", "build"], {
Copy link
Contributor Author

@ehmicky ehmicky Nov 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling npm with spawn() does not work as intended on Windows without the shell option.
execa handles this case automatically though, providing with Windows support.

cwd: join(config.buildsFolder, project),
preferLocal: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/sindresorhus/execa#preferlocal
This removes the need for ../node_modules/.bin/....

cwd: join(config.buildsFolder, project),
encoding: "utf-8",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -10,8 +10,7 @@ const buildId = fileContents.toString();
// Return the data route for the given route
const getDataRouteForRoute = (route) => {
const filePath = getFilePathForRoute(route, "json");

return join("/_next", "data", buildId, filePath);
return `/_next/data/${buildId}${filePath}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paths in the _redirects file must use forward slashes. However, path.join() use backslashes on Windows.
Note that filePath is not prepended with /, otherwise this would create a double slash (which was previously normalized thanks to join().

@@ -40,10 +41,11 @@ describe("Routing", () => {
test("includes custom redirect rules", async () => {
// Read _redirects file
const contents = readFileSync(
join(PROJECT_PATH, "out_publish", "_redirects")
join(PROJECT_PATH, "out_publish", "_redirects"),
"utf8"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the need for toString().

);

const redirects = contents.toString().trim().split(/\n/);
const redirects = contents.trim().split(EOL);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows, this is \r\n.

expect(buildOutput).toMatch(
"Setting up API endpoints as Netlify Functions in out_functions/"
`Copying public${sep} folder to out_publish${sep}`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File paths are normalized with join() in the source code, so those messages print backslashes on Windows.

});
} catch (error) {
throw new Error(
`An error occurred during "npm run ${command}" in ${fromDirectory}: ${error.message}`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error.message includes stdout and stderr with Execa.

@ehmicky ehmicky mentioned this pull request Nov 23, 2020
Copy link
Contributor

@FinnWoelm FinnWoelm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ehmicky, this is amazing!! 🎉🎉🎉 Fantastic work, great to see execa in action, and thanks for all your detailed comments.

I checked it out on my (non-Windows) machine and the tests run very well 👍

PS: On a tangential note: If you have a better idea for how to handle the server-starting and -killing for these Cypress tests, I'd be all ears!

@ehmicky ehmicky merged commit 42b1444 into main Nov 24, 2020
@ehmicky ehmicky deleted the feat/windows branch November 24, 2020 11:57
lindsaylevine added a commit that referenced this pull request Jan 3, 2021
- Support for i18n in Next 10 ([#75](#75))
- dependabot: node-notifier from 8.0.0 to 8.0.1 ([#125](#125))
- Expose Netlify function params as netlifyFunctionParams ([#119](#119))
- Fix: local cypress cache control ([#118](#118))
- Fix: add res.finished to createResponseObject ([#117](#117))
- Fix: Windows support ([#101](#101))
- Improve logs for specified functions/publish dirs ([#100](#100))
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Windows support
2 participants