tslaws 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/browser.js +11 -2
- package/dist/check.js +4 -1
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +166 -22
- package/dist/compile.d.ts +7 -1
- package/dist/compile.js +95 -5
- package/dist/coverage.d.ts +17 -2
- package/dist/coverage.js +147 -10
- package/dist/github.d.ts +3 -1
- package/dist/github.js +12 -2
- package/dist/githubCli.d.ts +7 -0
- package/dist/githubCli.js +59 -26
- package/dist/projects.d.ts +28 -0
- package/dist/projects.js +185 -0
- package/dist/property.js +8 -1
- package/dist/propertyWorker.js +4 -1
- package/dist/runtimeEnv.d.ts +24 -0
- package/dist/runtimeEnv.js +108 -0
- package/env.d.ts +17 -0
- package/package.json +10 -6
package/dist/projects.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// Project discovery for a monorepo: a project is a directory holding a tslaws.json. Which directories to look in comes
|
|
2
|
+
// from the first source that names any: `projects` in the root tslaws.json, `workspaces` in package.json (npm, yarn
|
|
3
|
+
// and bun alike), or `packages:` in pnpm-workspace.yaml.
|
|
4
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
5
|
+
import { join, resolve } from 'node:path';
|
|
6
|
+
import { projectsProblem, readConfig } from "./coverage.js";
|
|
7
|
+
import { globMatcher } from "./globs.js";
|
|
8
|
+
import { customLawFile } from "./lawFiles.js";
|
|
9
|
+
/** Directories never searched, for projects or for laws files. */
|
|
10
|
+
const skipped = new Set(['node_modules', '.git', 'dist', '.tslaws']);
|
|
11
|
+
/** `./packages/a/` as `packages/a`; `.` and `./` as ''. */
|
|
12
|
+
function normalise(path) {
|
|
13
|
+
return path
|
|
14
|
+
.replaceAll('\\', '/')
|
|
15
|
+
.replace(/^(\.\/)+/, '')
|
|
16
|
+
.replace(/\/+$/, '')
|
|
17
|
+
.replace(/^\.$/, '');
|
|
18
|
+
}
|
|
19
|
+
function readJson(file) {
|
|
20
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
21
|
+
}
|
|
22
|
+
/** The raw `projects` list of tslaws.json in `dir`, or undefined when there is no file or no such key. */
|
|
23
|
+
function projectsOf(dir) {
|
|
24
|
+
const file = join(dir, 'tslaws.json');
|
|
25
|
+
if (!existsSync(file))
|
|
26
|
+
return undefined;
|
|
27
|
+
const raw = readJson(file);
|
|
28
|
+
if (typeof raw !== 'object' || raw === null || raw.projects === undefined)
|
|
29
|
+
return undefined;
|
|
30
|
+
const problem = projectsProblem(raw.projects);
|
|
31
|
+
if (problem !== undefined)
|
|
32
|
+
throw new Error(`tslaws.json: ${problem}`);
|
|
33
|
+
return raw.projects;
|
|
34
|
+
}
|
|
35
|
+
function workspacesOf(dir) {
|
|
36
|
+
const file = join(dir, 'package.json');
|
|
37
|
+
if (!existsSync(file))
|
|
38
|
+
return [];
|
|
39
|
+
let workspaces;
|
|
40
|
+
try {
|
|
41
|
+
workspaces = readJson(file)?.workspaces;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
const list = Array.isArray(workspaces) ? workspaces : workspaces?.packages;
|
|
47
|
+
return Array.isArray(list) ? list.filter((entry) => typeof entry === 'string') : [];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The `packages:` list of pnpm-workspace.yaml. Only the plain shapes pnpm documents are read: a block list of
|
|
51
|
+
* (optionally quoted) globs, or a one-line `[a, b]`. That keeps this free of a YAML dependency.
|
|
52
|
+
*/
|
|
53
|
+
export function pnpmPackages(text) {
|
|
54
|
+
const found = [];
|
|
55
|
+
let inList = false;
|
|
56
|
+
for (const line of text.split(/\r?\n/)) {
|
|
57
|
+
if (/^\s*(#.*)?$/.test(line))
|
|
58
|
+
continue;
|
|
59
|
+
if (/^\S/.test(line)) {
|
|
60
|
+
const key = /^packages\s*:\s*(.*?)\s*(?:#.*)?$/.exec(line);
|
|
61
|
+
inList = key !== null;
|
|
62
|
+
if (key?.[1]?.startsWith('[') === true) {
|
|
63
|
+
for (const item of key[1].replace(/^\[|\]$/g, '').split(',')) {
|
|
64
|
+
const value = item.trim().replace(/^(['"])(.*)\1$/, '$2');
|
|
65
|
+
if (value !== '')
|
|
66
|
+
found.push(value);
|
|
67
|
+
}
|
|
68
|
+
inList = false;
|
|
69
|
+
}
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (!inList)
|
|
73
|
+
continue;
|
|
74
|
+
const item = /^\s*-\s*(?:'([^']*)'|"([^"]*)"|([^#\s][^#]*?))\s*(?:#.*)?$/.exec(line);
|
|
75
|
+
const value = item?.[1] ?? item?.[2] ?? item?.[3];
|
|
76
|
+
if (value !== undefined && value !== '')
|
|
77
|
+
found.push(value);
|
|
78
|
+
}
|
|
79
|
+
return found;
|
|
80
|
+
}
|
|
81
|
+
function pnpmWorkspacesOf(dir) {
|
|
82
|
+
const file = join(dir, 'pnpm-workspace.yaml');
|
|
83
|
+
return existsSync(file) ? pnpmPackages(readFileSync(file, 'utf8')) : [];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Whether the glob `pattern` could match `path` or something below it, from its fixed leading segments alone. It lets
|
|
87
|
+
* the search skip a whole `apps/` tree when the globs are `packages/*`.
|
|
88
|
+
*/
|
|
89
|
+
function reaches(pattern, path) {
|
|
90
|
+
const want = pattern.split('/');
|
|
91
|
+
const have = path.split('/');
|
|
92
|
+
for (const [index, segment] of have.entries()) {
|
|
93
|
+
const expected = want[index];
|
|
94
|
+
if (expected === undefined)
|
|
95
|
+
return want[want.length - 1] === '**';
|
|
96
|
+
if (/[*?]/.test(expected))
|
|
97
|
+
return true;
|
|
98
|
+
if (expected !== segment)
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
/** Directories (relative POSIX paths) under `root` that hold a tslaws.json and match `globs`. */
|
|
104
|
+
function matchingProjects(root, globs) {
|
|
105
|
+
const patterns = globs.map((glob) => {
|
|
106
|
+
const negated = glob.startsWith('!');
|
|
107
|
+
return `${negated ? '!' : ''}${normalise(negated ? glob.slice(1) : glob)}`;
|
|
108
|
+
});
|
|
109
|
+
const matches = globMatcher(patterns);
|
|
110
|
+
const includes = patterns.filter((glob) => !glob.startsWith('!'));
|
|
111
|
+
const found = [];
|
|
112
|
+
const walk = (path) => {
|
|
113
|
+
const dir = path === '' ? root : join(root, path);
|
|
114
|
+
if (path !== '' && matches(path) && existsSync(join(dir, 'tslaws.json')))
|
|
115
|
+
found.push(path);
|
|
116
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
117
|
+
if (!entry.isDirectory() || skipped.has(entry.name))
|
|
118
|
+
continue;
|
|
119
|
+
const next = path === '' ? entry.name : `${path}/${entry.name}`;
|
|
120
|
+
if (includes.some((glob) => reaches(glob, next)))
|
|
121
|
+
walk(next);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
walk('');
|
|
125
|
+
return found;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The cheap test for "the root has laws of its own": a tsc-laws.lock beside it, any `*.laws.ts` file outside the
|
|
129
|
+
* projects, or a file the root's `laws` globs pick that declares a law. It reads directory names, and the contents of
|
|
130
|
+
* only those files the `laws` globs match; it skips node_modules, .git, dist and each project, and stops at the first
|
|
131
|
+
* hit.
|
|
132
|
+
*/
|
|
133
|
+
function rootHasLaws(root, projects) {
|
|
134
|
+
if (existsSync(join(root, 'tsc-laws.lock')))
|
|
135
|
+
return true;
|
|
136
|
+
const inProject = new Set(projects);
|
|
137
|
+
const custom = customLawFile(root, readConfig(root).laws);
|
|
138
|
+
const walk = (path) => {
|
|
139
|
+
for (const entry of readdirSync(path === '' ? root : join(root, path), { withFileTypes: true })) {
|
|
140
|
+
if (entry.isFile() &&
|
|
141
|
+
(entry.name.endsWith('.laws.ts') || custom(path === '' ? entry.name : `${path}/${entry.name}`)))
|
|
142
|
+
return true;
|
|
143
|
+
if (!entry.isDirectory() || skipped.has(entry.name))
|
|
144
|
+
continue;
|
|
145
|
+
const next = path === '' ? entry.name : `${path}/${entry.name}`;
|
|
146
|
+
if (!inProject.has(next) && walk(next))
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
150
|
+
};
|
|
151
|
+
return walk('');
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Every project under `root`, sorted by path. The root itself is one only when it has a tslaws.json and either that
|
|
155
|
+
* file has no `projects` key (a plain single project that happens to hold others) or the root carries laws of its
|
|
156
|
+
* own (see `rootHasLaws`); a root whose tslaws.json only lists projects is just the list.
|
|
157
|
+
*/
|
|
158
|
+
export function discoverProjects(root, opts = {}) {
|
|
159
|
+
const dir = resolve(root);
|
|
160
|
+
const listed = projectsOf(dir);
|
|
161
|
+
const globs = listed ?? [workspacesOf(dir), pnpmWorkspacesOf(dir)].find((list) => list.length > 0) ?? [];
|
|
162
|
+
const paths = globs.length === 0 ? [] : matchingProjects(dir, globs);
|
|
163
|
+
if (existsSync(join(dir, 'tslaws.json')) && (listed === undefined || rootHasLaws(dir, paths)))
|
|
164
|
+
paths.push('');
|
|
165
|
+
let projects = paths.sort().map((path) => ({ dir: path === '' ? dir : join(dir, path), path }));
|
|
166
|
+
if (opts.only !== undefined && opts.only.length > 0) {
|
|
167
|
+
const only = new Set(opts.only.map(normalise));
|
|
168
|
+
projects = projects.filter((project) => only.has(project.path));
|
|
169
|
+
}
|
|
170
|
+
if (opts.filter !== undefined && opts.filter.length > 0) {
|
|
171
|
+
const matches = globMatcher(opts.filter.map((glob) => (glob.startsWith('!') ? `!${normalise(glob.slice(1))}` : normalise(glob))));
|
|
172
|
+
projects = projects.filter((project) => matches(project.path));
|
|
173
|
+
}
|
|
174
|
+
return projects;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Whether `cwd` is the root of a monorepo run: it has no tslaws.json but discovery finds projects below it, or its
|
|
178
|
+
* tslaws.json has a `projects` list.
|
|
179
|
+
*/
|
|
180
|
+
export function isMonorepoRoot(cwd) {
|
|
181
|
+
const dir = resolve(cwd);
|
|
182
|
+
if (!existsSync(join(dir, 'tslaws.json')))
|
|
183
|
+
return discoverProjects(dir).length > 0;
|
|
184
|
+
return projectsOf(dir) !== undefined;
|
|
185
|
+
}
|
package/dist/property.js
CHANGED
|
@@ -3,6 +3,7 @@ import { readdirSync, statSync } from 'node:fs';
|
|
|
3
3
|
import { join, resolve } from 'node:path';
|
|
4
4
|
import ts from 'typescript';
|
|
5
5
|
import { GeneratorFactory } from "./generators.js";
|
|
6
|
+
import { lawEnv } from "./coverage.js";
|
|
6
7
|
import { arbitraryOf, UnsupportedType } from "./plan.js";
|
|
7
8
|
import { WorkerPool } from "./pool.js";
|
|
8
9
|
export { isDiscard, jsPath, runLaw, runProperty, summarise } from "./propertyRun.js";
|
|
@@ -127,6 +128,12 @@ export async function runLawGuarded(outDir, law, options, plans) {
|
|
|
127
128
|
};
|
|
128
129
|
current.once('message', onMessage);
|
|
129
130
|
// Functions cannot cross to the worker; it needs only the numbers.
|
|
130
|
-
current.postMessage({
|
|
131
|
+
current.postMessage({
|
|
132
|
+
outDir,
|
|
133
|
+
law,
|
|
134
|
+
plans,
|
|
135
|
+
options: { ...options, onEvent: undefined },
|
|
136
|
+
env: lawEnv(options.cwd, 'node'),
|
|
137
|
+
});
|
|
131
138
|
});
|
|
132
139
|
}
|
package/dist/propertyWorker.js
CHANGED
|
@@ -2,12 +2,15 @@ import { createRequire } from 'node:module';
|
|
|
2
2
|
import { parentPort } from 'node:worker_threads';
|
|
3
3
|
import { arbitraryOf } from "./plan.js";
|
|
4
4
|
import { jsPath, runLaw, summarise } from "./propertyRun.js";
|
|
5
|
+
import { installLoaderHooks, setImportMetaEnv } from "./runtimeEnv.js";
|
|
5
6
|
// Property tests run here so that a law over code that never terminates costs a timeout, not the whole check.
|
|
6
7
|
// The main process read the types and sends the generators as plans, so a worker never loads the TypeScript compiler.
|
|
7
8
|
const require = createRequire(import.meta.url);
|
|
9
|
+
installLoaderHooks();
|
|
8
10
|
parentPort.on('message', async (request) => {
|
|
9
11
|
try {
|
|
10
|
-
const { outDir, law, plans, options } = request;
|
|
12
|
+
const { outDir, law, plans, options, env } = request;
|
|
13
|
+
setImportMetaEnv(env);
|
|
11
14
|
const generators = plans.map(arbitraryOf);
|
|
12
15
|
// The worker outlives a check, and the code may have been recompiled since the last one.
|
|
13
16
|
for (const loaded of Object.keys(require.cache))
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** The global that `import.meta.env` is rewritten to, in compiled laws, bundles and natively loaded TypeScript. */
|
|
2
|
+
export declare const importMetaEnvGlobal = "__tslawsImportMetaEnv";
|
|
3
|
+
/** The value of `import.meta.env` in every place a law runs. */
|
|
4
|
+
export type ImportMetaEnv = Record<string, string | boolean>;
|
|
5
|
+
/** The `env` setting of tslaws.json: a name to a string (or a boolean, for `DEV`, `PROD` and `SSR`). */
|
|
6
|
+
export declare function parseEnvConfig(raw: unknown): Record<string, string | boolean>;
|
|
7
|
+
/**
|
|
8
|
+
* The environment of a law: every `process.env` entry, then `MODE` (`test`), `DEV` (true), `PROD` (false), `SSR` (true
|
|
9
|
+
* in Node, false in a page) and `BASE_URL` (`/`), then the `env` of tslaws.json, which overrides all of it. `MODE` and
|
|
10
|
+
* `BASE_URL` keep the value `process.env` gives them.
|
|
11
|
+
*/
|
|
12
|
+
export declare function importMetaEnvOf(configured: Record<string, string | boolean> | undefined, surface: 'node' | 'browser', processEnv?: NodeJS.ProcessEnv): ImportMetaEnv;
|
|
13
|
+
/** Sets `import.meta.env` for everything loaded afterwards in this thread. */
|
|
14
|
+
export declare function setImportMetaEnv(env: ImportMetaEnv): void;
|
|
15
|
+
/**
|
|
16
|
+
* Teaches Node's own loading of TypeScript two things a bundler does: a relative import without an extension
|
|
17
|
+
* (`export * from './database'`) finds the `.ts` file, and `import.meta.env` reads the environment of the laws. Both
|
|
18
|
+
* apply to project TypeScript only, never to anything under `node_modules`. Needs `module.registerHooks`
|
|
19
|
+
* (Node 22.15, 23.5 or later); on an older Node the hooks are skipped and such imports fail as they did before.
|
|
20
|
+
* Returns whether the hooks are in place. Registers once per thread.
|
|
21
|
+
*/
|
|
22
|
+
export declare function installLoaderHooks(): boolean;
|
|
23
|
+
/** Everything a law needs before its module loads: the hooks and the environment. */
|
|
24
|
+
export declare function prepareRuntime(env: ImportMetaEnv): void;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// What laws need at run time to load project code written for a bundler: `import.meta.env`, and relative imports without
|
|
2
|
+
// an extension in TypeScript files that Node loads itself. Nothing here loads the TypeScript compiler, so a property worker
|
|
3
|
+
// can import it.
|
|
4
|
+
import nodeModule from 'node:module';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
/** The global that `import.meta.env` is rewritten to, in compiled laws, bundles and natively loaded TypeScript. */
|
|
7
|
+
export const importMetaEnvGlobal = '__tslawsImportMetaEnv';
|
|
8
|
+
/** The `env` setting of tslaws.json: a name to a string (or a boolean, for `DEV`, `PROD` and `SSR`). */
|
|
9
|
+
export function parseEnvConfig(raw) {
|
|
10
|
+
if (typeof raw !== 'object' || raw === null || Array.isArray(raw))
|
|
11
|
+
throw new Error(`tslaws.json: "env" must be an object of names to strings, got ${JSON.stringify(raw)}`);
|
|
12
|
+
for (const [key, value] of Object.entries(raw))
|
|
13
|
+
if (typeof value !== 'string' && typeof value !== 'boolean')
|
|
14
|
+
throw new Error(`tslaws.json: "env.${key}" must be a string or a boolean, got ${JSON.stringify(value)}`);
|
|
15
|
+
return raw;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The environment of a law: every `process.env` entry, then `MODE` (`test`), `DEV` (true), `PROD` (false), `SSR` (true
|
|
19
|
+
* in Node, false in a page) and `BASE_URL` (`/`), then the `env` of tslaws.json, which overrides all of it. `MODE` and
|
|
20
|
+
* `BASE_URL` keep the value `process.env` gives them.
|
|
21
|
+
*/
|
|
22
|
+
export function importMetaEnvOf(configured, surface, processEnv = process.env) {
|
|
23
|
+
const env = {};
|
|
24
|
+
for (const [key, value] of Object.entries(processEnv))
|
|
25
|
+
if (value !== undefined)
|
|
26
|
+
env[key] = value;
|
|
27
|
+
return {
|
|
28
|
+
...env,
|
|
29
|
+
MODE: processEnv.MODE ?? 'test',
|
|
30
|
+
DEV: true,
|
|
31
|
+
PROD: false,
|
|
32
|
+
SSR: surface === 'node',
|
|
33
|
+
BASE_URL: processEnv.BASE_URL ?? '/',
|
|
34
|
+
...configured,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** Sets `import.meta.env` for everything loaded afterwards in this thread. */
|
|
38
|
+
export function setImportMetaEnv(env) {
|
|
39
|
+
globalThis[importMetaEnvGlobal] = env;
|
|
40
|
+
}
|
|
41
|
+
const sourceFile = /\.[cm]?tsx?$/;
|
|
42
|
+
const unresolved = ['.ts', '.tsx', '.mts', '.js', '/index.ts', '/index.tsx'];
|
|
43
|
+
const javascriptEnding = /\.[cm]?jsx?$/;
|
|
44
|
+
const slash = (path) => path.replaceAll(String.fromCharCode(92), '/');
|
|
45
|
+
/** Whether `url` is project TypeScript: a TypeScript file that is not inside a dependency. */
|
|
46
|
+
function isProjectSource(url) {
|
|
47
|
+
if (url === undefined || !url.startsWith('file:'))
|
|
48
|
+
return false;
|
|
49
|
+
const path = slash(fileURLToPath(url));
|
|
50
|
+
return sourceFile.test(path) && !path.includes('/node_modules/');
|
|
51
|
+
}
|
|
52
|
+
let installed;
|
|
53
|
+
/**
|
|
54
|
+
* Teaches Node's own loading of TypeScript two things a bundler does: a relative import without an extension
|
|
55
|
+
* (`export * from './database'`) finds the `.ts` file, and `import.meta.env` reads the environment of the laws. Both
|
|
56
|
+
* apply to project TypeScript only, never to anything under `node_modules`. Needs `module.registerHooks`
|
|
57
|
+
* (Node 22.15, 23.5 or later); on an older Node the hooks are skipped and such imports fail as they did before.
|
|
58
|
+
* Returns whether the hooks are in place. Registers once per thread.
|
|
59
|
+
*/
|
|
60
|
+
export function installLoaderHooks() {
|
|
61
|
+
if (installed !== undefined)
|
|
62
|
+
return installed;
|
|
63
|
+
installed = false;
|
|
64
|
+
const { registerHooks } = nodeModule;
|
|
65
|
+
if (typeof registerHooks !== 'function')
|
|
66
|
+
return installed;
|
|
67
|
+
registerHooks({
|
|
68
|
+
resolve(specifier, context, nextResolve) {
|
|
69
|
+
try {
|
|
70
|
+
return nextResolve(specifier, context);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (!specifier.startsWith('.') || !isProjectSource(context.parentURL))
|
|
74
|
+
throw error;
|
|
75
|
+
const stem = specifier.replace(javascriptEnding, '');
|
|
76
|
+
const candidates = unresolved.map((ending) => `${specifier}${ending}`);
|
|
77
|
+
// `./a.js` names a `.ts` file in TypeScript, as `./a` does for a bundler.
|
|
78
|
+
if (stem !== specifier)
|
|
79
|
+
candidates.push(`${stem}.ts`, `${stem}.tsx`, `${stem}.mts`);
|
|
80
|
+
for (const candidate of candidates) {
|
|
81
|
+
try {
|
|
82
|
+
return nextResolve(candidate, context);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// The next candidate.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
load(url, context, nextLoad) {
|
|
92
|
+
const loaded = nextLoad(url, context);
|
|
93
|
+
if (loaded.source === undefined || loaded.source === null || !isProjectSource(url))
|
|
94
|
+
return loaded;
|
|
95
|
+
const text = typeof loaded.source === 'string' ? loaded.source : new TextDecoder().decode(loaded.source);
|
|
96
|
+
if (!text.includes('import.meta.env'))
|
|
97
|
+
return loaded;
|
|
98
|
+
return { ...loaded, source: text.replace(/\bimport\.meta\.env\b/g, `globalThis.${importMetaEnvGlobal}`) };
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
installed = true;
|
|
102
|
+
return installed;
|
|
103
|
+
}
|
|
104
|
+
/** Everything a law needs before its module loads: the hooks and the environment. */
|
|
105
|
+
export function prepareRuntime(env) {
|
|
106
|
+
installLoaderHooks();
|
|
107
|
+
setImportMetaEnv(env);
|
|
108
|
+
}
|
package/env.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// The type of `import.meta.env` in code that laws reach. tslaws adds this file to a build when the project declares no
|
|
2
|
+
// `ImportMeta.env` of its own (Vite's `vite/client` and rsbuild's types do). Reference it from the project to give an
|
|
3
|
+
// editor the same type: `/// <reference types="tslaws/env" />`.
|
|
4
|
+
interface ImportMetaEnv {
|
|
5
|
+
// Any, as in Vite's own declaration: a variable is usually a string, but the project knows which, and a narrower index
|
|
6
|
+
// type would turn every `import.meta.env.VITE_X` into a type error in code that compiles under a bundler.
|
|
7
|
+
readonly [key: string]: any;
|
|
8
|
+
readonly MODE: string;
|
|
9
|
+
readonly DEV: boolean;
|
|
10
|
+
readonly PROD: boolean;
|
|
11
|
+
readonly SSR: boolean;
|
|
12
|
+
readonly BASE_URL: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface ImportMeta {
|
|
16
|
+
readonly env: ImportMetaEnv;
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tslaws",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Runs and checks `law` declarations compiled by tsc-laws: property tests, SMT proofs, tamper lock.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"tslaws": "bin/tslaws.js"
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"bin",
|
|
10
10
|
"dist",
|
|
11
|
+
"env.d.ts",
|
|
11
12
|
"hooks",
|
|
12
13
|
"ui"
|
|
13
14
|
],
|
|
@@ -42,6 +43,9 @@
|
|
|
42
43
|
"types": "./dist/adapters/element.d.ts",
|
|
43
44
|
"default": "./dist/adapters/element.js"
|
|
44
45
|
},
|
|
46
|
+
"./env": {
|
|
47
|
+
"types": "./env.d.ts"
|
|
48
|
+
},
|
|
45
49
|
"./internal/*": "./dist/*.js",
|
|
46
50
|
"./package.json": "./package.json"
|
|
47
51
|
},
|
|
@@ -73,11 +77,11 @@
|
|
|
73
77
|
}
|
|
74
78
|
},
|
|
75
79
|
"optionalDependencies": {
|
|
76
|
-
"tslaws-compiler-darwin-arm64": "0.
|
|
77
|
-
"tslaws-compiler-darwin-x64": "0.
|
|
78
|
-
"tslaws-compiler-linux-arm64-gnu": "0.
|
|
79
|
-
"tslaws-compiler-linux-x64-gnu": "0.
|
|
80
|
-
"tslaws-compiler-win32-x64-msvc": "0.
|
|
80
|
+
"tslaws-compiler-darwin-arm64": "0.10.0",
|
|
81
|
+
"tslaws-compiler-darwin-x64": "0.10.0",
|
|
82
|
+
"tslaws-compiler-linux-arm64-gnu": "0.10.0",
|
|
83
|
+
"tslaws-compiler-linux-x64-gnu": "0.10.0",
|
|
84
|
+
"tslaws-compiler-win32-x64-msvc": "0.10.0"
|
|
81
85
|
},
|
|
82
86
|
"publishConfig": {
|
|
83
87
|
"access": "public"
|