-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
重构 APPDevTool initTemplate模块
- Loading branch information
Showing
3 changed files
with
39 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 34 additions & 77 deletions
111
for-vue2/src/packages/plugins/APPDevTool/common/initTemplate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters