環(huán)境
node v16.10.0 npm v8.3.0
在使用npm
管理第三方模塊時,這些模塊通常會被安裝在 node_modules
目錄下。當(dāng)我們需要把模塊給其他小伙伴或者搭建npm
私服時,需要將node_modules
的所有模塊生成N個packname-version.tgz
文件,方便其他小伙伴使用npm install packname-version.tgz
命令安裝或者使用npm publish packname-version.tgz
命令發(fā)布到私服時,這個nodejs
腳本或許對你有一些幫助??。
下面是實現(xiàn)這一功能的腳本,具體實現(xiàn)過程如下:
1. 遍歷目錄,查找所有的package.json文件
首先,我們需要遍歷node_modules
目錄,查找所有的 package.json
文件,這里我們使用遞歸實現(xiàn)。
function findPackageJson(directory) {
const results = [];
const files = fs.readdirSync(directory);
for (const file of files) {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
results.push(...findPackageJson(filePath));
} else if (file === "package.json") {
results.push(filePath);
}
}
return results;
}
2. 執(zhí)行 npm pack 命令,生成 .tgz 文件
接下來,我們需要執(zhí)行 npm pack
命令,生成一個壓縮文件,這里我們使用 child_process
模塊的 exec
方法來執(zhí)行命令。
function packModule(moduleDir) {
return new Promise((resolve, reject) => {
exec("npm pack", { cwd: moduleDir }, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
const tgzFile = path.join(moduleDir, `${stdout.trim()}`);
resolve(tgzFile);
}
});
});
}
在執(zhí)行完 npm pack
命令后,會在當(dāng)前目錄下生成一個以模塊名為前綴的壓縮文件,例如:lodash-4.17.21.tgz
。
3. 移動 .tgz 文件到 packs 目錄
最后,我們需要將生成的壓縮文件移動到指定的目錄下,這里我們使用 fs
模塊的 renameSync
方法實現(xiàn)。
function moveTgzFile(tgzFile) {
const fileName = path.basename(tgzFile);
const destFile = path.join(packsDir, fileName);
fs.renameSync(tgzFile, destFile);
console.log(`Moved ${fileName} to ${destFile}`);
}
將以上三個步驟結(jié)合起來,就可以實現(xiàn)將所有依賴包打包成壓縮文件,并將其移動到指定目錄下的功能了。
完整代碼
// 根據(jù) node modules下的依賴 利用npm pack 命令生成xxx.tgz依賴包 并放在packs目錄下
const fs = require("fs");
const path = require("path");
const { exec } = require("child_process");
// C:\Users\Lenovo\Desktop\pack\node_modules
const modulesDir = path.join(__dirname, "node_modules");
const packsDir = path.join(__dirname, "packs");
// 遍歷目錄,查找所有的 package.json 文件
function findPackageJson(directory) {
const results = [];
const files = fs.readdirSync(directory);
for (const file of files) {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
results.push(...findPackageJson(filePath));
} else if (file === "package.json") {
results.push(filePath);
}
}
return results;
}
// 執(zhí)行 `npm pack` 命令,生成 `.tgz` 文件
function packModule(moduleDir) {
return new Promise((resolve, reject) => {
exec("npm pack", { cwd: moduleDir }, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
const tgzFile = path.join(moduleDir, `${stdout.trim()}`);
resolve(tgzFile);
}
});
});
}
// 移動 `.tgz` 文件到 `packs` 目錄
function moveTgzFile(tgzFile) {
const fileName = path.basename(tgzFile);
const destFile = path.join(packsDir, fileName);
fs.renameSync(tgzFile, destFile);
console.log(`Moved ${fileName} to ${destFile}`);
}
// 查找所有的 package.json 文件,執(zhí)行 `npm pack`,并移動生成的 `.tgz` 文件
findPackageJson(modulesDir).forEach((packageJsonFile) => {
const moduleDir = path.dirname(packageJsonFile);
packModule(moduleDir)
.then(moveTgzFile)
.catch((err) => console.error(`Error packing ${moduleDir}: ${err.message}`));
});
思考文章來源:http://www.zghlxwxcb.cn/news/detail-769319.html
當(dāng)node_modules
里的模塊很多時,這個腳本的運行時間可能會很長(old cpu
??),所以還有很多優(yōu)化空間,仔細(xì)觀察node_modules
文件夾的目錄結(jié)構(gòu)會發(fā)現(xiàn)當(dāng)目錄名稱包含@
符號時,這個目錄下可能存在多個依賴包 ,普通目錄一個依賴包。所以findPackageJson
方法可以做如下優(yōu)化:文章來源地址http://www.zghlxwxcb.cn/news/detail-769319.html
// 遍歷目錄 當(dāng)目錄下存在 package.json 文件時 退出當(dāng)前循環(huán)
function findPackageJson(directory) {
const results = [];
const files = fs.readdirSync(directory);
for (const file of files) {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
const packagePath = path.join(filePath, "package.json")
if (fs.existsSync(packagePath)) {
results.push(packagePath);
continue;
}
results.push(...findPackageJson(filePath));
}
}
return results;
}
到了這里,關(guān)于npm pack 命令生成離線npm模塊/npm依賴包的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!