Skip to content

Commit

Permalink
fix: Enhance .gitignore handling using 'git ls-files'
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanpaudel93 committed Dec 22, 2023
1 parent 3cd7902 commit ca1a21b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
8 changes: 1 addition & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { exitWithError, getTags, getTitle } from './lib/common';
const PATH = '.';
// const FOLDER_TO_ZIP = '.git'; // Only compress `.git` folder
const FOLDER_TO_ZIP = '.'; // Compress the full repo
const USE_GITIGNORE = true; // Use `.gitignore` to avoid compressing secrets

// Set up a regex for repo names compliance
const NAME_REGEX = /^[a-zA-Z0-9._-]+$/;
Expand All @@ -35,12 +34,7 @@ async function main() {
// compress the repo
let zipBuffer;
try {
zipBuffer = await zipRepoJsZip(
title,
PATH,
FOLDER_TO_ZIP,
USE_GITIGNORE
);
zipBuffer = await zipRepoJsZip(title, PATH, FOLDER_TO_ZIP);
} catch (error) {
console.error('Error zipping repository:', error);
process.exit(1);
Expand Down
81 changes: 56 additions & 25 deletions src/lib/zipHelper.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,94 @@
import fs from 'fs';
import { promises as fsPromises } from 'fs';
import path from 'path';
import JSZip from 'jszip';
import { exec } from 'child_process';

export function writeBufferToFile(buffer: Buffer, filename: string) {
const PL_TMP_PATH = '.protocol.land';

export async function writeBufferToFile(buffer: Buffer, filename: string) {
try {
fs.writeFileSync(filename, buffer);
await fsPromises.writeFile(filename, buffer);
console.log(`File "${filename}" written successfully.`);
} catch (error) {
console.error('Error writing file: ', error);
}
}

function loadIgnoreList(rootPath: string) {
const gitignorePath = path.join(rootPath, '.gitignore');
if (fs.existsSync(gitignorePath)) {
const gitignoreContent = fs.readFileSync(gitignorePath, 'utf-8');
return gitignoreContent
.split('\n')
.map((line) => line.trim())
.filter((line) => line && !line.startsWith('#'));
}
return [];
export async function getGitTrackedFiles() {
return new Promise<string[]>((resolve, reject) => {
exec('git ls-files', { encoding: 'utf-8' }, (error, stdout) => {
if (error) {
reject(new Error('Error getting git tracked files'));
} else {
resolve(stdout.trim().split('\n'));
}
});
});
}

export async function getGitDir() {
return new Promise<string>((resolve, reject) => {
exec(
'git rev-parse --git-dir',
{ encoding: 'utf-8' },
(error, stdout) => {
if (error) {
reject(new Error('Error getting git directory'));
} else {
resolve(stdout.trim());
}
}
);
});
}

export async function zipRepoJsZip(
mainPath: string,
zipRoot: string,
folderToZip?: string,
useGitignore?: boolean
folderToZip?: string
) {
if (!folderToZip) folderToZip = zipRoot;

const ignoreSet = new Set(useGitignore ? loadIgnoreList(zipRoot) : []);
const filesToInclude: string[] = [];

const zip = new JSZip();
const gitdir = await getGitDir();

const filesToInclude: string[] = [];
const ignoreFilesList = [path.join(gitdir, PL_TMP_PATH)];

const walk = (currentPath: string) => {
const items = fs.readdirSync(currentPath);
const walk = async (currentPath: string) => {
const items = await fsPromises.readdir(currentPath);

for (const item of items) {
const itemPath = path.join(currentPath, item);

if (ignoreSet.has(item)) {
if (
ignoreFilesList.some((ignorePath) =>
itemPath.startsWith(ignorePath)
)
) {
continue;
}

if (fs.statSync(itemPath).isDirectory()) {
walk(itemPath);
const stats = await fsPromises.stat(itemPath);

if (stats.isDirectory()) {
await walk(itemPath);
} else {
filesToInclude.push(itemPath);
}
}
};
walk(folderToZip);

await walk(gitdir);

const gitTrackedFiles = await getGitTrackedFiles();

filesToInclude.push(...gitTrackedFiles);

const zip = new JSZip();

for (const file of filesToInclude) {
const content = fs.readFileSync(file);
const content = await fsPromises.readFile(file);
const relativePath = `${mainPath ? mainPath + '/' : ''}${path.relative(
zipRoot,
file
Expand Down

0 comments on commit ca1a21b

Please sign in to comment.