Skip to content
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
28 changes: 28 additions & 0 deletions .github/workflows/publish-jsr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: publish·jsr

on:
release:
types: [published]
workflow_dispatch:

concurrency:
group: publish/jsr/${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read
id-token: write # OIDC for tokenless JSR publish

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- name: dry-run
run: deno publish --dry-run --allow-slow-types --no-check --unstable-fs --unstable-ffi
- name: publish
if: github.event_name == 'release' || github.event_name == 'workflow_dispatch'
run: deno publish --allow-slow-types --no-check --unstable-fs --unstable-ffi
Comment on lines +24 to +28
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ $ npm install libpkgx
# ^^ https://npmjs.com/libpkgx
```

Or with [Deno]:
Or with [Deno] via [JSR](https://jsr.io):

```ts
import * as pkgx from "https://deno.land/x/libpkgx/mod.ts"
import * as pkgx from "jsr:@pkgx/libpkgx"
```

> `deno.land/x/libpkgx` is frozen (registry is read-only). Use JSR for new versions.

## Usage

```ts
Expand Down
56 changes: 50 additions & 6 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,69 @@
{
"name": "@pkgx/libpkgx",
"version": "0.23.0",
"license": "Apache-2.0",
"exports": {
".": "./mod.ts",
"./hooks/useConfig.ts": "./src/hooks/useConfig.ts",
"./hooks/usePantry.ts": "./src/hooks/usePantry.ts",
"./utils/error.ts": "./src/utils/error.ts",
"./utils/host.ts": "./src/utils/host.ts",
"./utils/read-lines.ts": "./src/utils/read-lines.ts",
"./utils/semver.ts": "./src/utils/semver.ts",
"./plumbing/hydrate.ts": "./src/plumbing/hydrate.ts"
},
Comment on lines +5 to +14
"publish": {
"include": [
"LICENSE.txt",
"README.md",
"mod.ts",
"src/**/*.ts",
"vendor/**"
],
"exclude": [
"src/**/*.test.ts",
"src/hooks/useTestConfig.ts",
"fixtures",
"examples",
"scripts",
"dist",
".github"
]
},
"compilerOptions": {
"allowJs": false,
"strict": true
},
"pkgx": "deno~2.0",
"tasks": {
"test": "deno test --parallel --unstable-fs --unstable-ffi --allow-all",
"typecheck": "deno check ./mod.ts"
"typecheck": "deno check ./mod.ts",
"publish:dry": "deno publish --dry-run --allow-slow-types --no-check --allow-dirty --unstable-fs --unstable-ffi"
},
"lint": {
"include": ["src/"],
"exclude": ["**/*.test.ts"]
"include": ["src/", "mod.ts"],
"exclude": ["**/*.test.ts", "vendor/", "src/hooks/useTestConfig.ts"],
"rules": {
// Prototype extensions (Array.compact, Promise.swallow, etc.) use
// `declare global`, which JSR forbids without --allow-slow-types.
// Explicit public return types are fixed; globals are a larger refactor.
"exclude": ["no-slow-types"]
}
},
"test": {
"include": ["src/"],
"exclude": ["dist/"]
},
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.6",
"@std/crypto": "jsr:@std/crypto@^1",
"@std/encoding": "jsr:@std/encoding@^1",
"@std/fs": "jsr:@std/fs@^1",
"@std/io": "jsr:@std/io@^0.225.0",
"@std/io/write-all": "jsr:@std/io@^0.225.0/write-all",
"@std/path": "jsr:@std/path@^1",
"@std/testing": "jsr:@std/testing@^1.0.3",
"is-what": "https://deno.land/x/is_what@v4.1.15/src/index.ts",
"outdent": "https://deno.land/x/outdent@v0.8.0/mod.ts"
"@std/yaml": "jsr:@std/yaml@^1",
"is-what": "npm:is-what@4.1.15",
"outdent": "npm:outdent@0.8.0"
}
}
3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import "./src/utils/misc.ts"
import { flatmap, validate } from "./src/utils/misc.ts"

