|
1 | 1 | import { spawnSync } from 'node:child_process'; |
2 | | -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
3 | | -import { tmpdir } from 'node:os'; |
4 | | -import { dirname, join } from 'node:path'; |
| 2 | +import { dirname } from 'node:path'; |
5 | 3 | import { fileURLToPath } from 'node:url'; |
6 | 4 |
|
7 | 5 | const typesDirectory = dirname(dirname(fileURLToPath(import.meta.url))); |
8 | | -const templatePath = join(typesDirectory, 'buf.gen.yaml'); |
9 | | -const args = process.argv.slice(2); |
10 | | - |
11 | | -const getCommit = (providedArgs) => { |
12 | | - if (providedArgs.length === 0) { |
13 | | - return undefined; |
14 | | - } |
15 | | - |
16 | | - if (providedArgs.length === 1 && !providedArgs[0].startsWith('--')) { |
17 | | - return providedArgs[0]; |
18 | | - } |
19 | | - |
20 | | - if (providedArgs.length === 2 && providedArgs[0] === '--commit') { |
21 | | - return providedArgs[1]; |
22 | | - } |
23 | | - |
24 | | - if (providedArgs.length === 1 && providedArgs[0].startsWith('--commit=')) { |
25 | | - return providedArgs[0].slice('--commit='.length); |
26 | | - } |
27 | | - |
28 | | - throw new Error('Usage: pnpm gen-types [commit] or pnpm gen-types --commit <commit>'); |
29 | | -}; |
30 | | - |
31 | | -let temporaryDirectory; |
32 | 6 |
|
33 | 7 | try { |
34 | | - const commit = getCommit(args); |
35 | | - let bufArgs = ['generate']; |
36 | | - |
37 | | - if (commit !== undefined) { |
38 | | - if (commit.length === 0 || /\s/.test(commit)) { |
39 | | - throw new Error('The commit must not contain whitespace.'); |
40 | | - } |
41 | | - |
42 | | - const template = readFileSync(templatePath, 'utf8'); |
43 | | - const branchPattern = /^(\s*)branch: main$/gm; |
44 | | - const branchMatches = template.match(branchPattern); |
45 | | - |
46 | | - if (branchMatches?.length !== 2) { |
47 | | - throw new Error('Expected both protobuf inputs to use branch: main.'); |
48 | | - } |
49 | | - |
50 | | - const customTemplate = template.replace( |
51 | | - branchPattern, |
52 | | - (_, indentation) => `${indentation}ref: ${JSON.stringify(commit)}`, |
53 | | - ); |
54 | | - |
55 | | - temporaryDirectory = mkdtempSync(join(tmpdir(), 'osac-gen-types-')); |
56 | | - const temporaryTemplatePath = join(temporaryDirectory, 'buf.gen.yaml'); |
57 | | - writeFileSync(temporaryTemplatePath, customTemplate); |
58 | | - bufArgs = ['generate', '--template', temporaryTemplatePath]; |
59 | | - } |
60 | | - |
61 | | - const result = spawnSync('buf', bufArgs, { |
| 8 | + const result = spawnSync('buf', ['generate'], { |
62 | 9 | cwd: typesDirectory, |
63 | 10 | stdio: 'inherit', |
64 | 11 | }); |
|
71 | 18 | } catch (error) { |
72 | 19 | process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); |
73 | 20 | process.exitCode = 1; |
74 | | -} finally { |
75 | | - if (temporaryDirectory) { |
76 | | - rmSync(temporaryDirectory, { force: true, recursive: true }); |
77 | | - } |
78 | 21 | } |
0 commit comments