what-is-my-tech-stack
Version:
Analyze project dependencies and generate a human-readable tech stack description
65 lines • 2.22 kB
JavaScript
import fs from 'fs';
import path from 'path';
export class FileReader {
/**
* Checks if a file exists at the given path
*/
static fileExists(filePath) {
try {
return fs.existsSync(filePath);
}
catch (error) {
return false;
}
}
/**
* Reads and parses a package.json file
*/
static async readPackageJson(filePath) {
try {
const absolutePath = path.resolve(filePath);
if (!this.fileExists(absolutePath)) {
throw new Error(`package.json not found at ${absolutePath}`);
}
const fileContent = await fs.promises.readFile(absolutePath, 'utf-8');
return JSON.parse(fileContent);
}
catch (error) {
throw new Error(`Error reading package.json: ${error.message}`);
}
}
/**
* Reads and parses a requirements.txt file
*/
static async readRequirementsTxt(filePath) {
try {
const absolutePath = path.resolve(filePath);
if (!this.fileExists(absolutePath)) {
throw new Error(`requirements.txt not found at ${absolutePath}`);
}
const fileContent = await fs.promises.readFile(absolutePath, 'utf-8');
return fileContent
.split('\n')
.map((line) => line.trim())
.filter((line) => line && !line.startsWith('#')); // Remove empty lines and comments
}
catch (error) {
throw new Error(`Error reading requirements.txt: ${error.message}`);
}
}
/**
* Detects the type of project based on the files present in the directory
*/
static detectProjectType(directoryPath) {
const hasPackageJson = this.fileExists(path.join(directoryPath, 'package.json'));
const hasRequirementsTxt = this.fileExists(path.join(directoryPath, 'requirements.txt'));
if (hasPackageJson && hasRequirementsTxt)
return 'both';
if (hasPackageJson)
return 'node';
if (hasRequirementsTxt)
return 'python';
return 'unknown';
}
}
//# sourceMappingURL=fileReader.js.map