import host, { SupportedArchitecture, SupportedPlatform } from "./src/utils/host.ts"
import host from "./src/utils/host.ts"
import type { SupportedArchitecture, SupportedPlatform } from "./src/utils/host.ts"
import SemVer, * as semver from "./src/utils/semver.ts"
import Path from "./src/utils/Path.ts"

Expand Down
16 changes: 8 additions & 8 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as is_what from "https://deno.land/x/is_what@v4.1.15/src/index.ts"
import * as is_what from "is-what"
export { is_what }

import { type PlainObject } from "https://deno.land/x/is_what@v4.1.15/src/index.ts"
import type { PlainObject } from "is-what"
export type { PlainObject }

import * as outdent from "https://deno.land/x/outdent@v0.8.0/mod.ts"
import * as outdent from "outdent"
export { outdent }

// importing super specifically to reduce final npm bundle size
import * as crypto from "jsr:@std/crypto@1"
import { moveSync } from "jsr:@std/fs@1"
import { writeAll } from "jsr:@std/io@^0.225.0"
import { parse as parseYaml, parseAll as parseYamlALL } from "jsr:@std/yaml@1"
import { SEPARATOR as SEP, fromFileUrl } from "jsr:@std/path@1"
import * as crypto from "@std/crypto"
import { moveSync } from "@std/fs"
import { writeAll } from "@std/io/write-all"
import { parse as parseYaml, parseAll as parseYamlALL } from "@std/yaml"
import { SEPARATOR as SEP, fromFileUrl } from "@std/path"

const streams = { writeAll }
const fs = { moveSync }
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/useCache.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Stowage } from "../types.ts"
import type { Stowage } from "../types.ts"
import useConfig from "./useConfig.ts"
import host from "../utils/host.ts"
import type Path from "../utils/Path.ts"

