Skip to content

Commit

Permalink
remove *nix assumption, use node script
Browse files Browse the repository at this point in the history
  • Loading branch information
chadoh committed Jan 29, 2024
1 parent 2518dec commit 487345d
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 100 deletions.
102 changes: 102 additions & 0 deletions initialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'dotenv/config';
import { mkdirSync, readdirSync, statSync, writeFileSync } from 'fs';
import { execSync } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';

console.log("###################### Initializing ########################");

// Get dirname (equivalent to the Bash version)
const __filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(__filename);

// variable for later setting pinned version of soroban in "$(dirname/target/bin/soroban)"
const soroban = "soroban"

// Function to execute and log shell commands
function exe(command) {
console.log(command);
execSync(command, { stdio: 'inherit' });
}

function fund_all() {
exe(`${soroban} keys generate ${process.env.SOROBAN_ACCOUNT}`);
exe(`${soroban} keys fund ${process.env.SOROBAN_ACCOUNT}`);
}

function build_all() {
exe(`rm ${dirname}/target/wasm32-unknown-unknown/release/*.wasm`);
exe(`rm ${dirname}/target/wasm32-unknown-unknown/release/*.d`);
exe(`${soroban} contract build`);
}

function filenameNoExtension(filename) {
return path.basename(filename, path.extname(filename));
}

function deploy(wasm) {
exe(`(${soroban} contract deploy --wasm ${wasm} --ignore-checks) > ${dirname}/.soroban/contract-ids/${filenameNoExtension(wasm)}.txt`);
}

function deploy_all() {
const contractsDir = `${dirname}/.soroban/contract-ids`;
mkdirSync(contractsDir, { recursive: true });

const wasmFiles = readdirSync(`${dirname}/target/wasm32-unknown-unknown/release`).filter(file => file.endsWith('.wasm'));

wasmFiles.forEach(wasmFile => {
deploy(`${dirname}/target/wasm32-unknown-unknown/release/${wasmFile}`);
});
}

function bind(contract) {
const filenameNoExt = filenameNoExtension(contract);
exe(`${soroban} contract bindings typescript --contract-id $(cat ${contract}) --output-dir ${dirname}/packages/${filenameNoExt} --overwrite`);
}

function bind_all() {
const contractIdsDir = `${dirname}/.soroban/contract-ids`;
const contractFiles = readdirSync(contractIdsDir);

contractFiles.forEach(contractFile => {
const contractPath = path.join(contractIdsDir, contractFile);
if (statSync(contractPath).size > 0) { // Check if file is not empty
bind(contractPath);
}
});
}

function importContract(contract) {
const filenameNoExt = filenameNoExtension(contract);
const outputPath = `${dirname}/src/contracts/${filenameNoExt}.ts`;
const importContent = `import * as Client from '${filenameNoExt}';\n` +
`import { rpcUrl } from './util';\n\n` +
`export default new Client.Contract({\n` +
` ...Client.networks.standalone,\n` +
` rpcUrl,\n` +
`});\n`;

writeFileSync(outputPath, importContent);
console.log(`Created import for ${filenameNoExt}`);
}

function import_all() {
const contractIdsDir = `${dirname}/.soroban/contract-ids`;
const contractFiles = readdirSync(contractIdsDir);

contractFiles.forEach(contractFile => {
const contractPath = path.join(contractIdsDir, contractFile);
if (statSync(contractPath).size > 0) { // Check if file is not empty
importContract(contractPath);
}
});
}

exe('echo $SOROBAN_ACCOUNT $SOROBAN_NETWORK_PASSPHRASE $SOROBAN_RPC_URL');

// Calling the functions (equivalent to the last part of your bash script)
fund_all();
build_all();
deploy_all();
bind_all();
import_all();
97 changes: 0 additions & 97 deletions initialize.sh

This file was deleted.

15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"type": "module",
"version": "0.0.1",
"scripts": {
"dev": "./initialize.sh && astro dev",
"start": "./initialize.sh && astro dev",
"init": "node initialize.js",
"dev": "npm run init && astro dev",
"start": "npm run init && astro dev",
"build": "astro check && astro build",
"preview": "astro preview",
"astro": "astro"
Expand All @@ -16,5 +17,8 @@
},
"workspaces": [
"packages/*"
]
],
"devDependencies": {
"dotenv": "^16.4.1"
}
}

0 comments on commit 487345d

Please sign in to comment.