Skip to content

Commit

Permalink
Add script to replace taobao npm registry
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar committed May 29, 2024
1 parent 984f585 commit d9735f0
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
107 changes: 107 additions & 0 deletions scripts/replace-taobao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const { recursive } = require('./util');
const { existsSync, statSync, readJSON, writeJSON } = require('fs-extra');

const enginePath = process.argv[2];

console.log(`enginePath: ${enginePath}`);

if (!enginePath) {
process.exit(-1);
}

const FOLDER_DIR = enginePath;
const MATCH_REG =
/^https?:\/\/registry\.npm\.taobao\.org\/((?:(?:@\w+?\/)?[^\/])*?)\//;
const lockPathList = []; // 存放package.log文件路径

// 遍历查询所有的.lock文件
const queryPackageLockFile = () => {
recursive(FOLDER_DIR, (file) => {
if (!file.includes('node_modules')) {
lockPathList.push(file);
}
});
};

// 获取网上对应的依赖的信息
const fetchNpmPackageInfo = async (name, version) => {
const result = await fetch(
`https://registry.npmmirror.com/${name}/${version}`,
);
if (result.status === 200) {
const data = await result.json();
if (data) return data.dist;

return undefined;
}

return undefined;
};

const replacePackageLockFile = async (
packageLockJsonFile,
packageKey = 'packages',
) => {
if (!existsSync(packageLockJsonFile)) return false;
const stat = statSync(packageLockJsonFile);
if (!stat.isFile()) return false;

const json = await readJSON(packageLockJsonFile);

if (!json[packageKey]) return true;
const packageKeys = Object.keys(json[packageKey]);
let changed = false;
let count = 0;
for (let i = 0, len = packageKeys.length; i < len; i++) {
const key = packageKeys[i];
const target = json[packageKey][key];
if (!target.resolved) continue;

const regList = target.resolved.match(MATCH_REG);
if (regList && regList[1]) {
const value = json[packageKey][key];
const { version } = value;
const dist = await fetchNpmPackageInfo(regList[1], version);
if (!dist) {
console.log(
`【Error: 服务端未找到依赖 ${regList[1]}, 版本 ${version}】`,
);
continue;
}

if (
!value ||
!value['integrity'] ||
value['integrity'] !== dist.integrity
) {
json[packageKey][key]['resolved'] = dist.tarball;
json[packageKey][key]['integrity'] = dist.integrity;
count++;
changed = true;
}
}
}

if (changed) {
await writeJSON(packageLockJsonFile, json, { spaces: 2 });
console.log(`【Info: ${packageLockJsonFile},修改${count}个依赖,】`);
}
return true;
};

const main = async () => {
console.log('【Info: 扫描package-lock.json文件】');
queryPackageLockFile();
console.log(`【Info: 扫描完成, 总共有${lockPathList.length}个文件】`);
console.log(lockPathList);

for (let i = 0, len = lockPathList.length; i < len; i++) {
// 匹配新版的package-lock.json文件
await replacePackageLockFile(lockPathList[i]);

// 匹配老版的package-lock.json文件
await replacePackageLockFile(lockPathList[i], 'dependencies');
}
};

main();
22 changes: 22 additions & 0 deletions scripts/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { basename, join } = require('path');
const { statSync, readdirSync } = require('fs-extra');

const recursive = (file, handle) => {
if (!file) return;
const stat = statSync(file);
const filename = basename(file);
if (stat.isDirectory()) {
const list = readdirSync(file);
list.forEach((name) => {
recursive(join(file, name), handle);
});
}
// 忽略 meta 文件
else if (filename === 'package-lock.json') {
handle(file);
}
};

module.exports = {
recursive,
};
44 changes: 44 additions & 0 deletions scripts/version_compare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

# Given version
version="3.8.3"

# Version to compare
compare_version="$1"

# Function to execute if the version is less than the given version
function do_something {
echo "Executing task for version less than $version"
exit 1
}

# Function to execute if the version is not less than the given version
function do_something_else {
echo "Executing task for version not less than $version"
exit 0
}

# Split the version numbers into arrays
IFS='.' read -r -a version_parts <<< "$version"
IFS='.' read -r -a compare_version_parts <<< "$compare_version"

# Compare major version
if [[ ${compare_version_parts[0]} -lt ${version_parts[0]} ]]; then
do_something
elif [[ ${compare_version_parts[0]} -gt ${version_parts[0]} ]]; then
do_something_else
else
# Major version is the same, compare minor version
if [[ ${compare_version_parts[1]} -lt ${version_parts[1]} ]]; then
do_something
elif [[ ${compare_version_parts[1]} -gt ${version_parts[1]} ]]; then
do_something_else
else
# Minor version is the same, compare patch version
if [[ ${compare_version_parts[2]} -lt ${version_parts[2]} ]]; then
do_something
else
do_something_else
fi
fi
fi

0 comments on commit d9735f0

Please sign in to comment.