Skip to content

Commit

Permalink
feat: create-alita
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuoni committed Jan 6, 2022
1 parent 3627907 commit 6cb39a0
Show file tree
Hide file tree
Showing 35 changed files with 2,053 additions and 1,399 deletions.
3 changes: 3 additions & 0 deletions examples/boilerplate/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export default {
console: {},
inspx: {},
},
mobileLayout: true,
request: {},
// TODO: dva 配置内收
dva: {},
mfsu: {},
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"version": "0.0.0-alpha.1",
"version": "3.0.0-alpha.1",
"scripts": {
"bootstrap": "esno scripts/bootstrap.ts",
"build": "pnpm -r --filter ./packages run build",
Expand All @@ -17,7 +17,7 @@
},
"devDependencies": {
"@types/rimraf": "3.0.2",
"@umijs/utils": "4.0.0-beta.13",
"@umijs/utils": "4.0.0-beta.15",
"alita": "workspace:*",
"esno": "^0.10.1",
"husky": "^7.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/alita/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# alita

See our website [alitajs](https://alitajs.com) for more information.
See our website [alitajs](https://alitajs.com) for more information.
19 changes: 9 additions & 10 deletions packages/alita/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
"name": "alita",
"version": "0.0.0-alpha.1",
"version": "3.0.0-alpha.1",
"description": "v3 develop version",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"module": "index.esm.js",
"bin": {
"alita": "bin/alita.js"
},
"files": [
"dist"
],
Expand All @@ -18,7 +14,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/alitajs/alita-next-step"
"url": "https://github.com/alitajs/alita-next"
},
"authors": [
"chenxiaocong <[email protected]> (https://github.com/xiaohuoni)"
Expand All @@ -29,12 +25,15 @@
"publishConfig": {
"access": "public"
},
"bin": {
"alita": "bin/alita.js"
},
"dependencies": {
"@alita/plugins": "workspace:*",
"@umijs/preset-umi": "4.0.0-beta.13",
"@umijs/utils": "4.0.0-beta.13",
"@umijs/core": "4.0.0-beta.13",
"umi": "4.0.0-beta.13",
"@umijs/preset-umi": "4.0.0-beta.15",
"@umijs/utils": "4.0.0-beta.15",
"@umijs/core": "4.0.0-beta.15",
"umi": "4.0.0-beta.15",
"v8-compile-cache": "^2.3.0"
}
}
3 changes: 3 additions & 0 deletions packages/create-alita/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @alita/create-alita

See our website [alitajs](https://alitajs.com) for more information.
3 changes: 3 additions & 0 deletions packages/create-alita/bin/create-alita.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../dist/cli');
Empty file.
35 changes: 35 additions & 0 deletions packages/create-alita/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@alita/create-alita",
"version": "3.0.0-alpha.1",
"description": "create-alita",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist",
"templates"
],
"scripts": {
"build": "pnpm tsc",
"build:deps": "pnpm esno ../../scripts/bundleDeps.ts",
"dev": "pnpm build -- --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/alitajs/alita-next"
},
"authors": [
"chenxiaocong <[email protected]> (https://github.com/xiaohuoni)"
],
"license": "MIT",
"bugs": "https://github.com/alitajs/alita-next/issues",
"homepage": "https://github.com/alitajs/alita-next/tree/master/packages/create-alita#readme",
"publishConfig": {
"access": "public"
},
"bin": {
"create-alita": "bin/create-alita.js"
},
"dependencies": {
"@umijs/utils": "4.0.0-beta.15"
}
}
30 changes: 30 additions & 0 deletions packages/create-alita/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { chalk, yParser } from '@umijs/utils';
import { existsSync } from 'fs';
import { join } from 'path';

const args = yParser(process.argv.slice(2), {
alias: {
version: ['v'],
help: ['h'],
},
boolean: ['version'],
});

if (args.version && !args._[0]) {
args._[0] = 'version';
const local = existsSync(join(__dirname, '../.local'))
? chalk.cyan('@local')
: '';
const { name, version } = require('../package.json');
console.log(`${name}@${version}${local}`);
} else {
require('./')
.default({
cwd: process.cwd(),
args,
})
.catch((err: Error) => {
console.error(`Create failed, ${err.message}`);
console.error(err);
});
}
45 changes: 45 additions & 0 deletions packages/create-alita/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { rimraf } from '@umijs/utils';
import { existsSync } from 'fs';
import { join } from 'path';
import runGenerator from './index';

const fixtures = join(__dirname, '..', 'fixtures');

let oldCwd = process.cwd();

beforeEach(() => {
oldCwd = process.cwd();
});

afterEach(() => {
process.chdir(oldCwd);
});

test('generate app', async () => {
const cwd = join(fixtures, 'app');
await runGenerator({
cwd,
args: {
_: [],
$0: '',
default: true,
},
});
expect(existsSync(join(cwd, 'pages', 'index.tsx'))).toEqual(true);
rimraf.sync(cwd);
});

test('generate plugin', async () => {
const cwd = join(fixtures, 'plugin');
await runGenerator({
cwd,
args: {
plugin: true,
_: [],
$0: '',
default: true,
},
});
expect(existsSync(join(cwd, 'src', 'index.ts'))).toEqual(true);
rimraf.sync(cwd);
});
110 changes: 110 additions & 0 deletions packages/create-alita/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {
BaseGenerator,
installWithNpmClient,
prompts,
yParser,
} from '@umijs/utils';
import { join } from 'path';

const testData = {
name: 'umi-plugin-demo',
description: 'nothing',
mail: '[email protected]',
author: 'xiaohuoni',
org: 'alitajs',
version: require('../package').version,
npmClient: 'pnpm',
registry: 'https://registry.npmjs.org/',
};

export default async ({
cwd,
args,
}: {
cwd: string;
args: yParser.Arguments;
}) => {
const [_, name] = args._;
let npmClient = 'pnpm' as any;
let registry = 'https://registry.npmjs.org/';
// test ignore prompts
if (!args.default) {
const response = await prompts([
{
type: 'select',
name: 'npmClient',
message: 'Pick Npm Client',
choices: [
{ title: 'npm', value: 'npm' },
{ title: 'cnpm', value: 'cnpm' },
{ title: 'tnpm', value: 'tnpm' },
{ title: 'yarn', value: 'yarn' },
{ title: 'pnpm', value: 'pnpm', selected: true },
],
},
{
type: 'select',
name: 'registry',
message: 'Pick Bpm Registry',
choices: [
{
title: 'npm',
value: 'https://registry.npmjs.org/',
selected: true,
},
{ title: 'taobao', value: 'https://registry.npmmirror.com' },
],
},
]);
npmClient = response.npmClient;
registry = response.registry;
}

const pluginPrompts = [
{
name: 'name',
type: 'text',
message: `What's the plugin name?`,
default: name,
},
{
name: 'description',
type: 'text',
message: `What's your plugin used for?`,
},
{
name: 'mail',
type: 'text',
message: `What's your email?`,
},
{
name: 'author',
type: 'text',
message: `What's your name?`,
},
{
name: 'org',
type: 'text',
message: `Which organization is your plugin stored under github?`,
},
] as prompts.PromptObject[];

const generator = new BaseGenerator({
path: join(__dirname, '..', 'templates', args.plugin ? 'plugin' : 'app'),
target: name ? join(cwd, name) : cwd,
data: args.default
? testData
: {
version: require('../package').version,
npmClient,
registry,
},
questions: args.default ? [] : args.plugin ? pluginPrompts : [],
});
await generator.run();

if (!args.default) {
// install
installWithNpmClient({ npmClient });
}
};
8 changes: 8 additions & 0 deletions packages/create-alita/templates/app/.gitignore.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/node_modules
/.env.local
/.umirc.local.ts
/.umirc.local.js
/config/config.local.ts
/config/config.local.js
/src/.umi
/.umi
1 change: 1 addition & 0 deletions packages/create-alita/templates/app/.npmrc.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry={{{ registry }}}
3 changes: 3 additions & 0 deletions packages/create-alita/templates/app/.umirc.ts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
npmClient: '{{{ npmClient }}}'
};
22 changes: 22 additions & 0 deletions packages/create-alita/templates/app/layouts/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import { Link, Outlet } from 'umi';

