Skip to content

Commit

Permalink
Merge pull request #18 from labscommunity/fix/handle-gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
7i7o authored Dec 22, 2023
2 parents 3cd7902 + ebf347c commit e4ba7fd
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-pans-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@protocol.land/sync': patch
---

Update bundler node to Turbo & improve .gitignore handling
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
18 changes: 9 additions & 9 deletions src/lib/arweaveHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export async function getAddress() {

export async function uploadRepo(zipBuffer: Buffer, tags: Tag[]) {
try {
// upload compressed repo using bundlr
const bundlrTxId = await bundlrUpload(zipBuffer, tags);
console.log('Posted Tx to Bundlr: ', bundlrTxId);
return bundlrTxId;
// upload compressed repo using turbo
const turboTxId = await turboUpload(zipBuffer, tags);
console.log('Posted Tx to Turbo: ', turboTxId);
return turboTxId;
} catch (error) {
console.log('Error uploading using bundlr, trying with Arweave...');
console.log('Error uploading using turbo, trying with Arweave...');
// let Arweave throw if it encounters errors
const arweaveTxId = await arweaveUpload(zipBuffer, tags);
console.log('Posted Tx to Arweave: ', arweaveTxId);
Expand Down Expand Up @@ -60,11 +60,11 @@ async function arweaveUpload(zipBuffer: Buffer, tags: Tag[]) {
return tx.id;
}

export async function bundlrUpload(zipBuffer: Buffer, tags: Tag[]) {
if (!jwk) throw '[ bundlr ] No jwk wallet supplied';
export async function turboUpload(zipBuffer: Buffer, tags: Tag[]) {
if (!jwk) throw '[ turbo ] No jwk wallet supplied';

// Testing upload with arbundles
const node = 'https://node2.bundlr.network';
const node = 'https://turbo.ardrive.io';
const uint8ArrayZip = new Uint8Array(zipBuffer);
const signer = new ArweaveSigner(getWallet());

Expand All @@ -82,7 +82,7 @@ export async function bundlrUpload(zipBuffer: Buffer, tags: Tag[]) {

if (res.status >= 400)
throw new Error(
`[ bundlr ] Posting repo w/bundlr faile. Error: ${res.status} - ${res.statusText}`
`[ turbo ] Posting repo with turbo failed. Error: ${res.status} - ${res.statusText}`
);

return dataItem.id;
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 e4ba7fd

Please sign in to comment.