This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BEC-270 | Implement import-receipt (#23)
* Implement import-receipt script
- Loading branch information
1 parent
84b5c60
commit ab43a0e
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |