Skip to content

Commit

Permalink
fix: 对yk-cli做请求超时处理
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Oct 30, 2019
1 parent 14bea0c commit b2711d1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/yk-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yk-cli",
"version": "2.7.1",
"version": "2.7.2",
"description": "ykcli for ssr",
"main": "./lib/index.js",
"module": "./es/index.js",
Expand Down
29 changes: 17 additions & 12 deletions packages/yk-cli/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ const isTest = process.env.NODE_ENV === 'test'
* @returns {Promise<void>}
*/
export async function updateCli (): Promise<void> {
const { version } = await getWithPromise(url)
const remoteVersion = version.trim()
const localVersion = require(resolveApp('./package.json')).version.trim()
// 成功拿到版本号 且 版本号与本地版本号不一致则执行更新
if (remoteVersion !== localVersion) {
const spinner = ora('发现本地版本较旧,尝试更新yk-cli脚手架')
if (!isTest) {
spinner.start()
const { stdout } = await execWithPromise(`npm i -g --registry=https://registry.npm.taobao.org yk-cli@${version}`)
console.log(stdout, `更新完毕... 请您重新执行 ykcli init`)
spinner.succeed()
process.exit()
let localVersion
try {
localVersion = require(resolveApp('./package.json')).version.trim()
const { version } = await getWithPromise(url)
const remoteVersion = version.trim()
// 成功拿到版本号 且 版本号与本地版本号不一致则执行更新
if (remoteVersion !== localVersion) {
const spinner = ora('发现本地版本较旧,尝试更新yk-cli脚手架')
if (!isTest) {
spinner.start()
const { stdout } = await execWithPromise(`npm i -g --registry=https://registry.npm.taobao.org yk-cli@${version}`)
console.log(stdout, `更新完毕... 请您重新执行 ykcli init`)
spinner.succeed()
process.exit()
}
}
} catch (error) {
console.log('\x1b[91m' + `获取最新版本失败本次创建将使用本地版本${localVersion},若版本较旧请手动安装最新版本npm i -g yk-cli@latest或连接代理重试`)
}
}
33 changes: 18 additions & 15 deletions packages/yk-cli/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ export const processError = (err: string) => {
process.exit()
}
}

export const execWithPromise = promisify(exec)

export const downloadWithPromise = promisify(download)

export const resolveApp = (source: string) => {
// 以根目录为基准
return path.resolve(__dirname, `../../${source}`)
}

export const getWithPromise = (url: string): Promise<any> => {
return new Promise((resolve, reject) => {
let data: string = ''
Expand All @@ -37,10 +27,19 @@ export const getWithPromise = (url: string): Promise<any> => {
res.on('end', () => {
resolve(JSON.parse(data))
})
}).on('error', () => reject())
}).on('error', () => reject()).setTimeout(2000, () => reject())
})
}

export const execWithPromise = promisify(exec)

export const downloadWithPromise = promisify(download)

export const resolveApp = (source: string) => {
// 以根目录为基准
return path.resolve(__dirname, `../../${source}`)
}

/**
* http
* 缓存判断是否有效处理
Expand All @@ -52,10 +51,14 @@ export async function getVersionEffective (option: Optional): Promise<boolean> {
if (fs.existsSync(resolveApp('./cache'))) {
const url = option.language === 'typescript' ? tsUrl : jsUrl
const language = option.language === 'javascript' ? 'js' : 'ts'
const { version } = await getWithPromise(url)
const localVersion = require(resolveApp(`./cache/example/ssr-with-${language}/package.json`)).version.trim()
// 如果版本一样就不用更新
return version.trim() === localVersion
try {
const { version } = await getWithPromise(url)
const localVersion = require(resolveApp(`./cache/example/ssr-with-${language}/package.json`)).version.trim()
// 如果版本一样就不用更新
return version.trim() === localVersion
} catch (error) {
return true
}
}
return false
}
Expand Down

0 comments on commit b2711d1

Please sign in to comment.