-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(playground): add support for generating a new playground #2994
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
e14eb22
Add prompts and generate vue file
mapsandapps 61e5dc3
Generate other files
mapsandapps 62003d8
Merge branch 'main' into playground-hygen
mapsandapps af3ab6b
Use variables in headers
mapsandapps e648bc5
Add readme
mapsandapps 9fe4970
Add prompt with option to generate css files
mapsandapps 7f892c4
Add version to prompt and modify files accordingly
mapsandapps e550592
Simplify syntax
mapsandapps bce58e7
Update readme
mapsandapps 0c56e30
Refactor
mapsandapps 94e0d5d
Remove unnecessary code
mapsandapps be0ce65
Update comments
mapsandapps b886304
Adjust index and demo for css vs no css
mapsandapps a322b38
Clarify comments
mapsandapps 9905e53
Fix relative paths
mapsandapps be300d2
A couple fixes for css
mapsandapps e02db6e
Apply suggestions from code review
mapsandapps 9aa8d83
Address PR review
mapsandapps 5c37cb7
Address PR review
mapsandapps 7bb71b3
Add hint for component name casing
mapsandapps f04be20
Add Angular TS option
mapsandapps 705002b
Reformat to use advanced prompting
mapsandapps 8e9df52
Refactor where markdown output comes from
mapsandapps e96c425
Remove stuff I forgot to remove
mapsandapps ffc5df3
Remove default value for path; add validation
mapsandapps 4c041cb
Update copy
mapsandapps eaf5fd1
Improve validation of path
mapsandapps ecc4d1a
Add validation for component name
mapsandapps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Hygen templates | ||
|
||
The templates in this directory are intended to be used with [hygen](https://www.hygen.io/) to generate boilerplate files. Check out [the root package.json](../package.json) to see if there are any custom commands to use them (e.g. `npm run playground:new`). You can also run e.g. `hygen playground new` to use a generator. | ||
|
||
Some helpful docs links for updating/creating templates: | ||
|
||
- [enquirer](https://github.com/enquirer/enquirer#toggle-prompt) for building command line prompts | ||
- [inflection](https://www.hygen.io/docs/templates#helpers-and-inflections) and [change case](https://www.hygen.io/docs/templates#change-case-helpers) for e.g. changing the case of variables submitted via the prompts | ||
|
||
# New playground template | ||
|
||
## Generation | ||
|
||
To create a new playground, run `npm run playground:new`. This will walk you through some prompts to decide what files for the generator to create for the playground, and what their paths should be. | ||
|
||
The path defaults to `basic`. If there is already a basic playground, you'll want to input a different path for the playground. | ||
|
||
The CSS option will add extra files if you need to include custom CSS in your playground. | ||
|
||
If you need a component for multiple versions of Ionic Framework, you (currently) need to run the generator once for each version. | ||
|
||
## Usage | ||
|
||
Once you've generated your playground, you need to add it to the main markdown file in the docs (e.g. [docs/api/button.md](../docs/api/button.md)) by doing something similar to the following example: | ||
|
||
``` | ||
## Feature | ||
|
||
import Feature from '@site/static/usage/v7/button/feature/index.md'; | ||
|
||
<Feature /> | ||
``` |
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,7 @@ | ||
--- | ||
# this file's location depends on whether or not the css option or angular_ts option is selected via the prompt | ||
to: "<%= `static/usage/v${version}/${name.replace('ion-', '')}/${path}/${(css || angular_ts) ? 'angular/example_component_html.md' : 'angular.md'}` %>" | ||
--- | ||
```html | ||
<<%= name %>></<%= name %>> | ||
``` |
9 changes: 9 additions & 0 deletions
9
_templates/playground/new/angular_example_component_css.md.ejs.t
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 @@ | ||
--- | ||
# this file only gets generated if `css` (from the command line prompt) is true | ||
to: "<%= css ? `static/usage/v${version}/${name.replace('ion-', '')}/${path}/angular/example_component_css.md` : null %>" | ||
--- | ||
```css | ||
<%= name %> { | ||
/* styles go here */ | ||
} | ||
``` |
15 changes: 15 additions & 0 deletions
15
_templates/playground/new/angular_example_component_ts.md.ejs.t
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,15 @@ | ||
--- | ||
# this file only gets generated if `angular_ts` (from the command line prompt) is true | ||
to: "<%= angular_ts ? `static/usage/v${version}/${name.replace('ion-', '')}/${path}/angular/example_component_ts.md` : null %>" | ||
--- | ||
```ts | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-example', | ||
templateUrl: 'example.component.html',<% if (css){ %> | ||
styleUrls: ['./example.component.css'],<% } %> | ||
}) | ||
export class ExampleComponent { | ||
} | ||
``` |
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 @@ | ||
--- | ||
arbitrary: <% nameWithoutIon = name.replace('ion-', ''); numberOfAncestors = (path.match(/\//g) || []).length; directoryChanges = '../'.repeat(numberOfAncestors) %> | ||
to: "<%= `static/usage/v${version}/${nameWithoutIon}/${path}/demo.html` %>" | ||
--- | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title><%= h.changeCase.titleCase(nameWithoutIon) %></title> | ||
<link rel="stylesheet" href="<%= directoryChanges %>../../../common.css" /> | ||
<script src="<%= directoryChanges %>../../../common.js"></script> | ||
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps there's a way to do this without using relative paths? I couldn't quickly find one. |
||
<script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@<%= version %>/dist/ionic/ionic.esm.js"></script> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@<%= version %>/css/ionic.bundle.css" /><% if (css){ %> | ||
|
||
<style> | ||
<%= name %> { | ||
/* styles go here */ | ||
} | ||
</style><% } %> | ||
</head> | ||
|
||
<body> | ||
<ion-app> | ||
<ion-content> | ||
<div class="container"> | ||
<<%= name %>></<%= name %>> | ||
</div> | ||
</ion-content> | ||
</ion-app> | ||
</body> | ||
</html> |
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,64 @@ | ||
const changeCase = require('change-case'); | ||
|
||
// see types of prompts: | ||
// https://github.com/enquirer/enquirer/tree/master/examples | ||
// | ||
module.exports = { | ||
prompt: ({ inquirer }) => { | ||
const questions = [ | ||
{ | ||
type: 'input', | ||
name: 'name', | ||
message: 'Which component is this playground for?', | ||
initial: 'ion-button', | ||
validate(value) { | ||
return value.match(/^ion-[a-z/-]*[a-z]+$/) ? true : "Component name must be kebab-case and begin with 'ion-'"; | ||
}, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'path', | ||
message: 'What should the playground path be?', | ||
hint: 'e.g. `basic` or `theming/colors`', | ||
validate(value) { | ||
return value.match(/^[a-z]+[a-z/-]*[a-z]+$/) | ||
? true | ||
: "Path should begin and end with a letter and only contain lowercase letters, '-', or '/'"; | ||
}, | ||
}, | ||
{ | ||
type: 'select', | ||
name: 'version', | ||
message: 'Select the Ionic Framework version for the playground', | ||
initial: '7', | ||
choices: ['6', '7'], | ||
}, | ||
{ | ||
type: 'toggle', | ||
name: 'css', | ||
message: 'Generate custom CSS files?', | ||
enabled: 'Yes', | ||
disabled: 'No', | ||
}, | ||
{ | ||
type: 'toggle', | ||
name: 'angular_ts', | ||
message: 'Generate an Angular TypeScript file?', | ||
enabled: 'Yes', | ||
disabled: 'No', | ||
}, | ||
]; | ||
|
||
return inquirer.prompt(questions).then((answers) => { | ||
const componentName = changeCase.pascal(answers.path.split('/').pop()); | ||
console.log( | ||
`\nTo use this component in a docs markdown file, include\nthe following:\n\n## ${componentName}\n\nimport ${componentName} from '@site/static/usage/v7/${answers.name.replace( | ||
'ion-', | ||
'' | ||
)}/${answers.path}/index.md';\n\n<${componentName} />\n` | ||
); | ||
|
||
return answers; | ||
}); | ||
}, | ||
}; |
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,13 @@ | ||
--- | ||
arbitrary: <% nameWithoutIon = name.replace('ion-', '') %> | ||
# this template is only used if `css` (from the command line prompt) is false | ||
to: "<%= css ? null : `static/usage/v${version}/${nameWithoutIon}/${path}/index.md` %>" | ||
--- | ||
import Playground from '@site/src/components/global/Playground'; | ||
|
||
import javascript from './javascript.md'; | ||
import react from './react.md'; | ||
import vue from './vue.md'; | ||
import angular from './angular.md'; | ||
|
||
<Playground version="<%= version %>" code={{ javascript, react, vue, angular }} src="<%= `usage/v${version}/${nameWithoutIon}/${path}/demo.html` %>" /> |
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,37 @@ | ||
--- | ||
arbitrary: <% nameWithoutIon = name.replace('ion-', '') %> | ||
# this template is only used if `css` (from the command line prompt) is true | ||
to: "<%= css ? `static/usage/v${version}/${nameWithoutIon}/${path}/index.md` : null %>" | ||
--- | ||
import Playground from '@site/src/components/global/Playground'; | ||
|
||
import javascript from './javascript.md'; | ||
|
||
import react_main_tsx from './react/main_tsx.md'; | ||
import react_main_css from './react/main_css.md'; | ||
|
||
import vue from './vue.md'; | ||
|
||
import angular_example_component_html from './angular/example_component_html.md'; | ||
import angular_example_component_css from './angular/example_component_css.md'; | ||
|
||
<Playground | ||
version="<%= version %>" | ||
code={{ | ||
javascript, | ||
react: { | ||
files: { | ||
'src/main.tsx': react_main_tsx, | ||
'src/main.css': react_main_css, | ||
}, | ||
}, | ||
vue, | ||
angular: { | ||
files: { | ||
'src/app/example.component.html': angular_example_component_html, | ||
'src/app/example.component.css': angular_example_component_css, | ||
}, | ||
}, | ||
}} | ||
src="<%= `usage/v${version}/${nameWithoutIon}/${path}/demo.html` %>" | ||
/> |
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,12 @@ | ||
--- | ||
to: "<%= `static/usage/v${version}/${name.replace('ion-', '')}/${path}/javascript.md` %>" | ||
--- | ||
```html | ||
<<%= name %>></<%= name %>><% if (css){ %> | ||
|
||
<style> | ||
<%= name %> { | ||
/* styles go here */ | ||
} | ||
</style><% } %> | ||
``` |
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,19 @@ | ||
--- | ||
arbitrary: <% pascalName = h.changeCase.pascal(name) %> | ||
# this file's location depends on whether or not the css option is selected via the prompt | ||
to: "<%= `static/usage/v${version}/${name.replace('ion-', '')}/${path}/${css ? 'react/main_tsx.md' : 'react.md'}` %>" | ||
--- | ||
```tsx | ||
import React from 'react'; | ||
import { <%= pascalName %> } from '@ionic/react';<% if (css){ %> | ||
|
||
import './main.css';<% } %> | ||
|
||
function Example() { | ||
return ( | ||
<<%= pascalName %>></<%= pascalName %>> | ||
); | ||
} | ||
export default Example; | ||
``` | ||
|
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 @@ | ||
--- | ||
# this file only gets generated if `css` (from the command line prompt) is true | ||
to: "<%= css ? `static/usage/v${version}/${name.replace('ion-', '')}/${path}/react/main_css.md` : null %>" | ||
--- | ||
```css | ||
<%= name %> { | ||
/* styles go here */ | ||
} | ||
``` |
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,26 @@ | ||
--- | ||
arbitrary: <% pascalName = h.changeCase.pascal(name) %> | ||
to: "<%= `static/usage/v${version}/${name.replace('ion-', '')}/${path}/vue.md` %>" | ||
--- | ||
```html | ||
<template> | ||
<<%= name %>></<%= name %>> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { <%= pascalName %> } from '@ionic/vue'; | ||
import { defineComponent } from 'vue'; | ||
|
||
export default defineComponent({ | ||
components: { | ||
<%= pascalName %>, | ||
}, | ||
}); | ||
</script><% if (css){ %> | ||
|
||
<style scoped> | ||
<%= name %> { | ||
/* styles go here */ | ||
} | ||
</style><% } %> | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally optional, but it might be cool if the command could spit out this code for you to copy-paste, dynamically using the (formatted) name of the innermost directory created as the feature name. Don't worry about it if it would be a big lift, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea!! There's probably a more elegant way we could do it, but I added an MVP.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this with a little improvement, although it still would be better if it only gets logged if the files are successfully generated. (But at least now the copy doesn't state that the generation succeeded.)