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

Handle complex website #61

Merged
5 commits merged into from
May 30, 2022
Merged
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
26 changes: 10 additions & 16 deletions aeweb.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
#!/usr/bin/env node

const yargs = require('yargs')
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import about from './commands/about.js'
import generate_address from './commands/generate_address.js'
import deploy from './commands/deploy.js'

yargs.command(require('./commands/about'))
.help()
const y = yargs(hideBin(process.argv))

yargs.command((require('./commands/generate_address')))
.help()
y.command(about).help()
y.command(generate_address).help()
y.command(deploy).help()

yargs.command((require('./commands/deploy_file')))
.help()

yargs.command((require('./commands/deploy_folder')))
.help()

yargs.command((require('./commands/deploy_website')))
.help()


yargs.parse()
y.parse()
16 changes: 11 additions & 5 deletions commands/about.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const chalk = require('chalk')
const figlet = require('figlet')
import chalk from 'chalk'
import figlet from 'figlet'

exports.command = 'about'
const command = 'about'

exports.describe = 'Welcome to AeWeb'
const describe = 'Welcome to AeWeb'

exports.handler = function () {
const handler = function () {
console.log(chalk.green('\n', 'Hello and Welcome to AeWeb !', '\n'))
console.log(chalk.blue(figlet.textSync('AEWEB', {
font: "Alligator2"
})))
console.log(chalk.green('\n', 'Create your Website on top of Archethic Public Blockchain'))
console.log(chalk.green('\n'))
}

export default {
command,
describe,
handler
}
147 changes: 147 additions & 0 deletions commands/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import fs from "fs";
import archethic from "archethic";
import chalk from "chalk";
import path from "path";
import yesno from "yesno";
import zlib from 'zlib'

const command = "deploy";

const describe =
"Deploy a single file or all file inside a folder";

const builder = {
seed: {
describe:
"Seed is a string representing the transaction chain entropy to be able to derive and generate the keys for the transactions",
demandOption: true, // Required
type: "string",
},

endpoint: {
describe:
"Endpoint is the URL of a welcome node to receive the transaction",
demandOption: true, // Required
type: "string",
},

path: {
describe: "Path to the folder or the file to deploy",
demandOption: true, // Required
type: "string",
},
};

const handler = async function (argv) {
try {
// Derive address and get last transaction index
const seed = argv.seed
const endpoint = argv.endpoint
const firstAddress = archethic.deriveAddress(seed, 0)
const index = await archethic.getTransactionIndex(firstAddress, endpoint)

// Convert directory structure to json
console.log(chalk.blue('Creating file structure and compress content...'))

let json = {}
let argStats
try {
argStats = fs.statSync(argv.path)
if (argStats.isDirectory()) {
json = handleDirectory(argv.path)
} else {
json[path.basename(argv.path)] = handleFile(argv.path)
}

if (Object.keys(json).length === 0) {
throw { message: 'Folder ' + argv.path + ' is empty' }
}
} catch (e) {
throw e.message
}

// Create transaction
console.log(chalk.blue('Creating transaction, it may take a while...'))

const tx = archethic.newTransactionBuilder('hosting')
.setContent(JSON.stringify(json))
.build(seed, index)
.originSign(await archethic.getOriginKey(endpoint))

// Get transaction fees and ask user
const { fee: fees, rates: rates } = await archethic.getTransactionFee(tx, endpoint)

const ok = await yesno({
question: chalk.yellowBright(
"Total Fee Requirement would be : " +
fees.toFixed(2) +
" UCO ( $ " +
(rates.usd * fees).toFixed(2) +
" | € " +
(rates.eur * fees).toFixed(2) +
" ). Do you want to continue. (yes/no)"
),
});

if (ok) {
console.log(chalk.blue('Sending transaction...'))

await archethic.waitConfirmations(
tx.address,
endpoint,
nbConfirmations => {
console.log(chalk.blue("Got " + nbConfirmations + " confirmations"))
console.log(
chalk.cyanBright(
"See transaction in explorer:",
endpoint + '/explorer/transaction/' + Buffer.from(tx.address).toString('hex')
)
)
console.log(
chalk.green(
(argStats.isDirectory() ? "Website": "File") + " is deployed at:",
endpoint + "/api/web_hosting/" + firstAddress + '/'
)
)
}
)

await archethic.sendTransaction(tx, endpoint)
console.log(chalk.blue('Waiting transaction validation...'))
} else {
throw "User aborted website deployment."
}
} catch (e) {
console.log(chalk.red(e))
}
}

function handleFile(file) {
const data = fs.readFileSync(file)
return {
encodage: 'gzip',
content: zlib.gzipSync(data).toString('base64url')
}
}

function handleDirectory(entry) {
const stats = fs.statSync(entry)
let json = {}

if (stats.isDirectory()) {
fs.readdirSync(entry).forEach(child => {
json[path.basename(child)] = handleDirectory(entry + '/' + child)
});
} else {
json = handleFile(entry)
}

return json;
}

export default {
command,
describe,
builder,
handler
}
86 changes: 0 additions & 86 deletions commands/deploy_file.js

This file was deleted.

Loading