-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
陈书航
committed
Feb 24, 2022
1 parent
3daf856
commit 854e6f2
Showing
8 changed files
with
103 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'alita': major | ||
--- | ||
|
||
alita g pages pageName |
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,38 @@ | ||
import { Env, Service } from '@umijs/core'; | ||
import { rimraf } from '@umijs/utils'; | ||
import { existsSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
const fixtures = join(__dirname, '../../../fixtures'); | ||
const cwd = join(fixtures, 'generate'); | ||
|
||
async function runGenerator(args: any) { | ||
const service = new Service({ | ||
cwd, | ||
env: Env.test, | ||
plugins: [require.resolve('./page')], | ||
}); | ||
|
||
await service.run({ | ||
name: 'generate', | ||
args, | ||
}); | ||
} | ||
|
||
test('generate pages', async () => { | ||
await runGenerator({ | ||
_: ['generate', 'pages', 'index'], | ||
}); | ||
|
||
expect(existsSync(join(cwd, 'pages', 'index', 'index.tsx'))).toEqual(true); | ||
expect(existsSync(join(cwd, 'pages', 'index', 'index.less'))).toEqual(true); | ||
rimraf.sync(join(cwd, 'pages')); | ||
}); | ||
|
||
test('Generator not found', async () => { | ||
await expect( | ||
runGenerator({ | ||
_: ['generate', 'foo'], | ||
}), | ||
).rejects.toThrow(/Generator foo not found/); | ||
}); |
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,36 @@ | ||
import { GeneratorType } from '@umijs/core'; | ||
import { lodash, prompts, randomColor } from '@umijs/utils'; | ||
import { join } from 'path'; | ||
import { IApi } from 'umi'; | ||
|
||
export default (api: IApi) => { | ||
api.registerGenerator({ | ||
key: 'pages', | ||
name: 'Create page', | ||
description: 'Create a alita page by page name', | ||
type: GeneratorType.generate, | ||
fn: async (options) => { | ||
const { args, api, generateFile } = options; | ||
const [_, _name] = args._; | ||
let name = _name; | ||
|
||
if (!name) { | ||
const response = await prompts({ | ||
type: 'text', | ||
name: 'name', | ||
message: 'What is the name of page?', | ||
}); | ||
name = response.name; | ||
} | ||
|
||
generateFile({ | ||
path: join(__dirname, '../../templates/generate/page'), | ||
target: join(api.paths.absPagesPath, name), | ||
data: { | ||
color: randomColor(), | ||
name: lodash.upperFirst(name), | ||
}, | ||
}); | ||
}, | ||
}); | ||
}; |
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,3 @@ | ||
.center { | ||
background-color: {{{ color }}}; | ||
} |
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 @@ | ||
import React, { FC } from 'react'; | ||
import { useRequest } from 'alita'; | ||
import { query } from './service'; | ||
import styles from './index.less'; | ||
|
||
interface {{{ name }}}PageProps {} | ||
|
||
const {{{ name }}}Page: FC<{{{ name }}}PageProps> = () => { | ||
const { data } = useRequest(query); | ||
return <div className={styles.center}>Hello {data?.text}</div>; | ||
}; | ||
|
||
export default {{{ name }}}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,5 @@ | ||
import { request } from 'alita'; | ||
|
||
export async function query(): Promise<any> { | ||
return request('/api/hello', { method: 'post' }); | ||
} |