@adviser/cement 0.4.50 → 0.4.52-pre

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 (152) hide show
  1. package/cjs/cli/generate-version-ts-cmd.cjs +79 -0
  2. package/cjs/cli/generate-version-ts-cmd.cjs.map +1 -0
  3. package/cjs/cli/generate-version-ts-cmd.d.ts +3 -0
  4. package/cjs/cli/generate-version-ts-cmd.d.ts.map +1 -0
  5. package/cjs/cli/main.cjs +8 -3
  6. package/cjs/cli/main.cjs.map +1 -1
  7. package/cjs/cli/patch-version-cmd.cjs +3 -226
  8. package/cjs/cli/patch-version-cmd.cjs.map +1 -1
  9. package/cjs/cli/patch-version-cmd.d.ts +0 -4
  10. package/cjs/cli/patch-version-cmd.d.ts.map +1 -1
  11. package/cjs/cli/prepare-pubdir-cmd.cjs +150 -0
  12. package/cjs/cli/prepare-pubdir-cmd.cjs.map +1 -0
  13. package/cjs/cli/prepare-pubdir-cmd.d.ts +4 -0
  14. package/cjs/cli/prepare-pubdir-cmd.d.ts.map +1 -0
  15. package/cjs/cli/publish-tags-cmd.cjs +94 -0
  16. package/cjs/cli/publish-tags-cmd.cjs.map +1 -0
  17. package/cjs/cli/publish-tags-cmd.d.ts +3 -0
  18. package/cjs/cli/publish-tags-cmd.d.ts.map +1 -0
  19. package/cjs/cli/setup-deno-json-cmd.cjs +77 -0
  20. package/cjs/cli/setup-deno-json-cmd.cjs.map +1 -0
  21. package/cjs/cli/setup-deno-json-cmd.d.ts +3 -0
  22. package/cjs/cli/setup-deno-json-cmd.d.ts.map +1 -0
  23. package/cjs/cli/utils.cjs +93 -0
  24. package/cjs/cli/utils.cjs.map +1 -0
  25. package/cjs/cli/utils.d.ts +6 -0
  26. package/cjs/cli/utils.d.ts.map +1 -0
  27. package/cjs/http_header.cjs +27 -19
  28. package/cjs/http_header.cjs.map +1 -1
  29. package/cjs/http_header.d.ts +4 -1
  30. package/cjs/http_header.d.ts.map +1 -1
  31. package/cjs/http_header.test.cjs +32 -0
  32. package/cjs/http_header.test.cjs.map +1 -1
  33. package/cjs/version.cjs +1 -1
  34. package/cjs/version.cjs.map +1 -1
  35. package/cjs/version.d.ts.map +1 -1
  36. package/deno.json +1 -1
  37. package/esm/cli/generate-version-ts-cmd.d.ts +3 -0
  38. package/esm/cli/generate-version-ts-cmd.d.ts.map +1 -0
  39. package/esm/cli/generate-version-ts-cmd.js +43 -0
  40. package/esm/cli/generate-version-ts-cmd.js.map +1 -0
  41. package/esm/cli/main.js +6 -1
  42. package/esm/cli/main.js.map +1 -1
  43. package/esm/cli/patch-version-cmd.d.ts +0 -4
  44. package/esm/cli/patch-version-cmd.d.ts.map +1 -1
  45. package/esm/cli/patch-version-cmd.js +1 -220
  46. package/esm/cli/patch-version-cmd.js.map +1 -1
  47. package/esm/cli/prepare-pubdir-cmd.d.ts +4 -0
  48. package/esm/cli/prepare-pubdir-cmd.d.ts.map +1 -0
  49. package/esm/cli/prepare-pubdir-cmd.js +113 -0
  50. package/esm/cli/prepare-pubdir-cmd.js.map +1 -0
  51. package/esm/cli/publish-tags-cmd.d.ts +3 -0
  52. package/esm/cli/publish-tags-cmd.d.ts.map +1 -0
  53. package/esm/cli/publish-tags-cmd.js +58 -0
  54. package/esm/cli/publish-tags-cmd.js.map +1 -0
  55. package/esm/cli/setup-deno-json-cmd.d.ts +3 -0
  56. package/esm/cli/setup-deno-json-cmd.d.ts.map +1 -0
  57. package/esm/cli/setup-deno-json-cmd.js +41 -0
  58. package/esm/cli/setup-deno-json-cmd.js.map +1 -0
  59. package/esm/cli/utils.d.ts +6 -0
  60. package/esm/cli/utils.d.ts.map +1 -0
  61. package/esm/cli/utils.js +54 -0
  62. package/esm/cli/utils.js.map +1 -0
  63. package/esm/http_header.d.ts +4 -1
  64. package/esm/http_header.d.ts.map +1 -1
  65. package/esm/http_header.js +27 -19
  66. package/esm/http_header.js.map +1 -1
  67. package/esm/http_header.test.js +32 -0
  68. package/esm/http_header.test.js.map +1 -1
  69. package/esm/version.d.ts.map +1 -1
  70. package/esm/version.js +1 -1
  71. package/esm/version.js.map +1 -1
  72. package/package.json +42 -25
  73. package/src/cli/generate-version-ts-cmd.ts +43 -0
  74. package/src/cli/main.ts +6 -1
  75. package/src/cli/patch-version-cmd.ts +1 -289
  76. package/src/cli/prepare-pubdir-cmd.ts +156 -0
  77. package/src/cli/publish-tags-cmd.ts +69 -0
  78. package/src/cli/run.sh +10 -1
  79. package/src/cli/setup-deno-json-cmd.ts +51 -0
  80. package/src/cli/utils.ts +70 -0
  81. package/src/http_header.ts +34 -23
  82. package/ts/cjs/cli/generate-version-ts-cmd.d.ts +3 -0
  83. package/ts/cjs/cli/generate-version-ts-cmd.d.ts.map +1 -0
  84. package/ts/cjs/cli/generate-version-ts-cmd.js +79 -0
  85. package/ts/cjs/cli/generate-version-ts-cmd.js.map +1 -0
  86. package/ts/cjs/cli/main.js +8 -3
  87. package/ts/cjs/cli/main.js.map +1 -1
  88. package/ts/cjs/cli/patch-version-cmd.d.ts +0 -4
  89. package/ts/cjs/cli/patch-version-cmd.d.ts.map +1 -1
  90. package/ts/cjs/cli/patch-version-cmd.js +3 -226
  91. package/ts/cjs/cli/patch-version-cmd.js.map +1 -1
  92. package/ts/cjs/cli/prepare-pubdir-cmd.d.ts +4 -0
  93. package/ts/cjs/cli/prepare-pubdir-cmd.d.ts.map +1 -0
  94. package/ts/cjs/cli/prepare-pubdir-cmd.js +150 -0
  95. package/ts/cjs/cli/prepare-pubdir-cmd.js.map +1 -0
  96. package/ts/cjs/cli/publish-tags-cmd.d.ts +3 -0
  97. package/ts/cjs/cli/publish-tags-cmd.d.ts.map +1 -0
  98. package/ts/cjs/cli/publish-tags-cmd.js +94 -0
  99. package/ts/cjs/cli/publish-tags-cmd.js.map +1 -0
  100. package/ts/cjs/cli/setup-deno-json-cmd.d.ts +3 -0
  101. package/ts/cjs/cli/setup-deno-json-cmd.d.ts.map +1 -0
  102. package/ts/cjs/cli/setup-deno-json-cmd.js +77 -0
  103. package/ts/cjs/cli/setup-deno-json-cmd.js.map +1 -0
  104. package/ts/cjs/cli/utils.d.ts +6 -0
  105. package/ts/cjs/cli/utils.d.ts.map +1 -0
  106. package/ts/cjs/cli/utils.js +93 -0
  107. package/ts/cjs/cli/utils.js.map +1 -0
  108. package/ts/cjs/http_header.d.ts +4 -1
  109. package/ts/cjs/http_header.d.ts.map +1 -1
  110. package/ts/cjs/http_header.js +27 -19
  111. package/ts/cjs/http_header.js.map +1 -1
  112. package/ts/cjs/http_header.test.js +32 -0
  113. package/ts/cjs/http_header.test.js.map +1 -1
  114. package/ts/cjs/version.d.ts.map +1 -1
  115. package/ts/cjs/version.js +1 -1
  116. package/ts/cjs/version.js.map +1 -1
  117. package/ts/esm/cli/generate-version-ts-cmd.d.ts +3 -0
  118. package/ts/esm/cli/generate-version-ts-cmd.d.ts.map +1 -0
  119. package/ts/esm/cli/generate-version-ts-cmd.js +43 -0
  120. package/ts/esm/cli/generate-version-ts-cmd.js.map +1 -0
  121. package/ts/esm/cli/main.js +6 -1
  122. package/ts/esm/cli/main.js.map +1 -1
  123. package/ts/esm/cli/patch-version-cmd.d.ts +0 -4
  124. package/ts/esm/cli/patch-version-cmd.d.ts.map +1 -1
  125. package/ts/esm/cli/patch-version-cmd.js +1 -220
  126. package/ts/esm/cli/patch-version-cmd.js.map +1 -1
  127. package/ts/esm/cli/prepare-pubdir-cmd.d.ts +4 -0
  128. package/ts/esm/cli/prepare-pubdir-cmd.d.ts.map +1 -0
  129. package/ts/esm/cli/prepare-pubdir-cmd.js +113 -0
  130. package/ts/esm/cli/prepare-pubdir-cmd.js.map +1 -0
  131. package/ts/esm/cli/publish-tags-cmd.d.ts +3 -0
  132. package/ts/esm/cli/publish-tags-cmd.d.ts.map +1 -0
  133. package/ts/esm/cli/publish-tags-cmd.js +58 -0
  134. package/ts/esm/cli/publish-tags-cmd.js.map +1 -0
  135. package/ts/esm/cli/setup-deno-json-cmd.d.ts +3 -0
  136. package/ts/esm/cli/setup-deno-json-cmd.d.ts.map +1 -0
  137. package/ts/esm/cli/setup-deno-json-cmd.js +41 -0
  138. package/ts/esm/cli/setup-deno-json-cmd.js.map +1 -0
  139. package/ts/esm/cli/utils.d.ts +6 -0
  140. package/ts/esm/cli/utils.d.ts.map +1 -0
  141. package/ts/esm/cli/utils.js +54 -0
  142. package/ts/esm/cli/utils.js.map +1 -0
  143. package/ts/esm/http_header.d.ts +4 -1
  144. package/ts/esm/http_header.d.ts.map +1 -1
  145. package/ts/esm/http_header.js +27 -19
  146. package/ts/esm/http_header.js.map +1 -1
  147. package/ts/esm/http_header.test.js +32 -0
  148. package/ts/esm/http_header.test.js.map +1 -1
  149. package/ts/esm/version.d.ts.map +1 -1
  150. package/ts/esm/version.js +1 -1
  151. package/ts/esm/version.js.map +1 -1
  152. package/tsconfig.json +25 -0
