From 3d99ac3a404d9710a69d7d8e2724af6ad3dfd895 Mon Sep 17 00:00:00 2001 From: akitaSummer Date: Tue, 30 Jan 2024 17:43:38 +0800 Subject: [PATCH] fix: update args --- integration/index.2.test.js | 2 +- packages/cli/bin/rapid.js | 16 +++++++----- packages/cli/lib/deamon.js | 38 +++++++++++++++------------- packages/cli/lib/index.js | 8 +++--- packages/cli/lib/nydusd/fuse_mode.js | 10 ++++---- packages/cli/lib/nydusd/index.js | 8 +++--- packages/deamon/src/server.rs | 12 ++++----- 7 files changed, 50 insertions(+), 44 deletions(-) diff --git a/integration/index.2.test.js b/integration/index.2.test.js index cfc8462..bdd2346 100644 --- a/integration/index.2.test.js +++ b/integration/index.2.test.js @@ -236,7 +236,7 @@ describe('test/index.v2.test.js', () => { .fork(rapid, [ 'install', '--ignore-scripts', - '--nodaemon', + '--daemon=false', ], { cwd, }) diff --git a/packages/cli/bin/rapid.js b/packages/cli/bin/rapid.js index b5350a5..cef7e7b 100755 --- a/packages/cli/bin/rapid.js +++ b/packages/cli/bin/rapid.js @@ -33,16 +33,17 @@ const argv = yargs type: 'array', default: [], }) - .option('nodaemon', { + .option('daemon', { describe: 'Will not run deamon', type: 'boolean', + default: true, }); }, handler: async argv => { const ignoreScripts = argv['ignore-scripts']; const mode = argv.by || NpmFsMode.NPM; const productionMode = argv.production || argv.omit.includes('dev') || process.env.NODE_ENV === 'production'; - const nodaemon = argv.nodaemon; + const daemon = argv.daemon; const cwd = process.cwd(); const pkgRes = await util.readPkgJSON(); @@ -62,7 +63,7 @@ const argv = yargs nydusMode: NYDUS_TYPE.FUSE, ignoreScripts, productionMode, - nodaemon, + daemon, }); Alert.success('🚀 Success', [ @@ -80,15 +81,16 @@ const argv = yargs describe: 'Clean up the project', builder: yargs => { return yargs - .option('nodaemon', { - describe: 'Will not run deamon', + .option('daemon', { + describe: 'Will run deamon', type: 'boolean', + default: true, }); }, handler: async argv => { const cwd = argv.path || process.cwd(); - const nodaemon = argv.nodaemon; - await clean({ nydusMode: NYDUS_TYPE.FUSE, cwd, force: true, nodaemon }); + const daemon = argv.daemon; + await clean({ nydusMode: NYDUS_TYPE.FUSE, cwd, force: true, daemon }); console.log('[rapid] clean finished'); }, }) diff --git a/packages/cli/lib/deamon.js b/packages/cli/lib/deamon.js index 61bd8c2..a2e2a4e 100644 --- a/packages/cli/lib/deamon.js +++ b/packages/cli/lib/deamon.js @@ -27,12 +27,11 @@ const rapidDeamon = rsBindingPath const destinationFilePath = path.join(deamonDir, 'rapid_deamon'); -// TODO -const daemonPoint = 'http://localhost:33889'; + +const daemonPoint = 'http://unix'; const aliveUrl = `${daemonPoint}/alive`; const killUrl = `${daemonPoint}/kill`; -const delUrl = `${daemonPoint}/del-project`; -const addUrl = `${daemonPoint}/add-project`; +const projectUrl = `${daemonPoint}/project`; const checkDeamonAlive = async () => { try { @@ -42,7 +41,8 @@ const checkDeamonAlive = async () => { timeout: 1000, }); return result.status === 200; - } catch (_) { + } catch (error) { + debug('checkDeamonAlive error: ', error); return false; } }; @@ -55,21 +55,24 @@ const delProject = async projectName => { debug('delProject error: ', error); return false; } - + const configPath = path.join(metadataDir, `${projectName}.json`); try { - const configPath = path.join(metadataDir, `${projectName}.json`); const configBuffer = await fs.readFile(configPath); - config = JSON.parse(configBuffer.toString()); + } catch (error) { + debug('parse json error: ', error); + } + + try { await fs.rm(`${configPath}`); } catch (error) { - debug('delProject error: ', error); + debug('rm json error: ', error); return false; } try { - const result = await urllib.request(`${delUrl}`, { - method: 'POST', + const result = await urllib.request(`${projectUrl}`, { + method: 'DELETE', data: { projectPath: config.projectPath }, dataType: 'json', contentType: 'json', @@ -86,7 +89,7 @@ const addProject = async config => { try { await fs.mkdir(metadataDir, { recursive: true }); await fs.writeFile(path.join(metadataDir, `${config.projectName}.json`), JSON.stringify(config, null, 2)); - const result = await urllib.request(`${addUrl}`, { + const result = await urllib.request(`${projectUrl}`, { method: 'POST', data: config, dataType: 'json', @@ -94,7 +97,8 @@ const addProject = async config => { socketPath: deamonSocketPath, }); return result.status === 200 && result.data?.code === 0; - } catch (_) { + } catch (error) { + debug('addProject error: ', error); return false; } }; @@ -127,7 +131,8 @@ const killDeamon = async () => { socketPath: deamonSocketPath, }); return result.status === 200; - } catch (_) { + } catch (error) { + debug('killDeamon error: ', error); return false; } }; @@ -180,8 +185,6 @@ root: deamonAutoLauncher.enable(); } catch (e) { console.log(e); - } finally { - await runDeamon(); } }; @@ -197,9 +200,10 @@ const initDeamon = async () => { try { await fs.stat(destinationFilePath); - await runDeamon(); } catch (e) { await registerDeamon(); + } finally { + await runDeamon(); } }; diff --git a/packages/cli/lib/index.js b/packages/cli/lib/index.js index c11e639..2b9f101 100644 --- a/packages/cli/lib/index.js +++ b/packages/cli/lib/index.js @@ -36,7 +36,7 @@ exports.install = async options => { cwd: mountedInfo.mountPoint, pkg: options.pkg, force: true, - nodaemon: options.nodaemon, + daemon: options.daemon, }); console.timeEnd(`[rapid] ${nodeModulesDir} already mounted, try to clean`); } @@ -58,7 +58,7 @@ exports.install = async options => { await downloadDependency.download(options); assert(Object.keys(packageLock).length, '[rapid] depsJSON invalid.'); - await nydusd.startNydusFs(options.nydusMode, options.cwd, options.pkg, options.nodaemon); + await nydusd.startNydusFs(options.nydusMode, options.cwd, options.pkg, options.daemon); await util.ensureAccess(options.cwd, packageLock); @@ -71,7 +71,7 @@ exports.install = async options => { console.timeEnd('[rapid] run lifecycle scripts'); }; -exports.clean = async function clean({ nydusMode = NYDUS_TYPE.FUSE, cwd, force, pkg, nodaemon }) { +exports.clean = async function clean({ nydusMode = NYDUS_TYPE.FUSE, cwd, force, pkg, daemon }) { const listInfo = await util.listMountInfo(); if (!listInfo.length) { console.log('[rapid] no mount info found.'); @@ -87,7 +87,7 @@ exports.clean = async function clean({ nydusMode = NYDUS_TYPE.FUSE, cwd, force, pkg = pkgRes.pkg; } - await nydusd.endNydusFs(nydusMode, cwd, pkg, force, nodaemon); + await nydusd.endNydusFs(nydusMode, cwd, pkg, force, daemon); }; exports.list = async () => { diff --git a/packages/cli/lib/nydusd/fuse_mode.js b/packages/cli/lib/nydusd/fuse_mode.js index 2cd2e9d..4b5c577 100644 --- a/packages/cli/lib/nydusd/fuse_mode.js +++ b/packages/cli/lib/nydusd/fuse_mode.js @@ -30,10 +30,10 @@ const getProjectName = cwd => { return hashedFolderName; }; -async function startNydusFs(cwd, pkg, nodaemon) { +async function startNydusFs(cwd, pkg, daemon) { await nydusdApi.initDaemon(); - if (!nodaemon) { + if (daemon) { await initDeamon(); } @@ -51,7 +51,7 @@ async function startNydusFs(cwd, pkg, nodaemon) { console.log('[rapid] mount overlay, it may take a few seconds'); await mountOverlay(cwd, pkg, deamonConfig); - if (!nodaemon) { + if (daemon) { await addProject(deamonConfig); } } @@ -183,10 +183,10 @@ ${nodeModulesDir}`; config.overlays = overlays; } -async function endNydusFs(cwd, pkg, force = true, nodaemon) { +async function endNydusFs(cwd, pkg, force = true, daemon) { const allPkgs = await getAllPkgPaths(cwd, pkg); const umountCmd = force ? 'umount -f' : 'umount'; - if (!nodaemon) { + if (daemon) { await delProject(getProjectName(cwd)); } await Promise.all(allPkgs.map(async pkgPath => { diff --git a/packages/cli/lib/nydusd/index.js b/packages/cli/lib/nydusd/index.js index b92515e..914c628 100644 --- a/packages/cli/lib/nydusd/index.js +++ b/packages/cli/lib/nydusd/index.js @@ -31,20 +31,20 @@ exports.unregisterMode = function(mode) { fsImplMap[mode] = null; }; -exports.startNydusFs = async function(mode, cwd, pkg, nodaemon) { +exports.startNydusFs = async function(mode, cwd, pkg, daemon) { const impl = fsImplMap[mode]; assert(impl, `can not find fs impl for mode: ${mode}`); - await impl.start(cwd, pkg, nodaemon); + await impl.start(cwd, pkg, daemon); }; -exports.endNydusFs = async function(mode, cwd, pkg, force, nodaemon) { +exports.endNydusFs = async function(mode, cwd, pkg, force, daemon) { if (!mode || mode === NYDUS_TYPE.NATIVE) { console.log('[rapid] nydusd is not running, skip clean'); return; } const impl = fsImplMap[mode]; assert(impl, `can not find fs impl for mode: ${mode}`); - await impl.end(cwd, pkg, force, nodaemon); + await impl.end(cwd, pkg, force, daemon); }; exports.getNydusMode = async function(cwd) { diff --git a/packages/deamon/src/server.rs b/packages/deamon/src/server.rs index 36a6303..f17237e 100644 --- a/packages/deamon/src/server.rs +++ b/packages/deamon/src/server.rs @@ -5,7 +5,7 @@ use axum::{ extract::{connect_info, State}, http::{Request, StatusCode}, response::Json, - routing::{get, post}, + routing::{delete, get, post}, Router, }; use hyper_util::{ @@ -116,8 +116,8 @@ fn app( sender, }; Router::new() - .route("/add-project", post(handle_add)) - .route("/del-project", post(handle_del)) + .route("/project", post(handle_add)) + .route("/project", delete(handle_del)) .route("/alive", get(handle_alive)) .route("/kill", get(handle_kill)) .with_state(Arc::new(Mutex::new(state))) @@ -242,8 +242,8 @@ mod test { let response = app .oneshot( Request::builder() - .method(http::Method::POST) - .uri("/del-project") + .method(http::Method::DELETE) + .uri("/project") .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .body(Body::from(r#"{"projectPath":"mock_project"}"#)) .unwrap(), @@ -268,7 +268,7 @@ mod test { .oneshot( Request::builder() .method(http::Method::POST) - .uri("/add-project") + .uri("/project") .header(http::header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) .body(Body::from(r#"{"projectName":"mock_project","projectPath":"test_project","bootstraps":[],"nydusdApiMount":[],"overlays":[]}"#)) .unwrap(),