Skip to content

Commit

Permalink
fix(cli): 🔥 create script - templates
Browse files Browse the repository at this point in the history
  • Loading branch information
leifermendez committed Dec 10, 2022
1 parent 1036273 commit eebc3c9
Show file tree
Hide file tree
Showing 29 changed files with 417 additions and 61 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ jobs:
- name: Release @bot-whatsapp/create-bot-whatsapp
run: yarn node ./scripts/release.js --name=create-bot-whatsapp --version= --token="${{ secrets.NPM_TOKEN }}"

- name: Release @bot-whatsapp/create-bot-whatsapp
run: yarn node ./scripts/release.js --name=create-bot-whatsapp --version= --token="${{ secrets.NPM_TOKEN }}"

- name: Release @bot-whatsapp/database
run: yarn node ./scripts/release.js --name=database --version= --token="${{ secrets.NPM_TOKEN }}"

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules
/packages/create-bot-whatsapp/starters
/packages/*/node_modules
/packages/*/dist
/packages/*/docs/dist
Expand Down Expand Up @@ -28,6 +29,8 @@ tmp/
!.yarn/plugins/@yarnpkg/plugin-postinstall.cjs
.fleet/
example-app*/
base-*/
!starters/apps/base-*/
qr.svg
package-lock.json
yarn-error.log
Expand Down
10 changes: 7 additions & 3 deletions packages/cli/create-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ const copyFiles = async (from, to) => {
}
}

