Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate twoslash-cli #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/twoslash-cli/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
MIT License

Copyright (c) 2024-PRESENT Orta Therox + Anthony Fu <https://github.com/antfu>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


The MIT License (MIT)
Copyright (c) Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 changes: 22 additions & 0 deletions packages/twoslash-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# twoslash-vue

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]

Extended Twoslash for Vue SFC support.

> [!IMPORTANT]
> Working in Progress.

[📚 Documentation](https://twoslash.netlify.app/packages/vue)

## License

[MIT](./LICENSE) License © 2023-PRESENT [Anthony Fu](https://github.com/antfu)

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/twoslash-vue?style=flat&colorA=080f12&colorB=1fa669
[npm-version-href]: https://npmjs.com/package/twoslash-vue
[npm-downloads-src]: https://img.shields.io/npm/dm/twoslash-vue?style=flat&colorA=080f12&colorB=1fa669
[npm-downloads-href]: https://npmjs.com/package/twoslash-vue
12 changes: 12 additions & 0 deletions packages/twoslash-cli/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
entries: [
'src/index',
],
declaration: true,
clean: true,
rollup: {
emitCJS: true,
},
})
61 changes: 61 additions & 0 deletions packages/twoslash-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "twoslash-cli",
"type": "module",
"version": "0.1.0",
"description": "Twoslash for your file-system",
"author": "Anthony Fu <[email protected]>",
"license": "MIT",
"funding": "https://github.com/sponsors/antfu",
"homepage": "https://github.com/twoslashes/twoslash#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/twoslashes/twoslash.git",
"directory": "packages/twoslash-cli"
},
"bugs": "https://github.com/twoslashes/twoslash/issues",
"keywords": [
"twoslash",
"cli",
"shiki"
],
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/*",
"./dist/index.d.ts"
]
}
},
"bin": {
"twoslash": "./dist/cli.js",
"twoslash-cli": "./dist/cli.js"
},
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"dev": "unbuild --stub",
"prepublishOnly": "nr build",
"start": "esno src/index.ts"
},
"peerDependencies": {
"typescript": "*"
},
"dependencies": {
"chokidar": "^3.5.3",
"commander": "^11.1.0",
"twoslash": "workspace:*"
}
}
77 changes: 77 additions & 0 deletions packages/twoslash-cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* eslint-disable no-console */
import { readdirSync, statSync } from 'node:fs'
import { join } from 'node:path'
import process from 'node:process'
import { Command } from 'commander'
import chokidar from 'chokidar'

import { canConvert, runOnFile } from './'

const program = new Command()

program
.description(
`Converts md/ts/js/tsx/jsx files into HTML by running them through Shikiji Twoslash.

Examples:

Converts a bunch of ts files in the samples dir and creates .html files in renders

$ twoslash samples/*.ts renders

Render a few markdown files to .html files in the build folder

$ twoslash pages/one.md pages/two.md build`,
)
.option('-w, --watch', 'Watch for file updates and rerun Twoslash if necessary.')
.option('-s, --samples', 'Instead of rendering to HTML, spit out individual code blocks as files.')
.option('--sourceAlso', 'Also include a render of the source input. Only works on ts/tsx/js/jsx files.')
.option('--reactAlso', 'Also include a tsx file with the code embedded.')
.option('--lint', 'Don\'t actually render output files, just verify they work.')

.on('--help', () => {
console.log('\n')
console.log(' Reference:')
console.log(' - CLI Info:')
console.log(' https://github.com/twoslash/twoslash/tree/main/packages/twoslash-cli')
})

program.parse(process.argv)

const options = program.opts()
if (options.debug)
console.log(options)

const to = program.args.pop()!
if (!to)
throw new Error('Missing output folder')

const possibleFiles = program.args
.flatMap((from) => {
const stat = statSync(from)
return stat.isDirectory() ? readdirSync(from).map(p => join(from, p)) : [from]
})
.filter(canConvert)

if (possibleFiles.length === 0)
throw new Error('Could not find any md/ts/js/tsx/jsx files in the input')

const s = possibleFiles.length === 1 ? '' : 's'
console.log(`Twoslashifying ${possibleFiles.length} file${s} ${options.watch ? '(watch mode)' : ''}:\n`)

function run(from: string) {
runOnFile({
from,
to,
splitOutCodeSamples: options.samples,
alsoRenderSource: options.sourceAlso,
lint: options.lint,
reactAlso: options.reactAlso,
})
}

if (options.watch)
chokidar.watch(possibleFiles).on('all', (_, from) => run(from))

else
possibleFiles.forEach(run)
Loading
Loading