@@ -1,117 +1,7 @@
1
- import * as ts from "typescript";
2
1
  import { command, option, restPositionals, string } from "cmd-ts";
3
- import * as path from "node:path";
4
2
  import * as fs from "node:fs";
5
3
  import * as process from "node:process";
6
- import { $, cd, glob } from "zx";
7
-
8
- // Custom compiler host
9
- function createCompilerHost(options: ts.CompilerOptions, version: string): ts.CompilerHost {
10
- const host = ts.createCompilerHost(options);
11
- // eslint-disable-next-line @typescript-eslint/unbound-method
12
- const myGetSourceFile = host.getSourceFile;
13
-
14
- host.getSourceFile = (fileName, languageVersion, onError, shouldCreateNewSourceFile): ts.SourceFile => {
15
- const sourceFile = myGetSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile);
16
- if (!sourceFile) {
17
- throw new Error("getSourceFile is not defined");
18
- }
19
-
20
- // Patch version.ts during compilation
21
- if (fileName.endsWith("version.ts") && sourceFile) {
22
- const newText = `export const VERSION: string = "${version}";`;
23
- return ts.createSourceFile(fileName, newText, languageVersion);
24
- }
25
-
26
- return sourceFile;
27
- };
28
- return host;
29
- }
30
-
31
- function readTSConfig(configPath = "./tsconfig.json"): ts.CompilerOptions {
32
- // Read the config file
33
- // eslint-disable-next-line @typescript-eslint/unbound-method
34
- const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
35
-
36
- if (configFile.error) {
37
- throw new Error(`Error reading tsconfig: ${configFile.error.messageText as string}`);
38
- }
39
-
40
- // Parse and resolve the config
41
- const parsedConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath), undefined, configPath);
42
-
43
- if (parsedConfig.errors.length > 0) {
44
- const errors = parsedConfig.errors.map((err) => err.messageText as string).join("\n");
45
- throw new Error(`Error parsing tsconfig: ${errors}`);
46
- }
47
-
48
- return parsedConfig.options; // This is CompilerOptions
49
- }
50
-
51
- function versionFromPackageJson(): string {
52
- const packageJson = JSON.parse(fs.readFileSync("package.json").toString()) as { version: string };
53
- return packageJson.version;
54
- }
55
-
56
- const versionArgs = {
57
- version: option({
58
- long: "version",
59
- short: "v",
60
- defaultValue: () => getVersion(),
61
- defaultValueIsSerializable: true,
62
- type: string,
63
- description: "Version to patch in, defaults to reading from package.json.",
64
- }),
65
- versionFile: option({
66
- long: "versionFile",
67
- short: "f",
68
- defaultValue: () => "src/version.ts",
69
- defaultValueIsSerializable: true,
70
- type: string,
71
- description: "Path to the file containing the version, defaults to './src/version.ts'.",
72
- }),
73
-
74
- tsconfig: option({
75
- long: "tsconfig",
76
- short: "t",
77
- defaultValue: () => "tsconfig.json",
78
- defaultValueIsSerializable: true,
79
- type: string,
80
- description: "Path to the tsconfig.json file, defaults to './tsconfig.json'.",
81
- }),
82
- };
83
-
84
- export function generateVersionTsCmd(): ReturnType<typeof command> {
85
- return command({
86
- name: "fireproof build cli",
87
- description: "helps to build fp",
88
- version: "1.0.0",
89
- args: versionArgs,
90
- handler: (args) => {
91
- const options = readTSConfig(args.tsconfig);
92
- const host = createCompilerHost(options, args.version);
93
- const program = ts.createProgram([args.versionFile], options, host);
94
- program.emit();
95
- },
96
- }) as ReturnType<typeof command>;
97
- }
98
-
99
- function getVersion(iversion?: string): string {
100
- const ghref = iversion || process.env.GITHUB_REF || versionFromPackageJson() || "a/v0.0.0-smoke";
101
- let lastPart = ghref.split("/").slice(-1)[0];
102
- if (iversion) {
103
- return iversion.replace(/^[vdsp]/, "");
104
- }
105
- const short = $.sync`git rev-parse --short HEAD`.stdout.trim();
106
- if (process.env.GITHUB_REF) {
107
- lastPart = lastPart.replace(/^[vdsp]/, "");
108
- if (lastPart.match(/^\d+\.\d+\.\d+/)) {
109
- return lastPart;
110
- }
111
- return `0.0.0-dev-ci-${short}`;
112
- }
113
- return `0.0.0-dev-local-${Date.now()}`;
114
- }
4
+ import { getVersion } from "./utils.js";
115
5
 
