Skip to content

Commit

Permalink
fix: clean when install (#50)
Browse files Browse the repository at this point in the history
* fix: clean when install

* fix: clean when install

* fix: ci

* chore: test ci
  • Loading branch information
elrrrrrrr authored Nov 1, 2023
1 parent c44d1e1 commit 48d15f9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 54 deletions.
5 changes: 3 additions & 2 deletions integration/fixtures/esbuild/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"esbuild": "0.15.14"
}
}
},
"repository": "[email protected]:cnpm/rapid.git"
}
36 changes: 36 additions & 0 deletions integration/index.2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,40 @@ describe('test/index.v2.test.js', () => {
await assert.doesNotReject(fs.stat(path.join(cwd, 'node_modules', 'esbuild')));
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');
});

it('should auto clean when reinstall', async () => {
cwd = path.join(__dirname, './fixtures/esbuild');


await coffee
.fork(rapid, [
'install',
'--ignore-scripts',
], {
cwd,
})
.debug()
.expect('code', 0)
.end();

await coffee
.fork(rapid, [
'install',
'--ignore-scripts',
], {
cwd,
})
.debug()
.expect('code', 0)
.end();

// should use npm mode finally
const dirs = await fs.readdir(path.join(cwd, 'node_modules'));
assert.strictEqual(dirs.filter(dir => dir.includes('esbuild')).length, 2);
await assert.doesNotReject(fs.stat(path.join(cwd, 'node_modules/esbuild')));
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');

const res = await execa.command('mount', { stdio: 'pipe' });
assert(res.stdout.indexOf('integration/fixtures/esbuild/node_modules') === res.stdout.lastIndexOf('integration/fixtures/esbuild/node_modules'));
});
});
6 changes: 1 addition & 5 deletions packages/cli/bin/rapid.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { clean, install, list } = require('../lib/index.js');
const yargs = require('yargs');
const { NpmFsMode, NYDUS_TYPE } = require('../lib/constants.js');
const util = require('../lib/util');
const path = require('node:path');
const fuse_t = require('../lib/fuse_t');

yargs
Expand Down Expand Up @@ -68,10 +67,7 @@ yargs
aliases: [ 'c', 'unmount', 'uninstall' ],
describe: 'Clean up the project',
handler: async argv => {
let cwd = argv.path || process.cwd();
if (cwd.endsWith('node_modules') || cwd.endsWith('node_modules/')) {
cwd = path.dirname(cwd);
}
const cwd = argv.path || process.cwd();
await clean({ nydusMode: NYDUS_TYPE.FUSE, cwd, force: true });
console.log('[rapid] clean finished');
},
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ exports.install = async options => {
assert(Object.keys(packageLock).length, '[rapid] depsJSON invalid.');
await nydusd.startNydusFs(options.nydusMode, options.cwd, options.pkg);

console.time('[rapid] wait for access');
await util.ensureAccess(options.cwd, packageLock);
console.timeEnd('[rapid] wait for access');

console.time('[rapid] run lifecycle scripts');
await options.scripts.runLifecycleScripts(mirrorConfig);
console.timeEnd('[rapid] run lifecycle scripts');
Expand All @@ -74,10 +70,16 @@ exports.clean = async function clean({ nydusMode = NYDUS_TYPE.FUSE, cwd, force,
console.log('[rapid] no mount info found.');
return;
}

if (cwd.endsWith('node_modules') || cwd.endsWith('node_modules/')) {
cwd = path.dirname(cwd);
}

if (!pkg) {
const pkgRes = await util.readPkgJSON(cwd);
pkg = pkgRes.pkg;
}

await nydusd.endNydusFs(nydusMode, cwd, pkg, force);
};

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/lib/nydusd/fuse_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ ${nodeModulesDir}`);
${upper}=RW:${mnt}=RO \
${nodeModulesDir}`;
}
// console.info('[rapid] mountOverlay: `%s`', shScript);
// console.log('[rapid] mountOverlay: `%s`', shScript);
await execa.command(shScript);
bar.update(nodeModulesDir);
}));
Expand Down Expand Up @@ -164,6 +164,7 @@ async function endNydusFs(cwd, pkg, force = true) {
},
fallback: force
? async () => {
console.log(`[rapid] use fallback umount -f ${overlay}`);
await execa.command(`umount -f ${overlay}`);
}
: undefined,
Expand Down
48 changes: 6 additions & 42 deletions packages/cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,43 +423,6 @@ function validDep(pkg, productionMode, arch, platform) {

}

exports.ensureAccess = async function ensureAccess(cwd, packageLock) {
let access = false;
let targetPath;

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

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

let retry = 0;

// 如果找到了检测点,但是检测点不存在,等待检测点创建
while (!access) {
try {
await fs.access(targetPath);
access = true;
} catch (e) {
retry++;
console.log(
`[rapid] still waiting for access ${targetPath} retry after 50ms, retry count: ${retry}`
);
if (retry > 40) {
console.error(`[rapid] wait for access ${targetPath} timeout`);
throw e;
}
await this.sleep(50);
}
}
};

exports.getAllPkgPaths = async function getAllPkgPaths(cwd, pkg) {
const workspaces = await exports.getWorkspaces(cwd, pkg);
const allPkgs = Object.values(workspaces);
Expand Down Expand Up @@ -604,18 +567,19 @@ exports.readPackageLock = async function readPackageLock(cwd) {
};

// 列出当前 mount 的 fuse endpoint
// 目前只支持 fuse-t
exports.listMountInfo = async function listMountInfo() {

const { stdout } = await execa('mount');
// 拆分输出为每行
const mountLines = stdout.split('\n');

// 只过滤 node_modules 相关挂载点
// mac 下为 fuse-t
// fuse-t:/ on /Users/elr/Desktop/rapid-test/node_modules (nfs, nodev, nosuid, mounted by elr)
// linux 下为 overlay
// overlay on /__w/rapid/rapid/integration/fixtures/esbuild/node_modules type overlay (rw,relatime,lowerdir=/github/home/.rapid/cache/mnt/esbuild_e50a1b13a3655a1355d2816f13a77e23/root_d41d8cd98f00b204e9800998ecf8427e,upperdir=/github/home/.rapid/cache/esbuild_e50a1b13a3655a1355d2816f13a77e23/root_d41d8cd98f00b204e9800998ecf8427e/overlay/upper,workdir=/github/home/.rapid/cache/esbuild_e50a1b13a3655a1355d2816f13a77e23/root_d41d8cd98f00b204e9800998ecf8427e/overlay/workdir)
return mountLines.filter(_ => {
if (_.includes(nydusdMnt)) {
return false;
}
return _.includes(baseRapidModeDir()) || _.startsWith('fuse');
return _.includes('node_modules') && (_.startsWith('fuse-t:/') || _.startsWith('overlay'));
}).map(line => {
const parts = line.split(' ');
const device = parts[0];
Expand Down

0 comments on commit 48d15f9

Please sign in to comment.