Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
BEC-270 | Implement import-receipt (#23)
Browse files Browse the repository at this point in the history
* Implement import-receipt script
  • Loading branch information
Ashar2shahid authored Apr 15, 2022
1 parent 84b5c60 commit ab43a0e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"create-boilerplate": "ts-node src/create-boilerplate.ts",
"create-config": "ts-node src/create-config.ts",
"import-receipt": "ts-node src/import-receipt.ts",
"deploy-all": "yarn deploy-airnode; yarn deploy-airkeeper",
"deploy-airkeeper": "yarn ts-node src/deploy-airkeeper",
"deploy-airnode": "yarn ts-node src/deploy-airnode",
Expand Down
61 changes: 61 additions & 0 deletions src/import-receipt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { existsSync, mkdirSync, rmSync } from 'fs';
import { join } from 'path';
import { PromptObject } from 'prompts';
import { OperationsRepository } from './types';
import { runAndHandleErrors } from './utils/cli';
import { promptQuestions } from './utils/prompts';
import { readJsonFile, readOperationsRepository } from './utils/read-operations';
import { writeOperationsRepository } from './utils/write-operations';

const questions = (operationsRepository: OperationsRepository): PromptObject[] => {
return [
{
type: 'autocomplete',
name: 'name',
message: 'Which API Integration is the receipt for?',
choices: Object.keys(operationsRepository.apis).map((api) => ({ title: api, value: api })),
},
{
type: 'confirm',
name: 'receipt',
message: 'Have you placed the "receipt.json" in the "/import" folder?',
initial: true,
},
];
};

const main = async () => {
const importPath = join(__dirname, '..', 'import');
if (!existsSync(importPath)) mkdirSync(importPath, { recursive: true });

const operationsRepository = readOperationsRepository();
const response = await promptQuestions(questions(operationsRepository));
if (!response.receipt)
return console.log('Please place the "receipt.json" in the "/import" folder and re-run the command.');

const receipt = readJsonFile(join(importPath, 'receipt.json'));

const updatedOpsData = {
...operationsRepository,
apis: {
...operationsRepository.apis,
[response.name]: {
...operationsRepository.apis[response.name],
apiMetadata: {
...operationsRepository.apis[response.name].apiMetadata,
airnode: receipt.airnodeWallet.airnodeAddress,
xpub: receipt.airnodeWallet.airnodeXpub,
},
},
},
};

console.log(`🔑 HTTP Gateway API Url: ${receipt.api.httpGatewayUrl} (Please keep a note)`);
console.log(`🔑 HTTP Signed Data Gateway API Url: ${receipt.api.httpSignedDataGatewayUrl} (Please keep a note)`);
console.log(`Succesfully imported receipt for ${response.name}`);

writeOperationsRepository(updatedOpsData);
rmSync(importPath, { recursive: true });
};

runAndHandleErrors(main);

0 comments on commit ab43a0e

Please sign in to comment.