116
6
  export function patchVersionCmd(): ReturnType<typeof command> {
117
7
  return command({
@@ -145,181 +35,3 @@ export function patchVersionCmd(): ReturnType<typeof command> {
145
35
  },
146
36
  }) as ReturnType<typeof command>;
147
37
  }
148
-
149
- // node ./setup-jsr-json.cjs ./pubdir/deno.json
150
- function setupDenoJson(packageJsonFile: string, jsrJsonFile: string): void {
151
- const packageJson = JSON.parse(fs.readFileSync(packageJsonFile).toString()) as { dependencies: Record<string, string> };
152
- const jsrJson = JSON.parse(fs.readFileSync(jsrJsonFile).toString()) as {
153
- name: string;
154
- exports: Record<string, string>;
155
- imports: Record<string, string>;
156
- };
157
- // self imports
158
- const jsrJsonImports = Object.fromEntries(Object.entries(jsrJson.exports ?? {}).map(([k, v]) => [path.join(jsrJson.name, k), v]));
159
- const nodeJsonImports = Object.fromEntries(
160
- Array.from(Object.entries(packageJson.dependencies ?? {})).map(([k, v]) => [k, `npm:${k}@${v.replace(/^npm:/, "")}`]),
161
- );
162
- jsrJson.imports = { ...jsrJson.imports, ...jsrJsonImports, ...nodeJsonImports };
163
- fs.writeFileSync(jsrJsonFile, JSON.stringify(jsrJson, undefined, 2) + "\n");
164
- }
165
-
166
- export function setUpDenoJsonCmd(): ReturnType<typeof command> {
167
- return command({
168
- name: "setup-deno-json",
169
- description: "setup deno.json",
170
- version: "1.0.0",
171
- args: {
172
- packageJson: option({
173
- long: "packageJson",
174
- short: "p",
175
- defaultValue: () => "package.json",
176
- defaultValueIsSerializable: true,
177
- type: string,
178
- description: "Path to the package.json file, defaults to './package.json'.",
179
- }),
180
- jsrJson: option({
181
- long: "jsrJson",
182
- short: "j",
183
- defaultValue: () => "deno.json",
184
- defaultValueIsSerializable: true,
185
- type: string,
186
- description: "Path to the deno.json file, defaults to './deno.json'.",
187
- }),
188
- },
189
- handler: (args) => {
190
- // eslint-disable-next-line no-console
191
- console.log(`Setup deno.json ${args.jsrJson} from ${args.packageJson}`);
192
- setupDenoJson(args.packageJson, args.jsrJson);
193
- },
194
- }) as ReturnType<typeof command>;
195
- }
196
-
197
- export async function preparePubdir(pubdir: string, version: string, baseDir: string, srcDir: string): Promise<void> {
198
- // Set shell options equivalent to 'set -ex'
199
- $.verbose = true;
200
-
201
- // Build if not in CI
202
- if (process.env.IN_CI !== "in_ci") {
203
- await $`pnpm run build`;
204
- }
205
-
206
- // Clean and create pubdir
207
- await $`rm -rf ${pubdir}`;
208
- await $`mkdir -p ${pubdir}`;
209
-
210
- // Copy files to pubdir
211
- await $`cp -pr ${path.join(baseDir, ".gitignore")} ${path.join(baseDir, "README.md")} ${path.join(baseDir, "LICENSE")} ./dist/ts/ ${pubdir}/`;
212
-
213
- // Copy from dist/pkg
214
- cd("dist/pkg");
215
- await $`cp -pr . ../../${pubdir}/`;
216
- cd("../..");
217
-
218
- // Copy from src
219
- cd(srcDir);
220
- await $`cp -pr . ../${pubdir}/${srcDir}/`;
221
- cd("..");
222
-
223
- // Rename .js files to .cjs in pubdir/cjs
224
- const jsFiles = await glob(`${pubdir}/cjs/**/*.js`);
225
- for (const file of jsFiles) {
226
- const newFile = file.replace(/\.js$/, ".cjs");
227
- await $`mv ${file} ${newFile}`;
228
- }
229
-
230
- // Rename .js.map files to .cjs.map in pubdir/cjs
231
- const mapFiles = await glob(`${pubdir}/cjs/**/*.js.map`);
232
- for (const file of mapFiles) {
233
- const newFile = file.replace(/\.js\.map$/, ".cjs.map");
234
- await $`mv ${file} ${newFile}`;
235
- }
236
-
237
- // Run jscodeshift on .cjs files
238
- const cjsFiles = await glob(`${pubdir}/cjs/**/*.cjs`);
239
- if (cjsFiles.length > 0) {
240
- await $`pnpm exec jscodeshift --parser=babel -t=./to-cjs.js ${cjsFiles}`;
241
- }
242
-
243
- // Copy package.json
244
- await $`cp package.json ${pubdir}/`;
245
-
246
- // Clean up test files in pubdir/src
247
- cd(`${pubdir}/${srcDir}`);
248
- await $`rm -f test/test-exit-handler.* ./utils/stream-test-helper.ts`.catch(() => {
249
- // Ignore errors if files don't exist
250
- });
251
- cd("../..");
252
-
253
- // Remove __screenshots__ directories
254
- const screenshotDirs = await glob(`${pubdir}/${srcDir}/**/__screenshots__`);
255
- for (const dir of screenshotDirs) {
256
- await $`rm -rf ${dir}`;
257
- }
258
-
259
- // Remove test files
260
- const testFiles = await glob(`${pubdir}/${srcDir}/**/*.test.ts`);
261
- for (const file of testFiles) {
262
- await $`rm -f ${file}`;
263
- }
264
-
265
- // Copy deno.json
266
- await $`cp ./deno.json ./${pubdir}/`;
267
-
268
- // Patch version in package.json and deno.json
269
- await patchVersionCmd().handler({ version, files: [`${pubdir}/package.json`, `${pubdir}/deno.json`] });
270
- // await $`sh src/cli/run.sh patchVersion ./pubdir/package.json ./pubdir/deno.json`;
271
-
272
- // Setup JSR JSON
273
- await setUpDenoJsonCmd().handler({ packageJson: `${pubdir}/package.json`, jsrJson: `${pubdir}/deno.json` });
274
- // await $`node ./setup-jsr-json.cjs ./pubdir/deno.json`;
275
-
276
- // Pack and publish
277
- cd("pubdir");
278
- await $`pnpm pack`;
279
- await $`deno publish --dry-run --unstable-sloppy-imports --allow-dirty`;
280
- }
281
-
282
- export function preparePubdirCmd(): ReturnType<typeof command> {
283
- return command({
284
- name: "prepare-pubdir",
285
- description: "prepare pubdir",
286
- version: "1.0.0",
287
- args: {
288
- pubdir: option({
289
- long: "pubdir",
290
- short: "p",
291
- defaultValue: () => "pubdir",
292
- defaultValueIsSerializable: true,
293
- type: string,
294
- description: "Path to the pubdir, defaults to './pubdir'.",
295
- }),
296
- srcDir: option({
297
- long: "srcDir",
298
- short: "s",
299
- defaultValue: () => "src",
300
- defaultValueIsSerializable: true,
301
- type: string,
302
- description: "Path to the src directory, defaults to './src'.",
303
- }),
304
- baseDir: option({
305
- long: "baseDir",
306
- short: "b",
307
- defaultValue: () => "../",
308
- defaultValueIsSerializable: true,
309
- type: string,
310
- description: "Path to the base directory of the project, defaults to '../'.",
311
- }),
312
- version: option({
313
- long: "version",
314
- short: "v",
315
- defaultValue: () => getVersion(),
316
- defaultValueIsSerializable: true,
317
- type: string,
318
- description: "Version to patch in, defaults to reading from package.json.",
319
- }),
320
- },
321
- handler: async (args) => {
322
- await preparePubdir(args.pubdir, args.version, args.baseDir, args.srcDir);
323
- },
324
- }) as ReturnType<typeof command>;
325
- }
@@ -0,0 +1,156 @@
1
+ import { command, option, string } from "cmd-ts";
2
+ import * as path from "node:path";
3
+ import * as process from "node:process";
4
+ import { $, cd, glob } from "zx";
5
+ import { getVersion } from "./utils.js";
6
+ import { setUpDenoJsonCmd } from "./setup-deno-json-cmd.js";
7
+ import { patchVersionCmd } from "./patch-version-cmd.js";
8
+
9
+ function disableVerbose(fn: () => Promise<void>): Promise<void> {
10
+ const verbose = $.verbose;
11
+ $.verbose = false;
12
+ return fn().finally(() => {
13
+ $.verbose = verbose;
14
+ });
15
+ }
16
+
17
+ export async function preparePubdir(pubdir: string, version: string, baseDir: string, srcDir: string): Promise<void> {
18
+ // Set shell options equivalent to 'set -ex'
19
+ $.verbose = true;
20
+
21
+ // Build if not in CI
22
+ if (process.env.IN_CI !== "in_ci") {
23
+ await $`pnpm run build`;
24
+ }
25
+
26
+ // Clean and create pubdir
27
+ await $`rm -rf ${pubdir}`;
28
+ await $`mkdir -p ${pubdir}`;
29
+
30
+ // Copy files to pubdir
31
+ await $`cp -pr ${path.join(baseDir, ".gitignore")} ${path.join(baseDir, "README.md")} ${path.join(baseDir, "LICENSE")} ./dist/ts/ ${pubdir}/`;
32
+
33
+ // Copy from dist/pkg
34
+ cd("dist/pkg");
35
+ await $`cp -pr . ../../${pubdir}/`;
36
+ cd("../..");
37
+
38
+ // Copy from src
39
+ cd(srcDir);
40
+ await $`cp -pr . ../${pubdir}/${srcDir}/`;
41
+ cd("..");
42
+
43
+ // Rename .js files to .cjs in pubdir/cjs
44
+ const jsFiles = await glob(`${pubdir}/cjs/**/*.js`);
45
+
46
+ await disableVerbose(async () => {
47
+ for (const file of jsFiles) {
48
+ const newFile = file.replace(/\.js$/, ".cjs");
49
+ await $`mv ${file} ${newFile}`;
50
+ }
51
+
52
+ // Rename .js.map files to .cjs.map in pubdir/cjs
53
+ const mapFiles = await glob(`${pubdir}/cjs/**/*.js.map`);
54
+ for (const file of mapFiles) {
55
+ const newFile = file.replace(/\.js\.map$/, ".cjs.map");
56
+ await $`mv ${file} ${newFile}`;
57
+ }
58
+ });
59
+
60
+ // Run jscodeshift on .cjs files
61
+ const cjsFiles = await glob(`${pubdir}/cjs/**/*.cjs`);
62
+ if (cjsFiles.length > 0) {
63
+ await $`pnpm exec jscodeshift -s --parser=babel -t=./to-cjs.js ${cjsFiles}`;
64
+ }
65
+
66
+ // Copy package.json
67
+ await $`cp package.json ${pubdir}/`;
68
+
69
+ // Clean up test files in pubdir/src
70
+ cd(`${pubdir}/${srcDir}`);
71
+ await $`rm -f test/test-exit-handler.* ./utils/stream-test-helper.ts`.catch(() => {
72
+ // Ignore errors if files don't exist
73
+ });
74
+ cd("../..");
75
+
76
+ // Remove __screenshots__ directories
77
+ const screenshotDirs = await glob(`${pubdir}/${srcDir}/**/__screenshots__`);
78
+ for (const dir of screenshotDirs) {
79
+ await $`rm -rf ${dir}`;
80
+ }
81
+
82
+ // Remove test files
83
+ await disableVerbose(async () => {
84
+ const testFiles = await glob(`${pubdir}/${srcDir}/**/*.test.ts`);
85
+ for (const file of testFiles) {
86
+ await $`rm -f ${file}`;
87
+ }
88
+ });
89
+
90
+ // Copy tsconfig.json
91
+ await $`cp ./tsconfig.json ./${pubdir}/`;
92
+
93
+ // Copy deno.json
94
+ await $`cp ./deno.json ./${pubdir}/`;
95
+
96
+ // Patch version in package.json and deno.json
97
+ await patchVersionCmd().handler({ version, files: [`${pubdir}/package.json`, `${pubdir}/deno.json`] });
98
+ // await $`sh src/cli/run.sh patchVersion ./pubdir/package.json ./pubdir/deno.json`;
99
+
100
+ // Setup JSR JSON
101
+ await setUpDenoJsonCmd().handler({ packageJson: `${pubdir}/package.json`, jsrJson: `${pubdir}/deno.json` });
102
+ // await $`node ./setup-jsr-json.cjs ./pubdir/deno.json`;
103
+
104
+ // Pack and publish
105
+ cd("pubdir");
106
+ await $`pnpm pack 2>&1 | head -10 && echo "..."`;
107
+ await $`deno publish --dry-run --unstable-sloppy-imports --allow-dirty --quiet`;
108
+
109
+ // eslint-disable-next-line no-console
110
+ console.log(`Prepared ${pubdir} for version ${version}`);
111
+ }
112
+
113
+ export function preparePubdirCmd(): ReturnType<typeof command> {
114
+ return command({
115
+ name: "prepare-pubdir",
116
+ description: "prepare pubdir",
117
+ version: "1.0.0",
118
+ args: {
119
+ pubdir: option({
120
+ long: "pubdir",
121
+ short: "p",
122
+ defaultValue: () => "pubdir",
123
+ defaultValueIsSerializable: true,
124
+ type: string,
125
+ description: "Path to the pubdir, defaults to './pubdir'.",
126
+ }),
127
+ srcDir: option({
128
+ long: "srcDir",
129
+ short: "s",
130
+ defaultValue: () => "src",
131
+ defaultValueIsSerializable: true,
132
+ type: string,
133
+ description: "Path to the src directory, defaults to './src'.",
134
+ }),
135
+ baseDir: option({
136
+ long: "baseDir",
137
+ short: "b",
138
+ defaultValue: () => "../",
139
+ defaultValueIsSerializable: true,
140
+ type: string,
141
+ description: "Path to the base directory of the project, defaults to '../'.",
142
+ }),
143
+ version: option({
144
+ long: "version",
145
+ short: "v",
146
+ defaultValue: () => getVersion(),
147
+ defaultValueIsSerializable: true,
148
+ type: string,
149
+ description: "Version to patch in, defaults to reading from package.json.",
150
+ }),
151
+ },
152
+ handler: async (args) => {
153
+ await preparePubdir(args.pubdir, args.version, args.baseDir, args.srcDir);
154
+ },
155
+ }) as ReturnType<typeof command>;
156
+ }
@@ -0,0 +1,69 @@
1
+ import { command, option, string } from "cmd-ts";
2
+ import * as process from "node:process";
3
+ import { SemVer } from "semver";
4
+ import { versionFromPackageJson } from "./utils.js";
5
+
6
+ // Regex patterns for version parsing
7
+ const reVersionAlphaStart = /^[a-z](\d+\.\d+\.\d+.*)$/;
8
+ // const reVersionOptionalAlphaStart = /^[a-z]?(\d+\.\d+\.\d+.*)$/;
9
+ const reScopedVersion = /^[^@]+@(.*)$/;
10
+ const reEndVersion = /.*\/([^/]+)$/;
11
+
12
+ function getEnvVersion(version?: string, xenv = process.env): string {
13
+ let wversion = version || xenv.GITHUB_REF || versionFromPackageJson();
14
+ if (reEndVersion.test(wversion)) {
15
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
16
+ wversion = wversion.match(reEndVersion)[1];
17
+ }
18
+ const calculatedVersion = wversion.replace(reScopedVersion, "$1").replace(reVersionAlphaStart, "$1");
19
+ try {
20
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
21
+ new SemVer(calculatedVersion);
22
+ return calculatedVersion;
23
+ } catch (e) {
24
+ // Fallback to package.json version if parsing fails
25
+ return versionFromPackageJson();
26
+ }
27
+ }
28
+
29
+ export function publishTagsCmd(): ReturnType<typeof command> {
30
+ return command({
31
+ name: "publish-tags",
32
+ description: "Calculate npm publish tags based on version",
33
+ version: "1.0.0",
34
+ args: {
35
+ releaseVersion: option({
36
+ long: "release-version",
37
+ short: "r",
38
+ defaultValue: () => undefined,
39
+ defaultValueIsSerializable: true,
40
+ type: string,
41
+ description: "Version string to analyze, defaults to GITHUB_REF or package.json version.",
42
+ }),
43
+ },
44
+ handler: (args) => {
45
+ const calculatedVersion = getEnvVersion(args.releaseVersion);
46
+ const tags: string[] = [];
47
+
48
+ try {
49
+ const semVer = new SemVer(calculatedVersion);
50
+ // Check if the last prerelease identifier is a string
51
+ if (semVer.prerelease.length > 0) {
52
+ const lastIdentifier = semVer.prerelease[semVer.prerelease.length - 1];
53
+ if (typeof lastIdentifier === "string") {
54
+ tags.push("dev");
55
+ }
56
+ }
57
+ } catch (e) {
58
+ // eslint-disable-next-line no-console
59
+ console.warn(`Warn parsing version ${calculatedVersion}:`, e);
60
+ }
61
+
62
+ // Output the --tag options if there are tags
63
+ if (tags.length > 0) {
64
+ // eslint-disable-next-line no-console
65
+ console.log(tags.map((tag) => `--tag ${tag}`).join(" "));
66
+ }
67
+ },
68
+ }) as ReturnType<typeof command>;
69
+ }
package/src/cli/run.sh CHANGED
@@ -1,2 +1,11 @@
1
1
  #!/bin/bash
