Skip to content

Commit

Permalink
feat: add doc deploy (#2067)
Browse files Browse the repository at this point in the history
* feat: add doc deploy

* feat: add doc deploy
  • Loading branch information
clock157 authored and sorrycc committed Mar 7, 2019
1 parent 8786506 commit 0019414
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
8 changes: 7 additions & 1 deletion packages/umi-library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ $ umi-lib doc dev

# build doc
$ umi-lib doc build

# deploy doc to github.io
$ umi-lib doc deploy
```

## Cli
Expand Down Expand Up @@ -69,13 +72,15 @@ doc 包含 dev 和 build 两个子命令。
```bash
$ umi-lib doc dev
$ umi-lib doc build
$ umi-lib doc deploy
```

所有的命令行参数会透传给 docz,详见 [docz.site#project-configuration](https://www.docz.site/documentation/project-configuration)

注:

1. 不能传 `--config` 参数,通过 `--config` 指定的文件内容可全部配置在 `.umirc.library.js`[doc](#doc) 配置里。
2. 使用 `deploy` 之前请先执行 `build` 命令,文档部署后域名为:`https://yourname.github.io/your-repo`

## Config

Expand All @@ -87,7 +92,8 @@ $ umi-lib doc build
export default {
entry: 'src/foo.js',
doc: {
themeConfig: { mode: 'dark' }
themeConfig: { mode: 'dark' },
base: '/your-repo'
},
}
```
Expand Down
1 change: 1 addition & 0 deletions packages/umi-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"docz-plugin-css-temp": "0.13.1",
"docz-theme-default": "0.13.7",
"extend2": "1.0.0",
"gh-pages": "^2.0.1",
"happypack": "5",
"less": "3.9.0",
"less-plugin-npm-import": "2.1.0",
Expand Down
35 changes: 31 additions & 4 deletions packages/umi-library/src/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import * as assert from 'assert';
import { sync as resolveBin } from 'resolve-bin';
import { fork } from 'child_process';
import { join } from 'path';
import { writeFileSync } from 'fs';
import { writeFileSync, existsSync } from 'fs';
import { sync as mkdirp } from 'mkdirp';
import ghpages from 'gh-pages';
import getUserConfig, { CONFIG_FILES } from './getUserConfig';
import registerBabel from './registerBabel';
import chalk from 'chalk';

export default function ({ cwd, cmd, params = [] }) {
export default function({ cwd, cmd, params = [] }) {
assert.ok(
['build', 'dev', 'deploy'].includes(cmd),
`Invalid subCommand ${cmd}`,
Expand All @@ -29,6 +31,14 @@ export default function ({ cwd, cmd, params = [] }) {
'utf-8',
);

if (cmd === 'deploy') {
return deploy(cwd, params);
} else {
return devOrBuild(cmd, params);
}
}

function devOrBuild(cmd, params) {
return new Promise((resolve, reject) => {
const binPath = resolveBin('docz');
assert.ok(
Expand All @@ -46,7 +56,7 @@ export default {
`.trim(),
);

// test 时在 src 下没有 docrc.js
// test 时在 src 下没有 doczrc.js
if (__dirname.endsWith('src')) {
params.push('--config', join(__dirname, '../lib/doczrc.js'));
} else {
Expand All @@ -60,7 +70,7 @@ export default {
params.push('--help');
}
const child = fork(binPath, [cmd, ...params]);
child.on('exit', (code) => {
child.on('exit', code => {
if (code === 1) {
reject(new Error('Doc build failed'));
} else {
Expand All @@ -69,3 +79,20 @@ export default {
});
});
}

function deploy(cwd: string, params = {}) {
return new Promise((resolve, reject) => {
const distDir = join(cwd, '.docz/dist');
assert.ok(
existsSync(distDir),
`Please run ${chalk.green(`umi-lib doc build`)} first`,
);
ghpages.publish(distDir, params, err => {
if (err) {
reject(new Error(`Doc deploy failed. ${err.message}`));
} else {
resolve();
}
});
});
}

0 comments on commit 0019414

Please sign in to comment.