Skip to content

Commit

Permalink
Generators: Workaround for POSIX-to-Windows path conversion
Browse files Browse the repository at this point in the history
Closes #574
  • Loading branch information
Tobbe committed Jun 28, 2020
1 parent 7246c6c commit 2af8865
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ test('creates a single-word route name', () => {
const names = ['Home', 'home']

names.forEach((name) => {
expect(page.routes({ name: name, path: 'home' })).toEqual([
'<Route path="home" page={HomePage} name="home" />',
expect(page.routes({ name: name, path: '/' })).toEqual([
'<Route path="/" page={HomePage} name="home" />',
])
})
})
Expand Down
28 changes: 28 additions & 0 deletions packages/cli/src/commands/generate/page/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Listr from 'listr'
import camelcase from 'camelcase'
import pascalcase from 'pascalcase'
import terminalLink from 'terminal-link'
import { execSync } from 'child_process'

import { writeFilesTask, addRoutesToRouterTask } from 'src/lib'
import c from 'src/lib/colors'
Expand Down Expand Up @@ -78,6 +79,33 @@ export const builder = (yargs) => {
}

export const handler = async ({ name, path, force }) => {
if (process.platform === 'win32') {
// running `yarn rw g page home /` on Windows using GitBash
// POSIX-to-Windows path conversion will kick in.
// See https://github.com/git-for-windows/build-extra/blob/d715c9e/ReleaseNotes.md
// As a workaround we try to detect when this has happened, and reverse
// the action

try {
// `cygpath -m /` will return something like 'C:/Program Files/Git/\n'
const slashPath = execSync('cygpath -m /', {
stdio: ['ignore', 'pipe', 'ignore'],
})
.toString()
.trim()

// `yarn rw g page home /` =>
// page === 'C:/Program Files/Git'
// `yarn rw g page about /about` =>
// page === 'C:/Program Files/Git/about'
// Sometimes there is a / after 'Git' to match, sometimes there isn't
path = path.replace(new RegExp(`^${slashPath}?`), '/')
} catch {
// probably using PowerShell or cmd, in which case no special handling
// is needed
}
}

const tasks = new Listr(
[
{
Expand Down

0 comments on commit 2af8865

Please sign in to comment.