Skip to content

Commit

Permalink
feat(core): Settings profiles experiments
Browse files Browse the repository at this point in the history
These have to happen on main to have the schema published for iteration.
  • Loading branch information
oliversalzburg committed Nov 21, 2023
1 parent d266f30 commit 07cbd67
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 25 deletions.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion .lintstagedrc.js → lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
/** @type {import("lint-staged").Config} */
export default {
"package.json": "yarn prettier-package-json --write",
"*.{js,json,md,sh,ts,yml}": "prettier --write",
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"bugs": {
"url": "https://github.com/kitten-science/kitten-scientists/issues"
},
"type": "module",
"scripts": {
"build:all": "tsc --build",
"clean:all": "rm -rf packages/*/build packages/*/tsconfig.tsbuildinfo",
Expand Down
2 changes: 2 additions & 0 deletions packages/kitten-scientists/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"@types/dojo": "1.9.48",
"@types/jquery": "3.5.28",
"@types/semver": "7.5.6",
"@types/web": "0.0.119",
"json-schema-to-ts": "2.9.2",
"typescript": "5.3.2",
"vite": "5.0.0",
"vite-plugin-userscript": "0.1.3"
Expand Down
14 changes: 14 additions & 0 deletions packages/kitten-scientists/source/state/State.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { StateLoader } from "./StateLoader.js";

export class State {
readonly originUrl: string;

constructor(originUrl: string) {
this.originUrl = originUrl;
}

async resolve() {
const loader = new StateLoader(this);
return await loader.load();
}
}
89 changes: 89 additions & 0 deletions packages/kitten-scientists/source/state/StateLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { errorToRecord, unknownToError } from "@oliversalzburg/js-utils";
import { State } from "./State.js";

export interface ResolverStateView {
extends?: Array<string>;
}
export interface ReportEntry {
message: string;
context: Record<string, unknown> | undefined;
}

export class LoaderReport {
readonly origin: string;
#store = new Map<string, Array<ReportEntry>>();

constructor(origin: string) {
this.origin = origin;
}

#getStoreEntry(entry: string) {
if (!this.#store.has(entry)) {
this.#store.set(entry, new Array<ReportEntry>());
}
return this.#store.get(entry);
}

request(message: string, context?: Record<string, unknown>) {
this.#getStoreEntry("anonymous")?.push({ message, context });
}
failure(message: string, context?: Record<string, unknown>) {
this.#getStoreEntry("anonymous")?.push({ message, context });
}

log() {
for (const [profileId, entry] of this.#store.entries()) {
console.log(profileId);
console.dir(entry);
}
}
}

export class StateLoader {
readonly report: LoaderReport;
readonly state: State;
#children = new Array<State>();
#data: Record<string, unknown> | undefined;

get data() {
return this.#data;
}

constructor(state: State, report = new LoaderReport(state.originUrl)) {
this.state = state;
this.report = report;
}

async load(): Promise<LoaderReport> {
this.report.request(`Resolving ${this.state.originUrl}...`);

let data;
try {
const request = await fetch(this.state.originUrl);
data = (await request.json()) as Record<string, unknown>;
} catch (error) {
this.report.failure(
`Error while resolving ${this.state.originUrl}!`,
errorToRecord(unknownToError(error)),
);
return this.report;
}

// TODO: Validate again JSON schema.
this.#data = data;
await this.#resolveBases(this.#data);
return this.report;
}

async #resolveBases(state: ResolverStateView) {
if (!Array.isArray(state.extends) || state.extends.length === 0) {
return Promise.resolve([]);
}

for (const base of state.extends) {
this.#children.push(new State(base));
}

await Promise.all(this.#children.map(base => new StateLoader(base, this.report).load()));
}
}
14 changes: 14 additions & 0 deletions presets/build-fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://github.com/kitten-science/kitten-scientists/raw/main/schemas/draft-01/settings-profile.schema.json",
"v": "2.0.0-beta.8",
"bonfire": {
"enabled": true,
"trigger": 0,
"buildings": {
"hut": {
"enabled": true,
"max": -1
}
}
}
}
14 changes: 14 additions & 0 deletions presets/build-huts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://github.com/kitten-science/kitten-scientists/raw/main/schemas/draft-01/settings-profile.schema.json",
"v": "2.0.0-beta.8",
"bonfire": {
"enabled": true,
"trigger": 0,
"buildings": {
"field": {
"enabled": true,
"max": -1
}
}
}
}
8 changes: 8 additions & 0 deletions presets/enable-engine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://github.com/kitten-science/kitten-scientists/raw/main/schemas/draft-01/settings-profile.schema.json",
"v": "2.0.0-beta.8",
"engine": {
"enabled": true,
"interval": 1000
}
}
21 changes: 6 additions & 15 deletions presets/experiment.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
{
"$schema": "https://github.com/kitten-science/kitten-scientists/raw/main/schemas/draft-01/settings-profile.schema.json",
"v": "2.0.0-beta.8",
"baseline": "https://github.com/kitten-science/kitten-scientists/raw/main/baselines/absolute-zero.json",
"engine": {
"enabled": false,
"interval": 1000
},
"bonfire": {
"enabled": true,
"trigger": 0,
"buildings": {
"field": {
"enabled": true,
"max": -1
}
}
}
"extends": [
"https://github.com/kitten-science/kitten-scientists/raw/main/baselines/absolute-zero.json",
"https://github.com/kitten-science/kitten-scientists/raw/main/presets/enable-engine.json",
"https://github.com/kitten-science/kitten-scientists/raw/main/presets/build-fields.json",
"https://github.com/kitten-science/kitten-scientists/raw/main/presets/build-huts.json"
]
}
8 changes: 3 additions & 5 deletions .prettierrc.js → prettier.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module.exports = {
/** @type {import("prettier").Config} */
export default {
printWidth: 100,
arrowParens: "avoid",
plugins: [
require.resolve("prettier-plugin-organize-imports"),
require.resolve("prettier-plugin-sh"),
],
plugins: ["prettier-plugin-organize-imports"],
overrides: [
{
files: "*.md",
Expand Down
8 changes: 8 additions & 0 deletions schemas/.scripts/load-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { State } from "../../packages/kitten-scientists/build/state/State.js";

const state = new State(
"https://raw.githubusercontent.com/kitten-science/kitten-scientists/main/presets/experiment.json",
);

const report = await state.resolve();
report.log();
12 changes: 8 additions & 4 deletions schemas/draft-01/settings-profile.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
"pattern": "^(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)(?:-.+)$",
"type": "string"
},
"baseline": {
"description": "The URL of a KS settings baseline that this profile extends.",
"type": "string"
"extends": {
"description": "The URLs of KS settings profiles to extend.",
"items": {
"pattern": "^https://",
"type": "string"
},
"type": "array"
},
"engine": {
"additionalProperties": false,
Expand Down Expand Up @@ -1861,7 +1865,7 @@
"type": "object"
}
},
"required": ["$schema", "v", "baseline"],
"required": ["$schema", "v"],
"minProperties": 3,
"type": "object"
}
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,9 @@ __metadata:
"@types/dojo": "npm:1.9.48"
"@types/jquery": "npm:3.5.28"
"@types/semver": "npm:7.5.6"
"@types/web": "npm:0.0.119"
date-fns: "npm:2.30.0"
json-schema-to-ts: "npm:2.9.2"
semver: "npm:7.5.4"
tslib: "npm:2.6.2"
typescript: "npm:5.3.2"
Expand Down

0 comments on commit 07cbd67

Please sign in to comment.