Skip to content

Commit

Permalink
fix: update args
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Jan 30, 2024
1 parent fd207ae commit 3d99ac3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 44 deletions.
2 changes: 1 addition & 1 deletion integration/index.2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe('test/index.v2.test.js', () => {
.fork(rapid, [
'install',
'--ignore-scripts',
'--nodaemon',
'--daemon=false',
], {
cwd,
})
Expand Down
16 changes: 9 additions & 7 deletions packages/cli/bin/rapid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -62,7 +63,7 @@ const argv = yargs
nydusMode: NYDUS_TYPE.FUSE,
ignoreScripts,
productionMode,
nodaemon,
daemon,
});

Alert.success('🚀 Success', [
Expand All @@ -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');
},
})
Expand Down
38 changes: 21 additions & 17 deletions packages/cli/lib/deamon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -42,7 +41,8 @@ const checkDeamonAlive = async () => {
timeout: 1000,
});
return result.status === 200;
} catch (_) {
} catch (error) {
debug('checkDeamonAlive error: ', error);
return false;
}
};
Expand All @@ -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',
Expand All @@ -86,15 +89,16 @@ 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',
contentType: 'json',
socketPath: deamonSocketPath,
});
return result.status === 200 && result.data?.code === 0;
} catch (_) {
} catch (error) {
debug('addProject error: ', error);
return false;
}
};
Expand Down Expand Up @@ -127,7 +131,8 @@ const killDeamon = async () => {
socketPath: deamonSocketPath,
});
return result.status === 200;
} catch (_) {
} catch (error) {
debug('killDeamon error: ', error);
return false;
}
};
Expand Down Expand Up @@ -180,8 +185,6 @@ root:
deamonAutoLauncher.enable();
} catch (e) {
console.log(e);
} finally {
await runDeamon();
}
};

Expand All @@ -197,9 +200,10 @@ const initDeamon = async () => {

try {
await fs.stat(destinationFilePath);
await runDeamon();
} catch (e) {
await registerDeamon();
} finally {
await runDeamon();
}
};

Expand Down
8 changes: 4 additions & 4 deletions packages/cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand All @@ -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);
Expand All @@ -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.');
Expand All @@ -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 () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/lib/nydusd/fuse_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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 => {
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/lib/nydusd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions packages/deamon/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down

0 comments on commit 3d99ac3

Please sign in to comment.