diff --git a/.changeset/serious-pans-sort.md b/.changeset/serious-pans-sort.md new file mode 100644 index 0000000..f7091e8 --- /dev/null +++ b/.changeset/serious-pans-sort.md @@ -0,0 +1,5 @@ +--- +'@protocol.land/sync': patch +--- + +Update bundler node to Turbo & improve .gitignore handling diff --git a/src/index.ts b/src/index.ts index e2f36be..058da8a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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._-]+$/; @@ -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); diff --git a/src/lib/arweaveHelper.ts b/src/lib/arweaveHelper.ts index 3663307..545ca4b 100644 --- a/src/lib/arweaveHelper.ts +++ b/src/lib/arweaveHelper.ts @@ -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); @@ -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()); @@ -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; diff --git a/src/lib/zipHelper.ts b/src/lib/zipHelper.ts index e8f5e47..ca9de88 100644 --- a/src/lib/zipHelper.ts +++ b/src/lib/zipHelper.ts @@ -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((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((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