diff --git a/.gitignore b/.gitignore index 02d06c0..fd8a342 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ /.nyc_output /npm-debug.log /.idea -/package-lock.json \ No newline at end of file +/package-lock.json +/declaration \ No newline at end of file diff --git a/.npmignore b/.npmignore index fa34123..fa7d323 100644 --- a/.npmignore +++ b/.npmignore @@ -9,4 +9,5 @@ /tests /.idea /package-lock.json -/packages \ No newline at end of file +/packages +/declaration \ No newline at end of file diff --git a/package.json b/package.json index 7a30dcd..fb23d31 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "pri-plugin-dob", - "version": "1.1.6", - "types": "src/index.tsx", + "version": "2.0.0", + "types": "src/index.ts", "main": "dist/index.js", "scripts": { "start": "pri dev", @@ -16,16 +16,16 @@ "format": "tslint --fix './src/**/*.?(ts|tsx)' && prettier --write './src/**/*.?(ts|tsx)'" }, "devDependencies": { - "pri": "^1.0.0" + "pri": "^2.0.3" }, "pri": { - "web-entry": [ - "./dist/web-store/index.js", - "./dist/web-menu/index.js" - ], - "type": "plugin" + "type": "plugin", + "version": "2.0.4" }, "dependencies": { "@babel/runtime": "^7.0.0" + }, + "tnpm": { + "mode": "npm" } } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..ccf6cad --- /dev/null +++ b/src/index.ts @@ -0,0 +1,3 @@ +export const getPlugin = () => import('./plugin'); + +export const getUIPlugins = () => [import('./web-menu'), import('./web-store')]; diff --git a/src/index.tsx b/src/plugin/index.ts similarity index 83% rename from src/index.tsx rename to src/plugin/index.ts index d95531a..62c5af4 100644 --- a/src/index.tsx +++ b/src/plugin/index.ts @@ -2,8 +2,7 @@ import * as fs from 'fs-extra'; import * as _ from 'lodash'; import * as normalizePath from 'normalize-path'; import * as path from 'path'; -import * as prettier from 'prettier'; -import { pri, storesPath, tempJsEntryPath, tempPath, tempTypesPath } from 'pri'; +import { pri, storesPath, tempJsEntryPath, tempTypesPath } from 'pri'; import { addStore } from './methods'; const LAYOUT_TEMP = 'LayoutTempComponent'; @@ -23,18 +22,18 @@ interface IResult { }; } -export default async (instance: typeof pri) => { +export const entry = (instance: typeof pri) => { const storeFilePath = path.join(instance.projectRootPath, tempTypesPath.dir, 'stores.ts'); const storeFilePathInfo = path.parse(storeFilePath); - instance.build.pipeConfig(config => { - if (!config.resolve.alias) { - config.resolve.alias = {}; + instance.build.pipeConfig(buildConfig => { + if (!buildConfig.resolve.alias) { + buildConfig.resolve.alias = {}; } - config.resolve.alias['pri/stores'] = storeFilePath; + buildConfig.resolve.alias['pri/stores'] = storeFilePath; - return config; + return buildConfig; }); const whiteList = ['src/stores']; @@ -75,7 +74,7 @@ export default async (instance: typeof pri) => { } as IResult; }); - instance.project.onCreateEntry((analyseInfo: IResult, entry) => { + instance.project.onCreateEntry(async (analyseInfo: IResult, entryInfo) => { if (analyseInfo.projectAnalyseDob.storeFiles.length === 0) { if (fs.existsSync(storeFilePath)) { fs.removeSync(storeFilePath); @@ -85,15 +84,15 @@ export default async (instance: typeof pri) => { } // Connect normal pages - entry.pipe.set('normalPagesImportEnd', importEnd => { + entryInfo.pipe.set('normalPagesImportEnd', importEnd => { return ` ${importEnd}.then(component => Connect()(component.default)) `; }); // Connect layout - entry.pipe.set('analyseLayoutImportName', text => LAYOUT_TEMP); - entry.pipe.set('analyseLayoutBody', body => { + entryInfo.pipe.set('analyseLayoutImportName', text => LAYOUT_TEMP); + entryInfo.pipe.set('analyseLayoutBody', body => { return ` ${body} const ${LAYOUT} = Connect()(${LAYOUT_TEMP}) @@ -101,8 +100,8 @@ export default async (instance: typeof pri) => { }); // Connect markdown layout - entry.pipe.set('analyseMarkdownLayoutImportName', text => MARKDOWN_LAYOUT_TEMP); - entry.pipe.set('analyseMarkdownLayoutBody', body => { + entryInfo.pipe.set('analyseMarkdownLayoutImportName', text => MARKDOWN_LAYOUT_TEMP); + entryInfo.pipe.set('analyseMarkdownLayoutBody', body => { return ` ${body} const ${MARKDOWN_LAYOUT} = Connect()(${MARKDOWN_LAYOUT_TEMP}) @@ -113,7 +112,7 @@ export default async (instance: typeof pri) => { path.relative(path.join(tempJsEntryPath.dir), path.join(storeFilePathInfo.dir, storeFilePathInfo.name)) ); - entry.pipeAppHeader(header => { + entryInfo.pipeAppHeader(header => { return ` ${header} import { useStrict } from "dob" @@ -122,14 +121,14 @@ export default async (instance: typeof pri) => { `; }); - entry.pipeAppBody(body => { + entryInfo.pipeAppBody(body => { return ` ${body} useStrict() `; }); - entry.pipeAppRouter(router => { + entryInfo.pipeAppRouter(router => { return ` ${router} @@ -161,6 +160,8 @@ export default async (instance: typeof pri) => { export { stores } `; + const prettier = await import('prettier'); + // If has stores, create helper.ts fs.outputFileSync( storeFilePath, diff --git a/src/methods.ts b/src/plugin/methods.ts similarity index 57% rename from src/methods.ts rename to src/plugin/methods.ts index 4ef5bcf..5787e88 100644 --- a/src/methods.ts +++ b/src/plugin/methods.ts @@ -1,25 +1,26 @@ -import * as fs from "fs-extra" -import * as _ from "lodash" -import * as path from "path" -import * as prettier from "prettier" -import { storesPath } from "pri" +import * as fs from 'fs-extra'; +import * as _ from 'lodash'; +import * as path from 'path'; +import { storesPath } from 'pri'; export async function addStore( projectRootPath: string, options: { - name: string - withDemo: boolean + name: string; + withDemo: boolean; } ) { - const camelName = _.camelCase(options.name) - const camelUpperFirstName = _.upperFirst(camelName) - const kebabName = _.kebabCase(options.name) - const fileFullPath = path.join(projectRootPath, storesPath.dir, kebabName) + ".tsx" + const camelName = _.camelCase(options.name); + const camelUpperFirstName = _.upperFirst(camelName); + const kebabName = _.kebabCase(options.name); + const fileFullPath = path.join(projectRootPath, storesPath.dir, kebabName) + '.tsx'; if (fs.existsSync(fileFullPath)) { - throw Error(`${kebabName} already exist!`) + throw Error(`${kebabName} already exist!`); } + const prettier = await import('prettier'); + fs.outputFileSync( fileFullPath, prettier.format( @@ -28,7 +29,7 @@ export async function addStore( @observable export class ${camelUpperFirstName}Store { - ${options.withDemo ? `public testValue = 1` : ""} + ${options.withDemo ? `public testValue = 1` : ''} } export class ${camelUpperFirstName}Action { @@ -41,14 +42,14 @@ export async function addStore( this.${camelName}Store.testValue++ } ` - : "" + : '' } } `, { semi: false, - parser: "typescript" + parser: 'typescript' } ) - ) + ); } diff --git a/src/utils/md5.ts b/src/utils/md5.ts deleted file mode 100644 index 96e1f03..0000000 --- a/src/utils/md5.ts +++ /dev/null @@ -1,8 +0,0 @@ -import * as crypto from "crypto" - -export function md5(str: string) { - return crypto - .createHash("md5") - .update(str, "utf8") - .digest("hex") -} diff --git a/src/utils/npm.d.ts b/src/utils/npm.d.ts deleted file mode 100644 index 0af2bfc..0000000 --- a/src/utils/npm.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'normalize-path'; diff --git a/src/web-menu/index.tsx b/src/web-menu/index.tsx index 12ef5d7..6589ad6 100644 --- a/src/web-menu/index.tsx +++ b/src/web-menu/index.tsx @@ -1,23 +1,23 @@ -import { Icon } from "antd" -import { Connect } from "dob-react" -import * as React from "react" -import { NewStoreComponent } from "./new-store/new-store.component" -import * as S from "./style" -import { Props, State } from "./type" +import { Icon } from 'antd'; +import { Connect } from 'dob-react'; +import * as React from 'react'; +import { NewStoreComponent } from './new-store/new-store.component'; +import * as S from './style'; +import { Props, State } from './type'; -const TreeIcon = (props: any) => +const TreeIcon = (props: any) => ; @Connect class View extends React.Component { - public static defaultProps = new Props() - public state = new State() + public static defaultProps = new Props(); + public state = new State(); public render() { - return + return ; } } export default { - position: "menu", + position: 'menu', view: View -} +}; diff --git a/src/web-menu/new-store/form.tsx b/src/web-menu/new-store/form.tsx index d15a0dc..c9d637c 100644 --- a/src/web-menu/new-store/form.tsx +++ b/src/web-menu/new-store/form.tsx @@ -1,10 +1,10 @@ -import { Button, Form, Input, Switch } from "antd" -import { Connect } from "dob-react" -import * as React from "react" -import { Props, State } from "./new-store.type" -import * as S from "./style" +import { Button, Form, Input, Switch } from 'antd'; +import { Connect } from 'dob-react'; +import * as React from 'react'; +import { Props, State } from './new-store.type'; +import * as S from './style'; -const FormItem = Form.Item +const FormItem = Form.Item; const formItemLayout = { labelCol: { @@ -15,7 +15,7 @@ const formItemLayout = { xs: { span: 24 }, sm: { span: 16 } } -} +}; const tailFormItemLayout = { wrapperCol: { @@ -28,40 +28,40 @@ const tailFormItemLayout = { offset: 8 } } -} +}; function hasErrors(fieldsError: any) { - return Object.keys(fieldsError).some((field: string) => fieldsError[field]) + return Object.keys(fieldsError).some((field: string) => fieldsError[field]); } @Connect class FormComponent extends React.PureComponent { - public static defaultProps = new Props() - public state = new State() + public static defaultProps = new Props(); + public state = new State(); public render() { return (
- {this.props.form.getFieldDecorator("name", { - initialValue: "application", + {this.props.form.getFieldDecorator('name', { + initialValue: 'application', rules: [ { - type: "string", - message: "Name must be string!" + type: 'string', + message: 'Name must be string!' }, { required: true, - message: "Name is required!" + message: 'Name is required!' } ] })()} - {this.props.form.getFieldDecorator("withDemo", { + {this.props.form.getFieldDecorator('withDemo', { initialValue: true, - valuePropName: "checked" + valuePropName: 'checked' })()} @@ -71,14 +71,14 @@ class FormComponent extends React.PureComponent { - ) + ); } private handleSubmit = async (e: any) => { - e.preventDefault() - await this.props.ApplicationAction.addStore(this.props.form.getFieldsValue()) - this.props.onSuccess() - } + e.preventDefault(); + await this.props.ApplicationAction.addStore(this.props.form.getFieldsValue()); + this.props.onSuccess(); + }; } -export default Form.create()(FormComponent as any) as any +export default Form.create()(FormComponent as any) as any; diff --git a/src/web-menu/new-store/new-store.component.tsx b/src/web-menu/new-store/new-store.component.tsx index 82ee364..80dd58e 100644 --- a/src/web-menu/new-store/new-store.component.tsx +++ b/src/web-menu/new-store/new-store.component.tsx @@ -1,15 +1,15 @@ -import { Modal } from "antd" -import { Connect } from "dob-react" -import * as React from "react" -import { Props, State } from "./new-store.type" -import * as S from "./style" +import { Modal } from 'antd'; +import { Connect } from 'dob-react'; +import * as React from 'react'; +import { Props, State } from './new-store.type'; +import * as S from './style'; -import FormComponent from "./form" +import FormComponent from './form'; @Connect export class NewStoreComponent extends React.PureComponent { - public static defaultProps = new Props() - public state = new State() + public static defaultProps = new Props(); + public state = new State(); public render() { return ( @@ -29,22 +29,22 @@ export class NewStoreComponent extends React.PureComponent { - ) + ); } private showModal = () => { this.setState({ visible: true - }) - } + }); + }; private handleOk = () => { this.setState({ visible: false - }) - } + }); + }; private handleCancel = () => { this.setState({ visible: false - }) - } + }); + }; } diff --git a/src/web-menu/new-store/new-store.type.ts b/src/web-menu/new-store/new-store.type.ts index 6d37b82..ac67108 100644 --- a/src/web-menu/new-store/new-store.type.ts +++ b/src/web-menu/new-store/new-store.type.ts @@ -1,11 +1,11 @@ export class Props { - public form?: any + public form?: any; public onSuccess?: () => void = () => { // - } - [x: string]: any + }; + [x: string]: any; } export class State { - public visible = false + public visible = false; } diff --git a/src/web-menu/new-store/style.tsx b/src/web-menu/new-store/style.tsx index a23ec76..cd9f5b7 100644 --- a/src/web-menu/new-store/style.tsx +++ b/src/web-menu/new-store/style.tsx @@ -1,12 +1,12 @@ -import { Icon } from "antd" -import * as React from "react" -import styled from "styled-components" +import { Icon } from 'antd'; +import * as React from 'react'; +import styled from 'styled-components'; -export const MenuIcon = (props: any) => +export const MenuIcon = (props: any) => ; export const Container = styled.div` display: flex; -` +`; export const Button = styled.div` display: flex; @@ -21,4 +21,4 @@ export const Button = styled.div` &:hover { background-color: whitesmoke; } -` +`; diff --git a/src/web-menu/style.ts b/src/web-menu/style.ts index 2b081ca..c338983 100644 --- a/src/web-menu/style.ts +++ b/src/web-menu/style.ts @@ -1,3 +1,3 @@ -import styled from "styled-components" +import styled from 'styled-components'; -export const Container = styled.div`` +export const Container = styled.div``; diff --git a/src/web-menu/type.ts b/src/web-menu/type.ts index d1cf6e7..8290052 100644 --- a/src/web-menu/type.ts +++ b/src/web-menu/type.ts @@ -1,5 +1,5 @@ export class Props { - [x: string]: any + [x: string]: any; } export class State {} diff --git a/src/web-store/index.tsx b/src/web-store/index.tsx index a26d796..34b800a 100644 --- a/src/web-store/index.tsx +++ b/src/web-store/index.tsx @@ -1,31 +1,31 @@ -import { Icon } from "antd" -import { Connect } from "dob-react" -import * as React from "react" -import * as S from "./style" -import { Props, State } from "./type" +import { Icon } from 'antd'; +import { Connect } from 'dob-react'; +import * as React from 'react'; +import * as S from './style'; +import { Props, State } from './type'; -const TreeIcon = (props: any) => +const TreeIcon = (props: any) => ; @Connect class View extends React.Component { - public static defaultProps = new Props() - public state = new State() + public static defaultProps = new Props(); + public state = new State(); public render() { - return Stores Info: TODO + return Stores Info: TODO; } } export default { - position: "tree-stores", + position: 'tree-stores', view: View, init: (applicationAction: any) => { - applicationAction.event.on("freshProjectStatus", () => { - const dobInfo = applicationAction.applicationStore.status.analyseInfo.projectAnalyseDob + applicationAction.event.on('freshProjectStatus', () => { + const dobInfo = applicationAction.applicationStore.status.analyseInfo.projectAnalyseDob; if (dobInfo) { applicationAction.pipeTreeNode((treeData: any) => { treeData[0].children.push({ - key: "stores", + key: 'stores', title: `Stores (${dobInfo.storeFiles.length})`, icon: , disabled: dobInfo.storeFiles.length === 0, @@ -34,12 +34,12 @@ export default { key: index, title: storeFile.name, icon: - } + }; }) - }) - return treeData - }) + }); + return treeData; + }); } - }) + }); } -} +}; diff --git a/src/web-store/style.ts b/src/web-store/style.ts index 2b081ca..c338983 100644 --- a/src/web-store/style.ts +++ b/src/web-store/style.ts @@ -1,3 +1,3 @@ -import styled from "styled-components" +import styled from 'styled-components'; -export const Container = styled.div`` +export const Container = styled.div``; diff --git a/src/web-store/type.ts b/src/web-store/type.ts index d1cf6e7..8290052 100644 --- a/src/web-store/type.ts +++ b/src/web-store/type.ts @@ -1,5 +1,5 @@ export class Props { - [x: string]: any + [x: string]: any; } export class State {} diff --git a/tslint.json b/tslint.json index ffb38a8..e39869c 100644 --- a/tslint.json +++ b/tslint.json @@ -20,6 +20,7 @@ "no-implicit-dependencies": false, "no-object-literal-type-assertion": false, "no-submodule-imports": false, - "no-empty": true + "no-empty": true, + "interface-name": false } }