From 1be56a6c15580984bc34c3bbd3910d0d1c057284 Mon Sep 17 00:00:00 2001 From: Olga Shen Date: Fri, 11 Nov 2022 18:35:08 +0800 Subject: [PATCH] feat(src/cli.ts): add new command add-files --- README.md | 18 +++++++++++++++--- package-lock.json | 36 ++++++++++++++++++++++++++---------- package.json | 1 + rollup.config.js | 3 +++ src/cli.ts | 38 ++++++++++++++++++++++++++------------ 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 0d75e7a..17b879c 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,23 @@ console.log( ### CLI tool to add all files in a directory -```bash +``` $ export ESTUARY_API_KEY= -$ estuary-upload +$ estuary-upload add-dir +[ + ... + 'bafkreia2254bseihmsqw7fzxsk54p7nqmitn2ibhnjiafxtql6by54idv4', + 'bafkreigrov5qj25vp2vawsjlav5v3veihagovvddjodlydspmy2mtmks5q', + ... 900 more items +] +``` -... +### CLI tool to add specific files + +``` +$ estuary-upload -k add-files +[ + ... 'bafkreia2254bseihmsqw7fzxsk54p7nqmitn2ibhnjiafxtql6by54idv4', 'bafkreigrov5qj25vp2vawsjlav5v3veihagovvddjodlydspmy2mtmks5q', ... 900 more items diff --git a/package-lock.json b/package-lock.json index d7b764f..764bb4d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "MIT", "dependencies": { "axios": "^1.0.0", + "commander": "^9.4.1", "ipfs-car": "^0.8.1" }, "bin": { @@ -2553,11 +2554,12 @@ } }, "node_modules/commander": { - "version": "2.20.3", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", - "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", - "dev": true, - "optional": true + "version": "9.4.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-9.4.1.tgz", + "integrity": "sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==", + "engines": { + "node": "^12.20.0 || >=14" + } }, "node_modules/commondir": { "version": "1.0.1", @@ -7386,6 +7388,13 @@ "optionalDependencies": { "commander": "^2.20.3" } + }, + "node_modules/z-schema/node_modules/commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "dev": true, + "optional": true } }, "dependencies": { @@ -9326,11 +9335,9 @@ } }, "commander": { - "version": "2.20.3", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", - "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", - "dev": true, - "optional": true + "version": "9.4.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-9.4.1.tgz", + "integrity": "sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==" }, "commondir": { "version": "1.0.1", @@ -12922,6 +12929,15 @@ "lodash.get": "^4.4.2", "lodash.isequal": "^4.5.0", "validator": "^13.7.0" + }, + "dependencies": { + "commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "dev": true, + "optional": true + } } } } diff --git a/package.json b/package.json index ad9af9e..8afef2d 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "license": "MIT", "dependencies": { "axios": "^1.0.0", + "commander": "^9.4.1", "ipfs-car": "^0.8.1" }, "devDependencies": { diff --git a/rollup.config.js b/rollup.config.js index 7fa1e6f..37cb87b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -41,5 +41,8 @@ export default [ }, ], plugins: [typescript({ tsconfig: "./tsconfig.json" })], + external: [ + 'fs', 'commander', 'ipfs-car/pack/blob', 'ipfs-car/blockstore/memory', 'axios', + ], }, ]; diff --git a/src/cli.ts b/src/cli.ts index c8189f9..67716a6 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,25 +1,39 @@ import { Estuary } from './index.js'; import fs from 'fs'; +import { Command, Option, OptionValues } from 'commander'; -async function main() { - const args = process.argv.slice(2); - if (args.length !== 1) { - console.log( - 'Require target directory path argument. Example: estuary-upload .' - ); - return; - } - - const dirpath = args[0]; +async function addDir(dirpath: string, options: OptionValues, command: Command) { const files = await fs.promises.readdir(dirpath); - const apiKey = `${process.env.ESTUARY_API_KEY}`; + await addFiles(files.map((file) => `${dirpath}/${file}`), options, command); +} + +async function addFiles(files: Array, options: OptionValues, command: Command) { + const apiKey = command.optsWithGlobals().key; const estuary = new Estuary(apiKey); const cids = await Promise.all( - files.map((file) => estuary.addFromPath(`${dirpath}/${file}`)) + files.map((file) => estuary.addFromPath(file)) ); console.log(cids); } +async function main() { + const program = new Command('estuary-upload') + .addOption( + new Option('-k, --key ', 'Estuary API key') + .env('ESTUARY_API_KEY') + .makeOptionMandatory(true) + ); + program.command('add-dir') + .argument('') + .description('Add files in a directory.') + .action(addDir); + program.command('add-files') + .argument('') + .description('Add files.') + .action(addFiles); + await program.parseAsync(process.argv); +} + main() .then(() => process.exit(0)) .catch((error) => {