npm6分析

2019-12-18  本文已影响0人  小猋_a8f1

npm版本:6.12.1
缓存目录默认是~/.npm/_cacache, 有index-v5、content-v2、tmp子目录

以安装mkdirp为例分析, 找到下载的源码实际存储位置

npm v mkdirp

dist
.tarball: http://npmprivate.xxx.com/mkdirp/-/mkdirp-0.5.1.tgz
.shasum: 30057438eac6cf7f8c4767f38648d6697d75c903

grep -r "http://npmprivate.xxx.com/mkdirp/-/mkdirp-0.5.1.tgz" ~/.npm/_cacache/index-v5

/Users/xuezhijie/.npm/_cacache/index-v5/9a/31/0d6e1c20753f4caddf9a0f3255ae29844dd5720b8033df6b095199c7da57:52a4f41b14df35db86640b933fda71742ec6d4e2 {"key":"make-fetch-happen:request-cache:h
const cacache = require('cacache')
const fs = require('fs')

const cachePath = '/Users/xuezhijie/.npm/_cacache'

;(async function () {
  let data = await cacache.ls(cachePath)
  console.log(data)
  fs.writeFileSync('./cache.json', JSON.stringify(data))

  // await cacache.rm.all(cachePath)
  // console.log('removed');
})();

在生成的json文件找查找mkdirp

"make-fetch-happen:request-cache:http://npmprivate.xxx.com/mkdirp/-/mkdirp-0.5.1.tgz": {
    "key": "make-fetch-happen:request-cache:http://npmprivate.xxx.com/mkdirp/-/mkdirp-0.5.1.tgz",
    "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
    "path": "/Users/xuezhijie/.npm/_cacache/content-v2/sha1/30/05/7438eac6cf7f8c4767f38648d6697d75c903",
    "size": 4991,
    "time": 1576660727569,

path属性就是实际存储位置,验证下格式,发现是gzip,重名为tar.gz后解压就得到源码了

file /Users/xuezhijie/.npm/_cacache/content-v2/sha1/30/05/7438eac6cf7f8c4767f38648d6697d75c903

/Users/xuezhijie/.npm/_cacache/content-v2/sha1/30/05/7438eac6cf7f8c4767f38648d6697d75c903: gzip compressed data, from Unix, original size 38912
上一篇 下一篇

猜你喜欢

热点阅读