Skip to content

Commit

Permalink
Merge pull request #5 from valgaze/goodtweaks
Browse files Browse the repository at this point in the history
Fix bug in template, cli tidy-up
  • Loading branch information
valgaze authored Sep 4, 2021
2 parents a75232f + e5a048a commit abaeed2
Show file tree
Hide file tree
Showing 8 changed files with 18,475 additions and 245 deletions.
18,611 changes: 18,395 additions & 216 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"start": "ts-node src/launcher",
"start:dist": "npm run setup && cd dist && node src/launcher",
"write:json": "cd dist && node src/cli write:json",
"build": "tsc",
"setup": "npm i && tsc"
"build:global": "tsc",
"build": "./node_modules/typescript/bin/tsc",
"setup": "npm i && npm run build"
},
"keywords": [
"bot",
Expand All @@ -31,6 +32,10 @@
"files": [
"dist/**"
],
"repository": {
"type": "git",
"url": "https://github.com/valgaze/speedybot.git"
},
"devDependencies": {
"@types/node": "^13.13.52",
"@types/tape": "^4.13.2",
Expand All @@ -39,4 +44,4 @@
"ts-node": "^8.10.1",
"typescript": "^3.8.3"
}
}
}
16 changes: 16 additions & 0 deletions src/cli/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
askToken: 'What is your bot token? (leave blank to set later) ',
needsToken: `
Note: If you need a token, see below
> Create a new bot (recommended): https://developer.webex.com/my-apps/new/bot
> Regenerate access token from existing bot: https://developer.webex.com/my-apps`,
askDirectory: `What directory to install speedybot? (defaults to '$[directory]') `,
directoryAlreadyExists: `It looks like that directory already exists :(
Pick a different location & see here for instructions:
https://github.com/valgaze/speedybot/blob/master/quickstart.md`,

}
41 changes: 26 additions & 15 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@ npx speedybot <access_token> <dir_name> # sets access token and scaffolds + init
npx speedybot <access_token> # sets access token and scaffolds + inits from "speedybot"
npx speedybot # show help
*/
import { help, ascii_art, good, bad, askQuestion, loud } from './../../util/logger'
import { scaffoldGitclone, writeJSON, setupRepo, placeholder } from './../../util'
import { help, ascii_art, log, good, askQuestion, loud } from './../../util/logger'
import { scaffoldGitclone, writeJSON, setupRepo, placeholder, fillTemplate } from './../../util'
import { version } from './../../package.json';
import { resolve } from 'path';
import language from './en'
const [, , rootArg, token = null, directory = null] = process.argv; // Todo: get a proper arg parser
const command = rootArg;

main(command);

main(command, language);


// CLI runner
export async function main(command) {
export async function main(command, LANGUAGE) {
const normalized = command ? command.toLowerCase() : 'help'

if (normalized === '-v' || normalized === 'version') {
return log(version)
}

if (normalized === 'help') {
help()
}
Expand All @@ -30,19 +38,14 @@ export async function main(command) {
}

if (setupConfig.token === placeholder) {
const askToken = await askQuestion('What is your bot token? (leave blank to set later) ')
const askToken = await askQuestion(LANGUAGE.askToken)
if (askToken) {
setupConfig.token = askToken
} else {
loud(`
Note: If you need a token, see below
> Create a new bot (recommended): https://developer.webex.com/my-apps/new/bot
> Regenerate access token from existing bot: https://developer.webex.com/my-apps`)
loud(LANGUAGE.needsToken)
}
if (setupConfig.directory === 'speedybot') {
const askDir = await askQuestion(`What directory to install speedybot? (defaults to '${setupConfig.directory}') `)
const askDir = await askQuestion(fillTemplate(LANGUAGE.askDirectory, { directory: setupConfig.directory }))
if (askDir) {
setupConfig.directory = askDir
}
Expand All @@ -51,7 +54,14 @@ Note: If you need a token, see below

// Clone repo
try {
await scaffoldGitclone(setupConfig.directory)
loud(`Installing to '${setupConfig.directory}'...`)
try {
await scaffoldGitclone(setupConfig.directory)
} catch (e) {
loud(LANGUAGE.directoryAlreadyExists)
log(e.message)
return process.exit(1)
}
const commandList = [
`npm run setup`
]
Expand All @@ -60,11 +70,12 @@ Note: If you need a token, see below
commandList.push(`npm run write:json ${setupConfig.token}`)
commandList.push('npm start')
}

await setupRepo(setupConfig.directory, commandList)

finale(setupConfig.directory, setupConfig.token === placeholder)
} catch (e) {
bad(e)
loud(e)
}
}

Expand All @@ -85,7 +96,7 @@ Note: If you need a token, see below
}
export const finale = (directory: string, setToken?: boolean, setDependencies?: boolean) => {

good(`SETUP COMPLETE!s
good(`SETUP COMPLETE!
${setDependencies ? `- Enter the directory (${directory}) and run: npm run setup` : ''}
${setToken ? `- Make sure to set the 'token' field in ${directory}/settings/config.json` : ''}
Expand Down
27 changes: 24 additions & 3 deletions test/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test("Should pick a random choice and fill the template", (t) => {

const payload = {
phrases: ["Hey $[name], how's it going?", "Hi $[name], here's your $[flavor]"],
template: {
template: {
name: 'Joe',
flavor: 'mint'
}
Expand All @@ -19,7 +19,28 @@ test("Should pick a random choice and fill the template", (t) => {
if (renderedChoices.includes(res)) {
pass = true;
}

t.deepEqual(true, pass);

t.deepEqual(true, pass);
t.end()
});

test("Should take a string & fill in the template (& not crash if key name happened to be contaied 😶)", (t) => {
let pass = false;

const payload = {
phrases: `What directory to install speedybot ? (defaults to '$[directory]')`,
template: {
directory: 'speedybot',
}
}
const { phrases, template } = payload

const renderedChoices = [`What directory to install speedybot ? (defaults to 'speedybot')`];
const res = fillTemplate(phrases, template)
if (renderedChoices.includes(res)) {
pass = true;
}

t.deepEqual(true, pass);
t.end()
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"include": [
"src/**/*",
"test/**/*"
"test/**/*",
"package.json"
],
"exclude": [
"src/frontend",
Expand Down
9 changes: 3 additions & 6 deletions util/file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { execSync } from 'child_process'
import { resolve } from 'path'
import { bad } from './logger';
import { writeFileSync } from 'fs';
export const rootDir = resolve(__dirname, '..', '..') // Tricky: from /dist in CLI

Expand All @@ -12,21 +11,19 @@ export const scaffoldGitclone = (targetDir: string = 'speedybot') => {
cwd: resolve(process.cwd()),
});
} catch (e) {
bad(e)
process.exit(1)
throw e
}
}

export const setupRepo = (targetDir, commandSequence: string[]) => {
export const setupRepo = (targetDir: string, commandSequence: string[]) => {
const command = `cd ${targetDir} && ${commandSequence.join(' && ')}`
try {
execSync(command, {
stdio: [0, 1, 2],
cwd: resolve(process.cwd()),
});
} catch (e) {
bad(e)
process.exit(1)
throw e
}
}

Expand Down
2 changes: 1 addition & 1 deletion util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const fillTemplate = (utterances: string | string[], template: Base): str
target: string,
replacement: string
): string => {
if (!utterance.includes(target)) {
if (!utterance.includes(`$[${target}]`)) {
return utterance
} else {
return replacer(
Expand Down

0 comments on commit abaeed2

Please sign in to comment.