-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1d638a
commit 1982f57
Showing
6 changed files
with
195 additions
and
38 deletions.
There are no files selected for viewing
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,25 @@ | ||
import { configPath, Module, Project } from "../project/mod.ts"; | ||
import { runJob } from "../utils/worker_pool.ts"; | ||
import { WorkerRequest, WorkerResponse } from "./module_config_schema.worker.ts"; | ||
import { createWorkerPool } from "../utils/worker_pool.ts"; | ||
import { exists } from "../deps.ts"; | ||
|
||
const WORKER_POOL = createWorkerPool<WorkerRequest, WorkerResponse>({ | ||
source: import.meta.resolve("./module_config_schema.worker.ts"), | ||
// Leave 1 CPU core free | ||
count: Math.max(1, navigator.hardwareConcurrency - 1), | ||
}); | ||
|
||
export async function compileModuleConfigSchema( | ||
_project: Project, | ||
module: Module, | ||
): Promise<void> { | ||
if (await exists(configPath(module))) { | ||
const res = await runJob(WORKER_POOL, { module }); | ||
module.configSchema = res.moduleConfigSchema; | ||
|
||
// TODO: Validate schema | ||
} else { | ||
// TODO: Assert there is no config | ||
} | ||
} |
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,73 @@ | ||
// Runs synchronise TypeScript code to derive the schema from a script in a | ||
// background worker. | ||
|
||
/// <reference no-default-lib="true" /> | ||
/// <reference lib="deno.worker" /> | ||
|
||
import { tjs } from "./deps.ts"; | ||
import { configPath, Module } from "../project/module.ts"; | ||
|
||
export interface WorkerRequest { | ||
module: Module; | ||
} | ||
|
||
export interface WorkerResponse { | ||
moduleConfigSchema: tjs.Definition; | ||
} | ||
|
||
self.onmessage = async (ev) => { | ||
const { module } = ev.data as WorkerRequest; | ||
const moduleConfigPath = configPath(module); | ||
|
||
// TODO: Dupe of project.ts | ||
// https://docs.deno.com/runtime/manual/advanced/typescript/configuration#what-an-implied-tsconfigjson-looks-like | ||
const DEFAULT_COMPILER_OPTIONS = { | ||
"allowJs": true, | ||
"esModuleInterop": true, | ||
"experimentalDecorators": false, | ||
"inlineSourceMap": true, | ||
"isolatedModules": true, | ||
"jsx": "react", | ||
"module": "esnext", | ||
"moduleDetection": "force", | ||
"strict": true, | ||
"target": "esnext", | ||
"useDefineForClassFields": true, | ||
|
||
"lib": ["esnext", "dom", "dom.iterable"], | ||
"allowImportingTsExtensions": true, | ||
}; | ||
|
||
const validateConfig = { | ||
topRef: true, | ||
required: true, | ||
strictNullChecks: true, | ||
noExtraProps: true, | ||
esModuleInterop: true, | ||
|
||
// TODO: Is this needed? | ||
include: [moduleConfigPath], | ||
|
||
// TODO: Figure out how to work without this? Maybe we manually validate the request type exists? | ||
ignoreErrors: true, | ||
}; | ||
|
||
const program = tjs.getProgramFromFiles( | ||
[moduleConfigPath], | ||
DEFAULT_COMPILER_OPTIONS, | ||
); | ||
|
||
const moduleConfigSchema = tjs.generateSchema( | ||
program, | ||
"Config", | ||
validateConfig, | ||
[moduleConfigPath], | ||
); | ||
if (moduleConfigSchema === null) { | ||
throw new Error("Failed to generate config schema for " + moduleConfigPath); | ||
} | ||
|
||
self.postMessage({ | ||
moduleConfigSchema, | ||
} as WorkerResponse); | ||
}; |
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
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