Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

chore: config revamp #948

Merged
merged 28 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 22 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
32 changes: 13 additions & 19 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,21 @@ npm i capi

## Configuration

Create a `capi.config.ts` and specify the chains with which you'd like to
interact.
Create a `nets.ts` and specify the chains with which you'd like to interact.

```ts
import { binary, CapiConfig } from "capi"

export const config: CapiConfig = {
server: "https://capi.dev/",
chains: {
// 1. the Polkadot relay chain
polkadot: {
url: "wss://rpc.polkadot.io/",
version: "v0.9.40",
},
// 2. a Polkadot development network
polkadotDev: {
binary: binary("polkadot", "v0.9.38"),
chain: "polkadot-dev",
},
},
}
import { bins, dev, ws } from "capi"

const bin = bins({ polkadot: ["polkadot", "v0.9.38"] })

// The Polkadot relay chain
export const polkadot = ws({ url: "wss://rpc.polkadot.io/" })

// A Polkadot development network
export const polkadotDev = dev({
bin: bin.polkadot,
chain: "polkadot-dev",
})
```

## Command Line Tool
Expand Down
4 changes: 2 additions & 2 deletions _tasks/dnt.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { config } from "../capi.config.ts"
import { build, EntryPoint } from "../deps/dnt.ts"
import * as flags from "../deps/std/flags.ts"
import * as fs from "../deps/std/fs.ts"
import * as path from "../deps/std/path.ts"
import importMap from "../import_map.json" assert { type: "json" }
import * as nets from "../nets.ts"
import { normalizePackageName } from "../util/normalize.ts"

