Skip to content

Commit

Permalink
fix: use promise for prompts in personalize create tracking script
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Sep 20, 2023
1 parent e97e7ca commit 626db83
Showing 1 changed file with 50 additions and 45 deletions.
95 changes: 50 additions & 45 deletions src/v0/destinations/personalize/scripts/create-trackingid.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,65 @@ const {
const { fromEnv } = require('@aws-sdk/credential-providers');

const readline = require('readline');
let rl = readline.createInterface(process.stdin, process.stdout);

function promtForInput(questionText, envName) {
if (!process.env[envName]) {
rl.question(questionText, (input) => (process.env[envName] = input));
require('dotenv').config()

async function promtForInput(rl, questionText) {
return new Promise((resolve) => {
rl.question(questionText, (input) => {
resolve(input);
});
});
}

async function checkEnvAndpromtForInput(rl, questionText, envVar) {
if (process.env[envVar]) {
return;
}
process.env[envVar] = await promtForInput(rl, questionText);
}
// get inputs from user
promtForInput('AWS Access Key ID: ', 'AWS_ACCESS_KEY_ID');
promtForInput('AWS Secret Access Key: ', 'AWS_SECRET_ACCESS_KEY');
promtForInput('AWS REGION: ', 'AWS_REGION');
promtForInput('Name of Dataset Group:', 'DATASET_GROUP_NAME');
promtForInput(
'Number of fields in Schema in addition to USER_ID, TIMESTAMP, ITEM_ID: ',
'NUMBER_OF_FIELDS',
);
rl.close();
const datasetGroup = process.env.DATASET_GROUP_NAME;
const columns = [];
const type = [];

// eslint-disable-next-line radix
const noOfFields = parseInt(process.env.NUMBER_OF_FIELDS);
columns[0] = 'USER_ID';
columns[1] = 'ITEM_ID';
columns[2] = 'TIMESTAMP';
type[0] = 'string';
type[1] = 'string';
type[2] = 'long';

for (let i = 4; i <= noOfFields + 3; i += 1) {
columns[i - 1] = readline.question(`Name of field no. ${i}: `);
type[i - 1] = readline.question(`Type of field ${columns[i - 1]}: `);

async function collectInputs(rl) {
await checkEnvAndpromtForInput(rl, 'AWS Access Key ID: ', 'AWS_ACCESS_KEY_ID');
await checkEnvAndpromtForInput(rl, 'AWS Secret Access Key: ', 'AWS_SECRET_ACCESS_KEY');
await checkEnvAndpromtForInput(rl, 'AWS REGION: ', 'AWS_REGION');
await checkEnvAndpromtForInput(rl, 'Name of Dataset Group: ', 'DATASET_GROUP_NAME');
await checkEnvAndpromtForInput(rl,
'Number of fields in Schema in addition to USER_ID, TIMESTAMP, ITEM_ID: ',
'NUMBER_OF_FIELDS',
);
}

let schema = {
type: 'record',
name: 'Interactions',
namespace: 'com.amazonaws.personalize.schema',
fields: [
{ name: 'USER_ID', type: 'string' },
{ name: 'ITEM_ID', type: 'string' },
{ name: 'TIMESTAMP', type: 'long' },
],
version: '1.0',
};

if (noOfFields > 0) {
for (let i = 3; i < noOfFields + 3; i += 1) {
schema.fields.push({ name: columns[i], type: type[i] });
async function collectFileds(rl) {
const noOfFields = parseInt(process.env.NUMBER_OF_FIELDS);
let schema = {
type: 'record',
name: 'Interactions',
namespace: 'com.amazonaws.personalize.schema',
fields: [
{ name: 'USER_ID', type: 'string' },
{ name: 'ITEM_ID', type: 'string' },
{ name: 'TIMESTAMP', type: 'long' },
],
version: '1.0',
};

for (let i = 4; i <= noOfFields + 3; i += 1) {
const fieldName = await promtForInput(rl, `Name of field no. ${i}: `);
const typeName = await promtForInput(rl, `Type of field ${fieldName}: `)
schema.fields.push({ name: fieldName, type: typeName });

}
return schema;
}


(async function () {
let rl = readline.createInterface(process.stdin, process.stdout);
await collectInputs(rl);
const schema = await collectFileds(rl);
rl.close();
const datasetGroup = process.env.DATASET_GROUP_NAME;
try {
const client = new PersonalizeClient({
region: process.env.AWS_REGION,
Expand Down

0 comments on commit 626db83

Please sign in to comment.