Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from numbersprotocol/feature-cli-add-files
Browse files Browse the repository at this point in the history
feat(src/cli.ts): add new command add-files
  • Loading branch information
bafu authored Nov 13, 2022
2 parents 8b34062 + 1be56a6 commit 5ec3923
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 25 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ console.log(

### CLI tool to add all files in a directory

```bash
```
$ export ESTUARY_API_KEY=<api-key>
$ estuary-upload <file-dirpath>
$ estuary-upload add-dir <dirpath>
[
...
'bafkreia2254bseihmsqw7fzxsk54p7nqmitn2ibhnjiafxtql6by54idv4',
'bafkreigrov5qj25vp2vawsjlav5v3veihagovvddjodlydspmy2mtmks5q',
... 900 more items
]
```

...
### CLI tool to add specific files

```
$ estuary-upload -k <api-key> add-files <filepath...>
[
...
'bafkreia2254bseihmsqw7fzxsk54p7nqmitn2ibhnjiafxtql6by54idv4',
'bafkreigrov5qj25vp2vawsjlav5v3veihagovvddjodlydspmy2mtmks5q',
... 900 more items
Expand Down
36 changes: 26 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"license": "MIT",
"dependencies": {
"axios": "^1.0.0",
"commander": "^9.4.1",
"ipfs-car": "^0.8.1"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ export default [
},
],
plugins: [typescript({ tsconfig: "./tsconfig.json" })],
external: [
'fs', 'commander', 'ipfs-car/pack/blob', 'ipfs-car/blockstore/memory', 'axios',
],
},
];
38 changes: 26 additions & 12 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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 <dirpath>.'
);
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<string>, 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 <key>', 'Estuary API key')
.env('ESTUARY_API_KEY')
.makeOptionMandatory(true)
);
program.command('add-dir')
.argument('<dir>')
.description('Add files in a directory.')
.action(addDir);
program.command('add-files')
.argument('<files...>')
.description('Add files.')
.action(addFiles);
await program.parseAsync(process.argv);
}

main()
.then(() => process.exit(0))
.catch((error) => {
Expand Down

0 comments on commit 5ec3923

Please sign in to comment.