-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy-app.ts
79 lines (67 loc) · 3.54 KB
/
deploy-app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as fs from 'node:fs';
import * as path from 'node:path';
import {
calculateApplicationDeploymentCost,
deployApplication,
getApplication,
depositDeploymentStorageCost,
getAccount, PARTITION_SIZE, postDeployCleanup, uploadFile, withdrawAvailableBalance, deletePreviousFiles,
} from './utils';
import { formatNearAmount } from '@near-js/utils';
import { confirm } from '@inquirer/prompts';
interface BundleAsset {
contentPath: string;
fileContents: string;
filename: string;
partitions: number;
}
function aggregateBundle(deployerAccount: string, application: string, basePath: string) {
const distPath = path.resolve(process.env.PWD as string, basePath);
return fs.readdirSync(distPath, { recursive: true })
.map((f) => f.toString())
.filter((e) => (e.endsWith('.gz') || e.endsWith('.html')) && !e.endsWith('.html.gz'))
.reduce(({ files, aggregated }: { files: BundleAsset[], aggregated: { totalBytes: number, totalPartitions: number } }, filename) => {
const fileContents = fs.readFileSync(path.resolve(distPath, filename)).toString('base64');
const asset = {
contentPath: `${deployerAccount}/${application}/${filename.replace(/\.gz$/, '')}`,
fileContents,
filename: filename.replace(/\.gz$/, ''),
partitions: Math.ceil(fileContents.length / PARTITION_SIZE),
};
aggregated.totalPartitions += asset.partitions;
aggregated.totalBytes += fileContents.length;
files.push(asset);
return { files, aggregated };
}, { files: [], aggregated: { totalBytes: 0, totalPartitions: 0 } });
}
export async function deployApp() {
const [,, basePath, network, fileContract, deployerAccount, application, isLiveRun = true] = process.argv;
const deployer = getAccount({ accountId: deployerAccount, network });
const { files, aggregated: { totalBytes, totalPartitions } } = aggregateBundle(deployerAccount, application, basePath);
const { applicationStorageCost, breakdown } = await calculateApplicationDeploymentCost({ deployer, application, files, totalBytes, fileContract });
console.log(`${application} deployment calculated to cost ${formatNearAmount(applicationStorageCost)} N`, breakdown)
const answer = await confirm({ message: `Estimated cost to deploy is ${formatNearAmount(applicationStorageCost)} N. Continue?`, default: false });
if (!answer) {
console.log('Exiting deployment');
process.exit(1);
}
const isLive = isLiveRun === true;
if (isLive) {
await depositDeploymentStorageCost({ deployer, fileContract, applicationStorageCost })
const deployResult = await deployApplication({ deployer, fileContract, application, files: files.map(({ filename }) => filename) })
console.log({ deployResult })
}
for (let { contentPath, fileContents, filename, partitions } of files) {
console.log(`[${contentPath}] uploading in ${partitions} parts... (${filename})`)
await uploadFile({ deployer, fileContract, fileContents, filename, partitions, isLive, application });
}
console.log(`your application is now available at https://chain-hosted-ui.near.dev/${fileContract}/${deployerAccount}/${application}`)
console.log(`all files uploaded for ${application}, beginning post-deploy actions`)
if (isLive) {
const cleanupResult = await postDeployCleanup({ deployer, fileContract, application });
console.log('deployment completed', { cleanupResult });
await deletePreviousFiles({ deployer, fileContract, application, isLive })
await withdrawAvailableBalance({ deployer, fileContract });
}
}
deployApp().catch((e) => console.error(e));