feat(deno-lib): init (#89)

This commit is contained in:
winston 2023-11-23 10:09:17 +01:00 committed by GitHub
parent ee2160b1b3
commit 58f1b8d0c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 389 additions and 14 deletions

View File

@ -3,9 +3,9 @@ name: Build and populate cache
on:
push:
branches: [main]
paths: ["**.nix", "**.js", "**.ts", "**.rs", "Cargo.lock", "package.json", "package-lock.json"]
paths: ["**.nix", "**.rs", "Cargo.lock"]
pull_request:
paths: ["**.nix", "**.js", "**.ts", "**.rs", "Cargo.lock", "package.json", "package-lock.json"]
paths: ["**.nix", "**.rs", "Cargo.lock"]
workflow_dispatch:
jobs:

29
.github/workflows/deno.yml vendored Normal file
View File

@ -0,0 +1,29 @@
name: Deno Check
on:
push:
branches: [main]
paths: ["**/*.ts", "deno.*"]
pull_request:
branches: [main]
paths: ["**/*.ts", "deno.*"]
jobs:
deno-check:
name: Check Deno code
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Deno
uses: nekowinston/setup-deno@v1
with:
directory: deno-lib
- name: Check
run: |
shopt -s globstar
deno lint
deno check deno-lib/**/*.ts
deno fmt --check deno-lib/**/*.ts

View File

@ -0,0 +1,35 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"description": "A representation of a blog post",
"type": "object",
"required": ["title", "content", "author"],
"properties": {
"title": {
"type": "string"
},
"content": {
"type": "string"
},
"publishedDate": {
"type": "string"
},
"author": {
"type": "object",
"required": ["username"],
"properties": {
"username": {
"type": "string"
},
"email": {
"type": "string"
}
}
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
}

2
deno-lib/mod.ts Normal file
View File

@ -0,0 +1,2 @@
export { validateYaml } from "./validateYaml.ts";
export { updateReadme } from "./updateReadme.ts";

View File

@ -0,0 +1,85 @@
import {
assertEquals,
assertThrows,
} from "https://deno.land/std@0.207.0/assert/mod.ts";
import { updateReadme } from "./updateReadme.ts";
const readme_default = `
Text before the section.
<!-- AUTOGEN START -->
<!-- AUTOGEN END -->
Text after the section.
`;
Deno.test("updateReadme pass (defaults)", () => {
const newReadme = updateReadme(
readme_default,
"<new content>",
);
assertEquals(
newReadme,
`
Text before the section.
<!-- AUTOGEN START -->
<!-- the following section is auto-generated, do not edit -->
<new content>
<!-- AUTOGEN END -->
Text after the section.
`,
);
});
Deno.test("updateReadme fail (defaults)", () => {
assertThrows(() => {
updateReadme(
readme_default,
"bar-section-title",
{
section: "foo-section-title",
},
);
});
});
const readme_custom = `
Text before the section.
# Custom start
Arbitrary end marker.
Text after the section.
`;
Deno.test("updateReadme pass (customized)", () => {
const newReadme = updateReadme(
readme_custom,
"<new content>",
{
section: "custom",
preamble: "Some custom preamble.",
markers: {
start: "# Custom start",
end: "Arbitrary end marker",
},
},
);
assertEquals(
newReadme,
`
Text before the section.
# Custom start
Some custom preamble.
<new content>
Arbitrary end marker.
Text after the section.
`,
);
});

79
deno-lib/updateReadme.ts Normal file
View File

@ -0,0 +1,79 @@
/**
* Update a section of a README.md file.
*
* Assumes that the section is marked by HTML comments like so:
* ```md
* <!-- AUTOGEN START -->
* <!-- AUTOGEN END -->
* ```
*
* When section names are used, the markers look like this:
* ```md
* <!-- AUTOGEN:SECTION START -->
* <!-- AUTOGEN:SECTION END -->
* ```
*
* The section name is case-insensitive, as section markers are converted to
* uppercase.
*
* If there are multiple sections with the same name, only the first one will be
* updated.
*
* @param {string} readme
* The README.md file contents
* @param {string} newContent
* The new content to put in the section
* @param {object} options
* Options to customize the behavior
* @param {string} options.section
* The section name
* @param {string} options.preamble
* Text to insert after the start marker, usually a warning to the reader not to
* edit the section by hand
* @param {object} options.markers
* The start and end markers
* @param {string} [options.markers.start="<!-- AUTOGEN START -->"]
* The start marker
* @param {string} [options.markers.end="<!-- AUTOGEN END -->"]
* The end marker
*
* @returns {string}
* The updated README.md file contents
* @throws if the section is not found
*/
export const updateReadme = (
readme: string,
newContent: string,
options: {
section?: string;
preamble?: string;
markers?: {
start: string;
end: string;
};
} = {},
): string => {
const {
section = "",
preamble = "<!-- the following section is auto-generated, do not edit -->",
markers = {
start: `<!-- AUTOGEN${
section !== "" ? `:${section.toUpperCase()}` : ""
} START -->`,
end: `<!-- AUTOGEN${
section !== "" ? `:${section.toUpperCase()}` : ""
} END -->`,
},
} = options;
const wrapped = [markers.start, preamble, newContent, markers.end].join("\n");
Object.values(markers).map((m) => {
if (!readme.includes(m)) {
throw new Error(`Marker ${m} not found in README.md`);
}
});
const pre = readme.split(markers.start)[0];
const end = readme.split(markers.end)[1];
return pre + wrapped + end;
};

View File

@ -0,0 +1,42 @@
import { assertEquals } from "https://deno.land/std@0.207.0/assert/mod.ts";
import { validateYaml } from "./validateYaml.ts";
import blogPostSchema from "./fixtures/updateYaml/blogpost.schema.json" assert {
type: "json",
};
const blogPost = `title: "New Blog Post"
content: "This is the content of the blog post..."
publishedDate: "2023-08-25T15:00:00Z"
author:
username: "authoruser"
email: "author@example.com"
tags:
- "Technology"
- "Programming"`;
Deno.test("YAML parse pass", async () => {
const data = await validateYaml(blogPost, blogPostSchema);
assertEquals(data, {
title: "New Blog Post",
content: "This is the content of the blog post...",
publishedDate: "2023-08-25T15:00:00Z",
author: {
username: "authoruser",
email: "author@example.com",
},
tags: ["Technology", "Programming"],
});
});
Deno.test("YAML parse fail", async () => {
// first line removed to make the YAML invalid
const data = blogPost.split("\n").slice(1).join("\n");
await validateYaml(
data,
blogPostSchema,
).catch((err) => {
assertEquals(err[0].message, "must have required property 'title'");
});
});

24
deno-lib/validateYaml.ts Normal file
View File

@ -0,0 +1,24 @@
import Ajv, { type Schema } from "https://esm.sh/v135/ajv@8.12.0";
import { parse } from "https://deno.land/std@0.207.0/yaml/parse.ts";
/**
* Validates a string of YAML content against a JSON schema with Ajv.
* @param content A string containing YAML content
* @param schema A JSON schema
* @returns A promise that resolves to the parsed YAML content, verified against the schema. Rejects if the content is invalid.
*/
export const validateYaml = <T>(
content: string,
schema: Schema,
): Promise<T> => {
return new Promise((resolve, reject) => {
const ajv = new Ajv();
const validate = ajv.compile<T>(schema);
const data = parse(content);
if (!validate(data)) return reject(validate.errors);
return resolve(data);
});
};

11
deno.json Normal file
View File

@ -0,0 +1,11 @@
{
"fmt": {
"include": ["deno-lib/"]
},
"lint": {
"include": ["deno-lib/"]
},
"test": {
"include": ["deno-lib/"]
}
}

74
deno.lock generated Normal file
View File

@ -0,0 +1,74 @@
{
"version": "3",
"remote": {
"https://deno.land/std@0.207.0/assert/_constants.ts": "8a9da298c26750b28b326b297316cdde860bc237533b07e1337c021379e6b2a9",
"https://deno.land/std@0.207.0/assert/_diff.ts": "58e1461cc61d8eb1eacbf2a010932bf6a05b79344b02ca38095f9b805795dc48",
"https://deno.land/std@0.207.0/assert/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7",
"https://deno.land/std@0.207.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee",
"https://deno.land/std@0.207.0/assert/assert_almost_equals.ts": "e15ca1f34d0d5e0afae63b3f5d975cbd18335a132e42b0c747d282f62ad2cd6c",
"https://deno.land/std@0.207.0/assert/assert_array_includes.ts": "6856d7f2c3544bc6e62fb4646dfefa3d1df5ff14744d1bca19f0cbaf3b0d66c9",
"https://deno.land/std@0.207.0/assert/assert_equals.ts": "d8ec8a22447fbaf2fc9d7c3ed2e66790fdb74beae3e482855d75782218d68227",
"https://deno.land/std@0.207.0/assert/assert_exists.ts": "407cb6b9fb23a835cd8d5ad804e2e2edbbbf3870e322d53f79e1c7a512e2efd7",
"https://deno.land/std@0.207.0/assert/assert_false.ts": "0ccbcaae910f52c857192ff16ea08bda40fdc79de80846c206bfc061e8c851c6",
"https://deno.land/std@0.207.0/assert/assert_greater.ts": "ae2158a2d19313bf675bf7251d31c6dc52973edb12ac64ac8fc7064152af3e63",
"https://deno.land/std@0.207.0/assert/assert_greater_or_equal.ts": "1439da5ebbe20855446cac50097ac78b9742abe8e9a43e7de1ce1426d556e89c",
"https://deno.land/std@0.207.0/assert/assert_instance_of.ts": "3aedb3d8186e120812d2b3a5dea66a6e42bf8c57a8bd927645770bd21eea554c",
"https://deno.land/std@0.207.0/assert/assert_is_error.ts": "c21113094a51a296ffaf036767d616a78a2ae5f9f7bbd464cd0197476498b94b",
"https://deno.land/std@0.207.0/assert/assert_less.ts": "aec695db57db42ec3e2b62e97e1e93db0063f5a6ec133326cc290ff4b71b47e4",
"https://deno.land/std@0.207.0/assert/assert_less_or_equal.ts": "5fa8b6a3ffa20fd0a05032fe7257bf985d207b85685fdbcd23651b70f928c848",
"https://deno.land/std@0.207.0/assert/assert_match.ts": "c4083f80600bc190309903c95e397a7c9257ff8b5ae5c7ef91e834704e672e9b",
"https://deno.land/std@0.207.0/assert/assert_not_equals.ts": "9f1acab95bd1f5fc9a1b17b8027d894509a745d91bac1718fdab51dc76831754",
"https://deno.land/std@0.207.0/assert/assert_not_instance_of.ts": "0c14d3dfd9ab7a5276ed8ed0b18c703d79a3d106102077ec437bfe7ed912bd22",
"https://deno.land/std@0.207.0/assert/assert_not_match.ts": "3796a5b0c57a1ce6c1c57883dd4286be13a26f715ea662318ab43a8491a13ab0",
"https://deno.land/std@0.207.0/assert/assert_not_strict_equals.ts": "ca6c6d645e95fbc873d25320efeb8c4c6089a9a5e09f92d7c1c4b6e935c2a6ad",
"https://deno.land/std@0.207.0/assert/assert_object_match.ts": "d8fc2867cfd92eeacf9cea621e10336b666de1874a6767b5ec48988838370b54",
"https://deno.land/std@0.207.0/assert/assert_rejects.ts": "45c59724de2701e3b1f67c391d6c71c392363635aad3f68a1b3408f9efca0057",
"https://deno.land/std@0.207.0/assert/assert_strict_equals.ts": "b1f538a7ea5f8348aeca261d4f9ca603127c665e0f2bbfeb91fa272787c87265",
"https://deno.land/std@0.207.0/assert/assert_string_includes.ts": "b821d39ebf5cb0200a348863c86d8c4c4b398e02012ce74ad15666fc4b631b0c",
"https://deno.land/std@0.207.0/assert/assert_throws.ts": "63784e951475cb7bdfd59878cd25a0931e18f6dc32a6077c454b2cd94f4f4bcd",
"https://deno.land/std@0.207.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56",
"https://deno.land/std@0.207.0/assert/equal.ts": "9f1a46d5993966d2596c44e5858eec821859b45f783a5ee2f7a695dfc12d8ece",
"https://deno.land/std@0.207.0/assert/fail.ts": "c36353d7ae6e1f7933d45f8ea51e358c8c4b67d7e7502028598fe1fea062e278",
"https://deno.land/std@0.207.0/assert/mod.ts": "37c49a26aae2b254bbe25723434dc28cd7532e444cf0b481a97c045d110ec085",
"https://deno.land/std@0.207.0/assert/unimplemented.ts": "d56fbeecb1f108331a380f72e3e010a1f161baa6956fd0f7cf3e095ae1a4c75a",
"https://deno.land/std@0.207.0/assert/unreachable.ts": "4600dc0baf7d9c15a7f7d234f00c23bca8f3eba8b140286aaca7aa998cf9a536",
"https://deno.land/std@0.207.0/fmt/colors.ts": "34b3f77432925eb72cf0bfb351616949746768620b8e5ead66da532f93d10ba2",
"https://deno.land/std@0.207.0/yaml/_error.ts": "b59e2c76ce5a47b1b9fa0ff9f96c1dd92ea1e1b17ce4347ece5944a95c3c1a84",
"https://deno.land/std@0.207.0/yaml/_loader/loader.ts": "63ec7f0a265dbbabc54b25a4beefff7650e205160a2d75c7d8f8363b5f84851a",
"https://deno.land/std@0.207.0/yaml/_loader/loader_state.ts": "0841870b467169269d7c2dfa75cd288c319bc06f65edd9e42c29e5fced91c7a4",
"https://deno.land/std@0.207.0/yaml/_mark.ts": "dcd8585dee585e024475e9f3fe27d29740670fb64ebb970388094cad0fc11d5d",
"https://deno.land/std@0.207.0/yaml/_state.ts": "ef03d55ec235d48dcfbecc0ab3ade90bfae69a61094846e08003421c2cf5cfc6",
"https://deno.land/std@0.207.0/yaml/_type/binary.ts": "24d49614463a7339a8a16d894919c2ec18a10588ae360ec352093b60e2cc8b0d",
"https://deno.land/std@0.207.0/yaml/_type/bool.ts": "5bfa75da84343d45347b521ba4e5aeace9fe6f53447405290d53315a3fc20e66",
"https://deno.land/std@0.207.0/yaml/_type/float.ts": "056bd3cb9c5586238b20517511014fb24b0e36f98f9f6073e12da308b6b9808a",
"https://deno.land/std@0.207.0/yaml/_type/function.ts": "ff574fe84a750695302864e1c31b93f12d14ada4bde79a5f93197fc33ad17471",
"https://deno.land/std@0.207.0/yaml/_type/int.ts": "563ad074f0fa7aecf6b6c3d84135bcc95a8269dcc15de878de20ce868fd773fa",
"https://deno.land/std@0.207.0/yaml/_type/map.ts": "7b105e4ab03a361c61e7e335a0baf4d40f06460b13920e5af3fb2783a1464000",
"https://deno.land/std@0.207.0/yaml/_type/merge.ts": "8192bf3e4d637f32567917f48bb276043da9cf729cf594e5ec191f7cd229337e",
"https://deno.land/std@0.207.0/yaml/_type/mod.ts": "060e2b3d38725094b77ea3a3f05fc7e671fced8e67ca18e525be98c4aa8f4bbb",
"https://deno.land/std@0.207.0/yaml/_type/nil.ts": "606e8f0c44d73117c81abec822f89ef81e40f712258c74f186baa1af659b8887",
"https://deno.land/std@0.207.0/yaml/_type/omap.ts": "cfe59a294726f5cea705c39a61fd2b08199cf48f4ccd6b040cb550ec0f38d0a1",
"https://deno.land/std@0.207.0/yaml/_type/pairs.ts": "0032fdfe57558d21696a4f8cf5b5cfd1f698743177080affc18629685c905666",
"https://deno.land/std@0.207.0/yaml/_type/regexp.ts": "1ce118de15b2da43b4bd8e4395f42d448b731acf3bdaf7c888f40789f9a95f8b",
"https://deno.land/std@0.207.0/yaml/_type/seq.ts": "95333abeec8a7e4d967b8c8328b269e342a4bbdd2585395549b9c4f58c8533a2",
"https://deno.land/std@0.207.0/yaml/_type/set.ts": "f28ba44e632ef2a6eb580486fd47a460445eeddbdf1dbc739c3e62486f566092",
"https://deno.land/std@0.207.0/yaml/_type/str.ts": "a67a3c6e429d95041399e964015511779b1130ea5889fa257c48457bd3446e31",
"https://deno.land/std@0.207.0/yaml/_type/timestamp.ts": "706ea80a76a73e48efaeb400ace087da1f927647b53ad6f754f4e06d51af087f",
"https://deno.land/std@0.207.0/yaml/_type/undefined.ts": "94a316ca450597ccbc6750cbd79097ad0d5f3a019797eed3c841a040c29540ba",
"https://deno.land/std@0.207.0/yaml/_utils.ts": "26b311f0d42a7ce025060bd6320a68b50e52fd24a839581eb31734cd48e20393",
"https://deno.land/std@0.207.0/yaml/parse.ts": "1fbbda572bf3fff578b6482c0d8b85097a38de3176bf3ab2ca70c25fb0c960ef",
"https://deno.land/std@0.207.0/yaml/schema.ts": "96908b78dc50c340074b93fc1598d5e7e2fe59103f89ff81e5a49b2dedf77a67",
"https://deno.land/std@0.207.0/yaml/schema/core.ts": "fa406f18ceedc87a50e28bb90ec7a4c09eebb337f94ef17468349794fa828639",
"https://deno.land/std@0.207.0/yaml/schema/default.ts": "0047e80ae8a4a93293bc4c557ae8a546aabd46bb7165b9d9b940d57b4d88bde9",
"https://deno.land/std@0.207.0/yaml/schema/extended.ts": "0784416bf062d20a1626b53c03380e265b3e39b9409afb9f4cb7d659fd71e60d",
"https://deno.land/std@0.207.0/yaml/schema/failsafe.ts": "d219ab5febc43f770917d8ec37735a4b1ad671149846cbdcade767832b42b92b",
"https://deno.land/std@0.207.0/yaml/schema/json.ts": "5f41dd7c2f1ad545ef6238633ce9ee3d444dfc5a18101e1768bd5504bf90e5e5",
"https://deno.land/std@0.207.0/yaml/schema/mod.ts": "4472e827bab5025e92bc2eb2eeefa70ecbefc64b2799b765c69af84822efef32",
"https://deno.land/std@0.207.0/yaml/type.ts": "65553da3da3c029b6589c6e4903f0afbea6768be8fca61580711457151f2b30f",
"https://esm.sh/v135/ajv@8.12.0": "cc1a73af661466c7f4e6a94d93ece78542d700f2165bdb16a531e9db8856c5aa",
"https://esm.sh/v135/ajv@8.12.0/denonext/ajv.mjs": "4645df9093d0f8be0e964070a4a7aea8adea06e8883660340931f7a3f979fc65",
"https://esm.sh/v135/fast-deep-equal@3.1.3/denonext/fast-deep-equal.mjs": "6313b3e05436550e1c0aeb2a282206b9b8d9213b4c6f247964dd7bb4835fb9e5",
"https://esm.sh/v135/json-schema-traverse@1.0.0/denonext/json-schema-traverse.mjs": "c5da8353bc014e49ebbb1a2c0162d29969a14c325da19644e511f96ba670cc45",
"https://esm.sh/v135/uri-js@4.4.1/denonext/uri-js.mjs": "901d462f9db207376b39ec603d841d87e6b9e9568ce97dfaab12aa77d0f99f74"
}
}

View File

@ -13,16 +13,6 @@
"scripts": {
"contrast_test": "node ./contrast_test/index.js",
"docpuccin": "node ./docpuccin/index.js",
"inkcat": "node ./inkcat/index.js",
"palette_builder": "node ./palette_builder/index.js"
},
"devDependencies": {
"prettier": "^3.0.0"
},
"prettier": {
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
"inkcat": "node ./inkcat/index.js"
}
}

View File

@ -1,6 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json",
"bootstrap-sha": "e13db262ccc5c524a6d9ece3b2c84a405cb89f97",
"packages": {
"catwalk": {
"package-name": "catwalk",
@ -13,6 +12,11 @@
"whiskers": {
"package-name": "whiskers",
"release-type": "rust"
},
"deno-lib": {
"package-name": "deno-lib",
"release-type": "simple",
"release-as": "1.0.0"
}
},
"plugins": ["cargo-workspace"]