Skip to content

Commit

Permalink
use libtea (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl authored May 23, 2023
1 parent 9f4d3ae commit e2c6142
Show file tree
Hide file tree
Showing 78 changed files with 817 additions and 4,240 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
paths:
- import-map.json
- src/hooks/app.sync.ts
- src/hooks/useSync.ts
- src/prefab/install.ts
- .github/workflows/ci.sync.yml

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jobs:
- macos-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/cache # avoids sporadic 500s from deno’s CDN
- uses: denoland/setup-deno@v1 # using ourself to install deno could compromise the tests
- run: deno cache $(find . -name \*.ts)
- run: deno task test --coverage=cov_profile
- run: deno coverage cov_profile --lcov --exclude=tests/ --output=cov_profile.lcov
- uses: coverallsapp/github-action@v1
Expand All @@ -48,7 +48,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: teaxyz/setup@v0
- run: deno lint src/*/**.ts
- run: deno lint

typecheck:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</p>


# tea/cli 0.32.1
# tea/cli 0.33.0

`tea` puts the whole open source ecosystem at your fingertips:

Expand Down
17 changes: 14 additions & 3 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"coverage" : "scripts/run_coverage.sh",
"typecheck": "deno check --unstable ./src/app.ts",
// runs this source checkout for testing
// NOTE this doesn't currently work due (our bug)
"run": "deno run --unstable --allow-all src/app.ts",
// compiles to ./tea
"compile": "deno compile --allow-read --allow-write --allow-net --allow-run --allow-env --unstable --output $INIT_CWD/tea src/app.ts",
Expand All @@ -23,10 +22,22 @@
]
}
},
"lint": {
"include": ["src/", "scripts/"]
},
"tea": {
"dependencies": {
"deno.land": "^1.31.1"
"deno.land": "^1.33.3"
}
},
"importMap": "import-map.json"
"imports": {
"is-what": "https://deno.land/x/[email protected]/src/index.ts",
"jsonc": "https://deno.land/x/[email protected]/mod.ts",
"tea": "https://raw.github.com/teaxyz/lib/v0.1.3/mod.ts",
"tea/": "https://raw.github.com/teaxyz/lib/v0.1.3/src/",
"outdent": "https://deno.land/x/[email protected]/mod.ts",
"cliffy/": "https://deno.land/x/[email protected]/",
"deno/": "https://deno.land/[email protected]/",
"hooks": "./src/hooks/index.ts"
}
}
21 changes: 0 additions & 21 deletions import-map.json

This file was deleted.

11 changes: 5 additions & 6 deletions scripts/repair.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/usr/bin/env -S deno run -A

import { init } from "../src/init.ts";
import { useCellar } from "hooks"
import { Installation } from "types"
import { link } from "prefab"
import * as semver from "semver"
import { hooks, semver, Installation, prefab } from "tea"
import { useConfig } from "hooks"
const { useCellar } = hooks
const { link } = prefab

if (import.meta.main) {
init()
useConfig()

for (const project of Deno.args) {
await repairLinks(project)
Expand Down
19 changes: 9 additions & 10 deletions src/app.dump.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEnv, usePrint } from "hooks"
import { flatmap } from "utils"
import { isPlainObject } from "is_what"
import { useConfig, usePrint } from "hooks"
import { isPlainObject } from "is-what"
import { utils } from "tea"
const { flatmap } = utils

//TODO should read from the shell configuration files to get originals properly
//TODO don’t wait on each print, instead chain the promises to be more time-efficient
Expand All @@ -11,7 +12,7 @@ interface Parameters {
}

export default async function dump({ env, shell }: Parameters) {
const { TEA_REWIND, getEnvAsObject } = useEnv();
const { TEA_REWIND, obj: oldenv } = useConfig().env
const { print } = usePrint()

const [set, unset]= (() => {
Expand All @@ -37,11 +38,9 @@ export default async function dump({ env, shell }: Parameters) {
const is_env = env['SRCROOT']

if (is_env) {
const oldenv = getEnvAsObject()

// first rewind the env to the original state
if (oldenv['TEA_REWIND']) {
const rewind = JSON.parse(oldenv['TEA_REWIND']) as { revert: Record<string, string>, unset: string[] }
if (TEA_REWIND) {
const rewind = JSON.parse(TEA_REWIND) as { revert: Record<string, string>, unset: string[] }
delete oldenv['TEA_REWIND']

for (const key of rewind.unset) {
Expand All @@ -59,7 +58,7 @@ export default async function dump({ env, shell }: Parameters) {
}

// now calculate the new rewind
const TEA_REWIND = (() => {
const new_TEA_REWIND = (() => {
const revert: Record<string, string> = {}
const unset: string[] = []
for (const key of Object.keys(env)) {
Expand All @@ -79,7 +78,7 @@ export default async function dump({ env, shell }: Parameters) {
for (const [key, value] of Object.entries(env)) {
if (value) await print(set(key, value))
}
await print(set('TEA_REWIND', TEA_REWIND))
await print(set('TEA_REWIND', new_TEA_REWIND))

} else {
const unwind = flatmap(TEA_REWIND, JSON.parse) as { revert: Record<string, string>, unset: string[] }
Expand Down
24 changes: 11 additions & 13 deletions src/app.exec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { pkg as pkgutils, TeaError, chuzzle } from "utils"
import { ExitError, Installation } from "types"
import { useEnv, useConfig, useRun } from "hooks"
import { RunError } from "hooks/useRun.ts"
import { gray, red, teal } from "hooks/useLogger.ts"
import { useConfig, useRun, useLogger, RunError, Verbosity, ExitError } from "hooks"
import { Installation, Path, utils, TeaError } from "tea"
import { basename } from "deno/path/mod.ts"
import { isNumber } from "is_what"
import Path from "path"
import { isNumber } from "is-what"

export default async function(cmd: string[], env: Record<string, string>) {
const { TEA_FORK_BOMB_PROTECTOR } = useEnv()
const { TEA_FORK_BOMB_PROTECTOR } = useConfig().env
const { red, teal } = useLogger()

// ensure we cannot fork bomb the user since this is basically the worst thing tea/cli can do
let nobomb = chuzzle(parseInt(TEA_FORK_BOMB_PROTECTOR ?? '0')) ?? 0
let nobomb = parseInt(TEA_FORK_BOMB_PROTECTOR ?? '0').chuzzle() ?? 0
env['TEA_FORK_BOMB_PROTECTOR'] = `${++nobomb}`
if (nobomb > 20) throw new Error("FORK BOMB KILL SWITCH ACTIVATED")

try {
await useRun({cmd, env})
} catch (err) {
const { debug } = useConfig()
const debug = useConfig().modifiers.verbosity >= Verbosity.debug
const arg0 = cmd?.[0]

if (err instanceof TeaError) {
Expand All @@ -44,8 +41,9 @@ export default async function(cmd: string[], env: Record<string, string>) {
}

export async function repl(installations: Installation[], env: Record<string, string>) {
const { SHELL } = useEnv()
const pkgs_str = () => installations.map(({pkg}) => gray(pkgutils.str(pkg))).join(", ")
const { SHELL } = useConfig().env
const { gray } = useLogger()
const pkgs_str = () => installations.map(({pkg}) => gray(utils.pkg.str(pkg))).join(", ")

// going to stderr so that we don’t potentially break (nonsensical) pipe scenarios, eg.
// tea -E | env
Expand Down Expand Up @@ -84,7 +82,7 @@ export async function repl(installations: Installation[], env: Record<string, st
}

try {
await useRun({ cmd, env })
await useRun({cmd, env})
} catch (err) {
if (err instanceof RunError) {
throw new ExitError(err.code)
Expand Down
11 changes: 6 additions & 5 deletions src/app.help.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useConfig, usePrint } from "hooks"
import { undent } from "utils"
import { Verbosity } from "./hooks/useConfig.ts"
import { usePrint } from "hooks"
import undent from "outdent"

export default async function help() {
const { verbose } = useConfig()
export default async function help(verbosity = Verbosity.normal) {
const { print } = usePrint()

if (!verbose) {
if (verbosity < Verbosity.loud) {
// 10| 20| 30| 40| 50| 60| 70| | 80|
await print(undent`
usage:
Expand Down Expand Up @@ -42,6 +42,7 @@ export default async function help() {
--dry-run,-n don’t do anything, just print
--keep-going,-k keep going as much as possible after errors
--verbose,-v print version and then increase verbosity †
--quiet,-q status messages are more concise
--silent,-s no chat, no errors: only output the requested data
--cd,-C,--chdir <dir> change directory first
--chaste abstain from networking, installing packages, etc.
Expand Down
8 changes: 4 additions & 4 deletions src/app.magic.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { basename } from "deno/path/mod.ts"
import { undent } from "utils"
import Path from "path"
import { useEnv } from "./hooks/useConfig.ts"
import { useConfig } from "hooks"
import undent from "outdent"
import { Path } from "tea"

export default function(self: Path, shell?: string) {
const { SHELL } = useEnv()
const { SHELL } = useConfig().env
shell ??= basename(SHELL ?? "unknown")
const d = self.parent()

Expand Down
60 changes: 37 additions & 23 deletions src/app.main.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
import { usePrefix, useExec, useVirtualEnv, useVersion, useSync, usePrint, useConfig, useEnv } from "hooks"
import dump from "./app.dump.ts"
import help from "./app.help.ts"
import provides from "./app.provides.ts";
import magic from "./app.magic.ts"
import exec, { repl } from "./app.exec.ts"
import { pkg as pkgutils, flatmap } from "utils"
import Path from "path"
import { Verbosity } from "./types.ts"
import * as semver from "semver"
import { usePrefix, useExec, useVirtualEnv, useVersion, usePrint, useConfig, useLogger } from "hooks"
import { VirtualEnv } from "./hooks/useVirtualEnv.ts"
import { Verbosity } from "./hooks/useConfig.ts"
import { Path, utils, semver, hooks } from "tea"
import { basename } from "deno/path/mod.ts"
import { Args } from "./args.ts";
import exec, { repl } from "./app.exec.ts"
import provides from "./app.provides.ts"
import magic from "./app.magic.ts"
import dump from "./app.dump.ts"
import help from "./app.help.ts"
import { Args } from "./args.ts"
const { flatmap } = utils
const { useSync } = hooks

export async function run(args: Args) {
const { print } = usePrint();
const { execPath } = useConfig()
const { PATH, SHELL } = useEnv()
const { print } = usePrint()
const { arg0: execPath, env: { PATH, SHELL }, modifiers: { verbosity, json } } = useConfig()

if (args.cd) {
const chdir = args.cd
console.verbose({ chdir })
console.log({ chdir })
Deno.chdir(chdir.string)
}

if (args.sync) {
await useSync()
const logger = (({ new: make, logJSON }) => {
if (!json) {
const logger = make()
return {
syncing: () => logger.replace("syncing pantries…"),
syncd: () => logger.replace("]pantries sync’d ⎷")
}
} else return {
syncing: () => logJSON({status: "syncing"}),
syncd: () => logJSON({status: "syncd"})
}
})(useLogger())

await useSync(logger)
}

switch (args.mode) {
Expand Down Expand Up @@ -65,6 +77,8 @@ export async function run(args: Args) {
} else {
console.error("tea: empty pkg env")
}
} else if (json) {
await print(JSON.stringify({env}))
} else for (const key in env) {
const inferred = env[key].split(":")
const inherited = Deno.env.get(key)?.split(":") ?? []
Expand All @@ -74,7 +88,7 @@ export async function run(args: Args) {
break
case "dump": {
env['PATH'] = full_path().join(':')
env["TEA_PKGS"] = pkgs.map(pkgutils.str).join(":").trim()
env["TEA_PKGS"] = pkgs.map(utils.pkg.str).join(":").trim()
env["TEA_PREFIX"] ??= usePrefix().string
env["TEA_VERSION"] = useVersion()

Expand All @@ -83,7 +97,7 @@ export async function run(args: Args) {
} break
}} break
case "help":
await help()
await help(verbosity)
break
case "version":
await print(`tea ${useVersion()}`)
Expand All @@ -103,7 +117,7 @@ function announce(self: Path) {
const prefix = usePrefix().string
const version = useVersion()

switch (useConfig().verbosity) {
switch (useConfig().modifiers.verbosity) {
case Verbosity.debug:
if (self.basename() == "deno") {
console.debug({ deno: self.string, prefix, import: import.meta, tea: version })
Expand All @@ -117,7 +131,7 @@ function announce(self: Path) {
}

function injection({ args, inject }: Args) {
const { TEA_FILES, TEA_PKGS, SRCROOT, VERSION } = useEnv()
const { TEA_FILES, TEA_PKGS, SRCROOT, VERSION } = useConfig().env
const teaPkgs = TEA_PKGS?.trim()
//TODO if TEA_PKGS then extract virtual-env from that, don’t reinterpret it

Expand All @@ -133,7 +147,7 @@ function injection({ args, inject }: Args) {
cwd = file.parent()
}

if (useConfig().keepGoing) {
if (useConfig().modifiers.keepGoing) {
return useVirtualEnv(cwd).swallow(/^not-found/)
} else if (teaPkgs) {
/// if an env is defined then we still are going to try to read it
Expand All @@ -158,7 +172,7 @@ function injection({ args, inject }: Args) {
//TODO anything that isn’t an absolute path will crash
return {
env: {},
pkgs: TEA_PKGS!.split(":").map(pkgutils.parse),
pkgs: TEA_PKGS!.split(":").map(utils.pkg.parse),
teafiles: TEA_FILES.split(":").map(x => new Path(x)),
srcroot: new Path(SRCROOT),
version: flatmap(VERSION, semver.parse)
Expand All @@ -180,7 +194,7 @@ export function wut(args: Args): 'dump' | 'exec' | 'repl' | 'env' | 'dryrun' {
return true
})()

if (useConfig().dryrun) {
if (useConfig().modifiers.dryrun) {
return 'dryrun'
} else if (stack_mode) {
return 'dump'
Expand Down
4 changes: 2 additions & 2 deletions src/app.provides.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { which } from "hooks/useExec.ts"
import { ExitError } from "./types.ts"
import { ExitError } from "./hooks/useErrorHandler.ts"
import { which } from "./hooks/useExec.ts"

export default async function provides(args: string[]) {
let status = 0;
Expand Down
Loading

0 comments on commit e2c6142

Please sign in to comment.