UNPKG

@brighthustle/adonisjs-whatsapp

Version:

Connect your WhatsApp Cloud API with AdonisJS

174 lines (173 loc) 5.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const helpers_1 = require("@poppinss/utils/build/helpers"); const CONFIG_PARTIALS_BASE = './config/partials'; /** * Prompt choices for the provider selection */ const PROVIDER_PROMPT_CHOICES = [ { name: 'lucid', message: 'Lucid', hint: ' (Uses Data Models)', }, { name: 'local', message: 'Local', hint: ' (Uses ENV variable)', }, ]; /** * Prompt choices for the api provider selection */ const API_PROVIDER_PROMPT_CHOICES = [ { name: 'cloud-api', message: 'Cloud Api', hint: ' (Uses Whatsapp Cloud Api)', }, { name: 'msg91', message: 'Msg 91', hint: ' (Uses Msg 91 API Provider)', }, ]; /** * Returns absolute path to the stub relative from the templates * directory */ function getStub(...relativePaths) { return (0, path_1.join)(__dirname, 'templates', ...relativePaths); } /** * Creates the model file */ function makeModel(projectRoot, app, sink, state) { const modelsDirectory = app.resolveNamespaceDirectory('models') || 'app/Models'; const modelPath = (0, path_1.join)(modelsDirectory, `${state.modelName}.ts`); const template = new sink.files.MustacheFile(projectRoot, modelPath, getStub('model.txt')); if (template.exists()) { sink.logger.action('create').skipped(`${modelPath} file already exists`); return; } template.apply(state).commit(); sink.logger.action('create').succeeded(modelPath); } /** * Create the migration file */ function makeMigration(projectRoot, app, sink, state) { const migrationsDirectory = app.directoriesMap.get('migrations') || 'database'; const migrationPath = (0, path_1.join)(migrationsDirectory, `${Date.now()}_${state.whatsappTableName}.ts`); const template = new sink.files.MustacheFile(projectRoot, migrationPath, getStub('migrations/whatsapp_config.txt')); if (template.exists()) { sink.logger.action('create').skipped(`${migrationPath} file already exists`); return; } template.apply(state).commit(); sink.logger.action('create').succeeded(migrationPath); } /** * Makes the auth config file */ function makeConfig(projectRoot, app, sink, state) { const configDirectory = app.directoriesMap.get('config') || 'config'; const configPath = (0, path_1.join)(configDirectory, 'whatsapp.ts'); const template = new sink.files.MustacheFile(projectRoot, configPath, getStub('config/whatsapp.txt')); template.overwrite = true; const partials = { wa_config: getStub(CONFIG_PARTIALS_BASE, `whatsapp-${state.provider}-${state.apiProvider}.txt`), }; template.apply(state).partials(partials).commit(); sink.logger.action('create').succeeded(configPath); } /** * Prompts user for the model name */ async function getModelName(sink) { return sink.getPrompt().ask('Enter model name to be used for whatsapp config', { validate(value) { return !!value.trim().length; }, }); } /** * Prompts user for the table name */ async function getMigrationConsent(sink, tableName) { return sink .getPrompt() .confirm(`Create migration for the ${sink.logger.colors.underline(tableName)} table?`); } /** * Prompts user to select the provider */ async function getProvider(sink) { return sink.getPrompt().choice('Select provider for whatsapp config', PROVIDER_PROMPT_CHOICES, { validate(choice) { return choice && choice.length ? true : 'Select the provider for configuration of whatsapp'; }, }); } /** * Prompts user to select the api provider */ async function getApiProvider(sink) { return sink .getPrompt() .choice('Select provider for whatsapp config', API_PROVIDER_PROMPT_CHOICES, { validate(choice) { return choice && choice.length ? true : 'Select the provider for configuration of whatsapp'; }, }); } /** * Instructions to be executed when setting up the package. */ async function instructions(projectRoot, app, sink) { const state = { whatsappTableName: '', whatsappSchemaName: '', provider: 'lucid', apiProvider: 'cloud-api', }; state.provider = await getProvider(sink); state.apiProvider = await getApiProvider(sink); /** * Make model when provider is lucid otherwise prompt for the database * table name */ if (state.provider === 'lucid') { const modelName = await getModelName(sink); state.modelName = helpers_1.string.pascalCase(helpers_1.string.singularize(modelName.replace(/(\.ts|\.js)$/, ''))); state.whatsappTableName = helpers_1.string.pluralize(helpers_1.string.snakeCase(state.modelName)); state.modelReference = helpers_1.string.camelCase(helpers_1.string.singularize(state.modelName)); state.modelNamespace = `${app.namespacesMap.get('models') || 'App/Models'}/${state.modelName}`; const migrationConsent = await getMigrationConsent(sink, state.whatsappTableName); /** * Pascal case */ const camelCaseSchemaName = helpers_1.string.camelCase(`${state.whatsappTableName}_schema`); state.whatsappSchemaName = `${camelCaseSchemaName .charAt(0) .toUpperCase()}${camelCaseSchemaName.slice(1)}`; /** * Make model when prompted for it */ if (state.modelName) { makeModel(projectRoot, app, sink, state); } /** * Make users migration file */ if (migrationConsent) { makeMigration(projectRoot, app, sink, state); } } /** * Make config file */ makeConfig(projectRoot, app, sink, state); } exports.default = instructions;