@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
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { existsSync } from 'node:fs';
1
+ import { existsSync, realpathSync } from 'node:fs';
2
2
  import { readdir, readFile } from 'node:fs/promises';
3
3
  import { createRequire } from 'node:module';
4
- import { dirname, join, posix, relative } from 'node:path';
4
+ import { dirname, isAbsolute, join, posix, relative, resolve } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
6
 
7
7
  import {
@@ -60,14 +60,19 @@ export const DEPLOY_BUILD_TARGET = 'deploy-build';
60
60
 
61
61
  /** The workspace-wide manifests a release rewrites: the version of every member, and the lockfile mirror of it. */
62
62
  const RELEASE_REWRITTEN_WORKSPACE_INPUTS = ['{workspaceRoot}/package.json', '{workspaceRoot}/bun.lock'];
63
- const TYPESCRIPT_TOOLCHAIN_INPUTS = [
64
- ...RELEASE_REWRITTEN_WORKSPACE_INPUTS,
65
- '{workspaceRoot}/patches/**/*',
66
- '{workspaceRoot}/tsconfig.base.json',
67
- ];
63
+ /** Installed-toolchain files a TypeScript task reads that no config can name. */
64
+ const TYPESCRIPT_PATCH_INPUTS = ['{workspaceRoot}/patches/**/*'];
65
+ /**
66
+ * The workspace's own base config, hashed for every TypeScript task whether or
67
+ * not this project's chain reaches it: it is where a workspace puts the options
68
+ * it means to apply to everything, and a project with no TypeScript config of
69
+ * its own has no chain to reach it with.
70
+ */
71
+ const WORKSPACE_TSCONFIG_BASE_INPUT = '{workspaceRoot}/tsconfig.base.json';
72
+
68
73
  /**
69
- * The same toolchain for a check, with the two release-rewritten files hashed
70
- * by field instead of by byte.
74
+ * The two release-rewritten files hashed by field instead of by byte, for a
75
+ * check that only reads them.
71
76
  *
72
77
  * A `json` input is hashed by Nx's own hasher, so this costs no process. The
73
78
  * lockfile's `workspaces` section is the mirror of each member's manifest —
@@ -75,12 +80,161 @@ const TYPESCRIPT_TOOLCHAIN_INPUTS = [
75
80
  * hashed from the member's own `package.json`. Dropping that section keeps the
76
81
  * resolution table, which is the reason the lockfile is an input at all.
77
82
  */
78
- const VERSIONLESS_TOOLCHAIN_INPUTS = [
79
- ...TYPESCRIPT_TOOLCHAIN_INPUTS.filter((input) => !RELEASE_REWRITTEN_WORKSPACE_INPUTS.includes(input)),
83
+ const VERSIONLESS_TOOLCHAIN_MANIFEST_INPUTS = [
80
84
  { json: '{workspaceRoot}/package.json', excludeFields: ['version'] },
81
85
  { json: '{workspaceRoot}/bun.lock', excludeFields: ['workspaces'] },
82
86
  ];
83
87
 
88
+ /** A path segment Nx's workspace file map never carries, so a fileset naming it hashes nothing. */
89
+ const NODE_MODULES_SEGMENT = /(?:^|\/)node_modules(?:\/|$)/;
90
+
91
+ /**
92
+ * Every config file whose compiler options decide this project's TypeScript
93
+ * verdicts: the workspace base, plus every file reachable by `extends` from the
94
+ * configs its commands hand to `tsc`.
95
+ *
96
+ * Hashing only the named config is the bug this closes. A workspace whose
97
+ * project configs extend an INTERMEDIATE base — `packages/tsconfig.base.json`
98
+ * extending the root one — hashed the root and the named file and nothing in
99
+ * between, so flipping `noUnusedLocals` in the intermediate left every
100
+ * typecheck, test typecheck and emit serving its previous answer from cache:
101
+ * a pass for sources the compiler would now reject.
102
+ *
103
+ * Fail-closed. An `extends` that resolves to no file throws and names both
104
+ * files, because the only alternative is to drop an ancestor from the hash —
105
+ * which is the silence above. The ancestors this cannot name as a fileset are
106
+ * the two kinds Nx has no fileset for; see {@link configChainInput}.
107
+ */
108
+ function typescriptConfigChainInputs(configPaths: readonly string[], workspaceRoot: string): string[] {
109
+ const inputs = new Set([WORKSPACE_TSCONFIG_BASE_INPUT]);
110
+ const walked = new Set<string>();
111
+ for (const configPath of configPaths) {
112
+ addTypescriptConfigAncestors(configPath, workspaceRoot, inputs, walked);
113
+ }
114
+ return [...inputs];
115
+ }
116
+
117
+ /**
118
+ * One config's ancestors; `walked` both dedupes shared bases and breaks an
119
+ * `extends` cycle.
120
+ *
121
+ * The walk stops at an ancestor Nx cannot hash, because everything reachable
122
+ * from an installed config is installed too: the lockfile pins that whole
123
+ * subtree, and parsing it could only invent failures for a dependency's own
124
+ * chain.
125
+ */
126
+ function addTypescriptConfigAncestors(
127
+ configPath: string,
128
+ workspaceRoot: string,
129
+ inputs: Set<string>,
130
+ walked: Set<string>,
131
+ ): void {
132
+ if (walked.has(configPath)) return;
133
+ walked.add(configPath);
134
+ for (const specifier of typescriptConfigExtends(configPath)) {
135
+ const ancestor = resolveTypescriptConfigExtends(specifier, configPath);
136
+ const input = configChainInput(ancestor, configPath, workspaceRoot);
137
+ if (input === null) continue;
138
+ inputs.add(input);
139
+ addTypescriptConfigAncestors(ancestor.path, workspaceRoot, inputs, walked);
140
+ }
141
+ }
142
+
143
+ /** What one config extends: a single specifier, or the ordered list TypeScript 5.0 added. */
144
+ function typescriptConfigExtends(configPath: string): string[] {
145
+ const config: unknown = readJsonFile(configPath, { expectComments: true });
146
+ if (!isRecord(config)) throw new Error(`${configPath} is not a TypeScript configuration object`);
147
+ const extended = config.extends;
148
+ if (extended === undefined) return [];
149
+ const specifiers: unknown[] = Array.isArray(extended) ? extended : [extended];
150
+ return specifiers.map((specifier) => {
151
+ if (typeof specifier !== 'string' || specifier.length === 0) {
152
+ throw new Error(`${configPath}: "extends" must name config files, not ${JSON.stringify(extended)}`);
153
+ }
154
+ return specifier;
155
+ });
156
+ }
157
+
158
+ /** Where one ancestor's bytes live, which is what decides whether Nx can hash them. */
159
+ interface ConfigAncestor {
160
+ path: string;
161
+ /**
162
+ * Reached through Node module resolution rather than a path, so a lockfile
163
+ * decides which bytes are there — wherever the installer chose to put them.
164
+ * A workspace member resolved this way is still workspace source; see
165
+ * {@link configChainInput}.
166
+ */
167
+ installed: boolean;
168
+ }
169
+
170
+ /**
171
+ * One `extends` specifier as an absolute path, by TypeScript's own rules: a
172
+ * relative or rooted specifier is a path — with the `.json` extension it may
173
+ * omit — and anything else is a Node specifier resolved from the extending file.
174
+ */
175
+ function resolveTypescriptConfigExtends(specifier: string, fromConfigPath: string): ConfigAncestor {
176
+ if (specifier.startsWith('./') || specifier.startsWith('../') || isAbsolute(specifier)) {
177
+ const direct = resolve(dirname(fromConfigPath), specifier);
178
+ const candidates = direct.endsWith('.json') ? [direct] : [direct, `${direct}.json`];
179
+ const found = candidates.find((candidate) => existsSync(candidate));
180
+ if (found !== undefined) return { path: found, installed: false };
181
+ throw new Error(
182
+ `${fromConfigPath}: "extends": "${specifier}" names no file (looked for ${candidates.join(', ')}). ` +
183
+ 'Every config in the chain is hashed, so one that cannot be found cannot be left out of the hash either.',
184
+ );
185
+ }
186
+ // A package config may be named by file or bare; TypeScript accepts both.
187
+ const candidates = specifier.endsWith('.json') ? [specifier] : [specifier, `${specifier}/tsconfig.json`];
188
+ const nodeRequire = createRequire(fromConfigPath);
189
+ for (const candidate of candidates) {
190
+ try {
191
+ return { path: nodeRequire.resolve(candidate), installed: true };
192
+ } catch {
193
+ // Not this spelling of the package config; try the next, then refuse.
194
+ }
195
+ }
196
+ throw new Error(
197
+ `${fromConfigPath}: "extends": "${specifier}" resolves to no installed config (tried ${candidates.join(', ')}). ` +
198
+ 'Every config in the chain is hashed, so one that cannot be found cannot be left out of the hash either.',
199
+ );
200
+ }
201
+
202
+ /**
203
+ * The fileset name for one ancestor, or `null` when Nx cannot hash that file.
204
+ *
205
+ * Nx hashes workspace files that are not in node_modules, so two ancestor
206
+ * kinds cannot be named — and they are not the same thing:
207
+ *
208
+ * - a dependency's config: `bun.lock` and the root manifest decide which
209
+ * bytes are installed, and both are already inputs. Where they sit is the
210
+ * installer's business — bun keeps them in a global store outside the
211
+ * workspace entirely — so this is settled by HOW the specifier resolved,
212
+ * not by where it landed. A workspace member reached through its package
213
+ * name is workspace source, and is hashed as such.
214
+ * - a path above the workspace root: nothing hashes it and nothing pins it,
215
+ * so an edit there really can serve a stale verdict. Said out loud,
216
+ * because the silent version of exactly that is the bug this walk closes.
217
+ */
218
+ function configChainInput(ancestor: ConfigAncestor, fromConfigPath: string, workspaceRoot: string): string | null {
219
+ let relativePath = relative(workspaceRoot, ancestor.path).replaceAll('\\', '/');
220
+ if (relativePath.startsWith('../')) {
221
+ // Node resolves a package to its REAL path, and a workspace root can itself
222
+ // be reached through a symlink (macOS `/var` → `/private/var`): a workspace
223
+ // member then looks like a file above the root. Believe the real root
224
+ // before concluding anything is outside.
225
+ relativePath = relative(realpathSync(workspaceRoot), ancestor.path).replaceAll('\\', '/');
226
+ }
227
+ const outsideWorkspace = relativePath.length === 0 || relativePath.startsWith('../') || isAbsolute(relativePath);
228
+ if (!outsideWorkspace && !NODE_MODULES_SEGMENT.test(relativePath)) return `{workspaceRoot}/${relativePath}`;
229
+ if (!ancestor.installed) {
230
+ process.stderr.write(
231
+ `@smoothbricks/nx-plugin: ${fromConfigPath} extends ${ancestor.path}, which is outside the workspace. ` +
232
+ 'Nx hashes workspace files only, so a change to it will not invalidate this project\u2019s TypeScript tasks.\n',
233
+ );
234
+ }
235
+ return null;
236
+ }
237
+
84
238
  /**
85
239
  * A project's own manifest, hashed without the version a release rewrites.
86
240
  *
@@ -829,6 +983,8 @@ async function createProjectTargets(
829
983
  const validationTargets: string[] = [];
830
984
  const libTsconfigPath = join(absoluteProjectRoot, 'tsconfig.lib.json');
831
985
  const hasLibTsconfig = existsSync(libTsconfigPath);
986
+ const testTsconfigPath = join(absoluteProjectRoot, 'tsconfig.test.json');
987
+ const hasTestTsconfig = existsSync(testTsconfigPath);
832
988
  const cargoTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
833
989
  const isCargoWorkspace =
834
990
  existsSync(cargoTomlPath) && CARGO_WORKSPACE_PATTERN.test(await readFile(cargoTomlPath, 'utf-8'));
@@ -849,7 +1005,18 @@ async function createProjectTargets(
849
1005
  const crateInputs =
850
1006
  versionless !== null && hasCrateManifests ? await versionlessCrateInputs(projectRoot, workspaceRoot) : [];
851
1007
  const versionlessProjectInputs = versionless === null ? [] : [...VERSIONLESS_PACKAGE_INPUTS, ...crateInputs];
852
- const checkInputs = versionless === null ? TYPESCRIPT_TOOLCHAIN_INPUTS : VERSIONLESS_TOOLCHAIN_INPUTS;
1008
+ // The options a TypeScript task's answer really depends on, which is the
1009
+ // whole `extends` chain of the configs its commands name and not just those
1010
+ // two files. Resolved once per project: every target below shares it.
1011
+ const typescriptConfigChain = typescriptConfigChainInputs(
1012
+ [...(hasLibTsconfig ? [libTsconfigPath] : []), ...(hasTestTsconfig ? [testTsconfigPath] : [])],
1013
+ workspaceRoot,
1014
+ );
1015
+ const toolchainInputs = [...RELEASE_REWRITTEN_WORKSPACE_INPUTS, ...TYPESCRIPT_PATCH_INPUTS, ...typescriptConfigChain];
1016
+ const checkInputs =
1017
+ versionless === null
1018
+ ? toolchainInputs
1019
+ : [...TYPESCRIPT_PATCH_INPUTS, ...typescriptConfigChain, ...VERSIONLESS_TOOLCHAIN_MANIFEST_INPUTS];
853
1020
  const selfProduction = versionless === null ? 'production' : VERSIONLESS_PRODUCTION_INPUT;
854
1021
  const selfDefault = versionless === null ? 'default' : VERSIONLESS_DEFAULT_INPUT;
855
1022
  const dependencyProduction =
@@ -891,7 +1058,7 @@ async function createProjectTargets(
891
1058
  targets['tsc-js'] = {
892
1059
  executor: '@smoothbricks/nx-plugin:typescript-emit',
893
1060
  cache: true,
894
- inputs: ['production', '^production', ...TYPESCRIPT_TOOLCHAIN_INPUTS, '{projectRoot}/tsconfig.lib.json'],
1061
+ inputs: ['production', '^production', ...toolchainInputs, '{projectRoot}/tsconfig.lib.json'],
895
1062
  outputs: inferTypescriptOutputs(libTsconfigPath, packageJsonPath),
896
1063
  dependsOn: ['^*-js', ...(cargoWasmConfig ? ['cargo-wasm'] : [])],
897
1064
  options: {
@@ -913,7 +1080,6 @@ async function createProjectTargets(
913
1080
  };
914
1081
  }
915
1082
 
916
- const hasTestTsconfig = existsSync(join(absoluteProjectRoot, 'tsconfig.test.json'));
917
1083
  if (hasTestTsconfig) {
918
1084
  targets['typecheck-tests'] = {
919
1085
  executor: 'nx:run-commands',
@@ -0,0 +1,4 @@
1
+ import { fileURLToPath } from 'node:url';
2
+
3
+ /** Packaged assets, independent of the consumer's cwd and node_modules layout. */
4
+ export const managedAssetsRoot = fileURLToPath(new URL('../managed/', import.meta.url));