/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { findUpSync } from 'find-up-simple';
import { randomUUID } from 'crypto';
/**
* Gets the package version from package.json
* @returns The package version
*/
export function getPackageVersion(): string {
try {
// Use import.meta.url to get the current file path and navigate to package.json
const currentDir = fileURLToPath(new URL('.', import.meta.url));
const packageJsonPath = findUpSync('package.json', { cwd: currentDir });
if (!packageJsonPath) {
return '1.0.0'; // Default version if package.json not found
}
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
return packageJson.version || '1.0.0';
} catch (error) {
console.error('Error reading package version:', error);
return '1.0.0';
}
}
/**
* Generates a UUID v4 for use as x-amzn-requestid header
* @returns A UUID v4 string
*/
export function generateRequestId(): string {
return randomUUID();
}