Skip to content

Commit

Permalink
backend: Refactored the copy-package-id script.
Browse files Browse the repository at this point in the history
  • Loading branch information
kkomelin committed May 15, 2024
1 parent d2ffc39 commit b526062
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions packages/backend/scripts/copy-package-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@ const main = async () => {
const sourceFile = sourceFilePath(network, DEPLOYED_MODULE_NAME);
const targetFile = targetFilePath();

// Read SuiBase packageId file.
const data = await promises.readFile(sourceFile, "utf8");
const packageId = JSON.parse(data)[0];
// Read package ID from SuiBase packageId file.
const packageId = await readPackageId(sourceFile);

// Create .env.local file if it doesn't exist.
try {
await promises.writeFile(targetFile, "", { flag: "wx" });
} catch {}
await createFileIfNecessary(targetFile);

// Add VITE_CONTRACT_PACKAGE_ID variable to .env.local or update its value if it exists.
const envFileWriter = new EnvFileWriter(targetFile, false);
await envFileWriter.parse();
envFileWriter.set("VITE_CONTRACT_PACKAGE_ID", packageId);
await envFileWriter.save();
await setEnvVar(targetFile, "VITE_CONTRACT_PACKAGE_ID", packageId);
};

const sourceFilePath = (network, deployedModuleName) => {
Expand All @@ -58,6 +52,45 @@ const getNetworkFromArgs = () => {
}
};

/**
* Read package ID from SuiBase packageId file.
*
* @param {string} sourceFile
* @returns
*/
const readPackageId = async (sourceFile) => {
const data = await promises.readFile(sourceFile, "utf8");
return JSON.parse(data)[0];
};

/**
* Create a file if it doesn't exist.
*
* @param {string} filePath
* @returns
*/
const createFileIfNecessary = async (filePath) => {
try {
await promises.writeFile(filePath, "", { flag: "wx" });
} catch {}
};

/**
* Set the environment variable in the .env.local file.
*
* @param {string} envFilePath
* @param {string} name
* @param {string} value
* @returns
*/
const setEnvVar = async (envFilePath, name, value) => {
const envFileWriter = new EnvFileWriter(envFilePath, false);
await envFileWriter.parse();
envFileWriter.set(name, value);
await envFileWriter.save();
};

// Main entry point.
main().catch((e) => {
console.error(e);
});

0 comments on commit b526062

Please sign in to comment.