-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Settings profiles experiments
These have to happen on main to have the schema published for iteration.
- Loading branch information
1 parent
d266f30
commit 07cbd67
Showing
15 changed files
with
171 additions
and
25 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters