-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add @interslavic/cli package (#10)
- Loading branch information
Showing
6 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/*.js | ||
/src/sync/backup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env node | ||
|
||
require('./dist/cli/index.js'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "@interslavic/cli", | ||
"version": "1.1.1", | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/medzuslovjansky/database-engine.git", | ||
"directory": "packages/cli" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"bin": { | ||
"isv": "cli.js" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"lint": "eslint . --fix", | ||
"lint:ci": "eslint .", | ||
"test": ":" | ||
}, | ||
"devDependencies": { | ||
"@interslavic/eslint-config-database-engine": "^1.1.1", | ||
"@interslavic/jest-config-database-engine": "^1.0.8", | ||
"@interslavic/prettier-config-database-engine": "^1.0.8", | ||
"@interslavic/typescript-config-database-engine": "^1.1.1", | ||
"@types/unist": "^3.0.2", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.9.5" | ||
}, | ||
"dependencies": { | ||
"@interslavic/utils": "^2.3.2", | ||
"remark": "^15.0.1", | ||
"remark-parse": "^11.0.0", | ||
"remark-stringify": "^11.0.0", | ||
"tslib": "^2.6.2", | ||
"unified": "^11.0.4", | ||
"unist-util-visit": "^5.0.0", | ||
"yargs": "^17.7.2" | ||
}, | ||
"license": "GPL-3.0-or-later", | ||
"prettier": "@interslavic/prettier-config-database-engine", | ||
"eslintConfig": { | ||
"extends": "@interslavic/database-engine" | ||
}, | ||
"gitHead": "892bf66e8b7afaa13e5a9dd00ea6fe169ee936cb" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env node | ||
|
||
import * as process from 'node:process'; | ||
|
||
import yargs from 'yargs'; | ||
import { hideBin } from 'yargs/helpers'; | ||
|
||
import transliterate from './transliterate.js'; | ||
import type { TransliterateArgv } from './transliterate.js'; | ||
|
||
const cli = yargs(hideBin(process.argv)) | ||
.scriptName('isv') | ||
.usage('Usage: $0 <command> [options]') | ||
.usage('\nCommands:\n') | ||
.usage(' transliterate Transliterate plain or Markdown text') | ||
.command<TransliterateArgv>(transliterate) | ||
.demandCommand(1, 'Please specify a command') | ||
.strict() | ||
.help() | ||
.fail((message, error, yargs) => { | ||
if (error) { | ||
console.error(error.message + '\n'); | ||
} else { | ||
console.error(message + '\n'); | ||
} | ||
yargs.showHelp(); | ||
process.exit(1); | ||
}); | ||
|
||
if (process.argv.length === 2) { | ||
cli.showHelp(); | ||
} else { | ||
cli.parse(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as fs from 'node:fs'; | ||
|
||
import isv from '@interslavic/utils'; | ||
import remarkParse from 'remark-parse'; | ||
import remarkStringify from 'remark-stringify'; | ||
import { unified } from 'unified'; | ||
import type { Literal } from 'unist'; | ||
import { visit } from 'unist-util-visit'; | ||
import type { CommandModule } from 'yargs'; | ||
|
||
export interface TransliterateArgv { | ||
input?: string; | ||
output?: string; | ||
format?: 'plain' | 'markdown'; | ||
script: isv.FlavorisationBCP47Code; | ||
} | ||
|
||
const commandModule: CommandModule<unknown, TransliterateArgv> = { | ||
command: 'transliterate', | ||
aliases: ['t'], | ||
builder: { | ||
script: { | ||
alias: 's', | ||
describe: 'Target script', | ||
demandOption: true, | ||
choices: Object.values(isv.FlavorisationBCP47), | ||
}, | ||
format: { | ||
alias: 'f', | ||
describe: 'Input format', | ||
choices: ['plain', 'markdown'], | ||
default: 'plain', | ||
}, | ||
input: { | ||
alias: 'i', | ||
describe: 'Input file [default: stdin]', | ||
type: 'string', | ||
}, | ||
output: { | ||
alias: 'o', | ||
describe: 'Output file [default: stdout]', | ||
type: 'string', | ||
}, | ||
}, | ||
async handler(argv) { | ||
const { format, script, input, output } = argv; | ||
|
||
const processor = unified() | ||
.use(remarkParse) | ||
.use(() => (tree) => { | ||
visit(tree, 'text', (node: Literal) => { | ||
node.value = isv.transliterate(String(node.value), script); | ||
}); | ||
}) | ||
.use(remarkStringify); | ||
|
||
let inputData = ''; | ||
|
||
if (input) { | ||
inputData = await fs.promises.readFile(input, 'utf8'); | ||
} else { | ||
const stdin = process.stdin; | ||
stdin.setEncoding('utf8'); | ||
stdin.on('data', (chunk) => { | ||
inputData += chunk; | ||
}); | ||
await new Promise((resolve) => { | ||
stdin.on('end', resolve); | ||
}); | ||
} | ||
|
||
const file = | ||
format === 'plain' | ||
? isv.transliterate(inputData, script) | ||
: await processor.process(inputData); | ||
|
||
if (output) { | ||
await fs.promises.writeFile(output, String(file)); | ||
} else { | ||
process.stdout.write(String(file)); | ||
} | ||
}, | ||
}; | ||
|
||
export default commandModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "@interslavic/typescript-config-database-engine", | ||
"compilerOptions": { | ||
"module": "ES2022", | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"types": ["node", "jest"] | ||
}, | ||
"include": ["src"] | ||
} |