export default function Layout() {
return (
<div>
<h2>global layout</h2>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/users">/users</Link>
</li>
<li>
<Link to="/users/foo">/users/foo</Link>
</li>
</ul>
<Outlet />
</div>
);
}
17 changes: 17 additions & 0 deletions packages/create-alita/templates/app/package.json.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"private": true,
"scripts": {
"dev": "alita dev",
"build": "alita build",
"postinstall": "alita setup",
"start": "npm run dev"
},
"dependencies": {
"alita": "^{{{ version }}}"
},
"devDependencies": {
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"typescript": "^4.1.2"
}
}
5 changes: 5 additions & 0 deletions packages/create-alita/templates/app/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export default function HomePage() {
return <div>HomePage</div>;
}
9 changes: 9 additions & 0 deletions packages/create-alita/templates/app/pages/users.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

export default () => {
return (
<div>
<h2>users layout</h2>
</div>
);
};
3 changes: 3 additions & 0 deletions packages/create-alita/templates/app/pages/users/foo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';

export default () => <h2>user: Foo</h2>;
5 changes: 5 additions & 0 deletions packages/create-alita/templates/plugin/.fatherrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
target: 'node',
cjs: { type: 'babel', lazy: true },
disableTypeCheck: true,
};
2 changes: 2 additions & 0 deletions packages/create-alita/templates/plugin/.gitignore.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib
1 change: 1 addition & 0 deletions packages/create-alita/templates/plugin/.npmrc.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry={{{ registry }}}
Loading

0 comments on commit 6cb39a0

Please sign in to comment.