Skip to content

Commit

Permalink
feat: add cli for @extractus/extractus
Browse files Browse the repository at this point in the history
  • Loading branch information
SettingDust committed Jul 14, 2024
1 parent 7c4a8b3 commit 1e738c1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .env-cmdrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"test": {
"DEBUG": true,
"NODE_OPTIONS": "--loader=ts-node/esm --no-warnings=ExperimentalWarning --experimental-wasm-modules"
},
"debug": {
"DEBUG": true
}
}
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
],
"search.exclude": {
"**/node_modules": false
}
},
"typescript.tsdk": "node_modules\\typescript\\lib"
}
31 changes: 31 additions & 0 deletions packages/extractus/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { defineCommand, runMain } from 'citty'
import { isURLString } from 'extra-utils'
import { ofetch } from 'ofetch'
import path from 'path'
import fs from 'fs/promises'
import { extract } from './index.js'
import objectInspect from 'object-inspect'
import logger from './logger.js'

const main = defineCommand({
args: {
input: {
type: 'positional',
description: 'Url, html string, html file path',
required: true
}
},
async run({ args }) {
let input = args.input
if (isURLString(args.input)) {
input = await ofetch(args.input)
}
try {
const filepath = path.format(path.parse(input))
input = (await fs.readFile(filepath)).toString()
} catch (error) {}
logger.info(objectInspect(await extract(input), { indent: 2 }))
}
})

runMain(main)
1 change: 1 addition & 0 deletions packages/extractus/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const colors = {
WARN: chalk.yellow,
ERROR: chalk.red
}

logPrefix.reg(log)
log.enableAll()

Expand Down
8 changes: 7 additions & 1 deletion packages/extractus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"test": "env-cmd -r ../../.env-cmdrc -e test ava --config ../../ava.config.js",
"test:coverage": "env-cmd -r ../../.env-cmdrc -e test c8 -c ../../.c8rc.json npm test"
},
"bin": {
"extractus": "./bin/cli.js"
},
"exports": {
".": {
"types": "./index.ts",
Expand All @@ -28,16 +31,19 @@
"@extractus/utils": "workspace:^",
"@types/object-inspect": "^1.8.4",
"chalk": "^5.3.0",
"citty": "^0.1.6",
"consola": "^3.2.3",
"extra-utils": "^5.5.2",
"iterable-operator": "^4.0.6",
"loglevel": "^1.8.1",
"loglevel-plugin-prefix": "^0.8.4",
"micro-memoize": "^4.1.2",
"object-inspect": "^1.13.1",
"ofetch": "^1.3.4",
"type-fest": "^4.8.3"
},
"publishConfig": {
"access": "public"
},
"gitHead": "1ece19acc667d754b6163190ec2a02cc2ede6c04"
}
}
2 changes: 1 addition & 1 deletion packages/extractus/test/meta-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"url": "https://jsonld.com",
"author": {
"name": "jsonld",
"url": "https://jsonld.com"
"url": "/"
}
}
]
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 1e738c1

Please sign in to comment.