From b4b097df626f2c040638d3461c468462d23c7d49 Mon Sep 17 00:00:00 2001 From: Tim Green Date: Sat, 19 Nov 2022 01:22:54 +1100 Subject: [PATCH] Basic impl for the sync command. Also tweak oclif to support ESM. https://github.com/oclif/oclif/issues/1016 --- packages/cli/README.md | 4 ++-- packages/cli/bin/dev | 18 ++---------------- packages/cli/bin/dev.cmd | 3 --- packages/cli/bin/dev.js | 28 ++++++++++++++++++++++++++++ packages/cli/bin/run | 8 -------- packages/cli/bin/run.cmd | 3 --- packages/cli/bin/run.js | 8 ++++++++ packages/cli/package.json | 12 +++++++----- packages/cli/src/commands/sync.ts | 8 +++++++- packages/cli/tsconfig.json | 8 ++++++-- 10 files changed, 60 insertions(+), 40 deletions(-) delete mode 100644 packages/cli/bin/dev.cmd create mode 100755 packages/cli/bin/dev.js delete mode 100755 packages/cli/bin/run delete mode 100644 packages/cli/bin/run.cmd create mode 100755 packages/cli/bin/run.js diff --git a/packages/cli/README.md b/packages/cli/README.md index 8a0e367..6de2dc6 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -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 @@ -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)_ diff --git a/packages/cli/bin/dev b/packages/cli/bin/dev index f3a888c..333a825 100755 --- a/packages/cli/bin/dev +++ b/packages/cli/bin/dev @@ -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 "$@" diff --git a/packages/cli/bin/dev.cmd b/packages/cli/bin/dev.cmd deleted file mode 100644 index 8ae2b12..0000000 --- a/packages/cli/bin/dev.cmd +++ /dev/null @@ -1,3 +0,0 @@ -@echo off - -node "%~dp0\dev" %* diff --git a/packages/cli/bin/dev.js b/packages/cli/bin/dev.js new file mode 100755 index 0000000..8423d59 --- /dev/null +++ b/packages/cli/bin/dev.js @@ -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); diff --git a/packages/cli/bin/run b/packages/cli/bin/run deleted file mode 100755 index cb900e5..0000000 --- a/packages/cli/bin/run +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env node - -const oclif = require("@oclif/core"); - -oclif - .run() - .then(require("@oclif/core/flush")) - .catch(require("@oclif/core/handle")); diff --git a/packages/cli/bin/run.cmd b/packages/cli/bin/run.cmd deleted file mode 100644 index 968fc30..0000000 --- a/packages/cli/bin/run.cmd +++ /dev/null @@ -1,3 +0,0 @@ -@echo off - -node "%~dp0\run" %* diff --git a/packages/cli/bin/run.js b/packages/cli/bin/run.js new file mode 100755 index 0000000..96e42e2 --- /dev/null +++ b/packages/cli/bin/run.js @@ -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); diff --git a/packages/cli/package.json b/packages/cli/package.json index 7424a7d..85787bd 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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" @@ -47,8 +49,8 @@ ], "topicSeparator": " ", "topics": { - "hello": { - "description": "Say hello to the world and others" + "sync": { + "description": "Sync to Anki Desktop via AnkiConnect." } } }, diff --git a/packages/cli/src/commands/sync.ts b/packages/cli/src/commands/sync.ts index 800a052..af97074 100644 --- a/packages/cli/src/commands/sync.ts +++ b/packages/cli/src/commands/sync.ts @@ -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."; @@ -17,6 +19,10 @@ export default class Sync extends Command { public async run(): Promise { 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); + } } } diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index 6f6bbb3..8307ba3 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -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 + } }