Skip to content

Commit

Permalink
Basic impl for the sync command.
Browse files Browse the repository at this point in the history
Also tweak oclif to support ESM.
oclif/oclif#1016
  • Loading branch information
timgreen committed Nov 18, 2022
1 parent 187acbf commit b4b097d
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm install -g @anki.md/cli
$ ankimd COMMAND
running command...
$ ankimd (--version)
@anki.md/cli/0.0.0 linux-x64 node-v18.10.0
@anki.md/cli/0.0.1 linux-x64 node-v18.10.0
$ ankimd --help [COMMAND]
USAGE
$ ankimd COMMAND
Expand Down Expand Up @@ -71,6 +71,6 @@ EXAMPLES
$ ankimd sync deckA.md deckB.md
```

_See code: [dist/commands/sync.ts](https://github.com/timgreen/Anki.md/blob/v0.0.0/dist/commands/sync.ts)_
_See code: [dist/commands/sync.ts](https://github.com/timgreen/Anki.md/blob/v0.0.1/dist/commands/sync.ts)_

<!-- commandsstop -->
18 changes: 2 additions & 16 deletions packages/cli/bin/dev
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#!/usr/bin/env node
#!/bin/sh

const oclif = require("@oclif/core");

const path = require("path");
const project = path.join(__dirname, "..", "tsconfig.json");

// In dev mode -> use ts-node and dev plugins
process.env.NODE_ENV = "development";

require("ts-node").register({ project });

// In dev mode, always show stack traces
oclif.settings.debug = true;

// Start the CLI
oclif.run().then(oclif.flush).catch(oclif.Errors.handle);
npx ts-node ./bin/dev.js "$@"
3 changes: 0 additions & 3 deletions packages/cli/bin/dev.cmd

This file was deleted.

28 changes: 28 additions & 0 deletions packages/cli/bin/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env ts-node

/* eslint-disable node/shebang */
import oclif from "@oclif/core";
import path from "node:path";
import url from "node:url";
// eslint-disable-next-line node/no-unpublished-import
import { register } from "ts-node";

// In dev mode -> use ts-node and dev plugins
process.env.NODE_ENV = "development";

register({
project: path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
"..",
"tsconfig.json",
),
});

// In dev mode, always show stack traces
oclif.settings.debug = true;

// Start the CLI
oclif
.run(process.argv.slice(2), import.meta.url)
.then(oclif.flush)
.catch(oclif.Errors.handle);
8 changes: 0 additions & 8 deletions packages/cli/bin/run

This file was deleted.

3 changes: 0 additions & 3 deletions packages/cli/bin/run.cmd

This file was deleted.

8 changes: 8 additions & 0 deletions packages/cli/bin/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node

import oclif from "@oclif/core";

oclif
.run(process.argv.slice(2), import.meta.url)
.then(oclif.flush)
.catch(oclif.Errors.handle);
12 changes: 7 additions & 5 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
{
"name": "@anki.md/cli",
"version": "0.0.0",
"version": "0.0.1",
"description": "Anki.md CLI.",
"author": " @timgreen",
"bin": {
"ankimd": "./bin/run"
"ankimd": "./bin/run.js"
},
"homepage": "https://github.com/timgreen/Anki.md",
"license": "MIT",
"type": "module",
"main": "dist/index.js",
"repository": "timgreen/Anki.md",
"files": [
"/bin",
"/bin/run.js",
"/dist",
"/npm-shrinkwrap.json",
"/oclif.manifest.json"
],
"dependencies": {
"@anki.md/core": "^0.1.2",
"@oclif/core": "^1.19.1",
"@oclif/plugin-help": "^5",
"@oclif/plugin-plugins": "^2.1.6"
Expand Down Expand Up @@ -47,8 +49,8 @@
],
"topicSeparator": " ",
"topics": {
"hello": {
"description": "Say hello to the world and others"
"sync": {
"description": "Sync to Anki Desktop via AnkiConnect."
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/commands/sync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Command, Flags } from "@oclif/core";
import { ankiConnectSync, parse } from "@anki.md/core";
import * as fs from "fs";

export default class Sync extends Command {
static description = "Sync to Anki Desktop via AnkiConnect.";
Expand All @@ -17,6 +19,10 @@ export default class Sync extends Command {
public async run(): Promise<void> {
const { argv } = await this.parse(Sync);

this.log(argv.join(","));
for (const input of argv) {
this.log(input);
const deck = await parse(String(fs.readFileSync(input)));
await ankiConnectSync(deck);
}
}
}
8 changes: 6 additions & 2 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"compilerOptions": {
"declaration": true,
"importHelpers": true,
"module": "commonjs",
"moduleResolution": "node",
"module": "ES2020",
"outDir": "dist",
"rootDir": "src",
"strict": true,
"target": "es2019"
},
"include": ["src/**/*"]
"include": ["src/**/*"],
"ts-node": {
"esm": true
}
}

0 comments on commit b4b097d

Please sign in to comment.