初始化
This commit is contained in:
105
scripts/develop.js
Normal file
105
scripts/develop.js
Normal file
@@ -0,0 +1,105 @@
|
||||
const { spawn } = require('child_process')
|
||||
const readline = require('readline')
|
||||
|
||||
class DevelopClientScript {
|
||||
constructor() {
|
||||
if (DevelopClientScript.instance) {
|
||||
return DevelopClientScript.instance
|
||||
}
|
||||
DevelopClientScript.instance = this
|
||||
}
|
||||
|
||||
promptUser(question) {
|
||||
return new Promise((resolve) => {
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
})
|
||||
rl.question(question, (res) => {
|
||||
resolve(res)
|
||||
rl.close()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async runClient() {
|
||||
console.error('请选择你需要运行的客户端(回复数字后回车)')
|
||||
console.error('0.取消')
|
||||
console.error('1.微信小程序')
|
||||
console.error('2.公众号或者H5')
|
||||
const runClientRes = await this.promptUser('请输入运行的客户端:')
|
||||
switch (runClientRes) {
|
||||
case '0':
|
||||
break
|
||||
case '1':
|
||||
await this.runNpmScript('dev:mp-weixin')
|
||||
break
|
||||
case '2':
|
||||
await this.runNpmScript('dev:h5')
|
||||
break
|
||||
default:
|
||||
await this.runClient()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
runNpmScript(scriptName) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const isWindows = process.platform === 'win32'
|
||||
const command = isWindows ? 'cmd.exe' : 'npm'
|
||||
const args = isWindows
|
||||
? ['/c', 'npm', 'run', scriptName]
|
||||
: ['run', scriptName]
|
||||
|
||||
const runProcess = spawn(command, args)
|
||||
|
||||
runProcess.stdout.on('data', (data) => {
|
||||
console.log(data.toString())
|
||||
})
|
||||
|
||||
runProcess.stderr.on('data', (data) => {
|
||||
console.error(data.toString())
|
||||
})
|
||||
|
||||
runProcess.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
reject(
|
||||
new Error(
|
||||
`运行错误,请查看以下报错信息寻找解决方法: ${error.message}`
|
||||
)
|
||||
)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async run(targetVersion) {
|
||||
const currentVersion = process.versions.node
|
||||
|
||||
if (currentVersion < targetVersion) {
|
||||
throw new Error(
|
||||
`你的当前node版本为(${currentVersion}),需要安装目标版本为 ${targetVersion} 以上!!`
|
||||
)
|
||||
}
|
||||
|
||||
await this.runClient()
|
||||
}
|
||||
|
||||
static getInstance() {
|
||||
if (!DevelopClientScript.instance) {
|
||||
DevelopClientScript.instance = new DevelopClientScript()
|
||||
}
|
||||
return DevelopClientScript.instance
|
||||
}
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const develop = DevelopClientScript.getInstance()
|
||||
try {
|
||||
await develop.run('16.16.0')
|
||||
} catch (error) {
|
||||
console.error(error.message)
|
||||
}
|
||||
})()
|
||||
1
scripts/publish.js
Normal file
1
scripts/publish.js
Normal file
@@ -0,0 +1 @@
|
||||
const { spawn } = require('child_process')
|
||||
46
scripts/release.mjs
Normal file
46
scripts/release.mjs
Normal file
@@ -0,0 +1,46 @@
|
||||
import path from 'path'
|
||||
import fsExtra from 'fs-extra'
|
||||
import minimist from 'minimist'
|
||||
const { existsSync, remove, copy } = fsExtra
|
||||
const cwd = process.cwd()
|
||||
const argv = minimist(process.argv.slice(2), {
|
||||
alias: {
|
||||
target: 't',
|
||||
output: 'o'
|
||||
}
|
||||
})
|
||||
//打包发布路径,谨慎改动
|
||||
const releaseRelativePath = `../public/${argv.output}`
|
||||
const distPath = path.resolve(cwd, `dist/build/${argv.target}`)
|
||||
const releasePath = path.resolve(cwd, releaseRelativePath)
|
||||
|
||||
async function build() {
|
||||
if (existsSync(releasePath)) {
|
||||
await remove(releasePath)
|
||||
}
|
||||
console.log(
|
||||
`文件正在复制:dist/build/${argv.target} ==> ${releaseRelativePath}`
|
||||
)
|
||||
try {
|
||||
await copyFile(distPath, releasePath)
|
||||
} catch (error) {
|
||||
console.log(`\n ${error}`)
|
||||
}
|
||||
console.log(
|
||||
`文件已复制:dist/build/${argv.target} ==> ${releaseRelativePath}`
|
||||
)
|
||||
}
|
||||
|
||||
function copyFile(sourceDir, targetDir) {
|
||||
return new Promise((resolve, reject) => {
|
||||
copy(sourceDir, targetDir, (err) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
build()
|
||||
Reference in New Issue
Block a user