-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: collect metadata from console and upload
- Loading branch information
Showing
4 changed files
with
236 additions
and
9 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
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,161 @@ | ||
import { put } from "@vercel/blob"; | ||
import dotenv from "dotenv"; | ||
import moment from "moment"; | ||
|
||
import path from "path"; | ||
import * as readline from "readline"; | ||
|
||
export interface RoundMetadata { | ||
roundId: string; | ||
description: string; | ||
startsAt: Date; | ||
registrationEndsAt: Date; | ||
votingStartsAt: Date; | ||
votingEndsAt: Date; | ||
} | ||
|
||
interface IUploadMetadataProps { | ||
data: RoundMetadata; | ||
name: string; | ||
} | ||
|
||
dotenv.config({ path: path.resolve(import.meta.dirname, "../.env") }); | ||
|
||
export async function uploadRoundMetadata({ data, name }: IUploadMetadataProps): Promise<string> { | ||
try { | ||
const blob = await put(name, JSON.stringify(data), { | ||
access: "public", | ||
token: process.env.BLOB_READ_WRITE_TOKEN, | ||
}); | ||
|
||
return blob.url; | ||
} catch (e) { | ||
throw new Error(e as string); | ||
} | ||
} | ||
|
||
export async function collectMetadata(): Promise<RoundMetadata> { | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
const askRoundId = () => | ||
new Promise<string>((resolve) => { | ||
rl.question("How would you name your round? ", (answer) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`Your roundId is: ${answer}`); | ||
resolve(answer); | ||
}); | ||
}); | ||
|
||
const askDescription = () => | ||
new Promise<string>((resolve) => { | ||
rl.question("Could you briefly introduce this round? ", (answer) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`Your round description is: ${answer}`); | ||
resolve(answer); | ||
}); | ||
}); | ||
|
||
const askStartTime = () => | ||
new Promise<Date>((resolve, reject) => { | ||
rl.question("When would you like to start this round? (Please respond in the format yyyy-mm-dd) ", (answer) => { | ||
const isValid = moment(answer, "yyyy-mm-dd").isValid(); | ||
|
||
if (!isValid) { | ||
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors | ||
reject("Please answer in valid format."); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log("You would like to start this round at:", answer); | ||
resolve(new Date(answer)); | ||
}); | ||
}); | ||
|
||
const askRegistrationEndTime = () => | ||
new Promise<Date>((resolve, reject) => { | ||
rl.question( | ||
"When would you like to end the registration for applications? (Please respond in the format yyyy-mm-dd) ", | ||
(answer) => { | ||
const isValid = moment(answer, "yyyy-mm-dd").isValid(); | ||
|
||
if (!isValid) { | ||
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors | ||
reject("Please answer in valid format."); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(`Your application registration end date for this round is: ${answer}`); | ||
resolve(new Date(answer)); | ||
}, | ||
); | ||
}); | ||
|
||
const askVotingStartTime = () => | ||
new Promise<Date>((resolve, reject) => { | ||
rl.question( | ||
"When would you like to start the voting for this round? (Please respond in the format yyyy-mm-dd) ", | ||
(answer) => { | ||
const isValid = moment(answer, "yyyy-mm-dd").isValid(); | ||
|
||
if (!isValid) { | ||
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors | ||
reject("Please answer in valid format."); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(`Your voting start date for this round is: ${answer}`); | ||
resolve(new Date(answer)); | ||
}, | ||
); | ||
}); | ||
|
||
const askVotingEndTime = () => | ||
new Promise<Date>((resolve, reject) => { | ||
rl.question( | ||
"When would you like to end the voting for this round? (Please respond in the format yyyy-mm-dd) ", | ||
(answer) => { | ||
const isValid = moment(answer, "yyyy-mm-dd").isValid(); | ||
|
||
if (!isValid) { | ||
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors | ||
reject("Please answer in valid format."); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(`Your voting end date for this round is: ${answer}`); | ||
resolve(new Date(answer)); | ||
}, | ||
); | ||
}); | ||
|
||
const roundId = await askRoundId(); | ||
const description = await askDescription(); | ||
const startsAt = await askStartTime(); | ||
const registrationEndsAt = await askRegistrationEndTime(); | ||
const votingStartsAt = await askVotingStartTime(); | ||
const votingEndsAt = await askVotingEndTime(); | ||
|
||
rl.close(); | ||
|
||
return { | ||
roundId, | ||
description, | ||
startsAt, | ||
registrationEndsAt, | ||
votingStartsAt, | ||
votingEndsAt, | ||
}; | ||
} | ||
|
||
async function main(): Promise<void> { | ||
const metadata = await collectMetadata(); | ||
const url = await uploadRoundMetadata({ data: metadata, name: `${metadata.roundId}.json` }); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log("The url of uploaded metadata is:", url); | ||
} | ||
|
||
main(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.