Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move retrieval of package.json to utility function #1941

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}