@evandam93/dev-env 0.1.0 → 0.1.2

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 CHANGED
@@ -262,7 +262,8 @@ It reads each fact where it already lives, so nothing is kept by hand:
262
262
 
263
263
  - The processes a spec starts are the ones its single `startTestEnvironment` call does not leave out, so `without` must be a written-out list of names.
264
264
  - A process reaches the directories its `sources` name, and every file their code imports, following relative imports and the `paths` of the nearest `tsconfig.json`.
265
- - A spec reaches every file it imports, and a workspace package it imports, by the name its `package.json` gives it, as a whole.
265
+ Their code is what git sees there, tracked or new, so nothing git ignores is read, such as `node_modules` or a build's output, which a build running alongside may rewrite.
266
+ - A spec reaches every file it imports, also one imported with a query such as Vite's `?raw`, and a workspace package it imports, by the name its `package.json` gives it, as a whole.
266
267
  - A shared path runs every spec.
267
268
  The target's are `dev-env.ts`, `compose.yaml`, `compose.test.yaml`, `package.json`, `tsconfig.json`, `tsconfig.*.json` and `vitest.integration.ts`.
268
269
  The repository's are its root `package.json`, `tsconfig.json`, `.nvmrc`, `.node-version`, and its package manager's lockfile and configuration, such as `pnpm-workspace.yaml` or `.yarnrc.yml`.
@@ -273,6 +274,7 @@ It reads each fact where it already lives, so nothing is kept by hand:
273
274
  Bindings between processes are not followed: in a test environment a process answers only when the spec starts it.
274
275
 
275
276
  `@evandam93/dev-env/affected` exports the same selection, `layoutOf`, `graphOf`, `selectSpecs` and `forPullRequest`, for a test that pins which specs a change chooses.
277
+ `graphOf` reads the target from git, so it runs inside a git checkout.
276
278
 
277
279
  ## What it does not do
278
280
 
@@ -10,6 +10,7 @@
10
10
  * Paths are relative to the target, and a path that starts with `/` is
11
11
  * relative to the repository.
12
12
  */
13
+ import { execFileSync } from 'node:child_process';
13
14
  import { existsSync, globSync, readFileSync, statSync } from 'node:fs';
14
15
  import { dirname, join, relative, resolve } from 'node:path';
15
16
  import { createPathsMatcher, getTsconfig } from 'get-tsconfig';
@@ -68,8 +69,18 @@ const absoluteOf = (layout, path) => path.startsWith('/')
68
69
  const fileAt = (base) => ['', '.ts', '.tsx', '/index.ts', '/index.tsx']
69
70
  .map((suffix) => base + suffix)
70
71
  .find((path) => existsSync(path) && statSync(path).isFile());
71
- const requestsOf = (file) => {
72
- const text = readFileSync(file, 'utf8');
72
+ /** The text of a file, or `undefined` once it is gone, as a build or a branch may delete it while the selection reads. */
73
+ const textOf = (file) => {
74
+ try {
75
+ return readFileSync(file, 'utf8');
76
+ }
77
+ catch (error) {
78
+ if (error.code === 'ENOENT')
79
+ return undefined;
80
+ throw error;
81
+ }
82
+ };
83
+ const requestsOf = (file, text) => {
73
84
  const { module } = parse(file, text);
74
85
  return [
75
86
  ...module.staticImports.map((entry) => entry.moduleRequest.value),
@@ -113,11 +124,18 @@ const importedFrom = (layout, starts) => {
113
124
  if (seen.has(file))
114
125
  continue;
115
126
  seen.add(file);
116
- files.add(pathIn(layout, file));
117
127
  // A JSON or CSS file is reached, but imports nothing itself.
118
- if (!/\.[cm]?[jt]sx?$/.test(file))
128
+ if (!/\.[cm]?[jt]sx?$/.test(file)) {
129
+ files.add(pathIn(layout, file));
119
130
  continue;
120
- for (const request of requestsOf(file)) {
131
+ }
132
+ const text = textOf(file);
133
+ if (text === undefined)
134
+ continue;
135
+ files.add(pathIn(layout, file));
136
+ for (const imported of requestsOf(file, text)) {
137
+ // Vite's queries, such as ?raw, ask for the same file in another form.
138
+ const request = imported.replace(/\?.*$/, '');
121
139
  const workspace = layout.packages.get(packageNameOf(request));
122
140
  if (workspace !== undefined)
123
141
  packages.add(workspace);
@@ -174,16 +192,30 @@ export const specsIn = (layout) => globSync('**/*.integration.spec.ts', {
174
192
  cwd: layout.target,
175
193
  exclude: (path) => /(^|\/)node_modules(\/|$)/.test(path),
176
194
  }).sort();
195
+ /*
196
+ * The code under one of a process's sources, as git sees it: tracked and new
197
+ * files, less what git ignores, such as `node_modules` and build output. A
198
+ * build that rewrites its output while the selection reads would otherwise
199
+ * take files from under it, and its output is no code anyone changes.
200
+ */
201
+ const codeUnder = (layout, source) => execFileSync('git', [
202
+ 'ls-files',
203
+ '-z',
204
+ '--cached',
205
+ '--others',
206
+ '--exclude-standard',
207
+ '--',
208
+ source,
209
+ ], { cwd: layout.target, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] })
210
+ .split('\0')
211
+ .filter((path) => /\.(ts|tsx|js|mjs)$/.test(path));
177
212
  export const graphOf = (layout) => {
178
213
  const sourcesImports = new Map();
179
214
  const importsUnder = (source) => {
180
215
  const known = sourcesImports.get(source);
181
216
  if (known !== undefined)
182
217
  return known;
183
- const files = globSync(`${source}/**/*.{ts,tsx,js,mjs}`, {
184
- cwd: layout.target,
185
- exclude: (path) => /(^|\/)node_modules(\/|$)/.test(path),
186
- })
218
+ const files = codeUnder(layout, source)
187
219
  .filter(isRuntime)
188
220
  .map((path) => join(layout.target, path));
189
221
  const found = importedFrom(layout, files);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evandam93/dev-env",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
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
5
  "keywords": [
6
6
  "development-environment",