@smoothbricks/nx-plugin 0.4.15 → 0.4.16

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.
Files changed (114) hide show
  1. package/README.md +17 -0
  2. package/dist/deploy-policy.d.ts +26 -0
  3. package/dist/deploy-policy.d.ts.map +1 -0
  4. package/dist/deploy-policy.js +36 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +153 -14
  7. package/dist/managed-assets.d.ts +3 -0
  8. package/dist/managed-assets.d.ts.map +1 -0
  9. package/dist/managed-assets.js +3 -0
  10. package/dist/managed-files/ci-workflow.d.ts +215 -0
  11. package/dist/managed-files/ci-workflow.d.ts.map +1 -0
  12. package/dist/managed-files/ci-workflow.js +1082 -0
  13. package/dist/managed-files/context.d.ts +51 -0
  14. package/dist/managed-files/context.d.ts.map +1 -0
  15. package/dist/managed-files/context.js +206 -0
  16. package/dist/managed-files/files.d.ts +53 -0
  17. package/dist/managed-files/files.d.ts.map +1 -0
  18. package/dist/managed-files/files.js +248 -0
  19. package/dist/managed-files/generator.d.ts +16 -0
  20. package/dist/managed-files/generator.d.ts.map +1 -0
  21. package/dist/managed-files/generator.js +41 -0
  22. package/dist/managed-files/github-runs-on.d.ts +19 -0
  23. package/dist/managed-files/github-runs-on.d.ts.map +1 -0
  24. package/dist/managed-files/github-runs-on.js +48 -0
  25. package/dist/managed-files/managed-content.d.ts +45 -0
  26. package/dist/managed-files/managed-content.d.ts.map +1 -0
  27. package/dist/managed-files/managed-content.js +102 -0
  28. package/dist/managed-files/managed-format.d.ts +37 -0
  29. package/dist/managed-files/managed-format.d.ts.map +1 -0
  30. package/dist/managed-files/managed-format.js +94 -0
  31. package/dist/managed-files/paths.d.ts +8 -0
  32. package/dist/managed-files/paths.d.ts.map +1 -0
  33. package/dist/managed-files/paths.js +46 -0
  34. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +5 -0
  35. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -0
  36. package/dist/managed-files/pr-preview-cleanup-workflow.js +38 -0
  37. package/dist/managed-files/private-npm-policy.d.ts +7 -0
  38. package/dist/managed-files/private-npm-policy.d.ts.map +1 -0
  39. package/dist/managed-files/private-npm-policy.js +51 -0
  40. package/dist/managed-files/publish-workflow.d.ts +150 -0
  41. package/dist/managed-files/publish-workflow.d.ts.map +1 -0
  42. package/dist/managed-files/publish-workflow.js +989 -0
  43. package/dist/managed-files/tree.d.ts +30 -0
  44. package/dist/managed-files/tree.d.ts.map +1 -0
  45. package/dist/managed-files/tree.js +91 -0
  46. package/dist/platform-targets.d.ts +5 -0
  47. package/dist/platform-targets.d.ts.map +1 -0
  48. package/dist/platform-targets.js +4 -0
  49. package/dist/typecheck-test-policy.d.ts +5 -9
  50. package/dist/typecheck-test-policy.d.ts.map +1 -1
  51. package/dist/typecheck-test-policy.js +69 -99
  52. package/dist/workspace-config-policy.d.ts +2 -3
  53. package/dist/workspace-config-policy.d.ts.map +1 -1
  54. package/dist/workspace-config-policy.js +8 -3
  55. package/dist/workspace-manifest.d.ts +291 -0
  56. package/dist/workspace-manifest.d.ts.map +1 -0
  57. package/dist/workspace-manifest.js +6 -0
  58. package/dist/workspace-package-policy.d.ts +13 -0
  59. package/dist/workspace-package-policy.d.ts.map +1 -0
  60. package/dist/workspace-package-policy.js +24 -0
  61. package/generators.json +5 -0
  62. package/managed/raw/envrc +20 -0
  63. package/managed/raw/git-format-staged.yml +82 -0
  64. package/managed/raw/gitattributes +8 -0
  65. package/managed/raw/tooling/devenv +83 -0
  66. package/managed/raw/tooling/direnv/devenv.smoo.nix +422 -0
  67. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +251 -0
  68. package/managed/raw/tooling/direnv/merge-newer-pins.sh +48 -0
  69. package/managed/raw/tooling/direnv/repo-path +34 -0
  70. package/managed/raw/tooling/direnv/secret-references.ts +636 -0
  71. package/managed/raw/tooling/direnv/setup-environment.ts +585 -0
  72. package/managed/raw/tooling/direnv/toolchain-stamp.ts +88 -0
  73. package/managed/raw/tooling/git-hooks/commit-msg.sh +9 -0
  74. package/managed/raw/tooling/git-hooks/post-commit.sh +71 -0
  75. package/managed/raw/tooling/git-hooks/pre-commit.sh +18 -0
  76. package/managed/raw/tooling/git-hooks/pre-push.sh +48 -0
  77. package/managed/raw/tsconfig.lib.json +28 -0
  78. package/managed/templates/github/actions/cache-nix-devenv/action.yml +45 -0
  79. package/managed/templates/github/actions/cache-node-modules/action.yml +17 -0
  80. package/managed/templates/github/actions/cache-nx/action.yml +21 -0
  81. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +19 -0
  82. package/managed/templates/github/actions/save-nix-devenv/action.yml +32 -0
  83. package/managed/templates/github/actions/setup-devenv/action.yml +241 -0
  84. package/managed/templates/github/workflows/managed-files.yml +127 -0
  85. package/package.json +89 -5
  86. package/src/bun-bootstrap.test.ts +0 -6
  87. package/src/deploy-policy.ts +40 -0
  88. package/src/index.test.ts +225 -2
  89. package/src/index.ts +180 -14
  90. package/src/managed-assets.ts +4 -0
  91. package/src/managed-files/ci-workflow.ts +1365 -0
  92. package/src/managed-files/context.ts +240 -0
  93. package/src/managed-files/files.ts +324 -0
  94. package/src/managed-files/generator.test.ts +151 -0
  95. package/src/managed-files/generator.ts +60 -0
  96. package/src/managed-files/github-runs-on.ts +51 -0
  97. package/src/managed-files/managed-content.test.ts +147 -0
  98. package/src/managed-files/managed-content.ts +121 -0
  99. package/src/managed-files/managed-format.ts +102 -0
  100. package/src/managed-files/paths.ts +44 -0
  101. package/src/managed-files/pr-preview-cleanup-workflow.ts +44 -0
  102. package/src/managed-files/private-npm-policy.ts +62 -0
  103. package/src/managed-files/publish-workflow.ts +1523 -0
  104. package/src/managed-files/schema.json +8 -0
  105. package/src/managed-files/tree-permissions.test.ts +109 -0
  106. package/src/managed-files/tree.test.ts +123 -0
  107. package/src/managed-files/tree.ts +118 -0
  108. package/src/platform-targets.ts +4 -0
  109. package/src/typecheck-test-policy.test.ts +28 -3
  110. package/src/typecheck-test-policy.ts +46 -121
  111. package/src/workspace-config-policy.test.ts +14 -2
  112. package/src/workspace-config-policy.ts +9 -3
  113. package/src/workspace-manifest.ts +327 -0
  114. package/src/workspace-package-policy.ts +32 -0