const copyBaseApp = async () => {
const BASEP_APP_PATH_FROM = `${process.cwd()}/starters/apps/base`
const BASEP_APP_PATH_TO = `${process.cwd()}/example-app-base`
/**
* Copiar directorio con archivos
* @param {*} templateName
*/
const copyBaseApp = async (templateName = null) => {
const BASEP_APP_PATH_FROM = `${process.cwd()}/starters/apps/${templateName}`
const BASEP_APP_PATH_TO = `${process.cwd()}/${templateName}`
await copyFiles(BASEP_APP_PATH_FROM, BASEP_APP_PATH_TO)
}

Expand Down
74 changes: 38 additions & 36 deletions packages/cli/interactive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,18 @@ const startInteractive = async () => {
const questions = [
{
type: 'text',
name: 'exampeOpt',
message:
'Quieres crear una app de ejemplo "example-app-example"? (Y/n)',
},
// {
// type: 'text',
// name: 'dependencies',
// message:
// 'Quieres actualizar las librerias "whatsapp-web.js"? (Y/n)',
// },
{
type: 'text',
name: 'cleanTmp',
message: 'Quieres limpiar la session del bot? (Y/n)',
name: 'outDir',
message: 'Quieres crear un bot? (Y/n)',
},
{
type: 'multiselect',
name: 'providerWs',
message: 'Proveedor de Whatsapp',
choices: [
{ title: 'whatsapp-web.js', value: 'whatsapp-web.js' },
{ title: 'whatsapp-web.js (gratis)', value: 'wweb' },
{ title: 'Twilio', value: 'twilio' },
{ title: 'Baileys (gratis)', value: 'bailey', disabled: true },
{ title: 'API Oficial (Meta)', value: 'meta', disabled: true },
{ title: 'Twilio', value: 'twilio', disabled: true },
],
max: 1,
hint: 'Espacio para selecionar',
Expand All @@ -43,9 +32,10 @@ const startInteractive = async () => {
name: 'providerDb',
message: 'Cual base de datos quieres usar',
choices: [
{ title: 'JSONFile', value: 'json' },
{ title: 'MySQL', value: 'mysql', disabled: true },
{ title: 'Mongo', value: 'mongo', disabled: true },
{ title: 'Memory', value: 'memory' },
{ title: 'Mongo', value: 'mongo' },
{ title: 'MySQL', value: 'mysql' },
{ title: 'Json', value: 'json', disabled: true },
],
max: 1,
hint: 'Espacio para selecionar',
Expand All @@ -64,11 +54,12 @@ const startInteractive = async () => {
const {
dependencies = '',
cleanTmp = '',
exampeOpt = '',
outDir = '',
providerDb = [],
providerWs = [],
} = response
/**
* @deprecated
* Question
* @returns
*/
Expand All @@ -83,6 +74,7 @@ const startInteractive = async () => {
}

/**
* @deprecated
* Question
* @returns
*/
Expand All @@ -96,17 +88,28 @@ const startInteractive = async () => {
}
}

const createApp = async () => {
const answer = exampeOpt.toLowerCase() || 'n'
/**
* Crear una app (copiar plantilla)
* @returns
*/
const createApp = async (templateName = null) => {
if (!templateName)
throw new Error('TEMPLATE_NAME_INVALID: ', templateName)
const answer = outDir.toLowerCase() || 'n'
if (answer.includes('n')) return true

if (answer.includes('y')) {
await copyBaseApp()
return true
await copyBaseApp(templateName)
return outDir
}
}

/**
* Selccionar Provider (meta, twilio, etc...)
* @returns
*/
const vendorProvider = async () => {
const [answer] = providerWs
if (!providerWs.length) {
console.log(
red(
Expand All @@ -116,11 +119,15 @@ const startInteractive = async () => {
process.exit(1)
}
console.log(yellow(`'Deberia crer una carpeta en root/provider'`))
return true
return answer
}

/**
* Selecionar adaptador de base de datos
* @returns
*/
const dbProvider = async () => {
const answer = providerDb
const [answer] = providerDb
if (!providerDb.length) {
console.log(
red(
Expand All @@ -129,18 +136,13 @@ const startInteractive = async () => {
)
process.exit(1)
}
if (answer === 'json') {
console.log('Deberia crer una carpeta en root/data')
return 1
}
return answer
}

await createApp()
await installOrUdpateDep()
await cleanAllSession()
await vendorProvider()
await dbProvider()
await jsonConfig()
const providerAdapter = await vendorProvider()
const dbAdapter = await dbProvider()
const NAME_DIR = ['base', providerAdapter, dbAdapter].join('-')
await createApp(NAME_DIR)
}

module.exports = { startInteractive }
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bot-whatsapp/cli",
"version": "0.0.2-alpha.0",
"version": "0.0.4-alpha.0",
"description": "",
"main": "index.js",
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-bot-whatsapp/bin/create.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node
const main = require('../lib/bin/bundle.create.cjs')
const main = require('../lib/bundle.create-bot-whatsapp.cjs')
main()
14 changes: 6 additions & 8 deletions packages/create-bot-whatsapp/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const { startInteractive } = require('@bot-whatsapp/cli')
/**
* Main function
* Voy a llamar directo a CLI
* Temporalmente luego mejoro esta
* parte
* @returns
*/
const main = () => {
console.clear()
console.log(``)
console.log(`[PostInstall]: Este es el main function.`)
console.log(`[PostInstall]: 👌 Aqui podrias instalar cosas`)
console.log(``)
}
const main = () => startInteractive()

module.exports = main
13 changes: 8 additions & 5 deletions packages/create-bot-whatsapp/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "create-bot-whatsapp",
"version": "0.0.9-alpha.0",
"version": "0.0.13-alpha.0",
"description": "",
"main": "./lib/bin/bundle.create.cjs",
"main": "./lib/bundle.create-bot-whatsapp.cjs",
"files": [
"./starters/",
"./bin/create.js",
"./lib/bundle.create-bot-whatsapp.cjs"
],
"bin": "./bin/create.js",
"dependencies": {
"@bot-whatsapp/cli": "*"
},
"bin": {
"bot": "./lib/bin/bundle.create.cjs"
}
}
11 changes: 9 additions & 2 deletions packages/create-bot-whatsapp/rollup-create.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const banner = require('../../config/banner.rollup.json')
const commonjs = require('@rollup/plugin-commonjs')
const copy = require('rollup-plugin-copy')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const { join } = require('path')

const PATH = join(__dirname, 'lib', 'bin', 'bundle.create.cjs')
const PATH = join(__dirname, 'lib', 'bundle.create-bot-whatsapp.cjs')

module.exports = {
input: join(__dirname, 'index.js'),
Expand All @@ -12,5 +13,11 @@ module.exports = {
file: PATH,
format: 'cjs',
},
plugins: [commonjs(), nodeResolve()],
plugins: [
copy({
targets: [{ src: 'starters/*', dest: join(__dirname, 'starters') }],
}),
commonjs(),
nodeResolve(),
],
}
5 changes: 4 additions & 1 deletion packages/database/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "@bot-whatsapp/database",
"version": "0.0.2-alpha.0",
"version": "0.0.4-alpha.0",
"description": "Esto es el conector a mysql, pg, mongo",
"main": "./lib/mock/index.cjs",
"keywords": [],
"author": "",
"license": "ISC",
"files": [
"./lib/"
],
"dependencies": {
"dotenv": "^16.0.3",
"mongodb": "^4.11.0",
Expand Down
5 changes: 4 additions & 1 deletion packages/provider/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "@bot-whatsapp/provider",
"version": "0.0.7-alpha.0",
"version": "0.0.10-alpha.0",
"description": "Esto es el conector a Twilio, Meta, etc...",
"main": "./lib/mock/index.cjs",
"keywords": [],
"author": "",
"license": "ISC",
"files": [
"./lib/"
],
"dependencies": {
"@bot-whatsapp/bot": "*",
"qr-image": "^3.2.0"
Expand Down
12 changes: 12 additions & 0 deletions starters/apps/base-twilio-memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### BASE APP

Este bot contiene un flujo basico en el cual una persona (cliente) escribe **"hola"** y el bot responde.
- Bienvenido a mi tienda
- Como puedo ayudarte?
- Tengo: Zapatos Bolsos etc..

------
- [Discord](https://link.codigoencasa.com/DISCORD)
- [Twitter](https://twitter.com/leifermendez)
- [Youtube](https://www.youtube.com/watch?v=5lEMCeWEJ8o&list=PL_WGMLcL4jzWPhdhcUyhbFU6bC0oJd2BR)
- [Telegram](https://t.me/leifermendez)
28 changes: 28 additions & 0 deletions starters/apps/base-twilio-memory/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const {
createBot,
createProvider,
createFlow,
addKeyword,
} = require('@bot-whatsapp/bot')

const WebWhatsappProvider = require('@bot-whatsapp/provider/web-whatsapp')
const MockAdapter = require('@bot-whatsapp/database/mock')

const flowPrincipal = addKeyword(['hola', 'ole', 'HOLA'])
.addAnswer('Bienvenido a mi tienda')
.addAnswer('Como puedo ayudarte?')
.addAnswer(['Tengo:', 'Zapatos', 'Bolsos', 'etc..'])

const main = async () => {
const adapterDB = new MockAdapter()
const adapterFlow = createFlow([flowPrincipal])
const adapterProvider = createProvider(WebWhatsappProvider)

createBot({
flow: adapterFlow,
provider: adapterProvider,
database: adapterDB,
})
}

main()
16 changes: 16 additions & 0 deletions starters/apps/base-twilio-memory/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "bot-whatsapp-base",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"pre-copy": "cd .. && yarn run copy.lib example-app-base",
"start": "node app.js"
},
"keywords": [],
"dependencies": {
"whatsapp-web.js": "^1.18.3"
},
"author": "",
"license": "ISC"
}
12 changes: 12 additions & 0 deletions starters/apps/base-twilio-mongo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### BASE APP

Este bot contiene un flujo basico en el cual una persona (cliente) escribe **"hola"** y el bot responde.
- Bienvenido a mi tienda
- Como puedo ayudarte?
- Tengo: Zapatos Bolsos etc..

------
- [Discord](https://link.codigoencasa.com/DISCORD)
- [Twitter](https://twitter.com/leifermendez)
- [Youtube](https://www.youtube.com/watch?v=5lEMCeWEJ8o&list=PL_WGMLcL4jzWPhdhcUyhbFU6bC0oJd2BR)
- [Telegram](https://t.me/leifermendez)
28 changes: 28 additions & 0 deletions starters/apps/base-twilio-mongo/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const {
createBot,
createProvider,
createFlow,
addKeyword,
} = require('@bot-whatsapp/bot')

const WebWhatsappProvider = require('@bot-whatsapp/provider/web-whatsapp')
const MockAdapter = require('@bot-whatsapp/database/mock')

const flowPrincipal = addKeyword(['hola', 'ole', 'HOLA'])
.addAnswer('Bienvenido a mi tienda')
.addAnswer('Como puedo ayudarte?')
.addAnswer(['Tengo:', 'Zapatos', 'Bolsos', 'etc..'])

const main = async () => {
const adapterDB = new MockAdapter()
const adapterFlow = createFlow([flowPrincipal])
const adapterProvider = createProvider(WebWhatsappProvider)

createBot({
flow: adapterFlow,
provider: adapterProvider,
database: adapterDB,
})
}

main()
Loading

0 comments on commit eebc3c9

Please sign in to comment.