Skip to content

Commit

Permalink
fix: move retrieval of package.json to utility function (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelgrosso1 authored May 17, 2022
1 parent 86c37c5 commit ac5cbdf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
11 changes: 2 additions & 9 deletions src/gcs-resumable-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,13 @@ import {Readable, Writable} from 'stream';
import retry = require('async-retry');
import {RetryOptions, PreconditionOptions} from './storage';
import * as uuid from 'uuid';
import path = require('path');
import {getPackageJSON} from './util';

const NOT_FOUND_STATUS_CODE = 404;
const TERMINATED_UPLOAD_STATUS_CODE = 410;
const RESUMABLE_INCOMPLETE_STATUS_CODE = 308;
const DEFAULT_API_ENDPOINT_REGEX = /.*\.googleapis\.com/;
let packageJson: ReturnType<JSON['parse']> = {};
try {
// if requiring from 'build' (default)
packageJson = require(path.join(__dirname, '../../package.json'));
} catch (e) {
// if requiring directly from TypeScript context
packageJson = require(path.join(__dirname, '../package.json'));
}
const packageJson = getPackageJSON();

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

Expand Down
11 changes: 2 additions & 9 deletions src/nodejs-common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,8 @@ import {Duplex, DuplexOptions, Readable, Transform, Writable} from 'stream';
import {teenyRequest} from 'teeny-request';
import {Interceptor} from './service-object';
import * as uuid from 'uuid';
import path = require('path');
let packageJson: ReturnType<JSON['parse']> = {};
try {
// if requiring from 'build' (default)
packageJson = require(path.join(__dirname, '../../../package.json'));
} catch (e) {
// if requiring directly from TypeScript context
packageJson = require(path.join(__dirname, '../../package.json'));
}
import {getPackageJSON} from '../util';
const packageJson = getPackageJSON();

// eslint-disable-next-line @typescript-eslint/no-var-requires
const duplexify: DuplexifyConstructor = require('duplexify');
Expand Down
13 changes: 2 additions & 11 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import {Readable} from 'stream';
import {Bucket} from './bucket';
import {Channel} from './channel';
import {File} from './file';
import {normalize} from './util';
import {getPackageJSON, normalize} from './util';
import {HmacKey, HmacKeyMetadata, HmacKeyOptions} from './hmacKey';
import path = require('path');

export interface GetServiceAccountOptions {
userProject?: string;
Expand Down Expand Up @@ -615,15 +614,7 @@ export class Storage extends Service {
maxRetryValue = options.retryOptions.maxRetries;
}

let packageJson: ReturnType<JSON['parse']> = {};

try {
// if requiring from 'build' (default)
packageJson = require(path.join(__dirname, '../../package.json'));
} catch (e) {
// if requiring directly from TypeScript context
packageJson = require(path.join(__dirname, '../package.json'));
}
const packageJson = getPackageJSON();

const config = {
apiEndpoint: options.apiEndpoint!,
Expand Down
24 changes: 24 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,27 @@ export function formatAsUTCISO(

return resultString;
}

/**
* Attempts to retrieve package.json from either the typescript or build context.
* @returns {object} object representation of package.json
*/
export function getPackageJSON(): ReturnType<JSON['parse']> {
let packageJson: ReturnType<JSON['parse']> = undefined;
const possiblePaths = ['../../package.json', '../package.json'];

for (const path of possiblePaths) {
try {
packageJson = require(path);
break;
} catch {
packageJson = undefined;
}
}

if (packageJson) {
return packageJson;
}

throw new Error('Unable to find package.json');
}

0 comments on commit ac5cbdf

Please sign in to comment.