@@ -0,0 +1,240 @@
1
+ import { cargoCrossTestArchiveFile, cargoCrossTestTripleFromArchiveTarget } from '../cross-check-policy.js';
2
+ import { type DeploymentStage, PRODUCTION_PUSH_DEPLOY_TAG, stageDeploysProject } from '../deploy-policy.js';
3
+ import { MACOS_PLATFORM_TARGET_GLOBS, PLATFORM_TARGET_GLOBS } from '../platform-targets.js';
4
+ import { ciPushBranches, type NxProjectJson, type PackageJson, type PackageSmooGithub } from '../workspace-manifest.js';
5
+ import { isPublishablePackage, repositoryInfo } from '../workspace-package-policy.js';
6
+ import type { CiCrossTestArchive } from './ci-workflow.js';
7
+ import type { ManagedFileContext } from './files.js';
8
+ import { privateNpmWorkflowConfig } from './private-npm-policy.js';
9
+
10
+ export type WorkspaceProjects = Record<string, NxProjectJson>;
11
+ interface DeployTargetInfo {
12
+ exists: boolean;
13
+ provider?: 'cloudflare';
14
+ }
15
+
16
+ /** Pure derivation from the caller's resolved Nx graph and manifest contents. */
17
+ export function deriveManagedFileContext(
18
+ manifest: PackageJson,
19
+ packages: readonly PackageJson[],
20
+ nxProjects: WorkspaceProjects,
21
+ hasBunLock: boolean,
22
+ npmrc: string,
23
+ ): ManagedFileContext {
24
+ const rootRepository = repositoryInfo(manifest);
25
+ const github = manifest?.smoo?.github;
26
+ const macosRunsOn = getMacosRunsOn(manifest);
27
+ const sourceCheckouts = github?.sourceCheckouts;
28
+ const cargoCredentials = github?.cargoCredentials;
29
+ const stagingDeploy = deployTargetInfoFromProjects(nxProjects, 'staging');
30
+ const productionDeploy = deployTargetInfoFromProjects(nxProjects, 'production');
31
+ const targetNames = Object.values(nxProjects).flatMap((project) => Object.keys(project.targets ?? {}));
32
+ const platformTargetGlobs = platformTargetGlobsForTest(targetNames);
33
+ // The release workflow produces every platform family the graph carries
34
+ // EXCEPT the ones the repository excludes. An excluded family renders no job
35
+ // at all rather than a job whose failure blocks every release; the workflow
36
+ // that does own it says so itself.
37
+ const releasePlatformTargetGlobs = releasePlatformTargetGlobsFor(
38
+ platformTargetGlobs,
39
+ github?.releasePlatformFamiliesExcluded,
40
+ );
41
+ const privateNpm = privateNpmWorkflowConfig(manifest.smoo?.privateNpm, manifest, packages, npmrc);
42
+ // Cache registry identity plus the lockfile, never token values: a scope
43
+ // URL change in the committed .npmrc must invalidate the dependency cache,
44
+ // and the cache key must stay free of secrets. Shell entry never requires
45
+ // registry configuration; only actual registry operations resolve one.
46
+ const nodeModulesCacheKey = hasBunLock
47
+ ? `$${"{{ hashFiles('.npmrc', 'bun.lock', 'package.json', 'packages/*/package.json') }}"}`
48
+ : `$${"{{ hashFiles('.npmrc', 'bun.lockb', 'package.json', 'packages/*/package.json') }}"}`;
49
+ return {
50
+ hasReleasePackages: packages.some(
51
+ (pkg) =>
52
+ !!pkg.name &&
53
+ !!pkg.version &&
54
+ isPublishablePackage({ private: pkg.private === true, tags: pkg.nx?.tags ?? [] }) &&
55
+ rootRepository !== null &&
56
+ repositoryInfo(pkg)?.url === rootRepository.url,
57
+ ),
58
+ hasStagingDeployTargets: stagingDeploy.exists,
59
+ hasProductionDeployTargets: productionDeploy.exists,
60
+ hasBrowserTestTargets: hasExactTargetForTest(targetNames, 'test-browser'),
61
+ hasE2eDeploymentTargets: hasExactTargetForTest(targetNames, 'e2e-deployment'),
62
+ hasProductionPushDeployTargets: anyProjectHasTagForTest(nxProjects, PRODUCTION_PUSH_DEPLOY_TAG),
63
+ stagingDeployProvider: stagingDeploy.provider,
64
+ productionDeployProvider: productionDeploy.provider,
65
+ ciPushBranches: ciPushBranches(github),
66
+ ciRunsOn: getCiRunsOn(github),
67
+ macosRunsOn,
68
+ platformProducer: github?.platformProducer,
69
+ actionsProvider: github?.actionsProvider,
70
+ ciEnvironments: github?.environments,
71
+ ciDeploySecrets: github?.deploySecrets ?? {},
72
+ ciE2eSecrets: github?.e2eSecrets ?? {},
73
+ nodeModulesCacheKey,
74
+ repoName: manifest.name ?? 'monorepo',
75
+ platformTargetGlobs,
76
+ releasePlatformTargetGlobs,
77
+ sourceCheckouts,
78
+ cargoCredentials,
79
+ remoteCache: manifest?.smoo?.remoteCache,
80
+ macosPlatformArchitectures: MACOS_PLATFORM_TARGET_GLOBS.some((glob) => releasePlatformTargetGlobs.includes(glob))
81
+ ? macosPlatformArchitecturesForTest(targetNames)
82
+ : [],
83
+ privateNpm,
84
+ crossTestArchives: crossTestArchivesForTest(nxProjects),
85
+ };
86
+ }
87
+
88
+ export function hasExactTargetForTest(targetNames: Iterable<string>, target: string): boolean {
89
+ return [...targetNames].includes(target);
90
+ }
91
+
92
+ /**
93
+ * Test seam: the cross-built test archives this graph can produce, read back
94
+ * out of the inferred `cargo-cross-test-archive-<triple>` targets.
95
+ *
96
+ * Derived, not configured. The triple is declared exactly once — in the cargo
97
+ * workspace's own `[workspace.metadata.smoothbricks.test] cross-targets`, where
98
+ * a Rust target triple belongs — and both halves of the workflow are rendered
99
+ * from the targets that declaration produced. A second copy in `smoo.github`
100
+ * could name a triple the graph cannot build, and CI would discover that only
101
+ * on the runner.
102
+ *
103
+ * The path is the target's own output location, so the artifact the Linux job
104
+ * uploads and the file the macOS job's runner opens are the same string
105
+ * computed once.
106
+ */
107
+ export function crossTestArchivesForTest(projects: WorkspaceProjects): CiCrossTestArchive[] {
108
+ const archives: CiCrossTestArchive[] = [];
109
+ for (const project of Object.values(projects)) {
110
+ const projectRoot = project.root;
111
+ for (const targetName of Object.keys(project.targets ?? {})) {
112
+ const triple = cargoCrossTestTripleFromArchiveTarget(targetName);
113
+ if (triple === null) {
114
+ continue;
115
+ }
116
+ const file = cargoCrossTestArchiveFile(triple);
117
+ archives.push({
118
+ triple,
119
+ path: projectRoot === undefined || projectRoot === '.' ? file : `${projectRoot}/${file}`,
120
+ });
121
+ }
122
+ }
123
+ return archives.sort((left, right) => left.triple.localeCompare(right.triple));
124
+ }
125
+
126
+ /**
127
+ * The platform families the release workflow produces: the graph's families
128
+ * minus the ones the repository excludes. Excluding a family the graph does not
129
+ * have is not an error — a repository states what it does not release, and the
130
+ * statement stays true when the target is added later.
131
+ */
132
+ export function releasePlatformTargetGlobsFor(
133
+ platformTargetGlobs: readonly string[],
134
+ excluded: readonly string[] | undefined,
135
+ ): string[] {
136
+ if (!excluded || excluded.length === 0) return [...platformTargetGlobs];
137
+ const drop = new Set(excluded);
138
+ return platformTargetGlobs.filter((glob) => !drop.has(glob));
139
+ }
140
+
141
+ export function platformTargetGlobsForTest(targetNames: Iterable<string>): string[] {
142
+ const names = [...targetNames];
143
+ return PLATFORM_TARGET_GLOBS.filter((glob) => {
144
+ const suffix = glob.startsWith('*') ? glob.slice(1) : glob;
145
+ return names.some((name) => name.endsWith(suffix));
146
+ });
147
+ }
148
+
149
+ /**
150
+ * Test seam: the architectures the macOS platform job must fan out over,
151
+ * derived from the target names themselves so a repository that ships one
152
+ * architecture renders one matrix leg. Each leg owns one architecture's
153
+ * targets, which is what lets them build on separate runners: cargo takes an
154
+ * exclusive lock on a package's whole `target/` directory, so same-runner
155
+ * platform builds serialize no matter how many workers Nx is given.
156
+ */
157
+ export function macosPlatformArchitecturesForTest(targetNames: Iterable<string>): string[] {
158
+ const families = MACOS_PLATFORM_TARGET_GLOBS.map((glob) => glob.slice(1));
159
+ const architectures = new Set<string>();
160
+ for (const name of targetNames) {
161
+ for (const family of families) {
162
+ if (!name.endsWith(family)) {
163
+ continue;
164
+ }
165
+ const architecture = name.slice(0, -family.length).split('-').pop();
166
+ if (architecture) {
167
+ architectures.add(architecture);
168
+ }
169
+ }
170
+ }
171
+ return [...architectures].sort((left, right) => left.localeCompare(right));
172
+ }
173
+
174
+ /** Test seam: what the stage flow deploys, and with which provider, from graph nodes. */
175
+ export function deployTargetInfoFromProjects(projects: WorkspaceProjects, stage: DeploymentStage): DeployTargetInfo {
176
+ let exists = false;
177
+ let provider: DeployTargetInfo['provider'];
178
+ for (const project of Object.values(projects)) {
179
+ const info = deployTargetInfoFromProject(project, stage);
180
+ if (!info.exists) {
181
+ continue;
182
+ }
183
+ exists = true;
184
+ provider ??= info.provider;
185
+ }
186
+ return { exists, provider };
187
+ }
188
+
189
+ /** Test seam: whether any project in the graph carries the Nx tag. */
190
+ export function anyProjectHasTagForTest(projects: WorkspaceProjects, tag: string): boolean {
191
+ return Object.values(projects).some((project) => project.tags?.includes(tag));
192
+ }
193
+
194
+ /**
195
+ * A generated deploy job exists for the projects CI would actually deploy, which
196
+ * is `stageDeploysProject` and nothing else — the same rule `smoo github-ci
197
+ * nx-deploy` selects by, so the job and its selection cannot disagree. Reading
198
+ * the target's presence instead would render a deploy job, with cloud
199
+ * credentials, into a repository whose only wrangler manifests document a
200
+ * Durable Object binding for a published library's consumers.
201
+ *
202
+ * The command is read for the PROVIDER only: which cloud CLI runs says which
203
+ * credentials the job needs, and says nothing about whether CI runs it.
204
+ */
205
+ function deployTargetInfoFromProject(project: NxProjectJson, stage: DeploymentStage): DeployTargetInfo {
206
+ const deploy = project.targets?.deploy;
207
+ if (!deploy || !stageDeploysProject(project.tags, stage)) {
208
+ return { exists: false };
209
+ }
210
+ const command = deploy.options?.command ?? deploy.command;
211
+ return { exists: true, provider: deployProvider(typeof command === 'string' ? command : '') };
212
+ }
213
+
214
+ function deployProvider(command: string): DeployTargetInfo['provider'] {
215
+ return command.includes('wrangler ') ? 'cloudflare' : undefined;
216
+ }
217
+
218
+ function getCiRunsOn(github: PackageSmooGithub | undefined): string | string[] {
219
+ const configured = github?.runsOn;
220
+ if (configured === undefined) {
221
+ return 'ubuntu-latest';
222
+ }
223
+ if (typeof configured === 'string') {
224
+ return configured.length > 0 ? configured : 'ubuntu-latest';
225
+ }
226
+ const labels = configured.filter((label) => label.length > 0);
227
+ return labels.length > 0 ? labels : 'ubuntu-latest';
228
+ }
229
+
230
+ function getMacosRunsOn(packageJson: PackageJson | null | undefined): string | string[] {
231
+ const configured = packageJson?.smoo?.github?.macosRunsOn;
232
+ if (configured === undefined) {
233
+ return 'macos-latest';
234
+ }
235
+ if (typeof configured === 'string') {
236
+ return configured.length > 0 ? configured : 'macos-latest';
237
+ }
238
+ const labels = configured.filter((label) => label.length > 0);
239
+ return labels.length > 0 ? labels : 'macos-latest';
240
+ }
@@ -0,0 +1,324 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { managedAssetsRoot } from '../managed-assets.js';
4
+ import type {
5
+ NonEmptyArray,
6
+ PackageCargoCredentialsConfig,
7
+ PackagePrivateNpmConfig,
8
+ PackageRemoteCacheConfig,
9
+ PackageSmooGithub,
10
+ PackageSmooGithubEnvironments,
11
+ PackageSourceCheckoutConfig,
12
+ } from '../workspace-manifest.js';
13
+ import { type CiCrossTestArchive, privateNpmReadTokenJobEnv, renderCiWorkflowYaml } from './ci-workflow.js';
14
+ import { renderRunsOnLine } from './github-runs-on.js';
15
+ import { renderPrPreviewCleanupWorkflowYaml } from './pr-preview-cleanup-workflow.js';
16
+ import { renderPublishWorkflowYaml } from './publish-workflow.js';
17
+ import type { ManagedFile } from './tree.js';
18
+
19
+ type ManagedKind = 'raw' | 'template' | 'generated';
20
+
21
+ interface ManagedFileDescriptor {
22
+ kind: ManagedKind;
23
+ source: string;
24
+ target: string;
25
+ executable?: boolean;
26
+ releasePackagesOnly?: boolean;
27
+ cloudflareDeployOnly?: boolean;
28
+ }
29
+
30
+ export interface ManagedFileContext {
31
+ hasReleasePackages: boolean;
32
+ hasStagingDeployTargets: boolean;
33
+ hasProductionDeployTargets: boolean;
34
+ hasBrowserTestTargets: boolean;
35
+ hasE2eDeploymentTargets: boolean;
36
+ hasProductionPushDeployTargets: boolean;
37
+ stagingDeployProvider?: 'cloudflare';
38
+ productionDeployProvider?: 'cloudflare';
39
+ ciPushBranches: NonEmptyArray<string>;
40
+ ciRunsOn: string | string[];
41
+ /** macOS platform job runs-on labels from the root smoo config; default macos-latest. */
42
+ macosRunsOn: string | string[];
43
+ platformProducer?: PackageSmooGithub['platformProducer'];
44
+ actionsProvider?: PackageSmooGithub['actionsProvider'];
45
+ ciEnvironments?: PackageSmooGithubEnvironments;
46
+ ciDeploySecrets: Record<string, string>;
47
+ ciE2eSecrets: Record<string, string>;
48
+ nodeModulesCacheKey: string;
49
+ repoName: string;
50
+ platformTargetGlobs: string[];
51
+ /** Platform families the release workflow produces; excluded families are absent. */
52
+ releasePlatformTargetGlobs: string[];
53
+ macosPlatformArchitectures: string[];
54
+ /** Declared private-npm opt-in from the root smoo config; absent means fully public. */
55
+ privateNpm?: PackagePrivateNpmConfig;
56
+ /** Declared sibling source checkouts from the root smoo config; absent means none. */
57
+ sourceCheckouts?: PackageSourceCheckoutConfig[];
58
+ /** Declared Cargo private-dependency credentials from the root smoo config; absent means none. */
59
+ cargoCredentials?: PackageCargoCredentialsConfig;
60
+ /** Declared self-hosted Nx remote cache from the root smoo config; absent means local caching only. */
61
+ remoteCache?: PackageRemoteCacheConfig;
62
+ /**
63
+ * Cross-built test archives the graph can produce, one per foreign target
64
+ * triple the cargo workspace declares. Empty for a repository that declares
65
+ * none, which renders a CI workflow with no cross steps and no macOS job.
66
+ */
67
+ crossTestArchives: CiCrossTestArchive[];
68
+ }
69
+
70
+ const managedFiles: ManagedFileDescriptor[] = [
71
+ {
72
+ kind: 'raw',
73
+ source: 'envrc',
74
+ target: '.envrc',
75
+ },
76
+ {
77
+ kind: 'raw',
78
+ source: 'tooling/devenv',
79
+ target: 'tooling/devenv',
80
+ executable: true,
81
+ },
82
+ {
83
+ kind: 'raw',
84
+ source: 'tooling/direnv/repo-path',
85
+ target: 'tooling/direnv/repo-path',
86
+ executable: true,
87
+ },
88
+ {
89
+ kind: 'raw',
90
+ source: 'tooling/direnv/github-actions-bootstrap.sh',
91
+ target: 'tooling/direnv/github-actions-bootstrap.sh',
92
+ executable: true,
93
+ },
94
+ {
95
+ kind: 'raw',
96
+ source: 'tooling/direnv/setup-environment.ts',
97
+ target: 'tooling/direnv/setup-environment.ts',
98
+ executable: true,
99
+ },
100
+ {
101
+ kind: 'raw',
102
+ source: 'tooling/direnv/secret-references.ts',
103
+ target: 'tooling/direnv/secret-references.ts',
104
+ },
105
+ {
106
+ kind: 'raw',
107
+ source: 'tooling/direnv/toolchain-stamp.ts',
108
+ target: 'tooling/direnv/toolchain-stamp.ts',
109
+ executable: true,
110
+ },
111
+ {
112
+ kind: 'raw',
113
+ source: 'tooling/direnv/devenv.smoo.nix',
114
+ target: 'tooling/direnv/devenv.smoo.nix',
115
+ },
116
+ {
117
+ kind: 'raw',
118
+ source: 'tooling/git-hooks/pre-commit.sh',
119
+ target: 'tooling/git-hooks/pre-commit.sh',
120
+ executable: true,
121
+ },
122
+ {
123
+ kind: 'raw',
124
+ source: 'tooling/git-hooks/post-commit.sh',
125
+ target: 'tooling/git-hooks/post-commit.sh',
126
+ executable: true,
127
+ },
128
+ {
129
+ kind: 'raw',
130
+ source: 'tooling/git-hooks/commit-msg.sh',
131
+ target: 'tooling/git-hooks/commit-msg.sh',
132
+ executable: true,
133
+ },
134
+ {
135
+ kind: 'raw',
136
+ source: 'tooling/git-hooks/pre-push.sh',
137
+ target: 'tooling/git-hooks/pre-push.sh',
138
+ executable: true,
139
+ },
140
+ {
141
+ kind: 'raw',
142
+ source: 'git-format-staged.yml',
143
+ target: '.git-format-staged.yml',
144
+ },
145
+ {
146
+ kind: 'raw',
147
+ source: 'gitattributes',
148
+ target: '.gitattributes',
149
+ },
150
+ {
151
+ kind: 'raw',
152
+ source: 'tooling/direnv/merge-newer-pins.sh',
153
+ target: 'tooling/direnv/merge-newer-pins.sh',
154
+ executable: true,
155
+ },
156
+ {
157
+ kind: 'generated',
158
+ source: 'ci-workflow',
159
+ target: '.github/workflows/ci.yml',
160
+ },
161
+ {
162
+ kind: 'generated',
163
+ source: 'pr-preview-cleanup-workflow',
164
+ target: '.github/workflows/pr-preview-cleanup.yml',
165
+ cloudflareDeployOnly: true,
166
+ },
167
+ {
168
+ kind: 'generated',
169
+ source: 'publish-workflow',
170
+ target: '.github/workflows/publish.yml',
171
+ releasePackagesOnly: true,
172
+ },
173
+ {
174
+ kind: 'template',
175
+ source: 'github/workflows/managed-files.yml',
176
+ target: '.github/workflows/managed-files.yml',
177
+ },
178
+ {
179
+ kind: 'template',
180
+ source: 'github/actions/cache-nix-devenv/action.yml',
181
+ target: '.github/actions/cache-nix-devenv/action.yml',
182
+ },
183
+ {
184
+ kind: 'template',
185
+ source: 'github/actions/setup-devenv/action.yml',
186
+ target: '.github/actions/setup-devenv/action.yml',
187
+ },
188
+ {
189
+ kind: 'template',
190
+ source: 'github/actions/save-nix-devenv/action.yml',
191
+ target: '.github/actions/save-nix-devenv/action.yml',
192
+ },
193
+ {
194
+ kind: 'template',
195
+ source: 'github/actions/cache-node-modules/action.yml',
196
+ target: '.github/actions/cache-node-modules/action.yml',
197
+ },
198
+ {
199
+ kind: 'template',
200
+ source: 'github/actions/cache-ttsc-plugins/action.yml',
201
+ target: '.github/actions/cache-ttsc-plugins/action.yml',
202
+ },
203
+ {
204
+ kind: 'template',
205
+ source: 'github/actions/cache-nx/action.yml',
206
+ target: '.github/actions/cache-nx/action.yml',
207
+ },
208
+ ];
209
+
210
+ export const managedFileTargetsForTest = managedFiles.map(({ target, executable }) => ({ target, executable }));
211
+
212
+ /**
213
+ * Whether a repository of this shape has the file at all. A repository that owns
214
+ * no packages has nothing to publish, and one the stage flow does not deploy to
215
+ * Cloudflare has no preview stage to clean up.
216
+ */
217
+ function managedFileApplies(file: ManagedFileDescriptor, context: ManagedFileContext): boolean {
218
+ if (file.releasePackagesOnly === true && !context.hasReleasePackages && !context.hasProductionDeployTargets) {
219
+ return false;
220
+ }
221
+ return file.cloudflareDeployOnly !== true || context.stagingDeployProvider === 'cloudflare';
222
+ }
223
+
224
+ /** Test seam: the managed targets a repository of this shape gets, in file order. */
225
+ export function managedFileTargetsForContext(context: ManagedFileContext): string[] {
226
+ return managedFiles.filter((file) => managedFileApplies(file, context)).map((file) => file.target);
227
+ }
228
+
229
+ /** Render all packaged templates before staging any workspace changes. */
230
+ export function renderManagedFiles(context: ManagedFileContext): ManagedFile[] {
231
+ return managedFiles.map((file) => ({
232
+ target: file.target,
233
+ content: managedFileApplies(file, context) ? getManagedContent(file, context) : null,
234
+ executable: file.executable,
235
+ }));
236
+ }
237
+
238
+ /** Renders a generated workflow through its real managed-file descriptor, so the context wiring is covered. */
239
+ export function renderManagedWorkflowForTest(
240
+ source: 'ci-workflow' | 'publish-workflow',
241
+ context: ManagedFileContext,
242
+ ): string {
243
+ const file = managedFiles.find((candidate) => candidate.kind === 'generated' && candidate.source === source);
244
+ if (!file) {
245
+ throw new Error(`${source} is no longer a managed file`);
246
+ }
247
+ return getManagedContent(file, context);
248
+ }
249
+
250
+ function getManagedContent(file: ManagedFileDescriptor, context: ManagedFileContext): string {
251
+ if (file.kind === 'generated') {
252
+ if (file.source === 'ci-workflow') {
253
+ return renderCiWorkflowYaml({
254
+ actionsProvider: context.actionsProvider,
255
+ deploy: context.hasStagingDeployTargets,
256
+ // The production job's Cloudflare env pair keys off this too: it can only select stage-derived projects,
257
+ // whose provider is computed identically for staging and production.
258
+ deployProvider: context.stagingDeployProvider,
259
+ browserTests: context.hasBrowserTestTargets,
260
+ e2eDeployment: context.hasStagingDeployTargets && context.hasE2eDeploymentTargets,
261
+ pushBranches: context.ciPushBranches,
262
+ runsOn: context.ciRunsOn,
263
+ privateNpm: context.privateNpm,
264
+ sourceCheckouts: context.sourceCheckouts,
265
+ cargoCredentials: context.cargoCredentials,
266
+ remoteCache: context.remoteCache,
267
+ environments: context.ciEnvironments,
268
+ deploySecrets: context.ciDeploySecrets,
269
+ e2eSecrets: context.ciE2eSecrets,
270
+ productionOnPush: context.hasProductionPushDeployTargets,
271
+ crossTestArchives: context.crossTestArchives,
272
+ macosRunsOn: context.macosRunsOn,
273
+ platformProducer: context.platformProducer,
274
+ });
275
+ }
276
+ if (file.source === 'publish-workflow') {
277
+ return renderPublishWorkflowYaml({
278
+ deploy: context.hasProductionDeployTargets,
279
+ // A repo reaches this file on deploy targets alone. Without owned
280
+ // packages every release step throws, so render the deploy-only shape.
281
+ release: context.hasReleasePackages,
282
+ deployProvider: context.productionDeployProvider,
283
+ repoName: context.repoName,
284
+ platformTargetGlobs: context.releasePlatformTargetGlobs,
285
+ macosPlatformArchitectures: context.macosPlatformArchitectures,
286
+ runsOn: context.ciRunsOn,
287
+ macosRunsOn: context.macosRunsOn,
288
+ platformProducer: context.platformProducer,
289
+ actionsProvider: context.actionsProvider,
290
+ privateNpm: context.privateNpm,
291
+ sourceCheckouts: context.sourceCheckouts,
292
+ cargoCredentials: context.cargoCredentials,
293
+ remoteCache: context.remoteCache,
294
+ deploySecrets: context.ciDeploySecrets,
295
+ });
296
+ }
297
+ if (file.source === 'pr-preview-cleanup-workflow') {
298
+ return renderPrPreviewCleanupWorkflowYaml({ runsOn: context.ciRunsOn });
299
+ }
300
+ throw new Error(`Unknown generated managed file source ${file.source}`);
301
+ }
302
+ const sourcePath = join(managedAssetsRoot, file.kind === 'raw' ? 'raw' : 'templates', file.source);
303
+ const content = readFileSync(sourcePath, 'utf8');
304
+ if (file.kind === 'raw') {
305
+ return content;
306
+ }
307
+ return renderTemplate(context, content);
308
+ }
309
+
310
+ function renderTemplate(context: ManagedFileContext, template: string): string {
311
+ return template
312
+ .replaceAll('{{REPO_NAME}}', context.repoName)
313
+ .replaceAll('__SMOO_CI_PUSH_BRANCHES__', renderYamlFlowList(context.ciPushBranches))
314
+ .replaceAll('__SMOO_CI_RUNS_ON__', renderRunsOnLine(context.ciRunsOn))
315
+ .replaceAll(
316
+ '__SMOO_PRIVATE_INSTALL_ENV__',
317
+ context.privateNpm ? ` env:\n${privateNpmReadTokenJobEnv(context)}` : '',
318
+ )
319
+ .replaceAll('{{NODE_MODULES_CACHE_KEY}}', context.nodeModulesCacheKey);
320
+ }
321
+
322
+ function renderYamlFlowList(values: string[]): string {
323
+ return JSON.stringify(values);
324
+ }