2
- exec pnpm exec tsx $(dirname $0)/main.ts $@
2
+ TSX="npx tsx"
3
+ if [ -x ./node_modules/.bin/tsx ]
4
+ then
5
+ TSX=./node_modules/.bin/tsx
6
+ fi
7
+ if [ -x ../node_modules/.bin/tsx ]
8
+ then
9
+ TSX=../node_modules/.bin/tsx
10
+ fi
11
+ exec $TSX $(dirname $0)/main.ts $@
@@ -0,0 +1,51 @@
1
+ import { command, option, string } from "cmd-ts";
2
+ import * as path from "node:path";
3
+ import * as fs from "node:fs";
4
+
5
+ // node ./setup-jsr-json.cjs ./pubdir/deno.json
6
+ function setupDenoJson(packageJsonFile: string, jsrJsonFile: string): void {
7
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonFile).toString()) as { dependencies: Record<string, string> };
8
+ const jsrJson = JSON.parse(fs.readFileSync(jsrJsonFile).toString()) as {
9
+ name: string;
10
+ exports: Record<string, string>;
11
+ imports: Record<string, string>;
12
+ };
13
+ // self imports
14
+ const jsrJsonImports = Object.fromEntries(Object.entries(jsrJson.exports ?? {}).map(([k, v]) => [path.join(jsrJson.name, k), v]));
15
+ const nodeJsonImports = Object.fromEntries(
16
+ Array.from(Object.entries(packageJson.dependencies ?? {})).map(([k, v]) => [k, `npm:${k}@${v.replace(/^npm:/, "")}`]),
17
+ );
18
+ jsrJson.imports = { ...jsrJson.imports, ...jsrJsonImports, ...nodeJsonImports };
19
+ fs.writeFileSync(jsrJsonFile, JSON.stringify(jsrJson, undefined, 2) + "\n");
20
+ }
21
+
22
+ export function setUpDenoJsonCmd(): ReturnType<typeof command> {
23
+ return command({
24
+ name: "setup-deno-json",
25
+ description: "setup deno.json",
26
+ version: "1.0.0",
27
+ args: {
28
+ packageJson: option({
29
+ long: "packageJson",
30
+ short: "p",
31
+ defaultValue: () => "package.json",
32
+ defaultValueIsSerializable: true,
33
+ type: string,
34
+ description: "Path to the package.json file, defaults to './package.json'.",
35
+ }),
36
+ jsrJson: option({
37
+ long: "jsrJson",
38
+ short: "j",
39
+ defaultValue: () => "deno.json",
40
+ defaultValueIsSerializable: true,
41
+ type: string,
42
+ description: "Path to the deno.json file, defaults to './deno.json'.",
43
+ }),
44
+ },
45
+ handler: (args) => {
46
+ // eslint-disable-next-line no-console
47
+ console.log(`Setup deno.json ${args.jsrJson} from ${args.packageJson}`);
48
+ setupDenoJson(args.packageJson, args.jsrJson);
49
+ },
50
+ }) as ReturnType<typeof command>;
51
+ }
@@ -0,0 +1,70 @@
1
+ import * as ts from "typescript";
2
+ import * as path from "node:path";
3
+ import * as fs from "node:fs";
4
+ import * as process from "node:process";
5
+ import { $ } from "zx";
6
+
7
+ // Custom compiler host
8
+ export function createCompilerHost(options: ts.CompilerOptions, version: string): ts.CompilerHost {
9
+ const host = ts.createCompilerHost(options);
10
+ // eslint-disable-next-line @typescript-eslint/unbound-method
11
+ const myGetSourceFile = host.getSourceFile;
12
+
13
+ host.getSourceFile = (fileName, languageVersion, onError, shouldCreateNewSourceFile): ts.SourceFile => {
14
+ const sourceFile = myGetSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile);
15
+ if (!sourceFile) {
16
+ throw new Error("getSourceFile is not defined");
17
+ }
18
+
19
+ // Patch version.ts during compilation
20
+ if (fileName.endsWith("version.ts") && sourceFile) {
21
+ const newText = `export const VERSION: string = "${version}";`;
22
+ return ts.createSourceFile(fileName, newText, languageVersion);
23
+ }
24
+
25
+ return sourceFile;
26
+ };
27
+ return host;
28
+ }
29
+
30
+ export function readTSConfig(configPath = "./tsconfig.json"): ts.CompilerOptions {
31
+ // Read the config file
32
+ // eslint-disable-next-line @typescript-eslint/unbound-method
33
+ const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
34
+
35
+ if (configFile.error) {
36
+ throw new Error(`Error reading tsconfig: ${configFile.error.messageText as string}`);
37
+ }
38
+
39
+ // Parse and resolve the config
40
+ const parsedConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath), undefined, configPath);
41
+
42
+ if (parsedConfig.errors.length > 0) {
43
+ const errors = parsedConfig.errors.map((err) => err.messageText as string).join("\n");
44
+ throw new Error(`Error parsing tsconfig: ${errors}`);
45
+ }
46
+
47
+ return parsedConfig.options; // This is CompilerOptions
48
+ }
49
+
50
+ export function versionFromPackageJson(): string {
51
+ const packageJson = JSON.parse(fs.readFileSync("package.json").toString()) as { version: string };
52
+ return packageJson.version;
53
+ }
54
+
55
+ export function getVersion(iversion?: string): string {
56
+ const ghref = iversion || process.env.GITHUB_REF || versionFromPackageJson() || "a/v0.0.0-smoke";
57
+ let lastPart = ghref.split("/").slice(-1)[0];
58
+ if (iversion) {
59
+ return iversion.replace(/^[vdsp]/, "");
60
+ }
61
+ const short = $.sync`git rev-parse --short HEAD`.stdout.trim();
62
+ if (process.env.GITHUB_REF) {
63
+ lastPart = lastPart.replace(/^[vdsp]/, "");
64
+ if (lastPart.match(/^\d+\.\d+\.\d+/)) {
65
+ return lastPart;
66
+ }
67
+ return `0.0.0-dev-ci-${short}`;
68
+ }
69
+ return `0.0.0-dev-local-${Date.now()}`;
70
+ }