Skip to content

Commit

Permalink
fix: ensure access (#56)
Browse files Browse the repository at this point in the history
* fix: ensure access

* fix: access
  • Loading branch information
elrrrrrrr authored Nov 8, 2023
1 parent b1276f8 commit a60bd28
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ exports.install = async options => {
assert(Object.keys(packageLock).length, '[rapid] depsJSON invalid.');
await nydusd.startNydusFs(options.nydusMode, options.cwd, options.pkg);


await util.ensureAccess(options.cwd, packageLock);

// 存放原始依赖树,用于 npm 二次更新依赖
await util.storePackageLock(options.cwd, packageLock);

console.time('[rapid] run lifecycle scripts');
await options.scripts.runLifecycleScripts(mirrorConfig);
console.timeEnd('[rapid] run lifecycle scripts');
Expand Down
39 changes: 39 additions & 0 deletions packages/cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const debug = require('node:util').debuglog('rapid');
const path = require('node:path');
const assert = require('node:assert');
const fs = require('node:fs/promises');
const { existsSync } = require('node:fs');
const os = require('node:os');
Expand Down Expand Up @@ -434,6 +435,34 @@ function validDep(pkg, productionMode, arch, platform) {

}

exports.ensureAccess = async function ensureAccess(cwd, packageLock) {

let needAccess = false;

for (const [ pkgPath, pkgItem ] of Object.entries(packageLock.packages)) {
if (pkgPath.startsWith('node_modules') && !pkgItem.optional && !pkgItem.dev && !pkgItem.peer) {
needAccess = true;
break;
}
}

// 如果没有找到合适的检测点,直接返回
if (!needAccess) {
return;
}

await wrapRetry({
cmd: async () => {
const dirs = await fs.readdir(path.join(cwd, 'node_modules'));
assert(dirs.length > 0);
},
title: 'ensure node_modules access',
fallback: async () => {
console.warn('[rapid] ensure node_modules access failed');
},
});
};

exports.getAllPkgPaths = async function getAllPkgPaths(cwd, pkg) {
const workspaces = await exports.getWorkspaces(cwd, pkg);
const allPkgs = Object.values(workspaces);
Expand Down Expand Up @@ -608,6 +637,15 @@ exports.listMountInfo = async function listMountInfo() {
}).sort((a, b) => a.device.localeCompare(b.device));
};

async function storePackageLock(cwd, packageLock) {
const lockPath = path.join(cwd, 'node_modules', '.package-lock.json');
await fs.mkdir(path.dirname(lockPath), { recursive: true });
await fs.writeFile(
lockPath,
JSON.stringify(packageLock, null, 2)
);
}

exports.getWorkdir = getWorkdir;
exports.validDep = validDep;
exports.getDisplayName = getDisplayName;
Expand All @@ -628,3 +666,4 @@ exports.resolveBinMap = resolveBinMap;
exports.getFileEntryMode = getFileEntryMode;
exports.getEnv = getEnv;
exports.wrapRetry = wrapRetry;
exports.storePackageLock = storePackageLock;

0 comments on commit a60bd28

Please sign in to comment.