代码拉取完成,页面将自动刷新
同步操作将从 nashaofu/dingtalk 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
const fs = require('fs')
const axios = require('axios')
const chalk = require('chalk')
const http = require('http')
const https = require('https')
const registryUrl = require('registry-url')
const registryAuthToken = require('registry-auth-token')
const httpAgent = new http.Agent({
keepAlive: true,
maxSockets: 50
})
const httpsAgent = new https.Agent({
keepAlive: true,
maxSockets: 50
})
/**
* 把一个长的并发一步任务转换为
* 一个切片形式的串行任务
* @param {Array} tasks 任务数据
* @return {Promise<Array>} Promise对象按切片执行结果
*/
function parallelToSerial (tasks) {
const reslut = []
async function next () {
// 如果数据执行完之后就直接返回
if (!tasks.length) return reslut
// 执行处理逻辑
reslut.push(await tasks.shift()())
// 循环下一个切片
await next()
return reslut
}
return next()
}
/**
* 拉取最新的包
* @param {*} pkg
* @param {*} pkgInfo
*/
async function getPackageVersion (pkg, pkgInfo) {
console.log(`get ${pkg} ...`)
const scope = pkg.split('/')[0]
const registry = registryUrl(scope)
const authInfo = registryAuthToken(registry, { recursive: true })
const headers = {
accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'
}
if (authInfo) {
headers.authorization = `${authInfo.type} ${authInfo.token}`
}
const time = Date.now()
try {
const { data } = await axios.get(`${encodeURIComponent(pkg).replace(/^%40/, '@')}/latest`, {
baseURL: registry,
headers,
httpAgent,
httpsAgent
})
console.log(
chalk.bgGreen.black(' DONE '),
JSON.stringify(
{
...pkgInfo,
id: pkg,
status: 200,
time: Date.now() - time
},
null,
2
)
)
return data.version
} catch (e) {
const status = ((e || {}).response || {}).status
console.log(
chalk.bgRed.black(' ERROR '),
JSON.stringify(
{
...pkgInfo,
id: pkg,
status: status || e.response,
time: Date.now() - time
},
null,
2
)
)
}
}
/**
* 比较版本
* @param {*} pkg
* @param {*} type
*/
function diffVersion (pkg, type) {
return Object.keys(pkg[type]).map(key => async () => {
// 排除内部依赖
const version = await getPackageVersion(key, {
name: pkg.name,
type,
version: pkg[type][key]
})
pkg[type][key] = version ? `^${version}` : pkg[type][key]
})
}
const pkg = require('./package.json')
const dependencies = diffVersion(pkg, 'dependencies')
const devDependencies = diffVersion(pkg, 'devDependencies')
parallelToSerial(dependencies)
.then(() => parallelToSerial(devDependencies))
.then(() => {
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2))
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。