const { version: packageVersion, server: serverVersion } = flags.parse(Deno.args, {
Expand Down Expand Up @@ -55,7 +55,7 @@ await Promise.all([
license: "Apache-2.0",
repository: "github:paritytech/capi",
dependencies: Object.fromEntries(
Object.keys(config.chains ?? {}).map((key) => {
Object.keys(nets).map((key) => {
const name = normalizePackageName(key)
return [`@capi/${name}`, `${server}${hash}/${name}.tar`]
}),
Expand Down
4 changes: 2 additions & 2 deletions _tasks/generate_artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { hex } from "../crypto/mod.ts"
import * as $ from "../deps/scale.ts"
import { emptyDir } from "../deps/std/fs.ts"
import * as path from "../deps/std/path.ts"
import { devUser } from "../devnets/mod.ts"
import dprintConfig from "../dprint.json" assert { type: "json" }
import { compress } from "../util/compression.ts"
import { devUser } from "../nets/mod.ts"
import { compress } from "../util/mod.ts"

export const DEV_USER_COUNT = 100_000

Expand Down
24 changes: 15 additions & 9 deletions _tasks/use_remote.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// This task is used in CI to edit the import map to use capi.dev for codegen
// instead of the local server.

import { config } from "../capi.config.ts"
import { checkCodegenUploaded, syncConfig } from "../mod.ts"
import * as nets from "../nets.ts"
import { syncNets } from "../server/mod.ts"

const LOCAL_SERVER = "http://localhost:4646/"

const shaAbbrevLength = 8
const sha = Deno.env.get("CAPI_SHA")!.slice(0, shaAbbrevLength)

const oldServer = config.server
const capiServer = `https://capi.dev/@${sha}/`
config.server = capiServer

const importMap = JSON.parse(await Deno.readTextFile("import_map.json"))
const oldCodegenUrl = importMap.imports["@capi/"]

if (!oldCodegenUrl.startsWith(oldServer)) {
if (!oldCodegenUrl.startsWith(LOCAL_SERVER)) {
throw new Error("_tasks/use_remote.ts run twice")
}

const codegenUrl = capiServer + oldCodegenUrl.slice(oldServer.length)
const codegenUrl = capiServer + oldCodegenUrl.slice(LOCAL_SERVER.length)

console.log(codegenUrl)

Expand All @@ -27,11 +27,17 @@ importMap.imports[`${capiServer}capi/`] = "./"

await Deno.writeTextFile("import_map.json", JSON.stringify(importMap, null, 2))

const codegenHash = oldCodegenUrl.slice(oldServer.length).split("/")[0]
const codegenHash = oldCodegenUrl.slice(LOCAL_SERVER.length).split("/")[0]

if (await checkCodegenUploaded(capiServer, codegenHash)) {
if (await isUploaded(capiServer, codegenHash)) {
console.log("codegen already uploaded")
} else {
console.log("uploading codegen")
await syncConfig("target/capi", config)
await syncNets("https://capi.dev/", "target/capi", nets)
}

async function isUploaded(server: string, hash: string) {
const url = new URL(`upload/codegen/${hash}`, server)
const exists = await fetch(url, { method: "HEAD" })
return exists.ok
}
70 changes: 0 additions & 70 deletions capi.config.ts

This file was deleted.

11 changes: 8 additions & 3 deletions cli/bin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CapiBinary, resolveBinary } from "../devnets/binary.ts"
import { CapiBinary } from "../deps/capi_binary_builds.ts"

export default async function(
binary: string,
Expand All @@ -7,9 +7,14 @@ export default async function(
) {
if (!binary || !version) throw new Error("Must specify binary and version")

const binaryPath = await resolveBinary(new CapiBinary(binary, version))
const bin = new CapiBinary(binary, version)

const child = new Deno.Command(binaryPath, {
if (!(await bin.exists())) {
console.error("Downloading", bin.key)
await bin.download()
}

const child = new Deno.Command(bin.path, {
args,
stdin: "inherit",
stdout: "inherit",
Expand Down
18 changes: 0 additions & 18 deletions cli/resolveConfig.ts

This file was deleted.

19 changes: 19 additions & 0 deletions cli/resolveNets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as $ from "../deps/scale.ts"
import * as flags from "../deps/std/flags.ts"
import * as path from "../deps/std/path.ts"
import { NetSpec } from "../nets/mod.ts"

const $nets = $.record($.instance(NetSpec as new() => NetSpec, $.tuple(), () => []))

export async function resolveNets(...args: string[]): Promise<Record<string, NetSpec>> {
const { nets: netsPathRaw } = flags.parse(args, {
string: ["nets"],
default: { nets: "./nets.ts" },
})
const netsPath = path.resolve(netsPathRaw)
await Deno.stat(netsPath)
const nets = await import(path.toFileUrl(netsPath).toString())
$.assert($nets, nets)
for (const key in nets) nets[key]!.name = key
return nets
}
25 changes: 13 additions & 12 deletions cli/serve.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import * as flags from "../deps/std/flags.ts"
import { blue, gray, yellow } from "../deps/std/fmt/colors.ts"
import { serve } from "../deps/std/http.ts"
import { createTempDir } from "../devnets/createTempDir.ts"
import { createDevnetsHandler } from "../devnets/mod.ts"
import { createCorsHandler } from "../server/corsHandler.ts"
import { createErrorHandler } from "../server/errorHandler.ts"
import { createCodegenHandler } from "../server/mod.ts"
import { InMemoryCache } from "../util/cache/memory.ts"
import { FsCache } from "../util/cache/mod.ts"
import {
createCodegenHandler,
createCorsHandler,
createDevnetsHandler,
createErrorHandler,
} from "../server/mod.ts"
import { FsCache, InMemoryCache } from "../util/cache/mod.ts"
import { gracefulExit } from "../util/mod.ts"
import { resolveConfig } from "./resolveConfig.ts"
import { tempDir } from "../util/tempDir.ts"
import { resolveNets } from "./resolveNets.ts"

export default async function(...args: string[]) {
const { port, "--": cmd, out } = flags.parse(args, {
Expand All @@ -21,9 +22,9 @@ export default async function(...args: string[]) {
"--": true,
})

const config = await resolveConfig(...args)
const nets = await resolveNets(...args)

const tempDir = await createTempDir()
const devnetTempDir = await tempDir(out, "devnet")

const href = `http://localhost:${port}/`

Expand All @@ -39,7 +40,7 @@ export default async function(...args: string[]) {
.catch(() => false)

if (!running) {
const devnetsHandler = createDevnetsHandler(tempDir, config, signal)
const devnetsHandler = createDevnetsHandler(devnetTempDir, nets, signal)
const codegenHandler = createCodegenHandler(dataCache, tempCache)
const handler = createCorsHandler(createErrorHandler(async (request) => {
const { pathname } = new URL(request.url)
Expand Down Expand Up @@ -75,7 +76,7 @@ export default async function(...args: string[]) {
args,
signal,
env: {
DEVNETS_SERVER: `http://localhost:${port}/devnets/`,
CAPI_SERVER: href,
},
})
const status = await command.spawn().status
Expand Down
20 changes: 13 additions & 7 deletions cli/sync.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import * as flags from "../deps/std/flags.ts"
import { blue, gray } from "../deps/std/fmt/colors.ts"
import { assertEquals } from "../deps/std/testing/asserts.ts"
import { createTempDir } from "../devnets/createTempDir.ts"
import { syncConfig } from "../devnets/mod.ts"
import { syncNets } from "../server/mod.ts"
import { normalizePackageName } from "../util/mod.ts"
import { resolveConfig } from "./resolveConfig.ts"
import { tempDir } from "../util/tempDir.ts"
import { resolveNets } from "./resolveNets.ts"

export default async function(...args: string[]) {
const {
"import-map": importMapFile,
"package-json": packageJsonFile,
check,
out,
server,
} = flags.parse(args, {
string: ["config", "import-map", "package-json"],
string: ["config", "import-map", "out", "package-json", "server"],
boolean: ["check"],
default: {
server: "https://capi.dev/",
out: "target/capi",
},
})

const config = await resolveConfig(...args)
const config = await resolveNets(...args)

const tempDir = await createTempDir()
const devnetTempDir = await tempDir(out, "devnet")

const baseUrl = await syncConfig(tempDir, config)
const baseUrl = await syncNets(server, devnetTempDir, config)

if (importMapFile) {
syncFile(importMapFile, (importMap) => {
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"cache": "deno task capi serve -- deno cache -r=http://localhost:4646/",
"check": "deno task capi serve -- deno cache --check",
"star": "deno task run _tasks/star.ts && deno task check target/star.ts",
"sync": "mkdir -p target && deno task capi serve -- deno task capi sync --import-map import_map.json",
"sync": "mkdir -p target && deno task capi serve -- deno task capi sync --server http://localhost:4646/ --import-map import_map.json",
"run": "deno task capi serve -- deno run -A -r=http://localhost:4646/"
}
}
Loading