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(cli): allow ts be extracted from markdown #1042

Merged
merged 1 commit into from
Dec 27, 2024
Merged
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
4 changes: 4 additions & 0 deletions docs/markdown-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ await $`whoami`
await $`echo ${__dirname}`
```

```ts
await $`pwd`
```

The `__filename` will be pointed to **markdown.md**:

```js
Expand Down
17 changes: 7 additions & 10 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export async function writeAndImport(
filepath: string,
origin = filepath
) {
await fs.writeFile(filepath, script.toString())
await fs.writeFile(filepath, script)
try {
process.once('exit', () => fs.rmSync(filepath, { force: true }))
await importPath(filepath, origin)
Expand All @@ -185,21 +185,18 @@ export async function importPath(
filepath: string,
origin = filepath
): Promise<void> {
const contents = await fs.readFile(filepath)
const { ext, base, dir } = path.parse(filepath)
const tempFilename = getFilepath(dir, base)

if (ext === '') {
return writeAndImport(await fs.readFile(filepath), tempFilename, origin)
return writeAndImport(contents, tempFilename, origin)
}
if (ext === '.md') {
return writeAndImport(
transformMarkdown(await fs.readFile(filepath)),
tempFilename,
origin
)
return writeAndImport(transformMarkdown(contents), tempFilename, origin)
}
if (argv.install) {
const deps = parseDeps(await fs.readFile(filepath))
const deps = parseDeps(contents)
await installDeps(deps, dir, argv.registry)
}

Expand All @@ -215,13 +212,13 @@ export function injectGlobalRequire(origin: string) {
Object.assign(globalThis, { __filename, __dirname, require })
}

export function transformMarkdown(buf: Buffer): string {
export function transformMarkdown(buf: Buffer | string): string {
const source = buf.toString()
const output = []
let state = 'root'
let codeBlockEnd = ''
let prevLineIsEmpty = true
const jsCodeBlock = /^(```{1,20}|~~~{1,20})(js|javascript)$/
const jsCodeBlock = /^(```{1,20}|~~~{1,20})(js|javascript|ts|typescript)$/
const shCodeBlock = /^(```{1,20}|~~~{1,20})(sh|shell|bash)$/
const otherCodeBlock = /^(```{1,20}|~~~{1,20})(.*)$/
for (const line of source.split(/\r?\n/)) {
Expand Down
2 changes: 1 addition & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const nameRe =
/^(?<name>(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*)\/?.*$/i
const versionRe = /^@(?<version>[~^]?(v?[\dx*]+([-.][\d*a-z-]+)*))/i

export function parseDeps(content: Buffer): Record<string, string> {
export function parseDeps(content: Buffer | string): Record<string, string> {
return depseek(content.toString() + '\n', { comments: true }).reduce<
Record<string, string>
>((m, { type, value }, i, list) => {
Expand Down
Loading