Micro-generators
WinJS has many built-in micro-generators to help you quickly complete tedious tasks during development.
How to Use
The following command will list all currently available generators. You can select the functionality you want to use through an interactive interface, with detailed prompts provided.
$ win generate
# or
$ win gYou can also use the corresponding generator with win g <generatorName>.
Generator List
Page Generator
Quickly generate a new page with the following usage methods.
Basic Usage
Interactive input of page name and file generation method:
win g page
? What is the name of page? › mypage
? How dou you want page files to be created? › - Use arrow-keys. Return to submit.
❯ mypage/index.{vue,less}
mypage.{vue,less}Direct generation:
$ win g page foo
Write: src/pages/foo.vue
Write: src/pages/foo.lessGenerate page as directory, with component and style files under the directory:
$ win g page bar --dir
Write: src/pages/bar/index.less
Write: src/pages/bar/index.vueNested page generation:
$ win g page far/far/away/kingdom
Write: src/pages/far/far/away/kingdom.vue
Write: src/pages/far/far/away/kingdom.lessBatch generation of multiple pages:
$ win g page page1 page2 a/nested/page3
Write: src/pages/page1.vue
Write: src/pages/page1.less
Write: src/pages/page2.vue
Write: src/pages/page2.less
Write: src/pages/a/nested/page3.vue
Write: src/pages/a/nested/page3.lessCustomizing Page Template Content
If the default template used by the page generator doesn't meet your needs, you can customize the template content.
Execute the --eject command:
$ win g page --ejectAfter executing the command, the page generator will write its original template to the project's /templates/page directory:
.
├── package.json
└── templates
└── page
├── index.less.tpl
└── index.vue.tplUsing Template Variables
Both template files support template syntax. You can insert variables like this:
<script setup>
import { ref } from 'vue';
const message = '{{{msg}}}'
const count = {{{count}}}
</script>
<template>
<div class="page page-{{{name}}}">
<div class="page-content">
{{{ name }}}
</div>
</div>
</template>
<style src="./index{{{ cssExt }}}"></style>You can customize parameter values:
$ win g page foo --msg "Hello World" --count 10After running the command, the generated page content is as follows:
<script setup>
import { ref } from 'vue';
const message = 'Hello World'
const count = 10
</script>
<template>
<div class="page page-foo">
<div class="page-content">
foo
</div>
</div>
</template>
<style src="./index.less"></style>If you don't need to use template variables, you can omit the .tpl suffix, shortening index.vue.tpl to index.vue and index.less.tpl to index.less.
Preset Variables
In the content generated in the previous section, we didn't specify name, but it was still set with a value. This is because it belongs to preset variables in the template. Below are all the preset variables currently available in page templates:
| Parameter | Default Value | Description |
|---|---|---|
name | - | The name of the current file. If you execute pnpm win g page foo, it will generate pages/foo.vue and pages/foo.less files, where the name value is "foo". |
color | - | A randomly generated RGB color. |
cssExt | less | The suffix of the style file. |
For more template syntax content, please check mustache.
dir Mode
When not using dir mode, if your page template folder only customizes one template file, missing files will automatically use the default template files.
If using dir mode, the generated content will be consistent with your page custom template folder. Default templates are only used when the page custom template folder is empty. If your page custom template folder content is as follows:
.
├── a.vue
└── index.vue.tplThe generated directory will be:
.
├── a.vue
└── index.vueFallback
If you want to continue using the default template, you can specify --fallback, which will no longer use user-customized templates:
$ win g page foo --fallbackComponent Generator
Generate components needed for the project in the src/components/ directory. Like the page generator, the component generator also has multiple generation methods.
Basic Usage
Interactive generation:
$ win g component
✔ Please input you component Name … foo
Write: src/components/Foo/index.js
Write: src/components/Foo/Foo.vueDirect generation:
$ win g component bar
Write: src/components/Bar/index.js
Write: src/components/Bar/Bar.vueNested generation:
$ win g component group/subgroup/baz
Write: src/components/group/subgroup/Baz/index.js
Write: src/components/group/subgroup/Baz/Baz.vueBatch generation:
$ win g component apple banana orange
Write: src/components/Apple/index.js
Write: src/components/Apple/Apple.vue
Write: src/components/Banana/index.js
Write: src/components/Banana/Banana.vue
Write: src/components/Orange/index.js
Write: src/components/Orange/Orange.vueCustomizing Component Template Content
Like the page generator, the component generator also supports customizing template content. First, write the original template to the project's /templates/component directory:
$ win g component --ejectUsing Template Variables
$ win g component foo --msg "Hello World"Custom component templates can omit the .tpl suffix. You can shorten index.js.tpl to index.js and component.vue.tpl to component.vue.
The component generator will generate content consistent with your custom template folder. You can add more custom template files as needed.
Preset Variables
| Parameter | Default Value | Description |
|---|---|---|
compName | - | The name of the current component. If you execute pnpm win g component foo, the compName value is Foo. |
Fallback
$ win g component foo --fallbackRouteAPI Generator
Generate template files for routeAPI functionality.
Interactive generation:
$ win g api
✔ please input your api name: … starwar/people
Write: api/starwar/people.tsDirect generation:
$ win g api films
Write: api/films.tsNested generator:
$ win g api planets/[id]
Write: api/planets/[id].tsBatch generation:
$ win g api spaceships vehicles species
Write: api/spaceships.ts
Write: api/vehicles.ts
Write: api/species.tsMock Generator
Generate template files for Mock functionality. For specific Mock implementation, refer to the documentation.
Interactive generation:
$ win g mock
✔ please input your mock file name … auth
Write: mock/auth.tsDirect generation:
$ win g mock acl
Write: mock/acl.tsNested generation:
$ win g mock users/profile
Write: mock/users/profile.tsPrettier Configuration Generator
Generate prettier configuration for the project. After command execution, WinJS will generate recommended prettier configuration and install corresponding dependencies.
$ win g prettier
info - Write package.json
info - Update package.json for scripts
info - Write .prettierrc.js
info - Write .prettierignoreThen, you can directly execute the command npm run format to format the relevant code style in the project.
Tailwind CSS Configuration Generator
Enable Tailwind CSS configuration for the project. After command execution, win will generate Tailwind CSS configuration and install corresponding dependencies.
$ win g tailwindcss
info - Write package.json
set config:tailwindcss on /Users/win/playground/.winrc.ts
set config:plugins on /Users/win/playground/.winrc.ts
info - Update .winrc.ts
info - Write tailwind.config.js
info - Write tailwind.cssPrecommit Configuration Generator
Generate precommit configuration for the project. After command execution, win will add husky and Git commit message format validation behavior, formatting Git staged code by default before each Git commit.
Note: If it's an initialized
app,pcproject, this generator is usually not needed because husky is already configured
$ win g precommit
info - Update package.json for devDependencies
info - Update package.json for scripts
info - Write .lintstagedrc
info - Create .husky
info - Write commit-msg
info - Write pre-commitUno CSS Configuration Generator
Enable UnoCSS configuration for the project. After command execution, WinJS will generate Uno CSS configuration and install corresponding dependencies.
$ win g unocss
info - Update package.json for devDependencies
set config:unocss on /.winrc.ts
set config:plugins on /.winrc.ts
info - Update .winrc.ts
info - Write uno.config.ts