Skip to content

Commit

Permalink
feat: alita g pages pageName
Browse files Browse the repository at this point in the history
  • Loading branch information
陈书航 committed Feb 24, 2022
1 parent 3daf856 commit 854e6f2
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/three-owls-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'alita': major
---

alita g pages pageName
3 changes: 2 additions & 1 deletion packages/alita/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"url": "https://github.com/alitajs/alita"
},
"authors": [
"chenxiaocong <xiaohuoni@gmail.com> (https://github.com/xiaohuoni)"
"chenxiaocong <xiaohuoni@gmail.com> (https://github.com/xiaohuoni)",
"hang1017 <437132124@qq.com> (https://github.com/hang1017)"
],
"license": "MIT",
"bugs": "https://github.com/alitajs/alita/issues",
Expand Down
38 changes: 38 additions & 0 deletions packages/alita/src/commands/generate/page.test.ts
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/);
});
36 changes: 36 additions & 0 deletions packages/alita/src/commands/generate/page.ts
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),
},
});
},
});
};
1 change: 1 addition & 0 deletions packages/alita/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default (api: IApi) => {
require.resolve('./features/config/config'),
require.resolve('./features/apptype'),
require.resolve('./features/umiExports'),
require.resolve('./commands/generate/page'),
require.resolve('@alita/plugins/dist/aconsole'),
require.resolve('@alita/plugins/dist/keepalive'),
require.resolve('@alita/plugins/dist/mainpath'),
Expand Down
3 changes: 3 additions & 0 deletions packages/alita/src/templates/generate/page/index.less.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.center {
background-color: {{{ color }}};
}
13 changes: 13 additions & 0 deletions packages/alita/src/templates/generate/page/index.tsx.tpl
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;
5 changes: 5 additions & 0 deletions packages/alita/src/templates/generate/page/service.ts.tpl
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' });
}

0 comments on commit 854e6f2

Please sign in to comment.