From 04a2d1147df81a11dae70be8fc24c96deaf1aca5 Mon Sep 17 00:00:00 2001 From: "Xc@centOs" Date: Wed, 17 Apr 2024 14:57:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86dependency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/create.js | 69 +++++++++++-- src/create.ts | 97 +++++++++++++++++-- template/src/configuration/dependency.ts | 2 +- template/src/configuration/relation.ts | 6 ++ template/src/entities/Store.ts | 12 +-- template/src/exceptionHandler.ts | 46 +-------- template/web/src/AppError.tsx | 26 +---- template/web/src/oak.config.json | 4 - template/web/src/types/router.ts | 2 +- templateFiles/SystemProvider.ts | 36 ------- .../src => templateFiles}/features/index.ts | 0 11 files changed, 170 insertions(+), 130 deletions(-) delete mode 100644 template/web/src/oak.config.json delete mode 100644 templateFiles/SystemProvider.ts rename {template/src => templateFiles}/features/index.ts (100%) diff --git a/lib/create.js b/lib/create.js index 0913ad4..84ce3d7 100644 --- a/lib/create.js +++ b/lib/create.js @@ -2,6 +2,9 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.update = exports.create = void 0; const tslib_1 = require("tslib"); +const ts = tslib_1.__importStar(require("typescript")); +const fs_1 = require("fs"); +const { factory } = ts; const file_handle_1 = require("./file-handle"); const enum_1 = require("./enum"); const config_1 = require("./config"); @@ -12,6 +15,7 @@ const axios_1 = tslib_1.__importDefault(require("axios")); const tip_style_1 = require("./tip-style"); const shelljs_1 = tslib_1.__importDefault(require("shelljs")); const rename_1 = require("./rename"); +const assert_1 = tslib_1.__importDefault(require("assert")); const DEFAULT_PROJECT_NAME = 'oak_template'; const DEFAULT_PROJECT_TITLE = 'oak template project'; const prompt = [ @@ -32,6 +36,30 @@ const prompt = [ message: 'description', }, ]; +/** + * 将项目的依赖关系加入 + */ +function addDependencies(dependencies) { + const dependencyFile = (0, path_1.join)(process.cwd(), 'src', 'configuration', 'dependency.ts'); + const program = ts.createProgram([ + dependencyFile, + ], {}); + const sourceFile = program.getSourceFile(dependencyFile); + const { statements } = sourceFile; + const stmt1 = statements[1]; + (0, assert_1.default)(ts.isVariableStatement(stmt1)); + const { declarationList: { declarations: [vd] } } = stmt1; + const { name, initializer } = vd; + (0, assert_1.default)(ts.isIdentifier(name) && name.text === 'dependencyConfiguration'); + (0, assert_1.default)(ts.isArrayLiteralExpression(initializer)); + Object.assign(initializer, { + elements: dependencies.map(ele => factory.createStringLiteral(ele)) + }); + const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); + const result = printer.printList(ts.ListFormat.SourceFileStatements, factory.createNodeArray(statements), sourceFile); + (0, fs_1.writeFileSync)(dependencyFile, result, { flag: 'w' }); + (0, tip_style_1.Success)((0, tip_style_1.success)(`${dependencyFile} has been built.`)); +} /** * @name 检查项目名是否已存在 * @param dirName @@ -60,12 +88,18 @@ async function getOakCliVersion() { * @returns */ async function getMiniVersion() { - const res = await axios_1.default.get(config_1.MINI_VERSION_URL); - const versions = JSON.parse(res.data['json_data'])['total']; - const versionsSort = versions.sort((a, b) => { - return b['percentage'] - a['percentage']; - }); - return versionsSort[0]['sdkVer']; + try { + const res = await axios_1.default.get(config_1.MINI_VERSION_URL); + const versions = JSON.parse(res.data['json_data'])['total']; + const versionsSort = versions.sort((a, b) => { + return b['percentage'] - a['percentage']; + }); + return versionsSort[0]['sdkVer']; + } + catch (err) { + (0, tip_style_1.Warn)((0, tip_style_1.warn)(`can't access a-o-s weixin mini version, using 3.4.1`)); + return '3.4.1'; + } } async function createWechatMpBoilplate(dir, isDev, isUpdate) { // 获取微信小程序稳定基础版本库 @@ -176,6 +210,29 @@ async function create(dirName, cmd) { shell.cd(dirName).exec('npm install'); */ (0, rename_1.renameProject)(rootPath, name, title, DEFAULT_PROJECT_NAME, DEFAULT_PROJECT_TITLE); (0, tip_style_1.Success)(`${(0, tip_style_1.success)(`Successfully created project ${(0, tip_style_1.primary)(name)}, directory name is ${(0, tip_style_1.primary)(dirName)}`)}`); + const { useOgb, moreDeps } = await inquirer_1.default.prompt([{ + name: 'useOgb', + type: 'confirm', + message: 'add oak-general-business into dependency?', + default: true, + }, { + name: 'moreDeps', + type: 'input', + message: 'do you have more dependent oak-family libraries? type their names, use comma as separator.', + default: '', + }]); + const deps = []; + if (useOgb) { + deps.push('oak-general-business'); + } + if (moreDeps) { + deps.push(...(moreDeps.split(',').map(ele => ele.trim()).filter(ele => !!ele))); + } + if (deps.length > 0) { + shelljs_1.default.cd(dirName); + addDependencies(deps); + } + (0, tip_style_1.Success)(`${(0, tip_style_1.success)(`Ok, type 'npm install' to install libs, then start!`)}`); } catch (err) { (0, tip_style_1.Error)((0, tip_style_1.error)('create error')); diff --git a/src/create.ts b/src/create.ts index 99024b9..e62c3c0 100644 --- a/src/create.ts +++ b/src/create.ts @@ -1,3 +1,7 @@ +import * as ts from 'typescript'; +import { writeFileSync } from 'fs'; +const { factory } = ts; + import { copyFolder, checkFileExistsAndCreate, @@ -39,8 +43,9 @@ import { warn, Warn, } from './tip-style'; -import shell from 'shelljs'; +import shell, { cat } from 'shelljs'; import { renameProject } from './rename'; +import assert from 'assert'; const DEFAULT_PROJECT_NAME = 'oak_template'; const DEFAULT_PROJECT_TITLE = 'oak template project'; @@ -63,6 +68,41 @@ const prompt = [ }, ]; +/** + * 将项目的依赖关系加入 + */ +function addDependencies(dependencies: string[]) { + const dependencyFile = join(process.cwd(), 'src', 'configuration', 'dependency.ts'); + const program = ts.createProgram([ + dependencyFile, + ], {}); + + const sourceFile = program.getSourceFile(dependencyFile); + + const { statements } = sourceFile!; + + const stmt1 = statements[1]; + assert(ts.isVariableStatement(stmt1)); + const { declarationList: { declarations: [ vd ]}} = stmt1; + const { name, initializer } = vd; + assert(ts.isIdentifier(name) && name.text === 'dependencyConfiguration'); + assert(ts.isArrayLiteralExpression(initializer!)); + Object.assign(initializer, { + elements: dependencies.map( + ele => factory.createStringLiteral(ele) + ) + }); + + const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); + const result = printer.printList( + ts.ListFormat.SourceFileStatements, + factory.createNodeArray(statements), + sourceFile!); + + writeFileSync(dependencyFile, result, { flag: 'w' }); + Success(success(`${dependencyFile} has been built.`)); +} + /** * @name 检查项目名是否已存在 * @param dirName @@ -95,12 +135,18 @@ async function getOakCliVersion() { * @returns */ async function getMiniVersion() { - const res = await axios.get(MINI_VERSION_URL); - const versions: Array = JSON.parse(res.data['json_data'])['total']; - const versionsSort = versions.sort((a: any, b: any) => { - return b['percentage'] - a['percentage']; - }); - return versionsSort[0]['sdkVer']; + try { + const res = await axios.get(MINI_VERSION_URL); + const versions: Array = JSON.parse(res.data['json_data'])['total']; + const versionsSort = versions.sort((a: any, b: any) => { + return b['percentage'] - a['percentage']; + }); + return versionsSort[0]['sdkVer']; + } + catch (err) { + Warn(warn(`can't access a-o-s weixin mini version, using 3.4.1`)); + return '3.4.1'; + } } @@ -277,11 +323,13 @@ export async function create(dirName: string, cmd: any) { Warn(warn('Sorry, this script requires npm! Please install npm!')); shell.exit(1); } + /* Success(`${success(`Waiting...`)}`); Success(`${success(`Dependencies are now being installed`)}`); shell.cd(dirName).exec('npm install'); */ renameProject(rootPath, name, title, DEFAULT_PROJECT_NAME, DEFAULT_PROJECT_TITLE); + Success( `${success( `Successfully created project ${primary( @@ -289,6 +337,41 @@ export async function create(dirName: string, cmd: any) { )}, directory name is ${primary(dirName)}` )}` ); + + const { useOgb, moreDeps } = await inquirer.prompt([{ + name: 'useOgb', + type: 'confirm', + message: 'add oak-general-business into dependency?', + default: true, + }, { + name: 'moreDeps', + type: 'input', + message: 'do you have more dependent oak-family libraries? type their names, use comma as separator.', + default: '', + }]); + const deps = [] as string[]; + if (useOgb) { + deps.push('oak-general-business'); + } + if (moreDeps) { + deps.push( + ...((moreDeps).split(',').map( + ele => ele.trim() + ).filter( + ele => !!ele + )) + ); + } + if (deps.length > 0) { + shell.cd(dirName); + addDependencies(deps); + } + + Success( + `${success( + `Ok, type 'npm install' to install libs, then start!` + )}` + ); } catch (err) { Error(error('create error')); Error(error(err)); diff --git a/template/src/configuration/dependency.ts b/template/src/configuration/dependency.ts index fac4af2..d165e12 100644 --- a/template/src/configuration/dependency.ts +++ b/template/src/configuration/dependency.ts @@ -1,5 +1,5 @@ import { DependencyConfiguration } from 'oak-domain/lib/types/Configuration'; -const dependencyConfiguration: DependencyConfiguration = ['oak-general-business']; +const dependencyConfiguration: DependencyConfiguration = []; export default dependencyConfiguration; diff --git a/template/src/configuration/relation.ts b/template/src/configuration/relation.ts index 365d73d..beb1da2 100644 --- a/template/src/configuration/relation.ts +++ b/template/src/configuration/relation.ts @@ -10,4 +10,10 @@ export const selectFreeEntities = [ ]; export const updateFreeDict: UpdateFreeDict = { +}; + +export default { + authDeduceRelationMap, + selectFreeEntities, + updateFreeDict, }; \ No newline at end of file diff --git a/template/src/entities/Store.ts b/template/src/entities/Store.ts index 840e007..978ca1f 100644 --- a/template/src/entities/Store.ts +++ b/template/src/entities/Store.ts @@ -6,16 +6,16 @@ import { SingleGeo, } from 'oak-domain/lib/types/DataType'; import { ActionDef, EntityDesc } from 'oak-domain/lib/types'; -import { Schema as Area } from 'oak-general-business/lib/entities/Area'; -import { Schema as ExtraFile } from 'oak-general-business/lib/entities/ExtraFile'; +// import { Schema as Area } from 'oak-general-business/lib/entities/Area'; +// import { Schema as ExtraFile } from 'oak-general-business/lib/entities/ExtraFile'; import { EntityShape } from 'oak-domain/lib/types/Entity'; export interface Schema extends EntityShape { coordinate: SingleGeo; //坐标 - area: Area; //地区 + // area: Area; //地区 name: String<32>; //名称 addrDetail: String<32>; //地址详情 - files: Array; //封面图 + // files: Array; //封面图 } type IAction = 'online' | 'offline' | 'disable'; @@ -58,10 +58,10 @@ const entityDesc: EntityDesc< name: '门店', attr: { coordinate: '位置', - area: '地区', + // area: '地区', name: '名称', addrDetail: '地址详情', - files: '照片', + // files: '照片', iState: '状态', }, action: { diff --git a/template/src/exceptionHandler.ts b/template/src/exceptionHandler.ts index f2347fd..ba44180 100644 --- a/template/src/exceptionHandler.ts +++ b/template/src/exceptionHandler.ts @@ -1,9 +1,3 @@ -import { - OakTokenExpiredException, - OakMobileUnsetException, - OakUserInfoUncompletedException, - OakUserInfoLoadingException, -} from 'oak-general-business'; import { OakException, OakUnloggedInException, @@ -35,42 +29,7 @@ export const handler = async (reason: any, features: AFD) => { { isGoBack: true }, true ); - } else if (reason instanceof OakTokenExpiredException) { - await features.token.logout(); - features.navigator.navigateTo( - { - url: '/login', - }, - { isGoBack: true }, - true - ); - } /* else if (reason instanceof OakUserUnpermittedException) { - features.navigator.redirectTo( - { - url: '/result/403', - }, - undefined, - true - ); - } */ else if (reason instanceof OakMobileUnsetException) { - features.navigator.navigateTo( - { - url: '/mobile/me', - }, - undefined, - true - ); - } else if (reason instanceof OakUserInfoUncompletedException) { - const userId = features.token.getUserId(); - features.navigator.navigateTo( - { - url: '/user/manager/upsert', - oakId: userId, - }, - undefined, - true - ); - } else if (reason instanceof OakInputIllegalException) { + } else if (reason instanceof OakInputIllegalException) { features.message.setMessage({ content: reason.message, type: 'error', @@ -80,9 +39,6 @@ export const handler = async (reason: any, features: AFD) => { content: reason.message, type: 'error', }); - } else if (reason instanceof OakUserInfoLoadingException) { - // 暂不处理,在oak-general-business的page的生命周期里,等token加载完成了会刷新页面的 - console.warn(reason); } else if (reason instanceof ExampleException) { console.log('在此处理ExampleException'); } else { diff --git a/template/web/src/AppError.tsx b/template/web/src/AppError.tsx index 9e32dcc..222e277 100644 --- a/template/web/src/AppError.tsx +++ b/template/web/src/AppError.tsx @@ -7,10 +7,9 @@ import { OakNetworkException, OakServerProxyException, } from 'oak-domain/lib/types/Exception'; -import { OakTokenExpiredException } from 'oak-general-business'; -import { ECode } from 'oak-general-business/es/types/ErrorPage'; +import { ECode } from 'oak-frontend-base/es/types/ErrorPage'; const ErrorPage = lazy( - () => import('oak-general-business/es/components/common/errorPage') + () => import('oak-frontend-base/es/components/errorPage') ); interface ErrorProps { @@ -21,27 +20,6 @@ function Error(props: ErrorProps) { const { error } = props; if (error instanceof OakException) { - if (error instanceof OakTokenExpiredException) { - return ( - - - - - - ); - } if (error instanceof OakNetworkException) { // 网络中断出现的异常 return ( diff --git a/template/web/src/oak.config.json b/template/web/src/oak.config.json deleted file mode 100644 index bb417b6..0000000 --- a/template/web/src/oak.config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "theme": { - } -} \ No newline at end of file diff --git a/template/web/src/types/router.ts b/template/web/src/types/router.ts index 94b651d..2b70b28 100644 --- a/template/web/src/types/router.ts +++ b/template/web/src/types/router.ts @@ -44,7 +44,7 @@ type RoutePath = string | undefined | null; type IsFirst = boolean; // 项目路径别名 -export type Project = '@project' | '@oak-general-business' | '@oak-frontend-base'; +export type Project = '@project'; // [项目别名, 文件path,[嵌套路由顶层path],设置根路由, 路由path] diff --git a/templateFiles/SystemProvider.ts b/templateFiles/SystemProvider.ts deleted file mode 100644 index 035dd8b..0000000 --- a/templateFiles/SystemProvider.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { - String, - Text, - Price -} from 'oak-domain/lib/types/DataType'; -import { EntityShape } from 'oak-domain/lib/types/Entity'; -import { Schema as System } from 'oak-general-business/lib/entities/System'; -import { Schema as ArticleMenu } from 'oak-general-business/lib/entities/ArticleMenu'; -import { EntityDesc } from 'oak-domain/lib/types'; - -export interface Schema extends EntityShape { - name: String<32>; - system: System; - systems?: System[]; - articleMenus?: ArticleMenu[]; -}; - -export type Relation = 'owner' | 'manager'; - -const entityDesc: EntityDesc = { - locales: { - zh_CN: { - name: '系统供应商', - attr: { - name: '名称', - system: '系统', - systems: '系统', - articleMenus: '文章分类', - }, - r: { - owner: '所有者', - manager: '管理员', - }, - }, - } -} \ No newline at end of file diff --git a/template/src/features/index.ts b/templateFiles/features/index.ts similarity index 100% rename from template/src/features/index.ts rename to templateFiles/features/index.ts