Skip to content

Commit 9a87d9b

Browse files
committed
✨ commander cli for creating or destroying a database
commander cli for creating or destroying a database ✨ New feature
1 parent 3461222 commit 9a87d9b

File tree

3 files changed

+76
-30
lines changed

3 files changed

+76
-30
lines changed

README.md

+22-11
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,41 @@ Save this token somewhere safe, you will need it to authenticate with the Gist A
3232

3333
Now let's create a new database. The empty database will be created as single gist containing a single file called `database.json` with an empty JSON object: `{}`.
3434

35-
This package comes with a cli command to help you get started.
36-
37-
In your terminal run the following command:
35+
This package comes with a cli tool to help you perform common database operations.
3836

3937
```console
40-
GIST_TOKEN="xxxxxxxx" node node_modules/gist-database/dist/cli.modern.js
38+
Usage: gist-database [options]
39+
40+
Transform gist into a key/value datastore.
4141

42-
# or for a public gist
43-
GIST_TOKEN="xxxxxxxx" node node_modules/gist-database/dist/cli.modern.js public
42+
Options:
43+
-c --create Create a new gist database.
44+
-p --public Make the gist public. (default: false)
45+
-de --description <description> Description of the gist. (default: "")
46+
-des --destroy <destroy> Destroy a gist database. Provide the gist id of the database.
47+
-t --token <token> Gist token. Required for all operations.
48+
-h, --help display help for command
49+
```
4450

45-
# or for a public gist with a description
46-
GIST_TOKEN="xxxxxxxx" node node_modules/gist-database/dist/cli.modern.js public "My awesome database"
51+
To create a new database run the following command in your terminal:
52+
53+
```console
54+
npx gist-database create --token <your-token>
4755
```
4856

4957
If successful, you should see output similar to:
5058

5159
```json
5260
{
53-
"id": "xxxxxxxxxxxxxxxxxxx",
54-
"url": "https://api.github.com/gists/xxxxxxxxxxx"
61+
"id": "xxxxxxxxxxxx",
62+
"rawUrl": "https://api.github.com/gists/xxxxxxxxxxxx",
63+
"url": "https://gist.github.com/xxxxxxxxxxxx",
64+
"public": false,
65+
"description": ""
5566
}
5667
```
5768

58-
This is the gist containing your main database file. Save the `id` somewhere safe. You will need it to initialize the database.
69+
This is the gist containing your main database file. Save the `id` somewhere safe. You will need it to initialize your database instance.
5970

6071
## 📖 API
6172

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
},
9191
"dependencies": {
9292
"buffer": "6.0.3",
93+
"commander": "9.4.1",
9394
"cross-fetch": "3.1.5",
9495
"cryptr": "6.1.0",
9596
"is-plain-obj": "4.1.0",

src/cli.ts

+53-19
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,66 @@
11
#!/usr/bin/env node
22

33
import { GistDatabase } from './GistDatabase'
4+
import { program } from 'commander'
45

5-
if (!process.env.GIST_TOKEN) {
6-
console.error('GIST_TOKEN is required')
7-
process.exit(1)
6+
interface CommanderOptions {
7+
create: boolean
8+
description: string
9+
destroy: string
10+
public: boolean
11+
token: string
812
}
913

1014
const main = async () => {
11-
console.log('Creating database...')
15+
program
16+
.name('gist-database')
17+
.description('Transform gist into a key/value datastore.')
18+
.option('-c --create', 'Create a new gist database.')
19+
.option('-p --public', 'Make the gist public.', false)
20+
.option('-de --description <description>', 'Description of the gist.', '')
21+
.option(
22+
'-des --destroy <destroy>',
23+
'Destroy a gist database. Provide the gist id of the database.'
24+
)
25+
.requiredOption(
26+
'-t --token <token>',
27+
'Gist token. Required for all operations.'
28+
)
29+
try {
30+
program.parse(process.argv)
1231

13-
const isPublic = process.argv[2] === 'public'
32+
const options: CommanderOptions = program.opts()
1433

15-
const res = await GistDatabase.createDatabaseRoot({
16-
token: process.env.GIST_TOKEN,
17-
public: isPublic,
18-
description: process.argv[3]
19-
})
34+
if (options.create) {
35+
console.log('Creating database...')
36+
const res = await GistDatabase.createDatabaseRoot({
37+
token: options.token,
38+
public: options.public,
39+
description: options.description
40+
})
41+
console.log('Database created!')
42+
console.log({
43+
id: res.id,
44+
rawUrl: res.url,
45+
url: `https://gist.github.com/${res.id}`,
46+
public: options.public,
47+
description: options.description
48+
})
49+
} else if (options.destroy) {
50+
console.log('Destroying database...')
51+
const db = new GistDatabase({
52+
token: options.token,
53+
id: options.destroy
54+
})
55+
await db.destroy()
56+
console.log('Database destroyed!')
57+
}
58+
} catch (err) {
59+
console.error(err)
60+
process.exit(1)
61+
}
2062

21-
console.log('Database created')
22-
23-
console.log({
24-
id: res.id,
25-
rawUrl: res.url,
26-
url: `https://gist.github.com/${res.id}`,
27-
public: isPublic,
28-
description: process.argv[3]
29-
})
63+
process.exit()
3064
}
3165

3266
main()

0 commit comments

Comments
 (0)