Skip to content

Commit

Permalink
fix: 重构 APPDevTool initTemplate模块
Browse files Browse the repository at this point in the history
重构 APPDevTool initTemplate模块
  • Loading branch information
hewx815 committed Oct 26, 2023
1 parent 9c9317e commit b4d4fd6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ export default async function androidServer(argvs: Argvs) {
}
} else {
const defaultProjectPath = resolve(process.cwd(), './android');

projectPath = defaultProjectPath;

if (!checkPathExists(defaultProjectPath)) {
initTemplate({ initAndroid: true }, 'android');
initTemplate(projectPath, 'android');
}
projectPath = defaultProjectPath;
}

const resourceDir = userConfig.resourceDir || resolve(process.cwd(), './dist/build/app-plus');
Expand Down
111 changes: 34 additions & 77 deletions for-vue2/src/packages/plugins/APPDevTool/common/initTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,52 @@
import path from 'path';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { type Argvs } from '../index.js';
import {
log, err, checkPathExists, copyDir,
} from '../utils.js';

// TODO:此处逻辑有问题,需重构
export default function init(argvs: Argvs, platform: 'ios' | 'android' | 'all' = 'all') {
const baseIosTemplatePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../template/ios');

const baseAndroidTemplatePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../template/android');

const iosInitPath = path.resolve(process.cwd(), 'ios');

const androidInitPath = path.resolve(process.cwd(), 'android');

let userInitPath = process.cwd();

// 初始化初始化路径
if (argvs.initPath === true) {
err('initPath 参数需要指定路径');
return;
function initProject(
templatePath: string,
initPath: string,
) {
if (!checkPathExists(templatePath)) {
log(`缺少模板文件:${templatePath}`);
}

if (argvs.initPath) {
userInitPath = argvs.initPath;

if (checkPathExists(path.resolve(userInitPath, 'ios'))) {
log(`${userInitPath} 目录已存在, 请先删除`);
return;
}

if (checkPathExists(path.resolve(userInitPath, 'android'))) {
log(`${userInitPath} 目录已存在, 请先删除`);
return;
}

log('开始初始化模板');

copyDir(baseIosTemplatePath, path.resolve(userInitPath, 'ios'));
copyDir(baseAndroidTemplatePath, path.resolve(userInitPath, 'android'));

log('初始化模板成功');
} else {
switch (platform) {
case 'all':
if (checkPathExists(androidInitPath)) {
log(`${androidInitPath} 目录已存在, 请先删除`);
return;
}

if (checkPathExists(iosInitPath)) {
log(`${iosInitPath} 目录已存在,请先删除`);
return;
}

log('开始初始化模板');

copyDir(baseIosTemplatePath, iosInitPath);
copyDir(baseAndroidTemplatePath, androidInitPath);

log('初始化模板成功');
break;
if (checkPathExists(initPath)) {
log(`${initPath} 目录已存在, 请先删除`);
}

case 'android':
if (checkPathExists(androidInitPath)) {
log(`${androidInitPath} 目录已存在, 请先删除`);
return;
}
copyDir(templatePath, initPath);
}

log('开始初始化模板');
export default function initTemplate(
projectPath: string,
platform: 'ios' | 'android' | 'all',
) {
const iosTemplatePath = resolve(dirname(fileURLToPath(import.meta.url)), '../template/ios');

copyDir(baseAndroidTemplatePath, androidInitPath);
const androidTemplatePath = resolve(dirname(fileURLToPath(import.meta.url)), '../template/android');

log('初始化模板成功');
break;
const iosInitPath = resolve(projectPath, '');

case 'ios':
if (checkPathExists(iosInitPath)) {
log(`${iosInitPath} 目录已存在,请先删除`);
}
const androidInitPath = resolve(projectPath, '');

log('开始初始化模板');
switch (platform) {
case 'all':
initProject(androidTemplatePath, androidInitPath);
initProject(iosTemplatePath, iosInitPath);
break;

copyDir(baseIosTemplatePath, iosInitPath);
case 'android':
initProject(androidTemplatePath, androidInitPath);
break;

log('初始化模板成功');
break;
case 'ios':
initProject(iosTemplatePath, iosInitPath);
break;

default:
break;
}
default:
err('不支持的 platform');
break;
}
}
24 changes: 1 addition & 23 deletions for-vue2/src/packages/plugins/APPDevTool/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import iosServer from './iosServer/index.js';
import androidServer from './androidServer/index.js';
import { filterArgs } from './utils.js';
import { initTemplate, help } from './common/index.js';
import { help } from './common/index.js';

export interface Argvs {
init?: string | true;
initAndroid?: string | true;
initIos?: string | true;
initPath?: string | true;
dev?: string | true;
devAndroid?: string | true;
devIos?: string | true;
Expand All @@ -19,24 +15,6 @@ export interface Argvs {
const argvs: Argvs = filterArgs(process.argv);

async function start() {
// 初始化模板
if (argvs.init) {
initTemplate(argvs);
return;
}

// 初始化安卓模板
if (argvs.initAndroid) {
initTemplate(argvs, 'android');
return;
}

// 初始化苹果模板
if (argvs.initIos) {
initTemplate(argvs, 'ios');
return;
}

if (argvs.dev) {
await androidServer(argvs);
await iosServer(argvs);
Expand Down

0 comments on commit b4d4fd6

Please sign in to comment.