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

fix: enable dprint dep to be used offline #622

Merged
merged 8 commits into from
Feb 22, 2023
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
42 changes: 0 additions & 42 deletions _tasks/download_frame_metadata.ts

This file was deleted.

62 changes: 62 additions & 0 deletions _tasks/downloads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { chain as kusama } from "kusama/mod.ts"
import { chain as polkadot } from "polkadot/mod.ts"
import { chain as rococo } from "rococo/mod.ts"
import { chain as westend } from "westend/mod.ts"
import { emptyDir } from "../deps/std/fs.ts"
import dprintConfig from "../dprint.json" assert { type: "json" }
import { hex } from "../util/mod.ts"

const GENERATION_NOTICE = `// This file was generated by \`_tasks/downloads.ts\``

const response = await fetch(
dprintConfig.plugins.find((v) => v.startsWith("https://plugins.dprint.dev/typescript-"))!,
)
const wasm = new TextDecoder().decode(hex.encodeBuf(new Uint8Array(await response.arrayBuffer())))
const tsFormatterPath = new URL("../util/tsFormatter.ts", import.meta.url)
await Deno.writeTextFile(
tsFormatterPath,
`${GENERATION_NOTICE}
import { createFromBuffer } from "../deps/dprint.ts"
import dprintConfig from "../dprint.json" assert { type: "json" }
import { decode } from "./hex.ts"

const wasm = "${wasm}"

export const tsFormatter = createFromBuffer(decode(wasm))
const { indentWidth, lineWidth, typescript: config } = dprintConfig
tsFormatter.setConfig({ indentWidth, lineWidth }, config)
`,
)

const knownChains = { kusama, polkadot, westend, rococo }
const names = Object.keys(knownChains)
const outDir = new URL("../frame_metadata/_downloaded", import.meta.url)
await emptyDir(outDir)

const modFilePath = new URL("_downloaded/mod.ts", outDir)
const modFileContents = `${GENERATION_NOTICE}
import { hex } from "../../util/mod.ts"
import { $metadata } from "../mod.ts"

export const [
${names.join(",\n ")},
] = await Promise.all([
${names.map((name) => `download("${name}")`).join(",\n ")},
])

async function download(name: string) {
return $metadata.decode(
hex.decodeBuf((await Deno.readFile(new URL(\`./$\{name}.scale\`, import.meta.url))).slice(2)),
)
}
`
Deno.writeTextFileSync(modFilePath, modFileContents, { create: true })

await Promise.all(
Object.entries(knownChains).map(async ([name, chain]) => {
const result = await chain.connection.call("state_getMetadata").run()
const outPath = new URL(`_downloaded/${name}.scale`, outDir)
console.log(`Downloading ${name} metadata to "${outPath}".`)
await Deno.writeTextFile(outPath, result)
}),
)
2 changes: 1 addition & 1 deletion codegen/frame/File.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tsFormatter } from "../../deps/dprint.ts"
import { tsFormatter } from "../../util/tsFormatter.ts"

export class File {
constructor(public codeRaw = "") {}
Expand Down
9 changes: 0 additions & 9 deletions deps/dprint.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
export * from "https://deno.land/x/[email protected]/mod.ts"
import { createStreaming } from "https://deno.land/x/[email protected]/mod.ts"
import dprintConfig from "../dprint.json" assert { type: "json" }

const { indentWidth, lineWidth, typescript: config, plugins } = dprintConfig

export const tsFormatter = await createStreaming(
fetch(plugins.find((v) => v.startsWith("https://plugins.dprint.dev/typescript-"))!),
)
tsFormatter.setConfig({ indentWidth, lineWidth }, config)
2 changes: 1 addition & 1 deletion dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"semiColons": "asi"
},
"includes": ["**.{dockerfile,json,md,toml,ts,tsx}"],
"excludes": ["target"],
"excludes": ["target", "util/tsFormatter.ts"],
"plugins": [
"https://plugins.dprint.dev/dockerfile-0.3.0.wasm",
"https://plugins.dprint.dev/json-0.17.0.wasm",
Expand Down
6 changes: 3 additions & 3 deletions frame_metadata/_downloaded/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This file was generated by `_tasks/download_frame_metadata.ts`
import * as U from "../../util/mod.ts"
// This file was generated by `_tasks/downloads.ts`
import { hex } from "../../util/mod.ts"
import { $metadata } from "../mod.ts"

export const [
Expand All @@ -16,6 +16,6 @@ export const [

async function download(name: string) {
return $metadata.decode(
U.hex.decodeBuf((await Deno.readFile(new URL(`./${name}.scale`, import.meta.url))).slice(2)),
hex.decodeBuf((await Deno.readFile(new URL(`./${name}.scale`, import.meta.url))).slice(2)),
)
}
10 changes: 10 additions & 0 deletions util/tsFormatter.ts

Large diffs are not rendered by default.