Skip to content

Commit ac5cbdf

Browse files
authored
fix: move retrieval of package.json to utility function (#1941)
1 parent 86c37c5 commit ac5cbdf

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

src/gcs-resumable-upload.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,13 @@ import {Readable, Writable} from 'stream';
2828
import retry = require('async-retry');
2929
import {RetryOptions, PreconditionOptions} from './storage';
3030
import * as uuid from 'uuid';
31-
import path = require('path');
31+
import {getPackageJSON} from './util';
3232

3333
const NOT_FOUND_STATUS_CODE = 404;
3434
const TERMINATED_UPLOAD_STATUS_CODE = 410;
3535
const RESUMABLE_INCOMPLETE_STATUS_CODE = 308;
3636
const DEFAULT_API_ENDPOINT_REGEX = /.*\.googleapis\.com/;
37-
let packageJson: ReturnType<JSON['parse']> = {};
38-
try {
39-
// if requiring from 'build' (default)
40-
packageJson = require(path.join(__dirname, '../../package.json'));
41-
} catch (e) {
42-
// if requiring directly from TypeScript context
43-
packageJson = require(path.join(__dirname, '../package.json'));
44-
}
37+
const packageJson = getPackageJSON();
4538

4639
export const PROTOCOL_REGEX = /^(\w*):\/\//;
4740

src/nodejs-common/util.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,8 @@ import {Duplex, DuplexOptions, Readable, Transform, Writable} from 'stream';
2929
import {teenyRequest} from 'teeny-request';
3030
import {Interceptor} from './service-object';
3131
import * as uuid from 'uuid';
32-
import path = require('path');
33-
let packageJson: ReturnType<JSON['parse']> = {};
34-
try {
35-
// if requiring from 'build' (default)
36-
packageJson = require(path.join(__dirname, '../../../package.json'));
37-
} catch (e) {
38-
// if requiring directly from TypeScript context
39-
packageJson = require(path.join(__dirname, '../../package.json'));
40-
}
32+
import {getPackageJSON} from '../util';
33+
const packageJson = getPackageJSON();
4134

4235
// eslint-disable-next-line @typescript-eslint/no-var-requires
4336
const duplexify: DuplexifyConstructor = require('duplexify');

src/storage.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import {Readable} from 'stream';
2121
import {Bucket} from './bucket';
2222
import {Channel} from './channel';
2323
import {File} from './file';
24-
import {normalize} from './util';
24+
import {getPackageJSON, normalize} from './util';
2525
import {HmacKey, HmacKeyMetadata, HmacKeyOptions} from './hmacKey';
26-
import path = require('path');
2726

2827
export interface GetServiceAccountOptions {
2928
userProject?: string;
@@ -615,15 +614,7 @@ export class Storage extends Service {
615614
maxRetryValue = options.retryOptions.maxRetries;
616615
}
617616

618-
let packageJson: ReturnType<JSON['parse']> = {};
619-
620-
try {
621-
// if requiring from 'build' (default)
622-
packageJson = require(path.join(__dirname, '../../package.json'));
623-
} catch (e) {
624-
// if requiring directly from TypeScript context
625-
packageJson = require(path.join(__dirname, '../package.json'));
626-
}
617+
const packageJson = getPackageJSON();
627618

628619
const config = {
629620
apiEndpoint: options.apiEndpoint!,

src/util.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,27 @@ export function formatAsUTCISO(
166166

167167
return resultString;
168168
}
169+
170+
/**
171+
* Attempts to retrieve package.json from either the typescript or build context.
172+
* @returns {object} object representation of package.json
173+
*/
174+
export function getPackageJSON(): ReturnType<JSON['parse']> {
175+
let packageJson: ReturnType<JSON['parse']> = undefined;
176+
const possiblePaths = ['../../package.json', '../package.json'];
177+
178+
for (const path of possiblePaths) {
179+
try {
180+
packageJson = require(path);
181+
break;
182+
} catch {
183+
packageJson = undefined;
184+
}
185+
}
186+
187+
if (packageJson) {
188+
return packageJson;
189+
}
190+
191+
throw new Error('Unable to find package.json');
192+
}

0 commit comments

Comments
 (0)