-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Shopify/@cartogram/cli-page-command
- Loading branch information
Showing
10 changed files
with
157 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {page as default} from './page'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {Env, ComponentType} from '../../../types'; | ||
import {componentName, validComponentName} from '../../../utilities'; | ||
|
||
const PAGES_DIRECTORY = 'src/pages'; | ||
|
||
/** | ||
* Scaffold a new Hydrogen page. | ||
*/ | ||
export async function page(env: Env) { | ||
const {ui, fs, workspace} = env; | ||
|
||
const name = await ui.ask('What do you want to name this page?', { | ||
validate: validComponentName, | ||
default: 'Products', | ||
name: 'name', | ||
}); | ||
|
||
const url = await ui.ask('What is the url path to this page?', { | ||
default: '/products', | ||
name: 'name', | ||
}); | ||
|
||
const urlSegments = url.split('/'); | ||
const lastUrlSegment = urlSegments.pop() || ''; | ||
const extension = (await workspace.isTypeScript) ? 'tsx' : 'jsx'; | ||
|
||
const path = fs.join( | ||
workspace.root(), | ||
PAGES_DIRECTORY, | ||
...urlSegments, | ||
componentName(lastUrlSegment, ComponentType.Server, extension) | ||
); | ||
|
||
fs.write( | ||
path, | ||
(await import('./templates/page-jsx')).default({ | ||
name, | ||
path: fs.join( | ||
PAGES_DIRECTORY, | ||
componentName(url, ComponentType.Server, extension) | ||
), | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default function ({name, path}: {name: string; path: string}) { | ||
return ` | ||
export default function ${name}({request, response, ...serverState}) { | ||
return ( | ||
<div>${name} component at \`${path}\`</div> | ||
); | ||
} | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {withCli} from '../../../../testing'; | ||
|
||
describe('page', () => { | ||
it('scaffolds a basic JSX page with a name', async () => { | ||
await withCli(async ({run, fs}) => { | ||
await run('create page', { | ||
name: 'Products', | ||
url: 'products', | ||
}); | ||
|
||
expect(await fs.read('src/pages/products.server.jsx')).toBe( | ||
`export default function Products({request, response, ...serverState}) { | ||
return ( | ||
<div>Products component at \`src/pages/products.server.jsx\`</div> | ||
); | ||
} | ||
` | ||
); | ||
}); | ||
}); | ||
|
||
it('scaffolds a basic TSX page with a name when a tsconfig exists', async () => { | ||
await withCli(async ({run, fs}) => { | ||
await fs.write('tsconfig.json', JSON.stringify({}, null, 2)); | ||
await run('create page', { | ||
name: 'Collections', | ||
url: 'collections', | ||
}); | ||
|
||
expect(await fs.read('src/pages/collections.server.tsx')).toBe( | ||
`export default function Collections({request, response, ...serverState}) { | ||
return ( | ||
<div>Collections component at \`src/pages/collections.server.tsx\`</div> | ||
); | ||
} | ||
` | ||
); | ||
}); | ||
}); | ||
|
||
it('supports nested and dynamic components', async () => { | ||
await withCli(async ({run, fs}) => { | ||
await fs.write('tsconfig.json', JSON.stringify({}, null, 2)); | ||
await run('create page', { | ||
name: 'ProductDetails', | ||
url: 'products/[handle]', | ||
}); | ||
|
||
expect(await fs.read('src/pages/products/[handle].server.tsx')).toBe( | ||
`export default function ProductDetails({request, response, ...serverState}) { | ||
return ( | ||
<div>ProductDetails component at \`src/pages/products/[handle].server.tsx\`</div> | ||
); | ||
} | ||
` | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {pascalCase} from 'change-case'; | ||
import {ComponentType} from '../types'; | ||
|
||
function getReactComponentTypeSuffix(component: ComponentType) { | ||
switch (component) { | ||
case ComponentType.Client: | ||
return 'client'; | ||
case ComponentType.Server: | ||
return 'server'; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
export function componentName( | ||
name: string, | ||
type: ComponentType, | ||
extension: string | ||
) { | ||
return [name, getReactComponentTypeSuffix(type), extension] | ||
.filter((fp) => fp) | ||
.join('.'); | ||
} | ||
|
||
export function validComponentName(name: string) { | ||
const suggested = pascalCase(name); | ||
if (name === suggested) { | ||
return true; | ||
} | ||
|
||
return `Invalid component name. Try ${suggested} instead.`; | ||
} |