export default function useCache() {
export default function useCache(): { path: (stowage: Stowage) => Path } {
return { path }
}

const path = (stowage: Stowage) => {
const path = (stowage: Stowage): Path => {
const { pkg, type } = stowage
const stem = pkg.project.replaceAll("/", "∕")

Expand Down
12 changes: 10 additions & 2 deletions src/hooks/useCellar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Package, PackageRequirement, Installation } from "../types.ts"
import type { Package, PackageRequirement, Installation } from "../types.ts"
import { PkgxError } from "../utils/error.ts"
import * as pkgutils from "../utils/pkg.ts"
import SemVer from "../utils/semver.ts"
Expand All @@ -14,7 +14,15 @@ export class InstallationNotFoundError extends PkgxError {
}
}

export default function useCellar() {
export type UseCellar = {
has: (pkg: Package | PackageRequirement | Path) => Promise<Installation | undefined>
ls: (project: string) => Promise<Installation[]>
keg: (pkg: Package) => Path
resolve: (pkg: Package | PackageRequirement | Path | Installation) => Promise<Installation>
shelf: (project: string) => Path
}

export default function useCellar(): UseCellar {
const config = useConfig()

/// eg. ~/.pkgx/deno.land
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function platform_data_home_default(home: Path, { LOCALAPPDATA }: { LOCALAPPDATA

const SEP = Deno.build.os == 'windows' ? ';' : ':'

export function ConfigDefault(env = Deno.env.toObject()): Config {
export function ConfigDefault(env: Record<string, string> = Deno.env.toObject()): Config {
const home = flatmap(env['PKGX_HOME'], x => new Path(x)) ?? Path.home()
const prefix = flatmap(env['PKGX_DIR']?.trim(), x => new Path(x)) ??
flatmap(env['XDG_DATA_HOME'], x => new Path(x).join("pkgx")) ??
Expand Down Expand Up @@ -115,7 +115,7 @@ function boolize(input: string | undefined): boolean | undefined {
}
}

function initialized() {
function initialized(): boolean {
return gt.sh_pkgx_config !== undefined
}

Expand Down
7 changes: 5 additions & 2 deletions src/hooks/useDownload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deno } from "../deps.ts"
const { crypto: crypto_, streams: { writeAll } } = deno
const { crypto } = crypto_
import { encodeHex } from "jsr:@std/encoding@1"
import { encodeHex } from "@std/encoding"
import { PkgxError, panic } from "../utils/error.ts"
import useConfig from "./useConfig.ts"
import useFetch from "./useFetch.ts"
Expand Down Expand Up @@ -80,7 +80,10 @@ function cache({ for: url }: {for: URL}): Path {
}
}

export default function useDownload() {
export default function useDownload(): {
download: typeof download
cache: typeof cache
} {
return {
download,
cache
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useInventory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Package, PackageRequirement } from "../types.ts"
import type { Package, PackageRequirement } from "../types.ts"
import { DownloadError } from "./useDownload.ts"
import SemVer from "../utils/semver.ts"
import useFetch from "./useFetch.ts"
Expand All @@ -14,7 +14,7 @@ export interface Inventory {
}
}

const select = async (rq: PackageRequirement | Package) => {
const select = async (rq: PackageRequirement | Package): Promise<SemVer | undefined> => {
const versions = await _internals.get(rq)

if ("constraint" in rq) {
Expand All @@ -24,7 +24,7 @@ const select = async (rq: PackageRequirement | Package) => {
}
}

const get = async (rq: PackageRequirement | Package) => {
const get = async (rq: PackageRequirement | Package): Promise<SemVer[]> => {
const { platform, arch } = host()
const url = new URL(`${useConfig().dist}/${rq.project}/${platform}/${arch}/versions.txt`)
const rsp = await useFetch(url)
Expand All @@ -47,7 +47,10 @@ const get = async (rq: PackageRequirement | Package) => {
return versions
}

export default function useInventory() {
export default function useInventory(): {
select: typeof select
get: typeof get
} {
return { select, get }
}

Expand Down
21 changes: 15 additions & 6 deletions src/hooks/useMoustaches.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Package, Installation } from "../types.ts"
import SemVer from "../utils/semver.ts"
import type { Package, Installation } from "../types.ts"
import type SemVer from "../utils/semver.ts"
import useConfig from "./useConfig.ts"
import useCellar from "./useCellar.ts"

function tokenizePackage(pkg: Package) {
function tokenizePackage(pkg: Package): { from: string, to: string }[] {
return [{ from: "prefix", to: useCellar().keg(pkg).string }]
}

function tokenizeVersion(version: SemVer, prefix = 'version') {
function tokenizeVersion(version: SemVer, prefix = 'version'): { from: string, to: string }[] {
const rv = [
{ from: prefix, to: `${version}` },
{ from: `${prefix}.major`, to: `${version.major}` },
Expand All @@ -23,13 +23,22 @@ function tokenizeVersion(version: SemVer, prefix = 'version') {
return rv
}

function apply(input: string, map: { from: string, to: string }[]) {
function apply(input: string, map: { from: string, to: string }[]): string {
return map.reduce((acc, {from, to}) =>
acc.replace(new RegExp(`(^\\$)?{{\\s*${from}\\s*}}`, "g"), to),
input)
}

export default function() {
export default function(): {
apply: typeof apply
tokenize: {
version: typeof tokenizeVersion
pkg: typeof tokenizePackage
deps: (deps: Installation[]) => { from: string, to: string }[]
pkgx: () => { from: string, to: string }[]
all: (pkg: Package, deps_: Installation[]) => { from: string, to: string }[]
}
} {
const config = useConfig()
const base = {
apply,
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useOffLicense.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Stowage } from "../types.ts"
import type { Stowage } from "../types.ts"
import host from "../utils/host.ts"
import useConfig from "./useConfig.ts";

type Type = 's3'

export default function useOffLicense(_type: Type) {
export default function useOffLicense(_type: Type): {
url: typeof url
key: typeof key
} {
return { url, key }
}

function key(stowage: Stowage) {
function key(stowage: Stowage): string {
const rv = [stowage.pkg.project]
if (stowage.type == 'bottle') {
const { platform, arch } = stowage.host ?? host()
Expand All @@ -24,6 +27,6 @@ function key(stowage: Stowage) {
return rv.join("/")
}

function url(stowage: Stowage) {
function url(stowage: Stowage): URL {
return new URL(`${useConfig().dist}/${key(stowage)}`)
}
Loading
Loading