Skip to content

Commit

Permalink
feat: block support --layout and config npmClient (#2468)
Browse files Browse the repository at this point in the history
* feat: block support --layout

* test: add test case for block --layout

* feat: support npmClient config of block add block doc
  • Loading branch information
Yu authored and sorrycc committed May 25, 2019
1 parent 5502704 commit 546dcef
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
14 changes: 14 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ export default {
}
```

### block <Badge text="2.7.0+"/>

- Type: `Object`
- Default: `{ defaultGitUrl: "https://github.com/umijs/umi-blocks" }`

```js
export default {
block: {
defaultGitUrl: "https://github.com/ant-design/pro-blocks",
npmClient: "cnpm",
}
}
```

## webpack

### chainWebpack
Expand Down
14 changes: 14 additions & 0 deletions docs/zh/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ export default {
}
```

### block <Badge text="2.7.0+"/>

- Type: `Object`
- Default: `{ defaultGitUrl: "https://github.com/umijs/umi-blocks" }`

```js
export default {
block: {
defaultGitUrl: "https://github.com/ant-design/pro-blocks",
npmClient: "cnpm", // 优先级低于 umi block add [block] --npm-client
}
}
```

## webpack

### chainWebpack
Expand Down
17 changes: 11 additions & 6 deletions packages/umi-build-dev/src/plugins/commands/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default api => {
);

const useYarn = existsSync(join(paths.cwd, 'yarn.lock'));
const defaultNpmClient = useYarn ? 'yarn' : 'npm';
const defaultNpmClient = blockConfig.npmClient || (useYarn ? 'yarn' : 'npm');
debug(`defaultNpmClient: ${defaultNpmClient}`);
debug(`args: ${JSON.stringify(args)}`);
const {
Expand All @@ -159,6 +159,7 @@ export default api => {
skipDependencies,
skipModifyRoutes,
wrap: isWrap,
layout: isLayout,
} = args;
const ctx = getCtx(url);
spinner.succeed();
Expand Down Expand Up @@ -375,14 +376,17 @@ export default api => {
initialValue: {
path: generator.path.toLowerCase(),
component: `.${generator.path}`,
...(isLayout ? { routes: [] } : {}),
},
});
try {
writeNewRoute(
newRouteConfig,
api.service.userConfig.file,
paths.absSrcPath,
);
if (!dryRun) {
writeNewRoute(
newRouteConfig,
api.service.userConfig.file,
paths.absSrcPath,
);
}
} catch (e) {
spinner.fail();
throw new Error(e);
Expand Down Expand Up @@ -447,6 +451,7 @@ Options for the ${chalk.cyan(`add`)} command:
${chalk.green(`--skip-modify-routes`)} don't modify the routes
${chalk.green(`--dry-run `)} for test, don't install dependencies and download
${chalk.green(`--no-wrap `)} add the block to a independent directory
${chalk.green(`--layout `)} add as a layout block (add route with empty children)
Examples:
Expand Down
19 changes: 18 additions & 1 deletion packages/umi-build-dev/src/plugins/commands/block/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@ class MockGenerator {
describe('umi block', () => {
it('run block command right', async () => {
const commandFn = jest.fn();
const routeConfigFn = jest.fn();
let commandHandler = null;
const mockApi = {
config: {},
service: {
userConfig: {
file: '/testpath',
},
},
config: {
routes: [],
},
log: {
error: e => {
console.error(e);
},
success: () => {},
},
applyPlugins: (name, { initialValue }) => {
if (name === '_modifyBlockNewRouteConfig') {
routeConfigFn(initialValue);
}
return initialValue;
},
Generator: MockGenerator,
Expand All @@ -47,13 +58,19 @@ describe('umi block', () => {
const { ctx, generator } = await commandHandler({
path: 'Test/NewPage',
wrap: false,
layout: true,
dryRun: true,
_: ['add', join(__dirname, '../../../fixtures/block/test-blocks/demo-with-dependencies')],
});
expect(ctx.isLocal).toEqual(true);
expect(ctx.routePath).toEqual('/Test/NewPage');
expect(ctx.pkg.name).toEqual('@umi-blocks/DemoWithDependencies');
expect(generator._opts.isPageBlock).toEqual(true);
expect(routeConfigFn).toBeCalledWith({
component: './Test/NewPage',
path: '/test/newpage',
routes: [],
});

const { ctx: ctx2, generator: generator2 } = await commandHandler({
path: 'Test/NewPage',
Expand Down

0 comments on commit 546dcef

Please sign in to comment.