Skip to content

Commit

Permalink
refactor: refactor doc generator
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Apr 8, 2018
1 parent 68a47da commit 56261d4
Show file tree
Hide file tree
Showing 44 changed files with 627 additions and 1,032 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ env:
- TASK=release
- TASK=test
- TASK=lint
- TASK=site:release
- TASK=scaffold:release

addons:
Expand Down
10 changes: 7 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
{
"type": "node",
"request": "launch",
"name": "Gulp task [site:build]",
"program": "${workspaceRoot}/node_modules/gulp/bin/gulp.js",
"name": "site generate",
"runtimeArgs": ["-r", "ts-node/register"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart",
"args": [
"site:build"
"${workspaceFolder}/scripts/site/generate.ts",
"architecture"
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ ng-alain 是一个标准的 Angular cli 构建的项目,因此你会非常熟
$ npm install
```

如果网络状况不佳,可以使用 [cnpm](//cnpmjs.org/) 进行加速
如果网络状况不佳,可以使用 [nrm](https://www.npmjs.com/package/nrm) 并切换国内NPM镜像进行加速,请务直接使用 **cnpm** 会导致关联包无法找到问题

```bash
$ npm start
Expand Down
2 changes: 0 additions & 2 deletions docs/how-to-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ type: Dev

ng-alain 的基础组件库来自 ng-zorro-antd,因此对于这类组件的使用方式你可以通过其官网得到很细说 [API](//ng.ant.design/) 文档说明,而对于 ng-alain 提供的组件则通过[组件页](/components)获取。

> ng-alain 依赖了一个 [ng-zorro-antd-extra](//cipchk.github.io/ng-zorro-antd-extra/) 类库,这是它和 ng-zorro-antd 一样,只是提供了一些 3.0 才会有的组件临时解决方案。
## 二、启动

项目从启动开始就需要从远程获取到一些配置信息(例如:菜单数据、用户数据等),脚手架的启动始于 [startup.service.ts](//github.com/cipchk/ng-alain/blob/master/src/app/core/startup/startup.service.ts);它返回的是一个 `Promise` 对象,因此你可以在这个方法体内做你想做的事,默认情况下做了很多有意思的事:
Expand Down
2 changes: 0 additions & 2 deletions docs/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type: Other
+ HTTP 拦截器
+ Angular 启动服务
+ `ng-zorro-antd` 基础组件服务
+ `ng-zorro-antd-extra` 基础组件扩展服务
+ `@delon/abc` 业务组件服务

**作用:** 贯穿整个应用的定义。
Expand All @@ -62,7 +61,6 @@ type: Other

+ Angular 通用模块:`CommonModule``FormsModule``RouterModule``ReactiveFormsModule`
+ `ng-zorro-antd` 基础组件模块
+ `ng-zorro-antd-extra` 基础扩展组件模块
+ `@delon/abc` 业务组件模块
+ 第三方通用依赖组件模块

Expand Down
14 changes: 3 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"lint:style": "stylelint \"{src}/**/*.less\" --syntax less",
"lint-staged": "lint-staged",
"test": "ng test -sr -cc",
"site:gen": "node scripts/site/main",
"site:release": "bash ./site.sh -b",
"site:gh": "bash ./site.sh -b -d",
"release": "bash ./build.sh",
Expand Down Expand Up @@ -70,6 +71,7 @@
"@angular/compiler-cli": "^5.0.0",
"@angular/language-service": "^5.0.0",
"@schematics/schematics": "^0.4.6",
"@types/fs-extra": "^5.0.1",
"@types/gulp": "^4.0.4",
"@types/htmlparser2": "^3.7.31",
"@types/jasmine": "~2.8.6",
Expand All @@ -85,17 +87,6 @@
"domhandler": "^2.4.1",
"fs-extra": "^5.0.0",
"gh-pages": "^1.1.0",
"gulp": "^3.9.1",
"gulp-bump": "^3.0.0",
"gulp-clean": "^0.4.0",
"gulp-cli": "^2.0.0",
"gulp-gh-pages": "^0.5.4",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.6.0",
"gulp-rollup": "^2.15.0",
"gulp-transform": "^3.0.5",
"gulp-util": "^3.0.8",
"gulp-wrap": "^0.13.0",
"htmlparser2": "^3.9.2",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
Expand All @@ -105,6 +96,7 @@
"karma-coverage-istanbul-reporter": "^1.4.2",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"klaw-sync": "^3.0.2",
"mark-twain": "^2.0.2",
"mkdirp": "^0.5.1",
"mockjs": "^1.0.1-beta3",
Expand Down
39 changes: 24 additions & 15 deletions packages/abc/simple-table/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,34 @@ radioChange | radio变化时回调,参数为当前所选 | `EventEmitter` | -
sortChange | 排序回调 | `EventEmitter` | -
filterChange | Filter回调 | `EventEmitter` | -

**方法**
### 组件方法

参数 | 说明
----|------
`export(urlOrData?: string | any[], opt?: STExportOptions)` | 导出Excel,确保已经注册 `AdXlsxModule` | `Function` | -
参数 | 返回值 | 说明
----|-----|-----
`load(pi = 1, extraParams?: any)` | `void` | 根据页码重新加载数据
`reset(extraParams?: any)` | `void` | 重置且重新设置 `pi``1`
`clearCheck()` | `void` | 清除所有 `checkbox`
`clearRadio()` | `void` | 清除所有 `radio`
`export(urlOrData?: string | any[], opt?: STExportOptions)` | `void` | 导出Excel,确保已经注册 `AdXlsxModule`

> `urlOrData` 一般用于需要导出所有页时重新指定带有所有数据的URL地址或本地数据数组,当所有参数都不指定时自动读书当前页数据。
**使用方式**

```ts
@Component({
template: `
<simple-table #st></simple-table>
<button (click)="st.load()"></button>
<button (click)="st.reset()">重置</button>
`
})
class TestComponent {
@ViewChild('st') comp: SimpleTableComponent;
// this.comp.load();
}
```

*STExportOptions*

参数 | 说明 | 类型 | 默认值
Expand All @@ -97,7 +117,6 @@ callback | 保存前的回调 | `(wb: WorkBook) => void` | -
----|------|-----|------
checked? | 选择框或单选框状态值 | `boolean` | -
disabled? | 选择框或单选框 `disabled` 值 | `boolean` | -
[key: string]: any | - | - | -

### SimpleTableColumn

Expand Down Expand Up @@ -209,13 +228,3 @@ reqReName | 重命名请求参数 `pi`、`ps`;例如:`{ pi: 'Page' }` `pi`
resReName | 重命名返回参数 `total``list`;例如:`{ total: 'Total' }` Total 会被当作 `total` | `Object` | -
sortReName | 重命名排序值,`columns` 的重命名高于属性 | `{ ascend?: string, descend?: string }` | -
modalParamsName | 指定模态框目标组件的接收参数名 | `string` | record

## 刷新表格

一般用于表单搜索时。

```html
<simple-table #st></simple-table>
<button (click)="st.load()"></button>
<button (click)="st.reset()">重置</button>
```
2 changes: 1 addition & 1 deletion packages/abc/simple-table/simple-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export class SimpleTableComponent implements OnInit, OnChanges, AfterViewInit, O
}

/**
* 重新加载列表
* 根据页码重新加载数据
*
* @param pi 指定当前页码,默认:`1`
* @param extraParams 重新指定 `extraParams` 值
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/token/simple/simple.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ describe('auth: simple.interceptor', () => {
http.get('/test', { responseType: 'text' }).subscribe(value => {
done();
});
const req = httpBed.expectOne('/test?token=123') as TestRequest;
expect(req.request.url).toContain(`token=123`);
const req = httpBed.expectOne(() => true) as TestRequest;
expect(req.request.url).toBe('/test?token=123');
req.flush('ok!');
});
});
Expand Down
File renamed without changes.
File renamed without changes.
164 changes: 164 additions & 0 deletions scripts/site/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import * as path from 'path';
import * as fs from 'fs';
import * as fse from 'fs-extra';
import { parseMd } from './utils/parse-md';
import { genUpperName, genUrl, generateDoc, includeAttributes, genComponentName, genSelector } from './utils/utils';
import { groupFiles } from './utils/group-files';
import { generateDemo } from './utils/generate-demo';

const target = process.argv[2];
const isSyncSpecific = !!target && (target !== 'init');

if (!target) throw new Error(`Should specify the generation type, 'init' is all module`);

const rootDir = path.resolve(__dirname, '../../');
const siteConfig = require(path.join(rootDir, 'site/site.config.js'));
const defaultLang = siteConfig.theme.defaultLang;

const AllMetas: any = Object.assign({ types: [] }, includeAttributes(siteConfig.theme, {}));

function generateModule(config: any) {
const srcPath = path.join(rootDir, config.src);
const targetPath = path.join(rootDir, config.dist);
const tpl = {
module: fs.readFileSync(path.join(rootDir, config.template.module)).toString('utf8'),
content: fs.readFileSync(path.join(rootDir, config.template.content)).toString('utf8')
};

const files = groupFiles(srcPath, config, isSyncSpecific, target, siteConfig);
const metas: any[] = [];
const components: any[] = [];

const modules: any = {
imports: [],
components: [],
routes: []
};

function appendToModule(componentName: string, name: string, filename: string, needRouter: boolean = true) {
modules.imports.push(`import { ${componentName} } from './${name}/${filename}';`);
modules.components.push(componentName);
if (needRouter) {
if (modules.routes.length <= 0 && config.defaultRoute) {
modules.routes.push(`{ path: '', redirectTo: '${config.defaultRoute}', pathMatch: 'full' }`);
}
modules.routes.push(`{ path: '${name}', component: ${componentName} }`);
}
}

files.forEach(item => {
// region: generate document file

const content = {};
const urls = {};
const contentMetas = {};
const i18n = Object.keys(item.data).length > 1;
// const titleBuffer = {};
Object.keys(item.data).forEach(lang => {
const filePath = item.data[lang];
content[lang] = parseMd(filePath, siteConfig);
urls[lang] = genUrl(rootDir, filePath);
// // split langs in meta
// if (typeof content[lang].meta.title === 'object') {
// Object.keys(content[lang].meta.title).forEach(titleLang => {
// if (!titleBuffer[titleLang]) {
// titleBuffer[titleLang] = content[lang].meta.title[titleLang];
// }
// });
// }
// content[lang].meta.title = typeof content[lang].meta.title === 'string' ?
// content[lang].meta.title : titleBuffer[lang];
contentMetas[lang] = content[lang].meta;
});
const meta = {
name: item.key,
i18n,
order: content[defaultLang].meta.order || 100,
cols: content[defaultLang].meta.cols || 1,
meta: contentMetas
};
metas.push(meta);

// endregion

// region: generate demo files
const demos = generateDemo(
rootDir,
item.key,
path.join(path.dirname(item.data[defaultLang]), 'demo'),
meta.cols,
config,
siteConfig
);
// endregion

// region: generate document file
const fileObject: any = {
componentName: genComponentName(config.name, meta.name),
selector: genSelector(config.name, meta.name),
item: JSON.stringify({
cols: meta.cols,
urls,
content,
demo: demos.data.length > 0
}),
demo: demos.tpl.left.length > 0 || demos.tpl.right.length > 0
};
const demoHTML: string[] = [ ];
if (fileObject.demo) {
demoHTML.push(`<nz-row [nzGutter]="16">`);
if (demos.tpl.left.length > 0 && demos.tpl.right.length > 0) {
demoHTML.push(`<nz-col nzSpan="12">${demos.tpl.left.join('')}</nz-col>`);
demoHTML.push(`<nz-col nzSpan="12">${demos.tpl.right.join('')}</nz-col>`);
} else {
demoHTML.push(`<nz-col nzSpan="24">${demos.tpl.left.join('')}${demos.tpl.right.join('')}</nz-col>`);
}

demoHTML.push('</nz-row>');
}
fileObject.demos = demoHTML.join('');
fileObject.codes = JSON.stringify(demos.data);
generateDoc(
fileObject,
fs.readFileSync(path.join(rootDir, config.template.content)).toString('utf8'),
path.join(rootDir, config.dist, item.key, `index.ts`)
);
// endregion

// region: register module
appendToModule(fileObject.componentName, item.key, 'index');
// demo
demos.data.forEach(demo => {
appendToModule(demo.componentName, item.key, demo.name, false);
});
// endregion
});

AllMetas.types.push(Object.assign({ list: metas }, includeAttributes(config, {})));

// region: generate module file
const moduleObj = {
name: config.name,
moduleName: genUpperName(config.name),
imports: modules.imports.join('\r\n'),
components: modules.components.join(',\r\n'),
routes: modules.routes.join(',\r\n')
};
generateDoc(
moduleObj,
fs.readFileSync(path.join(rootDir, config.template.module)).toString('utf8'),
path.join(rootDir, config.dist, `${config.name}.module.ts`)
);
// endregion
}

for (const m of siteConfig.modules) {
generateModule(m);
}

// generate meta file
generateDoc(
{ data: JSON.stringify(AllMetas) },
fs.readFileSync(path.join(rootDir, siteConfig.theme.meta.template)).toString('utf8'),
path.join(rootDir, siteConfig.theme.meta.dist, `meta.ts`)
);
1 change: 1 addition & 0 deletions scripts/site/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './generate';
4 changes: 2 additions & 2 deletions gulpfile.js → scripts/site/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const path = require('path');

// Register TS compilation.
require('ts-node').register({
project: path.join(__dirname, 'tools/gulp/tsconfig.json')
project: path.join(__dirname, 'tsconfig.json')
});

require('./tools/gulp/gulpfile');
require('./index');
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const JsonML = require('jsonml.js/lib/utils');
import { isHeading, generateSluggedId } from '../utils/utils';

export function toc(markdownData: any, config: any) {
const maxDepth = config.theme.tocMaxDepth || 6;
const maxDepth = config.theme.tocMaxDepth || 3;
const listItems = JsonML.getChildren(markdownData.content).filter((node: any) => {
const tagName = JsonML.getTagName(node);
return isHeading(tagName) && +tagName.charAt(1) <= maxDepth;
Expand Down
26 changes: 26 additions & 0 deletions scripts/site/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// TypeScript config file that will be used to compile the gulp tasks. The normal gulpfile will
// use TS-Node to compile the gulp tasks at runtime.
{
"compilerOptions": {
"experimentalDecorators": true,
"lib": [
"es2015",
"dom",
"es2016.array.include"
],
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./dist",
"strictNullChecks": true,
"noEmitOnError": true,
"noImplicitAny": true,
"target": "es5",
"types": [
"node"
],
"baseUrl": "."
},
"files": [
"**/*.ts"
]
}
Loading

0 comments on commit 56261d4

Please sign in to comment.