Skip to content

Commit

Permalink
fix: publish增加api businessType说明
Browse files Browse the repository at this point in the history
  • Loading branch information
whizbz11 committed Jul 18, 2020
1 parent fcfb940 commit bf944b4
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 29 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"html-webpack-plugin": "^3.2.0",
"ignore-styles": "^5.0.1",
"inquirer": "^7.0.1",
"jsdom": "^16.3.0",
"jszip": "^3.2.2",
"less": "^3.10.3",
"less-loader": "^5.0.0",
Expand All @@ -90,7 +91,8 @@
"webpack-dev-middleware": "^3.7.2",
"webpack-dev-server": "^3.10.1",
"webpack-hot-middleware": "^2.25.0",
"webpack-merge": "^4.2.2"
"webpack-merge": "^4.2.2",
"yonui-ys": "^1.1.4"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
Expand Down
16 changes: 14 additions & 2 deletions src/cli/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')
const FormData = require('form-data')
const fetch = require('node-fetch')
const chalk = require('chalk')
const { getPackageJson, getManifestJson } = require('../utils/index')
const { getPackageJson, getManifestJson, getLibraConfig } = require('../utils/index')
const { getRc } = require('./set')
const { CONFIG_FILE_NAME, YNPM_PUBLISH_URL } = require('../utils/globalConfig')

