Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: AO migration #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions lib/aoHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const {getWallet, log, AOS_PROCESS_ID} = require('./common.js');
const {
createDataItemSigner,
dryrun,
message,
result,
} = require('@permaweb/aoconnect');

function capitalizeFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}

function getTags(payload) {
return Object.entries(payload).map(([key, value]) => ({
name: capitalizeFirstLetter(key),
value,
}));
}

function extractMessage(text) {
const regex = /:\s*([^:!]+)!/;
const match = text.match(regex);
return match ? match[1].trim() : text;
}

async function sendMessage({tags, data}) {
const args = {
process: AOS_PROCESS_ID,
tags,
signer: createDataItemSigner(getWallet()),
};

if (data) {
args.data = data;
}

const messageId = await message(args);

const {Output} = await result({
message: messageId,
process: AOS_PROCESS_ID,
});

if (Output?.data?.output) {
throw new Error(extractMessage(Output?.data?.output));
}

return messageId;
}

async function getRepoFromPL(repoUrl) {
const id = `${repoUrl.replace(/.*:\/\//, '')}`;
const {Messages} = await dryrun({
process: AOS_PROCESS_ID,
tags: getTags({
Action: 'Get-Repo',
Id: id,
Fields: JSON.stringify([
'id',
'name',
'description',
'owner',
'deployments',
'deploymentBranch',
'contributors',
]),
}),
});

return JSON.parse(Messages[0].Data)?.result;
}

async function updateDeploymentBranch(repo, deploymentBranch) {
if (!repo.id) {
throw '[ AO ] No id to update repo ';
}

await sendMessage({
tags: getTags({
Action: 'Update-Repo-Details',
Id: repo.id,
'Deployment-Branch': deploymentBranch,
}),
});
}

async function addDeployment(repo, deployment) {
if (!repo.id) {
throw '[ AO ] No id to update repo ';
}

await sendMessage({
tags: getTags({
Action: 'Add-Deployment',
Id: repo.id,
Deployment: JSON.stringify(deployment),
}),
});

log(`Deployment Link: https://ar-io.net/${deployment.txId}\n`, {
color: 'green',
});
}

exports.addDeployment = addDeployment;
exports.updateDeploymentBranch = updateDeploymentBranch;
exports.getRepoFromPL = getRepoFromPL;
4 changes: 2 additions & 2 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ANSI_GREEN = '\x1b[32m';

const PL_TMP_PATH = '.protocol.land';
const GIT_CONFIG_KEYFILE = 'protocol.land.keyfile';
const getWarpContractTxId = () => 'w5ZU15Y2cLzZlu3jewauIlnzbKw-OAxbN9G5TbuuiDQ';
const AOS_PROCESS_ID = 'Mj6eddWvVjxROVUvUoz-ysr0lrcb36b9hNgF5NKLoLw';

const log = (message, options) => {
if (!options) {
Expand Down Expand Up @@ -120,7 +120,7 @@ async function withAsync(fn) {

exports.PL_TMP_PATH = PL_TMP_PATH;
exports.GIT_CONFIG_KEYFILE = GIT_CONFIG_KEYFILE;
exports.getWarpContractTxId = getWarpContractTxId;
exports.AOS_PROCESS_ID = AOS_PROCESS_ID;
exports.log = log;
exports.getJwkPath = getJwkPath;
exports.getWallet = getWallet;
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const fs = require('fs-extra');
const globby = require('globby');
const path = require('path');
const util = require('util');
const warpHelper = require('./warpHelper.js');
const aoHelper = require('./aoHelper.js');
const dragondeploy = require('./dragondeploy.js');
const common = require('./common.js');

Expand Down Expand Up @@ -251,7 +251,7 @@ exports.publish = function publish(basePath, config, callback) {
throw new Error("No 'index.html' file found for deployment");
}
const commit = await git.getLatestCommit();
const repo = await warpHelper.getRepoFromPL(repoUrl);
const repo = await aoHelper.getRepoFromPL(repoUrl);
const deployment = repo.deployments.find(
(deployment) => deployment.commitOid === commit.oid
);
Expand All @@ -267,7 +267,7 @@ exports.publish = function publish(basePath, config, callback) {
repo.deploymentBranch &&
options.branch !== repo.deploymentBranch)
) {
await warpHelper.updateDeploymentBranch(repo, options.branch);
await aoHelper.updateDeploymentBranch(repo, options.branch);
}

if (deployment) {
Expand All @@ -280,7 +280,7 @@ exports.publish = function publish(basePath, config, callback) {
commit,
repo
);
await warpHelper.addDeployment(repo, {
await aoHelper.addDeployment(repo, {
txId,
commitMessage: commit.message,
commitOid: commit.oid,
Expand Down
102 changes: 0 additions & 102 deletions lib/warpHelper.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"node": ">=10"
},
"dependencies": {
"@permaweb/aoconnect": "^0.0.48",
"arbundles": "^0.10.0",
"ardb": "^1.1.10",
"async": "^3.2.4",
Expand All @@ -41,8 +42,7 @@
"find-cache-dir": "^3.3.1",
"fs-extra": "^11.1.1",
"globby": "^6.1.0",
"mime": "^3.0.0",
"warp-contracts": "^1.4.30"
"mime": "^3.0.0"
},
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
Expand Down
Loading
Loading