@evandam93/dev-env 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +282 -2
- package/bin/dev-env.js +3 -0
- package/dist/abandoned.d.ts +2 -0
- package/dist/abandoned.js +66 -0
- package/dist/affected/graph.d.ts +43 -0
- package/dist/affected/graph.js +220 -0
- package/dist/affected/index.d.ts +5 -0
- package/dist/affected/index.js +3 -0
- package/dist/affected/layout.d.ts +11 -0
- package/dist/affected/layout.js +34 -0
- package/dist/affected/run.d.ts +10 -0
- package/dist/affected/run.js +96 -0
- package/dist/affected/select.d.ts +27 -0
- package/dist/affected/select.js +79 -0
- package/dist/checkout.d.ts +13 -0
- package/dist/checkout.js +40 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +98 -0
- package/dist/compose.d.ts +18 -0
- package/dist/compose.js +119 -0
- package/dist/environment.d.ts +33 -0
- package/dist/environment.js +387 -0
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/machine.d.ts +3 -0
- package/dist/machine.js +17 -0
- package/dist/manifest.d.ts +95 -0
- package/dist/manifest.js +91 -0
- package/dist/ports.d.ts +6 -0
- package/dist/ports.js +92 -0
- package/dist/processes.d.ts +13 -0
- package/dist/processes.js +116 -0
- package/dist/readiness.d.ts +6 -0
- package/dist/readiness.js +31 -0
- package/dist/shared.d.ts +9 -0
- package/dist/shared.js +81 -0
- package/dist/slots.d.ts +21 -0
- package/dist/slots.js +165 -0
- package/dist/state.d.ts +21 -0
- package/dist/state.js +36 -0
- package/dist/test-environment.d.ts +36 -0
- package/dist/test-environment.js +67 -0
- package/dist/workspace.d.ts +31 -0
- package/dist/workspace.js +140 -0
- package/package.json +75 -4
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { isTestMode, resolveCheckout, testModeOf } from './checkout.js';
|
|
2
|
+
import { resetTarget, startService, startTestTarget, stopService, } from './environment.js';
|
|
3
|
+
import { holderOf, releaseSlots, slotCountOnMachine, slotDirectoryOf, takeSlot, } from './slots.js';
|
|
4
|
+
import { readAllStates } from './state.js';
|
|
5
|
+
let held;
|
|
6
|
+
/*
|
|
7
|
+
* The slot belongs to this process, which runs one test file, so it frees
|
|
8
|
+
* when the file ends however it ends; a process killed outright leaves a
|
|
9
|
+
* record the next waiter sees is dead.
|
|
10
|
+
*/
|
|
11
|
+
const holdSlotUntilExit = async (name, exclusive) => {
|
|
12
|
+
const directory = slotDirectoryOf();
|
|
13
|
+
const holder = held ?? (await holderOf(name));
|
|
14
|
+
if (held === undefined) {
|
|
15
|
+
held = holder;
|
|
16
|
+
process.once('exit', () => releaseSlots(directory, holder));
|
|
17
|
+
for (const signal of ['SIGINT', 'SIGTERM'])
|
|
18
|
+
process.once(signal, () => {
|
|
19
|
+
releaseSlots(directory, holder);
|
|
20
|
+
// With no other listener left, the signal ends the process as it would have.
|
|
21
|
+
if (process.listenerCount(signal) === 0)
|
|
22
|
+
process.kill(process.pid, signal);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
await takeSlot({
|
|
26
|
+
directory,
|
|
27
|
+
size: await slotCountOnMachine(),
|
|
28
|
+
holder,
|
|
29
|
+
exclusive,
|
|
30
|
+
onWait: (reason) => process.stderr.write(`${name} waits for a lane slot: ${reason}.\n`),
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
const laneOf = (lane) => {
|
|
34
|
+
const chosen = lane ?? Number(process.env.VITEST_POOL_ID ?? '1');
|
|
35
|
+
return Number.isInteger(chosen) && chosen > 0 ? chosen : 1;
|
|
36
|
+
};
|
|
37
|
+
/*
|
|
38
|
+
* Starts with empty data, so a test never inherits rows or state from a file
|
|
39
|
+
* before it or from a run that died. A lane already running keeps its
|
|
40
|
+
* containers and ports, and only its data is thrown away, because starting
|
|
41
|
+
* containers is most of what a fresh environment costs.
|
|
42
|
+
*/
|
|
43
|
+
export const startTestEnvironment = async (target, options = {}) => {
|
|
44
|
+
const cwd = options.cwd ?? process.cwd();
|
|
45
|
+
const mode = testModeOf(laneOf(options.lane));
|
|
46
|
+
await holdSlotUntilExit(`${target} ${mode} in ${cwd}`, options.exclusive ?? process.env.DEV_ENV_EXCLUSIVE === '1');
|
|
47
|
+
const { name, services } = await startTestTarget(cwd, target, mode, {
|
|
48
|
+
without: options.without ?? [],
|
|
49
|
+
settings: options.settings ?? {},
|
|
50
|
+
});
|
|
51
|
+
return {
|
|
52
|
+
name,
|
|
53
|
+
services,
|
|
54
|
+
stop: () => resetTarget(cwd, target, mode),
|
|
55
|
+
stopService: (service) => stopService(cwd, target, mode, service),
|
|
56
|
+
startService: (service) => startService(cwd, target, mode, service),
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
/** Removes every test lane of the checkout, or of one target, for the end of a run. */
|
|
60
|
+
export const resetTestEnvironments = async (options = {}) => {
|
|
61
|
+
const cwd = options.cwd ?? process.cwd();
|
|
62
|
+
const checkout = await resolveCheckout(cwd);
|
|
63
|
+
await Promise.all(readAllStates(checkout.root)
|
|
64
|
+
.filter((state) => isTestMode(state.mode) &&
|
|
65
|
+
(options.target === undefined || state.target === options.target))
|
|
66
|
+
.map((state) => resetTarget(cwd, state.target, state.mode)));
|
|
67
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type PackageManager = 'npm' | 'pnpm' | 'yarn';
|
|
2
|
+
/** Whether the package.json in a directory makes its TypeScript ES modules. */
|
|
3
|
+
export declare const isModulePackage: (directory: string) => boolean;
|
|
4
|
+
/**
|
|
5
|
+
* The package manager the root package.json's `packageManager` names, or
|
|
6
|
+
* else the one whose lockfile the repository holds, or else npm.
|
|
7
|
+
*/
|
|
8
|
+
export declare const packageManagerOf: (root: string) => PackageManager;
|
|
9
|
+
/**
|
|
10
|
+
* The workspace globs of `pnpm-workspace.yaml`, or else of the root
|
|
11
|
+
* package.json's `workspaces`, as a list or under `packages`.
|
|
12
|
+
*/
|
|
13
|
+
export declare const workspaceGlobsOf: (root: string) => readonly string[];
|
|
14
|
+
/**
|
|
15
|
+
* Every workspace directory, absolute and sorted: each directory a workspace
|
|
16
|
+
* glob matches that holds a package.json, less those a `!` glob leaves out.
|
|
17
|
+
*/
|
|
18
|
+
export declare const workspaceDirectoriesOf: (root: string) => readonly string[];
|
|
19
|
+
/**
|
|
20
|
+
* Each workspace package by the name its package.json gives it, with its
|
|
21
|
+
* directory from the repository root, as `/libs/ui/`.
|
|
22
|
+
*/
|
|
23
|
+
export declare const workspacePackagesOf: (root: string) => ReadonlyMap<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* Repository paths every integration spec is built from without importing
|
|
26
|
+
* them: the root package.json, compiler settings and Node version, and the
|
|
27
|
+
* package manager's lockfile and workspace configuration.
|
|
28
|
+
*/
|
|
29
|
+
export declare const repositorySharedOf: (packageManager: PackageManager) => readonly string[];
|
|
30
|
+
/** The command that runs `bin`, installed in the repository, with `args`. */
|
|
31
|
+
export declare const commandOf: (packageManager: PackageManager, bin: string, args: readonly string[]) => readonly string[];
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* What dev-env reads of the repository it runs in: the workspaces its
|
|
3
|
+
* package manager knows, the names their package.json files give them, and
|
|
4
|
+
* which package manager installs them. Each is read where it already lives,
|
|
5
|
+
* so a repository configures nothing twice.
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, globSync, readFileSync, statSync } from 'node:fs';
|
|
8
|
+
import { join, relative } from 'node:path';
|
|
9
|
+
import { parse } from 'yaml';
|
|
10
|
+
import { DevEnvError } from './errors.js';
|
|
11
|
+
const LOCKFILES = [
|
|
12
|
+
['pnpm-lock.yaml', 'pnpm'],
|
|
13
|
+
['yarn.lock', 'yarn'],
|
|
14
|
+
['package-lock.json', 'npm'],
|
|
15
|
+
];
|
|
16
|
+
const readJson = (file) => {
|
|
17
|
+
try {
|
|
18
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw new DevEnvError(`${file} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const packageJsonOf = (directory) => {
|
|
25
|
+
const file = join(directory, 'package.json');
|
|
26
|
+
if (!existsSync(file))
|
|
27
|
+
return undefined;
|
|
28
|
+
const read = readJson(file);
|
|
29
|
+
return typeof read === 'object' && read !== null ? read : undefined;
|
|
30
|
+
};
|
|
31
|
+
/** Whether the package.json in a directory makes its TypeScript ES modules. */
|
|
32
|
+
export const isModulePackage = (directory) => packageJsonOf(directory)?.type === 'module';
|
|
33
|
+
/**
|
|
34
|
+
* The package manager the root package.json's `packageManager` names, or
|
|
35
|
+
* else the one whose lockfile the repository holds, or else npm.
|
|
36
|
+
*/
|
|
37
|
+
export const packageManagerOf = (root) => {
|
|
38
|
+
const named = packageJsonOf(root)?.packageManager;
|
|
39
|
+
if (typeof named === 'string') {
|
|
40
|
+
const manager = /^(npm|pnpm|yarn)@/.exec(named)?.[1];
|
|
41
|
+
if (manager !== undefined)
|
|
42
|
+
return manager;
|
|
43
|
+
}
|
|
44
|
+
return (LOCKFILES.find(([lockfile]) => existsSync(join(root, lockfile)))?.[1] ??
|
|
45
|
+
'npm');
|
|
46
|
+
};
|
|
47
|
+
const stringsIn = (value, source) => {
|
|
48
|
+
if (value === undefined || value === null)
|
|
49
|
+
return [];
|
|
50
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== 'string'))
|
|
51
|
+
throw new DevEnvError(`${source} must be a list of globs.`);
|
|
52
|
+
return value;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* The workspace globs of `pnpm-workspace.yaml`, or else of the root
|
|
56
|
+
* package.json's `workspaces`, as a list or under `packages`.
|
|
57
|
+
*/
|
|
58
|
+
export const workspaceGlobsOf = (root) => {
|
|
59
|
+
const pnpmWorkspace = join(root, 'pnpm-workspace.yaml');
|
|
60
|
+
if (existsSync(pnpmWorkspace)) {
|
|
61
|
+
const read = parse(readFileSync(pnpmWorkspace, 'utf8'));
|
|
62
|
+
return stringsIn(read?.packages, `The packages of ${pnpmWorkspace}`);
|
|
63
|
+
}
|
|
64
|
+
const workspaces = packageJsonOf(root)?.workspaces;
|
|
65
|
+
const source = `The workspaces of ${join(root, 'package.json')}`;
|
|
66
|
+
if (typeof workspaces === 'object' &&
|
|
67
|
+
workspaces !== null &&
|
|
68
|
+
!Array.isArray(workspaces))
|
|
69
|
+
return stringsIn(workspaces.packages, source);
|
|
70
|
+
return stringsIn(workspaces, source);
|
|
71
|
+
};
|
|
72
|
+
const IN_NODE_MODULES = /(^|\/)node_modules(\/|$)/;
|
|
73
|
+
const directoriesMatching = (root, glob) => new Set(globSync(glob.replace(/\/+$/, ''), {
|
|
74
|
+
cwd: root,
|
|
75
|
+
exclude: (path) => IN_NODE_MODULES.test(path),
|
|
76
|
+
})
|
|
77
|
+
.filter((path) => statSync(join(root, path)).isDirectory())
|
|
78
|
+
.map((path) => join(root, path)));
|
|
79
|
+
/**
|
|
80
|
+
* Every workspace directory, absolute and sorted: each directory a workspace
|
|
81
|
+
* glob matches that holds a package.json, less those a `!` glob leaves out.
|
|
82
|
+
*/
|
|
83
|
+
export const workspaceDirectoriesOf = (root) => {
|
|
84
|
+
const globs = workspaceGlobsOf(root);
|
|
85
|
+
const included = new Set();
|
|
86
|
+
for (const pattern of globs.filter((glob) => !glob.startsWith('!')))
|
|
87
|
+
for (const directory of directoriesMatching(root, pattern))
|
|
88
|
+
included.add(directory);
|
|
89
|
+
for (const pattern of globs.filter((glob) => glob.startsWith('!')))
|
|
90
|
+
for (const directory of directoriesMatching(root, pattern.slice(1)))
|
|
91
|
+
included.delete(directory);
|
|
92
|
+
return [...included]
|
|
93
|
+
.filter((directory) => directory !== root)
|
|
94
|
+
.filter((directory) => existsSync(join(directory, 'package.json')))
|
|
95
|
+
.toSorted();
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Each workspace package by the name its package.json gives it, with its
|
|
99
|
+
* directory from the repository root, as `/libs/ui/`.
|
|
100
|
+
*/
|
|
101
|
+
export const workspacePackagesOf = (root) => new Map(workspaceDirectoriesOf(root).flatMap((directory) => {
|
|
102
|
+
const name = packageJsonOf(directory)?.name;
|
|
103
|
+
return typeof name === 'string'
|
|
104
|
+
? [[name, `/${relative(root, directory)}/`]]
|
|
105
|
+
: [];
|
|
106
|
+
}));
|
|
107
|
+
/*
|
|
108
|
+
* What the package manager installs every workspace from, so a change to one
|
|
109
|
+
* may change any test environment.
|
|
110
|
+
*/
|
|
111
|
+
const PACKAGE_MANAGER_FILES = {
|
|
112
|
+
npm: ['/package-lock.json', '/.npmrc'],
|
|
113
|
+
pnpm: [
|
|
114
|
+
'/pnpm-lock.yaml',
|
|
115
|
+
'/pnpm-workspace.yaml',
|
|
116
|
+
'/.npmrc',
|
|
117
|
+
'/.pnpmfile.cjs',
|
|
118
|
+
],
|
|
119
|
+
yarn: ['/yarn.lock', '/.yarnrc.yml', '/.yarnrc'],
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Repository paths every integration spec is built from without importing
|
|
123
|
+
* them: the root package.json, compiler settings and Node version, and the
|
|
124
|
+
* package manager's lockfile and workspace configuration.
|
|
125
|
+
*/
|
|
126
|
+
export const repositorySharedOf = (packageManager) => [
|
|
127
|
+
'/package.json',
|
|
128
|
+
'/tsconfig.json',
|
|
129
|
+
'/.nvmrc',
|
|
130
|
+
'/.node-version',
|
|
131
|
+
...PACKAGE_MANAGER_FILES[packageManager],
|
|
132
|
+
];
|
|
133
|
+
// How each package manager runs a tool the repository installed, without fetching one it did not.
|
|
134
|
+
const RUNNERS = {
|
|
135
|
+
npm: ['npm', 'exec', '--no', '--'],
|
|
136
|
+
pnpm: ['pnpm', 'exec'],
|
|
137
|
+
yarn: ['yarn'],
|
|
138
|
+
};
|
|
139
|
+
/** The command that runs `bin`, installed in the repository, with `args`. */
|
|
140
|
+
export const commandOf = (packageManager, bin, args) => [...RUNNERS[packageManager], bin, ...args];
|
package/package.json
CHANGED
|
@@ -1,6 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evandam93/dev-env",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "One development environment per checkout and target, from a manifest beside your code, so worktrees run side by side with no ports pinned anywhere.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"development-environment",
|
|
7
|
+
"docker-compose",
|
|
8
|
+
"integration-tests",
|
|
9
|
+
"ports",
|
|
10
|
+
"vitest",
|
|
11
|
+
"worktree"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/eriktisme/dev-env#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/eriktisme/dev-env/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Erik van Dam",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/eriktisme/dev-env.git"
|
|
22
|
+
},
|
|
23
|
+
"bin": {
|
|
24
|
+
"dev-env": "./bin/dev-env.js"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"bin",
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./affected": {
|
|
37
|
+
"types": "./dist/affected/index.d.ts",
|
|
38
|
+
"default": "./dist/affected/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsc -p tsconfig.build.json",
|
|
47
|
+
"changeset": "changeset",
|
|
48
|
+
"format": "oxfmt",
|
|
49
|
+
"format:check": "oxfmt --check",
|
|
50
|
+
"lint": "oxlint .",
|
|
51
|
+
"prepare": "tsc -p tsconfig.build.json",
|
|
52
|
+
"release": "npm run build && changeset publish",
|
|
53
|
+
"tests": "vitest run --config vitest.unit.ts",
|
|
54
|
+
"tests:integration": "tsc -p tsconfig.build.json && vitest run --config vitest.integration.ts",
|
|
55
|
+
"type-check": "tsc --noEmit",
|
|
56
|
+
"version-packages": "changeset version && oxfmt CHANGELOG.md"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"get-tsconfig": "^4.14.3",
|
|
60
|
+
"oxc-parser": "^0.127.0",
|
|
61
|
+
"tsx": "^4.23.15",
|
|
62
|
+
"yaml": "^2.9.1"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@changesets/changelog-github": "^1.0.1",
|
|
66
|
+
"@changesets/cli": "^3.0.3",
|
|
67
|
+
"@types/node": "^24.0.0",
|
|
68
|
+
"oxfmt": "0.76.0",
|
|
69
|
+
"oxlint": "1.91.0",
|
|
70
|
+
"oxlint-tsgolint": "7.0.2005",
|
|
71
|
+
"typescript": "7.0.2",
|
|
72
|
+
"vitest": "5.1.0"
|
|
73
|
+
},
|
|
74
|
+
"engines": {
|
|
75
|
+
"node": ">=22.17"
|
|
76
|
+
}
|
|
77
|
+
}
|