@smoothbricks/nx-plugin 0.4.15 → 0.4.17
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 +17 -0
- package/dist/deploy-policy.d.ts +26 -0
- package/dist/deploy-policy.d.ts.map +1 -0
- package/dist/deploy-policy.js +36 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +153 -14
- package/dist/managed-assets.d.ts +3 -0
- package/dist/managed-assets.d.ts.map +1 -0
- package/dist/managed-assets.js +3 -0
- package/dist/managed-files/ci-workflow.d.ts +215 -0
- package/dist/managed-files/ci-workflow.d.ts.map +1 -0
- package/dist/managed-files/ci-workflow.js +1082 -0
- package/dist/managed-files/context.d.ts +51 -0
- package/dist/managed-files/context.d.ts.map +1 -0
- package/dist/managed-files/context.js +206 -0
- package/dist/managed-files/files.d.ts +53 -0
- package/dist/managed-files/files.d.ts.map +1 -0
- package/dist/managed-files/files.js +248 -0
- package/dist/managed-files/generator.d.ts +16 -0
- package/dist/managed-files/generator.d.ts.map +1 -0
- package/dist/managed-files/generator.js +41 -0
- package/dist/managed-files/github-runs-on.d.ts +19 -0
- package/dist/managed-files/github-runs-on.d.ts.map +1 -0
- package/dist/managed-files/github-runs-on.js +48 -0
- package/dist/managed-files/managed-content.d.ts +45 -0
- package/dist/managed-files/managed-content.d.ts.map +1 -0
- package/dist/managed-files/managed-content.js +102 -0
- package/dist/managed-files/managed-format.d.ts +37 -0
- package/dist/managed-files/managed-format.d.ts.map +1 -0
- package/dist/managed-files/managed-format.js +94 -0
- package/dist/managed-files/paths.d.ts +8 -0
- package/dist/managed-files/paths.d.ts.map +1 -0
- package/dist/managed-files/paths.js +46 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +5 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.js +38 -0
- package/dist/managed-files/private-npm-policy.d.ts +7 -0
- package/dist/managed-files/private-npm-policy.d.ts.map +1 -0
- package/dist/managed-files/private-npm-policy.js +51 -0
- package/dist/managed-files/publish-workflow.d.ts +150 -0
- package/dist/managed-files/publish-workflow.d.ts.map +1 -0
- package/dist/managed-files/publish-workflow.js +989 -0
- package/dist/managed-files/tree.d.ts +30 -0
- package/dist/managed-files/tree.d.ts.map +1 -0
- package/dist/managed-files/tree.js +91 -0
- package/dist/platform-targets.d.ts +5 -0
- package/dist/platform-targets.d.ts.map +1 -0
- package/dist/platform-targets.js +4 -0
- package/dist/typecheck-test-policy.d.ts +5 -9
- package/dist/typecheck-test-policy.d.ts.map +1 -1
- package/dist/typecheck-test-policy.js +69 -99
- package/dist/workspace-config-policy.d.ts +2 -3
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +8 -3
- package/dist/workspace-manifest.d.ts +291 -0
- package/dist/workspace-manifest.d.ts.map +1 -0
- package/dist/workspace-manifest.js +6 -0
- package/dist/workspace-package-policy.d.ts +13 -0
- package/dist/workspace-package-policy.d.ts.map +1 -0
- package/dist/workspace-package-policy.js +24 -0
- package/generators.json +5 -0
- package/managed/raw/envrc +20 -0
- package/managed/raw/git-format-staged.yml +82 -0
- package/managed/raw/gitattributes +8 -0
- package/managed/raw/tooling/devenv +83 -0
- package/managed/raw/tooling/direnv/devenv.smoo.nix +422 -0
- package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +251 -0
- package/managed/raw/tooling/direnv/merge-newer-pins.sh +48 -0
- package/managed/raw/tooling/direnv/repo-path +34 -0
- package/managed/raw/tooling/direnv/secret-references.ts +636 -0
- package/managed/raw/tooling/direnv/setup-environment.ts +585 -0
- package/managed/raw/tooling/direnv/toolchain-stamp.ts +88 -0
- package/managed/raw/tooling/git-hooks/commit-msg.sh +9 -0
- package/managed/raw/tooling/git-hooks/post-commit.sh +71 -0
- package/managed/raw/tooling/git-hooks/pre-commit.sh +18 -0
- package/managed/raw/tooling/git-hooks/pre-push.sh +48 -0
- package/managed/raw/tsconfig.lib.json +28 -0
- package/managed/templates/github/actions/cache-nix-devenv/action.yml +45 -0
- package/managed/templates/github/actions/cache-node-modules/action.yml +17 -0
- package/managed/templates/github/actions/cache-nx/action.yml +21 -0
- package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +19 -0
- package/managed/templates/github/actions/save-nix-devenv/action.yml +32 -0
- package/managed/templates/github/actions/setup-devenv/action.yml +245 -0
- package/managed/templates/github/workflows/managed-files.yml +127 -0
- package/package.json +89 -5
- package/src/bun-bootstrap.test.ts +0 -6
- package/src/deploy-policy.ts +40 -0
- package/src/index.test.ts +225 -2
- package/src/index.ts +180 -14
- package/src/managed-assets.ts +4 -0
- package/src/managed-files/ci-workflow.ts +1365 -0
- package/src/managed-files/context.ts +240 -0
- package/src/managed-files/files.ts +324 -0
- package/src/managed-files/generator.test.ts +151 -0
- package/src/managed-files/generator.ts +60 -0
- package/src/managed-files/github-host-cache-isolation.test.ts +60 -0
- package/src/managed-files/github-runs-on.ts +51 -0
- package/src/managed-files/managed-content.test.ts +147 -0
- package/src/managed-files/managed-content.ts +121 -0
- package/src/managed-files/managed-format.ts +102 -0
- package/src/managed-files/paths.ts +44 -0
- package/src/managed-files/pr-preview-cleanup-workflow.ts +44 -0
- package/src/managed-files/private-npm-policy.ts +62 -0
- package/src/managed-files/publish-workflow.ts +1523 -0
- package/src/managed-files/schema.json +8 -0
- package/src/managed-files/tree-permissions.test.ts +109 -0
- package/src/managed-files/tree.test.ts +123 -0
- package/src/managed-files/tree.ts +118 -0
- package/src/platform-targets.ts +4 -0
- package/src/typecheck-test-policy.test.ts +28 -3
- package/src/typecheck-test-policy.ts +46 -121
- package/src/workspace-config-policy.test.ts +14 -2
- package/src/workspace-config-policy.ts +9 -3
- package/src/workspace-manifest.ts +327 -0
- package/src/workspace-package-policy.ts +32 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/* biome-ignore-all lint/suspicious/noTemplateCurlyInString: Tests assert literal credential and GitHub expressions. */
|
|
2
|
+
import { afterEach, describe, expect, it } from 'bun:test';
|
|
3
|
+
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { readJson, type Tree, updateJson } from 'nx/src/devkit-exports.js';
|
|
7
|
+
import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports.js';
|
|
8
|
+
import { FsTree, flushChanges } from 'nx/src/generators/tree.js';
|
|
9
|
+
import type { PackageJson } from '../workspace-manifest.js';
|
|
10
|
+
import type { WorkspaceProjects } from './context.js';
|
|
11
|
+
import { generateManagedFiles } from './generator.js';
|
|
12
|
+
import { INLINE_LOCAL_BEGIN, INLINE_LOCAL_END, LOCAL_SECTION_MARKER } from './managed-content.js';
|
|
13
|
+
import { assertNoManagedConflicts } from './tree.js';
|
|
14
|
+
|
|
15
|
+
const repository = 'https://github.com/example/workspace.git';
|
|
16
|
+
const projects: WorkspaceProjects = { library: { root: 'packages/library', targets: { build: {}, test: {} } } };
|
|
17
|
+
const temporaryRoots: string[] = [];
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
for (const root of temporaryRoots.splice(0)) rmSync(root, { recursive: true, force: true });
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
function workspace(): Tree {
|
|
23
|
+
const tree = createTreeWithEmptyWorkspace({ formatter: 'none' });
|
|
24
|
+
tree.write(
|
|
25
|
+
'package.json',
|
|
26
|
+
JSON.stringify({ name: '@example/workspace', version: '1.0.0', repository, workspaces: ['packages/*'] }),
|
|
27
|
+
);
|
|
28
|
+
tree.write(
|
|
29
|
+
'packages/library/package.json',
|
|
30
|
+
JSON.stringify({
|
|
31
|
+
name: '@example/library',
|
|
32
|
+
version: '1.0.0',
|
|
33
|
+
repository,
|
|
34
|
+
nx: { name: 'library', tags: ['npm:public'] },
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
37
|
+
tree.write('bun.lock', '{}');
|
|
38
|
+
return tree;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function generate(tree: Tree, graph: WorkspaceProjects = projects): Promise<void> {
|
|
42
|
+
assertNoManagedConflicts(await generateManagedFiles(tree, graph, new Map()));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('managed-files generator', () => {
|
|
46
|
+
it('generates workflows and packaged bootstrap assets in a virtual workspace', async () => {
|
|
47
|
+
const tree = workspace();
|
|
48
|
+
await generate(tree);
|
|
49
|
+
expect(tree.read('.github/workflows/ci.yml', 'utf8')).toContain('name: CI');
|
|
50
|
+
expect(tree.read('.github/workflows/publish.yml', 'utf8')).toContain('name: Publish');
|
|
51
|
+
expect(tree.read('tooling/direnv/devenv.smoo.nix', 'utf8')).toContain('languages.rust');
|
|
52
|
+
expect(tree.listChanges().find((change) => change.path === 'tooling/devenv')?.options?.mode).toBe(0o755);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('uses manifest edits from Tree, not the checkout that runs the tests', async () => {
|
|
56
|
+
const tree = workspace();
|
|
57
|
+
updateJson<PackageJson>(tree, 'package.json', (pkg) => ({
|
|
58
|
+
...pkg,
|
|
59
|
+
smoo: { github: { pushBranches: ['trunk'], runsOn: 'ubuntu-24.04' } },
|
|
60
|
+
}));
|
|
61
|
+
await generate(tree);
|
|
62
|
+
const ci = tree.read('.github/workflows/ci.yml', 'utf8');
|
|
63
|
+
expect(ci).toContain(' - trunk');
|
|
64
|
+
expect(ci).toContain(' runs-on: ubuntu-24.04');
|
|
65
|
+
expect(ci).not.toContain('nixos-latest');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('discovers publication from workspace packages and does not publish another repository', async () => {
|
|
69
|
+
const tree = workspace();
|
|
70
|
+
updateJson<PackageJson>(tree, 'packages/library/package.json', (pkg) => ({
|
|
71
|
+
...pkg,
|
|
72
|
+
repository: 'https://github.com/example/other.git',
|
|
73
|
+
}));
|
|
74
|
+
await generate(tree);
|
|
75
|
+
expect(tree.exists('.github/workflows/publish.yml')).toBe(false);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('uses inferred graph targets rather than requiring explicit package target configuration', async () => {
|
|
79
|
+
const tree = workspace();
|
|
80
|
+
await generate(tree, {
|
|
81
|
+
library: {
|
|
82
|
+
root: 'packages/library',
|
|
83
|
+
tags: ['stage-deploy-target'],
|
|
84
|
+
targets: { deploy: { options: { command: 'wrangler deploy' } }, 'test-browser': {}, 'e2e-deployment': {} },
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
expect(tree.read('.github/workflows/ci.yml', 'utf8')).toContain('E2E Tests');
|
|
88
|
+
expect(tree.read('.github/workflows/ci.yml', 'utf8')).toContain('Browser Tests');
|
|
89
|
+
expect(tree.exists('.github/workflows/pr-preview-cleanup.yml')).toBe(true);
|
|
90
|
+
expect(readJson<PackageJson>(tree, 'packages/library/package.json').nx?.targets).toBeUndefined();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('preserves repository-owned Nix configuration and managed local sections', async () => {
|
|
94
|
+
const tree = workspace();
|
|
95
|
+
tree.write('tooling/direnv/devenv.nix', '{ ... }: { imports = [ ./devenv.smoo.nix ]; }');
|
|
96
|
+
tree.write('.gitattributes', `old\n${LOCAL_SECTION_MARKER}\n*.custom merge=custom\n`);
|
|
97
|
+
await generate(tree);
|
|
98
|
+
expect(tree.read('tooling/direnv/devenv.nix', 'utf8')).toBe('{ ... }: { imports = [ ./devenv.smoo.nix ]; }');
|
|
99
|
+
expect(tree.read('.gitattributes', 'utf8')).toEndWith(`${LOCAL_SECTION_MARKER}\n*.custom merge=custom\n`);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('rejects an ownership conflict before callers may flush the Tree', async () => {
|
|
103
|
+
const tree = workspace();
|
|
104
|
+
tree.write('.gitattributes', ['removed anchor', INLINE_LOCAL_BEGIN, 'local', INLINE_LOCAL_END].join('\n'));
|
|
105
|
+
await expect(generate(tree)).rejects.toThrow('before writing');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('does not resolve configured secrets or interpolate their values into generated files', async () => {
|
|
109
|
+
const tree = workspace();
|
|
110
|
+
updateJson<PackageJson>(tree, 'package.json', (pkg) => ({
|
|
111
|
+
...pkg,
|
|
112
|
+
smoo: {
|
|
113
|
+
secrets: { TEST_KEY: { command: ['sh', '-c', 'exit 99'] } },
|
|
114
|
+
privateNpm: { scope: '@private', readTokenEnv: 'TEST_KEY' },
|
|
115
|
+
},
|
|
116
|
+
}));
|
|
117
|
+
await generate(tree);
|
|
118
|
+
expect(tree.read('.github/workflows/ci.yml', 'utf8')).not.toContain('TEST_KEY');
|
|
119
|
+
expect(tree.read('.github/workflows/ci.yml', 'utf8')).not.toContain('exit 99');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('uses committed registry configuration from Tree when a private package is actually consumed', async () => {
|
|
123
|
+
const tree = workspace();
|
|
124
|
+
updateJson<PackageJson>(tree, 'package.json', (pkg) => ({
|
|
125
|
+
...pkg,
|
|
126
|
+
dependencies: { '@private/library': '1.0.0' },
|
|
127
|
+
smoo: { privateNpm: { scope: '@private' } },
|
|
128
|
+
}));
|
|
129
|
+
tree.write(
|
|
130
|
+
'.npmrc',
|
|
131
|
+
'@private:registry=https://registry.example.net/npm/\n//registry.example.net/npm/:_authToken=${PRIVATE_READ}\n',
|
|
132
|
+
);
|
|
133
|
+
await generate(tree);
|
|
134
|
+
expect(tree.read('.github/workflows/ci.yml', 'utf8')).toContain('PRIVATE_READ: ${{ secrets.PRIVATE_READ }}');
|
|
135
|
+
expect(tree.read('.github/workflows/ci.yml', 'utf8')).not.toContain('PRIVATE_WRITE');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('stages nothing on a fresh Tree after applying the first generation', async () => {
|
|
139
|
+
const root = mkdtempSync(join(tmpdir(), 'smoo-generator-'));
|
|
140
|
+
temporaryRoots.push(root);
|
|
141
|
+
const first = workspace();
|
|
142
|
+
await generate(first);
|
|
143
|
+
flushChanges(root, first.listChanges());
|
|
144
|
+
const second = new FsTree(root, false);
|
|
145
|
+
const results = await generateManagedFiles(second, projects);
|
|
146
|
+
assertNoManagedConflicts(results);
|
|
147
|
+
expect(second.listChanges()).toEqual([]);
|
|
148
|
+
expect(results.every((result) => result.action === 'unchanged' || result.action === 'skipped')).toBe(true);
|
|
149
|
+
expect(readFileSync(join(root, 'tooling/devenv'), 'utf8')).toBe(first.read('tooling/devenv', 'utf8') ?? '');
|
|
150
|
+
});
|
|
151
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createProjectGraphAsync, getProjects, joinPathFragments, readJson, type Tree } from 'nx/src/devkit-exports.js';
|
|
2
|
+
import { renderTypecheckTestFiles } from '../typecheck-test-policy.js';
|
|
3
|
+
import type { PackageJson } from '../workspace-manifest.js';
|
|
4
|
+
import { deriveManagedFileContext, type WorkspaceProjects } from './context.js';
|
|
5
|
+
import { type ManagedFileContext, renderManagedFiles } from './files.js';
|
|
6
|
+
import { formatManagedContent } from './managed-format.js';
|
|
7
|
+
import { inspectManagedPaths } from './paths.js';
|
|
8
|
+
import { assertNoManagedConflicts, type FileResult, type ManagedPathInfo, stageManagedFiles } from './tree.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* All workspace content comes from Tree. The supplied graph is Nx's resolved
|
|
12
|
+
* graph, not getProjects(Tree), which intentionally omits plugin-inferred targets.
|
|
13
|
+
* The shared test-config renderer reads staged manifests too; no generation
|
|
14
|
+
* installs dependencies or resolves secrets.
|
|
15
|
+
*/
|
|
16
|
+
export async function generateManagedFiles(
|
|
17
|
+
tree: Tree,
|
|
18
|
+
projects: WorkspaceProjects,
|
|
19
|
+
pathInfo?: ReadonlyMap<string, ManagedPathInfo>,
|
|
20
|
+
): Promise<FileResult[]> {
|
|
21
|
+
const manifest = tree.exists('package.json') ? readJson<PackageJson>(tree, 'package.json') : {};
|
|
22
|
+
const packageRoots = new Set([...getProjects(tree).values()].map((project) => project.root));
|
|
23
|
+
const packages = [...packageRoots]
|
|
24
|
+
.filter((root) => root !== '.' && root !== '')
|
|
25
|
+
.flatMap((root) => {
|
|
26
|
+
const path = joinPathFragments(root, 'package.json');
|
|
27
|
+
return tree.exists(path) ? [readJson<PackageJson>(tree, path)] : [];
|
|
28
|
+
});
|
|
29
|
+
return generateManagedFilesForContext(
|
|
30
|
+
tree,
|
|
31
|
+
deriveManagedFileContext(manifest, packages, projects, tree.exists('bun.lock'), tree.read('.npmrc', 'utf8') ?? ''),
|
|
32
|
+
pathInfo,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Shared rendering boundary; stage only after every consumer formatter has succeeded. */
|
|
37
|
+
export async function generateManagedFilesForContext(
|
|
38
|
+
tree: Tree,
|
|
39
|
+
context: ManagedFileContext,
|
|
40
|
+
pathInfo?: ReadonlyMap<string, ManagedPathInfo>,
|
|
41
|
+
): Promise<FileResult[]> {
|
|
42
|
+
const files = [...renderManagedFiles(context), ...renderTypecheckTestFiles(tree)];
|
|
43
|
+
for (const file of files) {
|
|
44
|
+
if (file.content !== null) file.content = await formatManagedContent(tree.root, file.target, file.content);
|
|
45
|
+
}
|
|
46
|
+
const paths =
|
|
47
|
+
pathInfo ??
|
|
48
|
+
inspectManagedPaths(
|
|
49
|
+
tree.root,
|
|
50
|
+
files.filter((file) => file.content !== null).map((file) => file.target),
|
|
51
|
+
);
|
|
52
|
+
return stageManagedFiles(tree, files, paths);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Native Nx generate/sync entry point. CLI calls the same transformation with its already-loaded graph. */
|
|
56
|
+
export default async function generator(tree: Tree): Promise<void> {
|
|
57
|
+
const graph = await createProjectGraphAsync();
|
|
58
|
+
const projects = Object.fromEntries(Object.entries(graph.nodes).map(([name, node]) => [name, node.data]));
|
|
59
|
+
assertNoManagedConflicts(await generateManagedFiles(tree, projects));
|
|
60
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
|
|
4
|
+
const action = readFileSync(
|
|
5
|
+
new URL('../../managed/templates/github/actions/setup-devenv/action.yml', import.meta.url),
|
|
6
|
+
'utf8',
|
|
7
|
+
);
|
|
8
|
+
const assignments = action.split('\n').filter((line) => line.trimStart().startsWith('scope=$('));
|
|
9
|
+
|
|
10
|
+
function scope(overrides: Record<string, string> = {}): string {
|
|
11
|
+
const assignment = assignments[0];
|
|
12
|
+
if (!assignment) throw new Error('The setup action must scope its mutable host caches');
|
|
13
|
+
const result = Bun.spawnSync(['bash', '-ceu', `${assignment}\nprintf '%s' "$scope"`], {
|
|
14
|
+
env: {
|
|
15
|
+
...process.env,
|
|
16
|
+
GITHUB_WORKFLOW: 'CI',
|
|
17
|
+
GITHUB_REF: 'refs/pull/10/merge',
|
|
18
|
+
GITHUB_JOB: 'main',
|
|
19
|
+
RUNNER_OS: 'Linux',
|
|
20
|
+
RUNNER_ARCH: 'X64',
|
|
21
|
+
...overrides,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
expect(result.exitCode).toBe(0);
|
|
25
|
+
return result.stdout.toString();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('host CI mutable cache isolation', () => {
|
|
29
|
+
test('Nx and devenv use the same exact workflow/ref/job/platform identity', () => {
|
|
30
|
+
expect(assignments).toHaveLength(2);
|
|
31
|
+
expect(assignments[0]).toBe(assignments[1]);
|
|
32
|
+
expect(action).toContain('/${lane:-default}/$scope');
|
|
33
|
+
expect(action).toContain('/${lane:-default}/$DEVENV_STATE_JOB/$scope');
|
|
34
|
+
expect(action).toContain('NX_CACHE_DIRECTORY=$NX_CACHE_ROOT/cache');
|
|
35
|
+
expect(action).toContain('NX_WORKSPACE_DATA_DIRECTORY=$NX_CACHE_ROOT/workspace-data');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('repeat runs stay warm but distinct PRs, jobs and platforms cannot share graph state', () => {
|
|
39
|
+
const original = scope();
|
|
40
|
+
expect(original).toMatch(/^[a-f0-9]{40}$/);
|
|
41
|
+
expect(scope({ GITHUB_RUN_ID: 'another-run', GITHUB_RUN_ATTEMPT: '2' })).toBe(original);
|
|
42
|
+
const identities: Record<string, string>[] = [
|
|
43
|
+
{ GITHUB_WORKFLOW: 'Publish' },
|
|
44
|
+
{ GITHUB_REF: 'refs/pull/11/merge' },
|
|
45
|
+
{ GITHUB_REF: 'refs/heads/main' },
|
|
46
|
+
{ GITHUB_JOB: 'e2e-deployment' },
|
|
47
|
+
{ RUNNER_OS: 'macOS' },
|
|
48
|
+
{ RUNNER_ARCH: 'ARM64' },
|
|
49
|
+
];
|
|
50
|
+
for (const other of identities) {
|
|
51
|
+
expect(scope(other)).not.toBe(original);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('similar-looking refs do not collide or become filesystem paths', () => {
|
|
56
|
+
expect(scope({ GITHUB_REF: 'refs/heads/feature/a' })).not.toBe(scope({ GITHUB_REF: 'refs/heads/feature-a' }));
|
|
57
|
+
expect(scope({ GITHUB_REF: 'refs/heads/Feature' })).not.toBe(scope({ GITHUB_REF: 'refs/heads/feature' }));
|
|
58
|
+
expect(scope({ GITHUB_REF: 'refs/heads/../../other' })).toMatch(/^[a-f0-9]{40}$/);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** Shared `runs-on:` emission for generated GitHub workflow YAML. */
|
|
2
|
+
|
|
3
|
+
export type WorkflowRunsOn = string | string[] | undefined;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The GitHub-hosted Linux runner. Also the fork-PR fallback of every gated
|
|
7
|
+
* self-hosted label, and the only runner class npmjs accepts provenance from.
|
|
8
|
+
*/
|
|
9
|
+
export const GITHUB_HOSTED_LINUX_RUNNER = 'ubuntu-latest';
|
|
10
|
+
|
|
11
|
+
export function isNixosRunner(runsOn: WorkflowRunsOn): boolean {
|
|
12
|
+
const labels = runsOn === undefined ? [] : typeof runsOn === 'string' ? [runsOn] : runsOn;
|
|
13
|
+
return labels.some((label) => label === 'nixos' || label.startsWith('nixos-'));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Trusted jobs use configured nixos self-hosted labels; fork PRs stay on
|
|
18
|
+
* ubuntu-latest (no access to the private runner fleet).
|
|
19
|
+
*/
|
|
20
|
+
export function githubUsesNixosRunnerExpr(): string {
|
|
21
|
+
return "(github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Full ` runs-on: …` line(s), indented for a job under `jobs.<id>:`.
|
|
26
|
+
* Nixos labels emit the multiline prettier-stable fork-gate expression.
|
|
27
|
+
*/
|
|
28
|
+
export function renderRunsOnLine(runsOn: WorkflowRunsOn): string {
|
|
29
|
+
if (!isNixosRunner(runsOn)) {
|
|
30
|
+
let value: string;
|
|
31
|
+
if (runsOn === undefined) {
|
|
32
|
+
value = 'ubuntu-latest';
|
|
33
|
+
} else if (typeof runsOn === 'string') {
|
|
34
|
+
value = runsOn.length > 0 ? runsOn : 'ubuntu-latest';
|
|
35
|
+
} else if (runsOn.length === 0) {
|
|
36
|
+
value = 'ubuntu-latest';
|
|
37
|
+
} else if (runsOn.length === 1) {
|
|
38
|
+
value = runsOn[0] ?? 'ubuntu-latest';
|
|
39
|
+
} else {
|
|
40
|
+
value = `[${runsOn.map((label) => `'${label}'`).join(', ')}]`;
|
|
41
|
+
}
|
|
42
|
+
return ` runs-on: ${value}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const labels = typeof runsOn === 'string' ? [runsOn] : runsOn;
|
|
46
|
+
// Multiline matches prettier so checked-in workflows stay byte-identical.
|
|
47
|
+
const nixosJson = JSON.stringify(labels);
|
|
48
|
+
return ` runs-on:
|
|
49
|
+
\${{ ${githubUsesNixosRunnerExpr()} &&
|
|
50
|
+
fromJSON('${nixosJson}') || 'ubuntu-latest' }}`;
|
|
51
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import fc from 'fast-check';
|
|
3
|
+
import {
|
|
4
|
+
extractInlineLocalBlocks,
|
|
5
|
+
INLINE_LOCAL_BEGIN,
|
|
6
|
+
INLINE_LOCAL_END,
|
|
7
|
+
LOCAL_SECTION_MARKER,
|
|
8
|
+
reinsertInlineLocalBlocks,
|
|
9
|
+
splitLocalSection,
|
|
10
|
+
} from './managed-content.js';
|
|
11
|
+
|
|
12
|
+
const MANAGED = '# managed content\npath merge=driver\n';
|
|
13
|
+
|
|
14
|
+
describe('managed-file local sections', () => {
|
|
15
|
+
it('content without a marker is entirely managed', () => {
|
|
16
|
+
const { managed, localTail } = splitLocalSection(MANAGED);
|
|
17
|
+
expect(managed).toBe(MANAGED);
|
|
18
|
+
expect(localTail).toBe('');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('everything from the marker onward is the repo-owned tail', () => {
|
|
22
|
+
const tail = `${LOCAL_SECTION_MARKER}\ncustom/*.jsonl merge=custom-log\n`;
|
|
23
|
+
const { managed, localTail } = splitLocalSection(`${MANAGED}\n${tail}`);
|
|
24
|
+
expect(managed).toBe(`${MANAGED}\n`);
|
|
25
|
+
expect(localTail).toBe(tail);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('a tail directly after the managed content tolerates the separating newline', () => {
|
|
29
|
+
// The compare rule accepts `managed === content + '\n'` when a tail exists,
|
|
30
|
+
// so update → check round-trips as unchanged.
|
|
31
|
+
const written = `${MANAGED}\n${LOCAL_SECTION_MARKER}\nextra\n`;
|
|
32
|
+
const { managed, localTail } = splitLocalSection(written);
|
|
33
|
+
expect(managed).toBe(`${MANAGED}\n`);
|
|
34
|
+
expect(localTail.startsWith(LOCAL_SECTION_MARKER)).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('managed-file inline local blocks', () => {
|
|
39
|
+
it('content with no inline markers extracts as fully managed, no blocks', () => {
|
|
40
|
+
const { withoutInline, blocks } = extractInlineLocalBlocks(MANAGED);
|
|
41
|
+
expect(withoutInline).toBe(MANAGED);
|
|
42
|
+
expect(blocks).toEqual([]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('a wrapped block is extracted and removed, anchored on the preceding line', () => {
|
|
46
|
+
const current = ['a:', ' - one', ' - two', INLINE_LOCAL_BEGIN, ' - repo-owned', INLINE_LOCAL_END, 'b:'].join(
|
|
47
|
+
'\n',
|
|
48
|
+
);
|
|
49
|
+
const { withoutInline, blocks } = extractInlineLocalBlocks(current);
|
|
50
|
+
expect(withoutInline).toBe(['a:', ' - one', ' - two', 'b:'].join('\n'));
|
|
51
|
+
expect(blocks).toEqual([{ anchor: ' - two', lines: ' - repo-owned' }]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('multiple blocks each anchor to their own preceding line', () => {
|
|
55
|
+
const current = [
|
|
56
|
+
'x',
|
|
57
|
+
INLINE_LOCAL_BEGIN,
|
|
58
|
+
'first',
|
|
59
|
+
INLINE_LOCAL_END,
|
|
60
|
+
'y',
|
|
61
|
+
INLINE_LOCAL_BEGIN,
|
|
62
|
+
'second',
|
|
63
|
+
INLINE_LOCAL_END,
|
|
64
|
+
'z',
|
|
65
|
+
].join('\n');
|
|
66
|
+
const { withoutInline, blocks } = extractInlineLocalBlocks(current);
|
|
67
|
+
expect(withoutInline).toBe(['x', 'y', 'z'].join('\n'));
|
|
68
|
+
expect(blocks).toEqual([
|
|
69
|
+
{ anchor: 'x', lines: 'first' },
|
|
70
|
+
{ anchor: 'y', lines: 'second' },
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('a begin marker with no preceding line throws rather than silently dropping content', () => {
|
|
75
|
+
const current = [INLINE_LOCAL_BEGIN, 'orphan', INLINE_LOCAL_END].join('\n');
|
|
76
|
+
expect(() => extractInlineLocalBlocks(current)).toThrow(/no preceding anchor/);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('an unterminated begin marker throws rather than silently dropping content', () => {
|
|
80
|
+
const current = ['a', INLINE_LOCAL_BEGIN, 'unterminated'].join('\n');
|
|
81
|
+
expect(() => extractInlineLocalBlocks(current)).toThrow(/no matching/);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('reinserting into fresh content with no blocks is a no-op', () => {
|
|
85
|
+
expect(reinsertInlineLocalBlocks(MANAGED, [])).toBe(MANAGED);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('reinserting splices the block back after its anchor in freshly rendered content', () => {
|
|
89
|
+
const fresh = ['a:', ' - one', ' - two', 'b:'].join('\n');
|
|
90
|
+
const result = reinsertInlineLocalBlocks(fresh, [{ anchor: ' - two', lines: ' - repo-owned' }]);
|
|
91
|
+
expect(result).toBe(
|
|
92
|
+
['a:', ' - one', ' - two', INLINE_LOCAL_BEGIN, ' - repo-owned', INLINE_LOCAL_END, 'b:'].join('\n'),
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('preserves marker indentation when refreshing nested configuration', () => {
|
|
97
|
+
const markerIndent = ' ';
|
|
98
|
+
const current = [
|
|
99
|
+
'patterns:',
|
|
100
|
+
" - '*.html'",
|
|
101
|
+
`${markerIndent}${INLINE_LOCAL_BEGIN}`,
|
|
102
|
+
" - '!**/templates/*.html'",
|
|
103
|
+
`${markerIndent}${INLINE_LOCAL_END}`,
|
|
104
|
+
].join('\n');
|
|
105
|
+
const { withoutInline, blocks } = extractInlineLocalBlocks(current);
|
|
106
|
+
expect(reinsertInlineLocalBlocks(withoutInline, blocks)).toBe(current);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("reinserting refuses when the anchor no longer appears — never silently drops the repo's customization", () => {
|
|
110
|
+
const fresh = ['a:', ' - one', 'b:'].join('\n'); // ' - two' is gone
|
|
111
|
+
expect(() => reinsertInlineLocalBlocks(fresh, [{ anchor: ' - two', lines: ' - repo-owned' }])).toThrow(
|
|
112
|
+
/matches no line/,
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('reinserting refuses an ambiguous multiply occurring anchor', () => {
|
|
117
|
+
const fresh = ['anchor', 'middle', 'anchor'].join('\n');
|
|
118
|
+
expect(() => reinsertInlineLocalBlocks(fresh, [{ anchor: 'anchor', lines: 'repo-owned' }])).toThrow(
|
|
119
|
+
/matches 2 lines/,
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('property: extract then reinsert round-trips to the original for unique, single-occurrence anchors', () => {
|
|
124
|
+
// Lines that can serve as anchors: non-empty, not a marker, and each drawn
|
|
125
|
+
// from a small alphabet so uniqueness is checkable — the round-trip only
|
|
126
|
+
// holds when an anchor line occurs exactly once in the managed section
|
|
127
|
+
// because ambiguous anchors are rejected rather than selecting one.
|
|
128
|
+
const linePool = fc.constantFrom('alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta');
|
|
129
|
+
fc.assert(
|
|
130
|
+
fc.property(
|
|
131
|
+
fc.uniqueArray(linePool, { minLength: 1, maxLength: 6 }),
|
|
132
|
+
fc.array(fc.string(), { maxLength: 3 }),
|
|
133
|
+
(anchors, blockLinesFlat) => {
|
|
134
|
+
const blockLines = blockLinesFlat.length > 0 ? [blockLinesFlat.join('|') || 'x'] : ['x'];
|
|
135
|
+
const fresh = anchors.join('\n');
|
|
136
|
+
const withBlocks = anchors
|
|
137
|
+
.map((anchor) => [anchor, INLINE_LOCAL_BEGIN, ...blockLines, INLINE_LOCAL_END].join('\n'))
|
|
138
|
+
.join('\n');
|
|
139
|
+
const { withoutInline, blocks } = extractInlineLocalBlocks(withBlocks);
|
|
140
|
+
expect(withoutInline).toBe(fresh);
|
|
141
|
+
const reinserted = reinsertInlineLocalBlocks(fresh, blocks);
|
|
142
|
+
expect(reinserted).toBe(withBlocks);
|
|
143
|
+
},
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/** Content ownership policy. No filesystem, process, environment or Nx dependencies. */
|
|
2
|
+
export class ManagedContentConflict extends Error {}
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Repos may append their own content to a managed file below this marker —
|
|
6
|
+
* e.g. extra merge drivers in .gitattributes. Everything from the marker
|
|
7
|
+
* line onward is preserved verbatim across updates and ignored by the
|
|
8
|
+
* drift check; the managed section above it stays byte-exact.
|
|
9
|
+
*/
|
|
10
|
+
export const LOCAL_SECTION_MARKER = '# smoo-local: everything below this line is repo-owned and preserved';
|
|
11
|
+
|
|
12
|
+
/** Split a managed target's content into the managed part and the repo-owned tail. */
|
|
13
|
+
export function splitLocalSection(current: string): { managed: string; localTail: string } {
|
|
14
|
+
// A marker mentioned inside a value or comment is not an ownership boundary.
|
|
15
|
+
let offset = 0;
|
|
16
|
+
for (const line of current.split('\n')) {
|
|
17
|
+
if (line.replace(/\r$/, '') === LOCAL_SECTION_MARKER) {
|
|
18
|
+
return { managed: current.slice(0, offset), localTail: current.slice(offset) };
|
|
19
|
+
}
|
|
20
|
+
offset += line.length + 1;
|
|
21
|
+
}
|
|
22
|
+
return { managed: current, localTail: '' };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A repo-owned block INSIDE the managed section — e.g. one extra pattern
|
|
27
|
+
* spliced into a formatter's list, where a trailing marker (LOCAL_SECTION_MARKER)
|
|
28
|
+
* can't express it because it isn't at the end of the file. Wrap it in
|
|
29
|
+
* `# smoo-local-begin` / `# smoo-local-end`; the block is anchored to the line
|
|
30
|
+
* immediately before `# smoo-local-begin`. On update, the block is re-spliced
|
|
31
|
+
* right after that same anchor line in the freshly rendered template — if the
|
|
32
|
+
* anchor no longer appears there (the template reworked that section), the
|
|
33
|
+
* update refuses rather than silently dropping the repo's customization.
|
|
34
|
+
*/
|
|
35
|
+
export const INLINE_LOCAL_BEGIN = '# smoo-local-begin';
|
|
36
|
+
export const INLINE_LOCAL_END = '# smoo-local-end';
|
|
37
|
+
|
|
38
|
+
export interface InlineLocalBlock {
|
|
39
|
+
anchor: string;
|
|
40
|
+
lines: string;
|
|
41
|
+
markerIndent?: string;
|
|
42
|
+
/** Distinguishes no lines from one deliberately blank line. */
|
|
43
|
+
empty?: true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Pull inline local blocks out of a managed section, returning the section
|
|
47
|
+
* with each block (and its markers) removed, plus the extracted blocks in
|
|
48
|
+
* the order they appeared. */
|
|
49
|
+
export function extractInlineLocalBlocks(managed: string): { withoutInline: string; blocks: InlineLocalBlock[] } {
|
|
50
|
+
const lines = managed.split('\n');
|
|
51
|
+
const kept: string[] = [];
|
|
52
|
+
const blocks: InlineLocalBlock[] = [];
|
|
53
|
+
let i = 0;
|
|
54
|
+
while (i < lines.length) {
|
|
55
|
+
const line = lines[i];
|
|
56
|
+
if (line !== undefined && line.trim() === INLINE_LOCAL_BEGIN) {
|
|
57
|
+
const anchor = kept.at(-1);
|
|
58
|
+
if (anchor === undefined) {
|
|
59
|
+
throw new ManagedContentConflict(`${INLINE_LOCAL_BEGIN} on line ${i + 1} has no preceding anchor line`);
|
|
60
|
+
}
|
|
61
|
+
const blockLines: string[] = [];
|
|
62
|
+
i += 1;
|
|
63
|
+
while (i < lines.length && lines[i]?.trim() !== INLINE_LOCAL_END) {
|
|
64
|
+
const blockLine = lines[i];
|
|
65
|
+
if (blockLine.trim() === INLINE_LOCAL_BEGIN) {
|
|
66
|
+
throw new ManagedContentConflict(`${INLINE_LOCAL_BEGIN} on line ${i + 1} is nested`);
|
|
67
|
+
}
|
|
68
|
+
blockLines.push(blockLine);
|
|
69
|
+
i += 1;
|
|
70
|
+
}
|
|
71
|
+
if (i >= lines.length) {
|
|
72
|
+
throw new ManagedContentConflict(
|
|
73
|
+
`${INLINE_LOCAL_BEGIN} anchored on "${anchor}" has no matching ${INLINE_LOCAL_END}`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
const markerIndent = line.slice(0, line.length - line.trimStart().length);
|
|
77
|
+
blocks.push({
|
|
78
|
+
anchor,
|
|
79
|
+
lines: blockLines.join('\n'),
|
|
80
|
+
...(blockLines.length === 0 ? { empty: true as const } : {}),
|
|
81
|
+
...(markerIndent === '' ? {} : { markerIndent }),
|
|
82
|
+
});
|
|
83
|
+
i += 1; // skip the END marker line itself
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (line.trim() === INLINE_LOCAL_END) {
|
|
87
|
+
throw new ManagedContentConflict(`${INLINE_LOCAL_END} on line ${i + 1} has no matching ${INLINE_LOCAL_BEGIN}`);
|
|
88
|
+
}
|
|
89
|
+
kept.push(line);
|
|
90
|
+
i += 1;
|
|
91
|
+
}
|
|
92
|
+
return { withoutInline: kept.join('\n'), blocks };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Re-splice extracted inline blocks into freshly rendered managed content,
|
|
96
|
+
* each immediately after its anchor line. A no-op when there are no blocks. */
|
|
97
|
+
export function reinsertInlineLocalBlocks(content: string, blocks: InlineLocalBlock[]): string {
|
|
98
|
+
if (blocks.length === 0) return content;
|
|
99
|
+
const lines = content.split('\n');
|
|
100
|
+
const additions = new Map<number, string[]>();
|
|
101
|
+
for (const block of blocks) {
|
|
102
|
+
const matches = lines.flatMap((line, index) => (line === block.anchor ? [index] : []));
|
|
103
|
+
if (matches.length !== 1) {
|
|
104
|
+
const reason = matches.length === 0 ? 'no line' : `${matches.length} lines`;
|
|
105
|
+
throw new ManagedContentConflict(
|
|
106
|
+
`${INLINE_LOCAL_BEGIN} block anchored on "${block.anchor}" matches ${reason} in the updated ` +
|
|
107
|
+
'template — reconcile the repo-owned block manually',
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
const index = matches[0];
|
|
111
|
+
const markerIndent = block.markerIndent ?? '';
|
|
112
|
+
const inserted = additions.get(index) ?? [];
|
|
113
|
+
inserted.push(
|
|
114
|
+
`${markerIndent}${INLINE_LOCAL_BEGIN}`,
|
|
115
|
+
...(block.empty ? [] : block.lines.split('\n')),
|
|
116
|
+
`${markerIndent}${INLINE_LOCAL_END}`,
|
|
117
|
+
);
|
|
118
|
+
additions.set(index, inserted);
|
|
119
|
+
}
|
|
120
|
+
return lines.flatMap((line, index) => [line, ...(additions.get(index) ?? [])]).join('\n');
|
|
121
|
+
}
|