Skip to content

Commit

Permalink
doc: improve README.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisGorbachev committed Jul 17, 2024
1 parent d313154 commit 858fc01
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

53 changes: 24 additions & 29 deletions README.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
#!/usr/bin/env -S deno run --allow-read --allow-run=git,cargo --allow-env=DENOEXEC_STRING_LITERAL_DEBUG
#!/usr/bin/env -S deno run --allow-read --allow-run=bash,git,cargo --allow-env --allow-sys

import * as toml from "jsr:@std/toml"
import { $, _ } from "https://deno.land/x/[email protected]/mod.ts";
import * as zx from 'npm:zx'

interface CargoToml {
package: {
name: string
name: string,
description: string
}
}

const dirname = import.meta.dirname
const decoder = new TextDecoder();
const theCargoTomlText = await Deno.readTextFile(`${dirname}/Cargo.toml`)
const theCargoToml = toml.parse(theCargoTomlText) as unknown as CargoToml
const packageName = theCargoToml.package.name
const bin = packageName
const help = await new Deno.Command("cargo", {
args: ['run', '--bin', bin, '--', '--help']
}).output();
if (!help.success) throw help
const repo = await getGitHubRepo()

async function getGitHubRepo() {
const url = (await $(_`git remote get-url origin`)).trim();
const match = url.match(/github\.com[:\/]([^.]+)/);
if (match) {
return match[1]
} else {
throw new Error(`Could not extract org/repo from ${url}`)
}
interface Repo {
url: string
}

const $ = zx.$({
cwd: import.meta.dirname
})

const parse = (input: { toString: () => string }) => JSON.parse(input.toString())

const theCargoToml: CargoToml = parse(await $`yj -t < Cargo.toml`)
const name = theCargoToml.package.name
const bin = name
const description = theCargoToml.package.description
const help = await $`cargo run --quiet --bin ${bin} -- --help`
const repo: Repo = parse(await $`gh repo view --json url`)

const autogenerated = `
<!-- DO NOT EDIT -->
<!-- This file is automatically generated by README.ts. -->
Expand All @@ -42,12 +37,12 @@ ${autogenerated}
# Create Rust GitHub repo
[![Build](https://github.com/${repo}/actions/workflows/ci.yml/badge.svg)](https://github.com/${repo})
[![Documentation](https://docs.rs/${packageName}/badge.svg)](https://docs.rs/${packageName})
[![Build](${repo.url}/actions/workflows/ci.yml/badge.svg)](${repo.url})
[![Documentation](https://docs.rs/${name}/badge.svg)](https://docs.rs/${name})
## Overview
\`${bin}\` is a CLI program that creates a new repository on GitHub, clones it locally, initializes a Rust project, copies the configs from a pre-existing directory.
${description}
## Examples
Expand Down Expand Up @@ -77,13 +72,13 @@ ${bin} --name my-new-project --cargo-init-args '--lib'
## Installation
\`\`\`shell
cargo install ${packageName}
cargo install ${name}
\`\`\`
## Usage
\`\`\`
${decoder.decode(help.stdout).trim()}
${help.stdout.trim()}
\`\`\`
## License
Expand Down

0 comments on commit 858fc01

Please sign in to comment.