GeezerVison is showing github.com — fix: improve package.json lookup for root name (#1530) · googleapis/gax-nodejs@3ec09cf
This repository was archived by the owner on Nov 18, 2025. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: tools/package.json +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 28 | 28 | "author": "Google API Authors", | 29 | 29 | "license": "Apache-2.0", | 30 | 30 | "dependencies": { | | 31 | + "@babel/core": "^7.22.5", | | 32 | + "@babel/traverse": "^7.22.5", | 31 | 33 | "google-gax": "^4.1.0", | 32 | 34 | "google-proto-files": "^4.1.0", | 33 | 35 | "protobufjs-cli": "1.1.2", | 34 | 36 | "rimraf": "^5.0.1", | 35 | | - "@babel/core": "^7.22.5", | 36 | | - "@babel/traverse": "^7.22.5", | 37 | 37 | "uglify-js": "^3.17.0", | | 38 | + "walk-up-path": "^3.0.1", | 38 | 39 | "walkdir": "^0.4.0" | 39 | 40 | }, | 40 | 41 | "repository": { | |
Collapse file Copy file name to clipboardExpand all lines: tools/src/compileProtos.ts +11-8Lines changed: 11 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ import * as path from 'path'; | 21 | 21 | import * as util from 'util'; | 22 | 22 | import * as pbjs from 'protobufjs-cli/pbjs'; | 23 | 23 | import * as pbts from 'protobufjs-cli/pbts'; | | 24 | +import {walkUp} from 'walk-up-path'; | 24 | 25 |
| 25 | 26 | export const gaxProtos = path.join( | 26 | 27 | require.resolve('google-gax'), | @@ -360,14 +361,16 @@ export async function generateRootName(directories: string[]): Promise<string> { | 360 | 361 | // It's OK to play some guessing game here: if we locate `package.json` | 361 | 362 | // with a package name, we'll use it; otherwise, we'll fallback to 'default'. | 362 | 363 | for (const directory of directories) { | 363 | | - const packageJson = path.resolve(directory, '..', 'package.json'); | 364 | | - if (fs.existsSync(packageJson)) { | 365 | | - const json = JSON.parse((await readFile(packageJson)).toString()) as { | 366 | | - name: string; | 367 | | - }; | 368 | | - const name = json.name.replace(/[^\w\d]/g, '_'); | 369 | | - const hopefullyUniqueName = `${name}_protos`; | 370 | | - return hopefullyUniqueName; | | 364 | + for (const p of walkUp(path.resolve(directory, '..'))) { | | 365 | + const packageJson = path.join(p, 'package.json'); | | 366 | + if (fs.existsSync(packageJson)) { | | 367 | + const json = JSON.parse((await readFile(packageJson)).toString()) as { | | 368 | + name: string; | | 369 | + }; | | 370 | + const name = json.name.replace(/[^\w\d]/g, '_'); | | 371 | + const hopefullyUniqueName = `${name}_protos`; | | 372 | + return hopefullyUniqueName; | | 373 | + } | 371 | 374 | } | 372 | 375 | } | 373 | 376 | return 'default'; | |
Collapse file Copy file name to clipboardExpand all lines: tools/test/compileProtos.ts +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -281,10 +281,17 @@ describe('compileProtos tool', () => { | 281 | 281 | assert.strictEqual(rootName, '_org_fake_package_protos'); | 282 | 282 | }); | 283 | 283 |
| 284 | | - it('falls back to the default name for protobuf root if unable to guess', async () => { | | 284 | + it('uses the nearest package.json to guess the root name', async () => { | 285 | 285 | const rootName = await compileProtos.generateRootName([ | 286 | 286 | path.join(__dirname, 'protoLists', 'empty'), | 287 | 287 | ]); | | 288 | + assert.strictEqual(rootName, 'gapic_tools_protos'); | | 289 | + }); | | 290 | + | | 291 | + it('falls back to the default name for protobuf root if unable to guess', async () => { | | 292 | + const rootName = await compileProtos.generateRootName([ | | 293 | + '/nonexistent/empty', | | 294 | + ]); | 288 | 295 | assert.strictEqual(rootName, 'default'); | 289 | 296 | }); | 290 | 297 |
| |
|