Skip to content

Commit

Permalink
Merge branch 'main' into feat-add-envpath-to-core
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Dec 27, 2024
2 parents eb5751b + ae83b4b commit 99d0d3b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
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

0 comments on commit 99d0d3b

Please sign in to comment.