Skip to content

Instantly share code, notes, and snippets.

@randallb
Created May 31, 2022 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randallb/c744fbed7872c92480fab410872a167e to your computer and use it in GitHub Desktop.
Save randallb/c744fbed7872c92480fab410872a167e to your computer and use it in GitHub Desktop.
[1] randallb@randallb-mbp> RUST_BACKTRACE=1 deno run bin/get_file.ts ~/rbcode
Check file:///Users/randallb/rbcode/bin/get_file.ts
============================================================
Deno has panicked. This is a bug in Deno. Please report this
at https://github.com/denoland/deno/issues/new.
If you can reliably reproduce this panic, include the
reproduction steps and re-run with the RUST_BACKTRACE=1 env
var set and include the backtrace in your report.
Platform: macos x86_64
Version: 1.21.3
Args: ["deno", "run", "bin/get_file.ts"]
thread 'main' panicked at 'internal error: entered unreachable code: Unexpected missing emit: https://deno.land/std@0.141.0/path/mod.ts', cli/proc_state.rs:590:25
stack backtrace:
0: _rust_begin_unwind
1: core::panicking::panic_fmt
2: deno::proc_state::ProcState::load
3: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
4: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
5: <deno_core::modules::RecursiveModuleLoad as futures_core::stream::Stream>::poll_next
6: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
7: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
8: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
9: deno::run_command::{{closure}}
10: deno::main::{{closure}}
11: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
12: deno_runtime::tokio_util::run_basic
13: deno::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
[1] randallb@randallb-mbp>
#!/usr/bin/env zsh
EXECUTIONFILE="${@:(-2):1}"
exec "/usr/bin/env" "-S deno run -q --lock=$EXECUTIONFILE.lock.json" "$@"
#!/usr/bin/env deno_shebang
import { resolve } from "https://deno.land/std/path/mod.ts";
import { getRichData } from "/infra/traverse/parse_rbcode_markdown.ts";
import { Schema } from "/my_types/rbcode_base.ts";
type Args = string[];
function combineItems(manuallyOrderedItems: string[] = [], directoryItems: string[]): string[] {
if (manuallyOrderedItems.length === 0) {
return directoryItems;
}
if (!manuallyOrderedItems.includes('...')) {
return manuallyOrderedItems;
}
const itemsAtTheEnd = directoryItems.filter(item => !manuallyOrderedItems.includes(item));
return [...manuallyOrderedItems.filter(item => item != '...'), ...itemsAtTheEnd];
}
export async function readRbcodeMarkdown(path: string): Promise<Schema> {
const realPath = resolve(path); // this seems to be triggering
const directoryName = realPath.split('/').pop()
let defaultEmoji = "🫥";
const overrides: Schema = {
name: directoryName,
absolutePath: realPath,
};
let fileStat;
let fileContents = ""
let pathToRead;
const directoryFiles = [];
try {
fileStat = await Deno.stat(realPath);
} catch {
console.error(`${realPath} does not exist`);
}
const filesToIgnore = [
'.DS_Store',
];
if (fileStat?.isDirectory) {
filesToIgnore.push(`${directoryName}.md`);
const potentialIndexFile = `${realPath}/${directoryName}.md`;
for await (const file of Deno.readDir(realPath)) {
if (!filesToIgnore.includes(file.name)) {
directoryFiles.push(file.name);
}
}
try {
if ((await Deno.stat(potentialIndexFile)).isFile) {
pathToRead = potentialIndexFile;
}
} catch {
console.error(`${potentialIndexFile} does not exist`);
defaultEmoji = "📁";
overrides.openablePath = undefined;
}
}
if (fileStat?.isFile) {
pathToRead = realPath;
}
if (pathToRead) {
fileContents = await Deno.readTextFile(pathToRead);
overrides.openablePath = pathToRead;
defaultEmoji = "📄";
}
try {
const output = getRichData(fileContents);
if (!output.emoji) {
output.emoji = defaultEmoji;
}
return { ...output, items: combineItems(output.items, directoryFiles), ...overrides };
} catch {
return {};
}
}
export async function main([filePath]: Args): Promise<Schema> {
return await readRbcodeMarkdown(filePath);
}
if (import.meta.main) {
const args = Deno.args as Args;
const output = await main(args);
console.log(JSON.stringify(output));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment