lambda-live-debugger
Version:
Debug Lambda functions locally like it is running in the cloud
77 lines (76 loc) • 2.29 kB
JavaScript
import fs from 'fs/promises';
import { outputFolder } from './constants.mjs';
import { Logger } from './logger.mjs';
import { getProjectDirname } from './getDirname.mjs';
import path from 'path';
/**
* Check if ".lldebugger" exists in .gitignore
*/
async function doesExistInGitIgnore() {
try {
const gitignoreContent = await fs.readFile(getGitIgnoreFileLocation(), 'utf-8');
// split by new line
const lines = gitignoreContent.split('\n').map((line) => line.trim());
// check if ".lldebugger" exists
const exists = lines.includes(outputFolder);
return exists;
}
catch {
return false;
}
}
/**
* Get the location of .gitignore
* @returns
*/
function getGitIgnoreFileLocation() {
return path.join(getProjectDirname(), '.gitignore');
}
/**
* Add ".lldebugger" to .gitignore if it doesn't exist
* @returns
*/
async function addToGitIgnore() {
Logger.log(`Adding ${outputFolder} to .gitignore.`);
try {
const exists = await doesExistInGitIgnore();
if (!exists) {
// does file exist?
try {
await fs.access(getGitIgnoreFileLocation());
}
catch {
await fs.writeFile(getGitIgnoreFileLocation(), `${outputFolder}\n`);
return;
}
// append to existing file
await fs.appendFile(getGitIgnoreFileLocation(), `\n${outputFolder}\n`);
}
else {
Logger.log(`${outputFolder} already exists in .gitignore`);
}
}
catch (err) {
Logger.error('Error adding to .gitignore', err);
}
}
/**
* Remove ".lldebugger" from .gitignore
*/
async function removeFromGitIgnore() {
Logger.verbose('Removing .gitignore entry...');
const exists = await doesExistInGitIgnore();
if (exists) {
const gitignoreContent = await fs.readFile(getGitIgnoreFileLocation(), 'utf-8');
const newContent = gitignoreContent.replace(`${outputFolder}\n`, '');
await fs.writeFile(getGitIgnoreFileLocation(), newContent);
}
else {
Logger.log(`${outputFolder} doesn't exist in .gitignore`);
}
}
export const GitIgnore = {
doesExistInGitIgnore,
addToGitIgnore,
removeFromGitIgnore,
};