Expand Down Expand Up @@ -58,11 +58,23 @@ function replacePackageName (packageJson, manifestJson) {

function getControlTypes (comps) {
const results = []
const output = getLibraConfig().output
// console.log(output);
const mainfestPath = path.resolve(output.dist, 'manifest.json');
const compMainfest = require(mainfestPath) || {};
const componentsInfo = compMainfest.components || [];
let i = 0;
for (const key in comps) {
const obj = {}
obj.name = key
const currentCompInfo = componentsInfo[i] || {}
obj.name = currentCompInfo.label
obj.path = comps[key]
obj.businessType = currentCompInfo.businessType
obj.code = key
obj.propList = currentCompInfo.api
// console.log('---', compMainfest);
results.push(obj)
i++;
}
return results
}
Expand Down
17 changes: 17 additions & 0 deletions src/utils/writeManifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { getManifestJson, getLibraConfig } = require('./index')
const path = require('path')
const fse = require('fs-extra')
const chalk = require('chalk')
const { JSDOM } = require('jsdom')
function customJsonStringify (key, value) {
if (typeof value === 'function') {
return `/Function(${value.toString()})/`
Expand All @@ -11,6 +12,22 @@ function customJsonStringify (key, value) {

const writeManifest = () => {
const output = getLibraConfig().output
// 创建 JSDOM,获得 window 对象
const { window } = new JSDOM('<div id="dum-id" ></div>');
global.window = window;
// 将被测函数需要用到的变量挂到全局
// global.localStorage = window.localStorage;
global.document = window.document;
global.navigator = { userAgent: 'node.js' }
global.window.matchMedia = window.matchMedia || function () {
return {
matches: false,
addListener: function () {},
removeListener: function () {}
};
};
global.location = { search: '' }

require('ignore-styles').default(['.sass', '.scss', '.png', '.jpg', '.jpeg', '.gif', '.css', '.less', '.svg'])
const library = require(path.resolve(output.dist, 'index.js'))
const manifestComponents = []
Expand Down
2 changes: 1 addition & 1 deletion templates/Project/components/_mdfComponent/buttonComp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import compUtils from './common';
export default (WrapComponent) => {
export default (WrapComponent, model) => {
return class ButtonComp extends React.Component {
componentDidMount () {
console.log('%c container didMount', 'color:red');
Expand Down
32 changes: 29 additions & 3 deletions templates/Project/components/_mdfComponent/common.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
const type2models = {
simple: 'SimpleModel',
list: 'ListModel',
grid: 'GridModel',
tag: 'TagModel',
tree: 'TreeModel',
refer: 'ReferModel'
}

const compUtils = {
bind: function (oThis) {
const currentModel = oThis.props.model;
bind: function (oThis, model) {
const newModelType = type2models[model];
if (newModelType && typeof cb !== 'undefined') {
const viewModel = oThis.props.viewModel || oThis.props.pageModel
const cItemName = oThis.props.cItemName;
oThis.newModel = new cb.models[newModelType](oThis.modelData || {});
if (cItemName) {
viewModel.removeProperty(cItemName)
viewModel.addProperty('new10', oThis.newModel)
}
}
const currentModel = oThis.newModel || oThis.props.model;
if (currentModel) currentModel.addListener(oThis);
},
unbind: function (oThis) {
const currentModel = oThis.props.model;
const currentModel = oThis.newModel || oThis.props.model;
if (currentModel) currentModel.removeListener(oThis);
},
setModelConfig: function (oThis, data) {
// if (!(typeof cb == 'undefined')) {
// const currentModel = oThis.newModel || oThis.props.model;
// cb.models.BaseModel.call(currentModel, data);
// }
oThis.modelData = data;
}
}

Expand Down
11 changes: 8 additions & 3 deletions templates/Project/components/_mdfComponent/containerComp.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import compUtils from './common';
export default (WrapComponent) => {
export default (WrapComponent, model) => {
return class ControlComp extends React.Component {
constructor (props) {
super();
}

componentDidMount () {
console.log('%c container didMount', 'color:red');
compUtils.bind(this);
compUtils.bind(this, model);
const { viewModel, meta } = this.props;
viewModel.on('updateViewMeta', args => {
const { code, visible } = args;
Expand All @@ -23,10 +23,15 @@ export default (WrapComponent) => {
compUtils.unbind(this);
}

setModelConfig = (data) => {
compUtils.setModelConfig(this, data)
}

render () {
const allProps = {
...this.props,
...this.state
...this.state,
setModelConfig: this.setModelConfig
}
return <WrapComponent {...allProps} />
}
Expand Down
14 changes: 10 additions & 4 deletions templates/Project/components/_mdfComponent/controlComp.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import compUtils from './common';
export default (WrapComponent) => {
export default (WrapComponent, model) => {
return class ControlComp extends React.Component {
constructor (props) {
super();
}

componentDidMount () {
console.log('%c container didMount', 'color:red');
compUtils.bind(this);
compUtils.bind(this, model);
}

componentWillUnmount () {
Expand All @@ -21,7 +21,8 @@ export default (WrapComponent) => {
* @param {*} value 当前输入框值
*/
afterValueChange = (value) => {
this.props.model.setValue(value);
const currentModel = this.newModel || this.props.model;
currentModel.setValue(value);
}

/**
Expand All @@ -35,11 +36,16 @@ export default (WrapComponent) => {
});
}

setModelConfig = (data) => {
compUtils.setModelConfig(this, data)
}

render () {
const allProps = {
...this.props,
...this.state,
afterValueChange: this.afterValueChange
afterValueChange: this.afterValueChange,
setModelConfig: this.setModelConfig
}
return <WrapComponent {...allProps} />
}
Expand Down
26 changes: 25 additions & 1 deletion templates/Project/components/_mdfComponent/demo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,33 @@ import React from 'react';
import { Input } from 'yonui-ys';

import MDFComponent from './index';
import metaConfig from './metaConfig';
import MetaConfig from './metaConfig';

// api 说明
// {
// name: 字段属性名,
// type: 属性类型,[array, bool, func, number, string, element, node ]
// defaultValue: 默认值,
// isMust: 是否必填,
// desc: 属性描述
// }
const api = [
{
name: 'value',
defaultValue: 1,
isMust: true,
desc: '代表输入框值'
},
{
name: 'className',
defaultValue: 'default-class',
isMust: false,
desc: '控件样式名称'
}
]

@MDFComponent({type: 'control'}) // @mdf 标识,参数改成对象模式,type,绑定模型是什么
@MetaConfig({name: '控件名字', api: api})
class Demo extends React.Component {
constructor (props) {
super(props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import ButtonComp from './buttonComp'
import ContainerComp from './containerComp'

function createComp (compInfo = {}) {
const { type } = compInfo
console.log('type', type);
return function (targetCompent, name, descriptor) {
console.log(targetCompent, name, descriptor)
return getAbstractCompFactory(type, targetCompent)
return getAbstractCompFactory(compInfo, targetCompent)
}
}

function getAbstractCompFactory (type = 'control', targetCompent) {
function getAbstractCompFactory (compInfo, targetCompent) {
const { type, model } = compInfo
console.log('type', type);
switch (type) {
case 'control':
return ControlComp(targetCompent);
return ControlComp(targetCompent, model);
case 'container':
return ContainerComp(targetCompent);
return ContainerComp(targetCompent, model);
case 'button':
return ButtonComp(targetCompent);
default:
Expand Down
3 changes: 3 additions & 0 deletions templates/Project/components/_mdfComponent/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import MDFComponent from './factory';
import MetaConfig from './metaConfig';
export {MDFComponent, MetaConfig}
47 changes: 39 additions & 8 deletions templates/Project/components/_mdfComponent/metaConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
// const metaConfig = {
// setConfig: function (data) {
// console.log('metaConfig-------', data);
// }
// }
function metaConfig (data) {
console.log('metaConfig-------', data);
// import React from 'react';

export default function MetaConfig (metaInfo = {}) {
const { name, api } = metaInfo
console.log('name', name, 'api', api);
return function (targetComponent, name, descriptor) {
console.log(targetComponent, name, descriptor)
const {defaultProps, propTypes} = aynalyProps(api)
targetComponent.defaultProps = defaultProps;
targetComponent.propTypes = propTypes;
}
}
/**
* 解析api,将默认的属性同步到类上
* @param {array} api
* api 中每个元素item说明
* {
* name: 字段属性名,
* type: 属性类型,[array, bool, func, number, string, element, node ]
* defaultValue: 默认值,
* isMust: 是否必填,
* desc: 属性描述
* }
*/
function aynalyProps (api = []) {
const defaultProps = {};
const propTypes = {};
api.forEach(item => {
const name = item?.name
if (name) {
const { type, defaultValue } = item;
if (defaultValue || defaultValue == 0)
defaultProps[name] = defaultValue
if (type) {
propTypes[name] = type
}
}
})
return {defaultProps, propTypes};
}
export default metaConfig
Binary file modified templates/project.tgz
Binary file not shown.

0 comments on commit bf944b4

Please sign in to comment.