新数据
This commit is contained in:
4
scripts/一键-小程序转H5.bat
Normal file
4
scripts/一键-小程序转H5.bat
Normal file
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
setlocal EnableExtensions
|
||||
cd /d "%~dp0.."
|
||||
powershell -ExecutionPolicy Bypass -File ".\scripts\一键-小程序转H5.ps1" %*
|
||||
174
scripts/一键-小程序转H5.ps1
Normal file
174
scripts/一键-小程序转H5.ps1
Normal file
@@ -0,0 +1,174 @@
|
||||
param(
|
||||
[string]$SourceMiniProgram = "miniprogram",
|
||||
[string]$OutputDir = "h5-from-miniprogram"
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
# Avoid treating stderr from native tools (npm/npx) as terminating errors.
|
||||
if ($PSVersionTable.PSVersion.Major -ge 7) {
|
||||
$PSNativeCommandUseErrorActionPreference = $false
|
||||
}
|
||||
|
||||
function Step($msg) {
|
||||
Write-Host ""
|
||||
Write-Host "==> $msg" -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
function Test-CommandAvailable($cmd) {
|
||||
if (-not (Get-Command $cmd -ErrorAction SilentlyContinue)) {
|
||||
throw "Missing command: $cmd. Please install it first."
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-NativeChecked($command, $errorMessage) {
|
||||
cmd /d /c $command
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw $errorMessage
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-NativeAllowFail($command) {
|
||||
cmd /d /c $command
|
||||
return $LASTEXITCODE
|
||||
}
|
||||
|
||||
function New-TaroPageStub($taroSrcDir, $pagePath) {
|
||||
$fullWithoutExt = Join-Path $taroSrcDir $pagePath
|
||||
$parentDir = Split-Path $fullWithoutExt -Parent
|
||||
$baseName = Split-Path $fullWithoutExt -Leaf
|
||||
New-Item -ItemType Directory -Path $parentDir -Force | Out-Null
|
||||
|
||||
$jsPath = Join-Path $parentDir ($baseName + ".js")
|
||||
$configPath = Join-Path $parentDir ($baseName + ".config.js")
|
||||
$scssPath = Join-Path $parentDir ($baseName + ".scss")
|
||||
|
||||
if (-not (Test-Path $jsPath)) {
|
||||
@"
|
||||
import { View, Text } from '@tarojs/components'
|
||||
|
||||
export default function PageStub() {
|
||||
return (
|
||||
<View className='page-stub'>
|
||||
<Text>This page is auto-generated from mini program conversion.</Text>
|
||||
<Text>Please migrate page logic manually.</Text>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
"@ | Set-Content -Path $jsPath -Encoding UTF8
|
||||
}
|
||||
|
||||
if (-not (Test-Path $configPath)) {
|
||||
@"
|
||||
export default definePageConfig({
|
||||
navigationBarTitleText: 'Migrating...'
|
||||
})
|
||||
"@ | Set-Content -Path $configPath -Encoding UTF8
|
||||
}
|
||||
|
||||
if (-not (Test-Path $scssPath)) {
|
||||
@"
|
||||
.page-stub {
|
||||
min-height: 100vh;
|
||||
padding: 32px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
"@ | Set-Content -Path $scssPath -Encoding UTF8
|
||||
}
|
||||
}
|
||||
|
||||
$root = Resolve-Path (Join-Path $PSScriptRoot "..")
|
||||
$src = Join-Path $root $SourceMiniProgram
|
||||
|
||||
if (-not (Test-Path $src)) {
|
||||
throw "Mini program source directory not found: $src"
|
||||
}
|
||||
|
||||
Test-CommandAvailable "node"
|
||||
Test-CommandAvailable "npm"
|
||||
Test-CommandAvailable "npx"
|
||||
|
||||
$temp = Join-Path $env:TEMP ("mp2h5-" + [Guid]::NewGuid().ToString("N"))
|
||||
$workspace = Join-Path $temp "source"
|
||||
$target = Join-Path $root $OutputDir
|
||||
$logFile = Join-Path $target "convert.log"
|
||||
|
||||
Step "Prepare directories"
|
||||
New-Item -ItemType Directory -Path $workspace -Force | Out-Null
|
||||
New-Item -ItemType Directory -Path $target -Force | Out-Null
|
||||
|
||||
Step "Copy mini program source to temp directory"
|
||||
robocopy $src $workspace /E /XD node_modules /NFL /NDL /NJH /NJS /nc /ns /np | Out-Null
|
||||
|
||||
Push-Location $workspace
|
||||
try {
|
||||
Step "Install temporary dependencies for converter"
|
||||
Invoke-NativeChecked "npm init -y >nul 2>&1" "Failed to initialize temporary npm project."
|
||||
Invoke-NativeChecked "npm install -D eslint@8.57.1 eslint-plugin-taro@3.3.20 @tarojs/cli-convertor@3.6.40 --no-fund --no-audit" "Failed to install temporary dependencies."
|
||||
|
||||
Step "Run mini program to Taro conversion"
|
||||
$convertExit = Invoke-NativeAllowFail "npx taro-convert > ""$logFile"" 2>&1"
|
||||
|
||||
$taroDir = Join-Path $workspace "taroConvert"
|
||||
if (-not (Test-Path $taroDir)) {
|
||||
throw "taroConvert directory was not generated. Check log: $logFile"
|
||||
}
|
||||
|
||||
if ($convertExit -ne 0) {
|
||||
Write-Host "Converter exited with code $convertExit, but taroConvert exists. Continue with build." -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
Step "Fill missing pages with auto-generated stubs"
|
||||
$sourceAppJsonPath = Join-Path $workspace "app.json"
|
||||
$taroSrcDir = Join-Path $taroDir "src"
|
||||
if (Test-Path $sourceAppJsonPath) {
|
||||
$rawAppJson = Get-Content $sourceAppJsonPath -Raw
|
||||
$pageMatches = [regex]::Matches($rawAppJson, '"pages\/[^"]+"')
|
||||
foreach ($match in $pageMatches) {
|
||||
$page = $match.Value.Trim('"')
|
||||
$normalized = $page.Replace("/", "\")
|
||||
$pageJsPath = Join-Path $taroSrcDir ($normalized + ".js")
|
||||
if (-not (Test-Path $pageJsPath)) {
|
||||
New-TaroPageStub -taroSrcDir $taroSrcDir -pagePath $normalized
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Step "Install dependencies in converted Taro project"
|
||||
Push-Location $taroDir
|
||||
try {
|
||||
Invoke-NativeChecked "npm install --no-fund --no-audit >> ""$logFile"" 2>&1" "Failed to install dependencies in converted Taro project."
|
||||
|
||||
Step "Build H5 output"
|
||||
Invoke-NativeChecked "npx taro build --type h5 >> ""$logFile"" 2>&1" "H5 build failed. Check log."
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
Step "Export build artifacts"
|
||||
$dist = Join-Path $taroDir "dist"
|
||||
if (-not (Test-Path $dist)) {
|
||||
throw "No dist directory produced by H5 build. Check log: $logFile"
|
||||
}
|
||||
|
||||
$outDist = Join-Path $target "dist"
|
||||
$outProject = Join-Path $target "taro-project"
|
||||
|
||||
if (Test-Path $outDist) { Remove-Item $outDist -Recurse -Force }
|
||||
if (Test-Path $outProject) { Remove-Item $outProject -Recurse -Force }
|
||||
|
||||
robocopy $dist $outDist /E /NFL /NDL /NJH /NJS /nc /ns /np | Out-Null
|
||||
robocopy $taroDir $outProject /E /XD node_modules /NFL /NDL /NJH /NJS /nc /ns /np | Out-Null
|
||||
|
||||
Step "Done"
|
||||
Write-Host "H5 output: $outDist" -ForegroundColor Green
|
||||
Write-Host "Taro project: $outProject" -ForegroundColor Green
|
||||
Write-Host "Log file: $logFile" -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
Write-Host "Note: Native mini program to H5 conversion usually needs some manual fixes. Search 'error' in convert.log first." -ForegroundColor Yellow
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
Reference in New Issue
Block a user