snips-sam
Version:
The Snips Assistant Manager
64 lines (53 loc) • 2.3 kB
text/typescript
import * as yargs from 'yargs';
import chalk from 'chalk';
import { Snipsfile } from './../../models/snipsfile';
import { Skill } from './../../models/skill';
import { Binding } from './../../models/binding';
import { toString as langToString } from './../../models/language';
import { ArgParser } from '../../utils/argparser';
import { cli } from '../../cli';
import { Exec } from '../../utils/exec';
export const command = 'skill [name]';
export const desc = 'Test a skill';
const head = require('lodash.head');
export const handler = async (argv: yargs.Argv) => {
const parser = new ArgParser(argv);
const skillName = argv.name;
const intentName = parser.getValue('intent');
const slots = parser.getArgd(['intent']);
if (!skillName || !intentName) {
const message = (!skillName) ? 'No skill name provided.' : 'No intent provided.';
cli.stream.error(chalk`${message}
Usage: {blue sam test skill <skillname> intent=<intentname> <slot1name>=<slot1value> ...}
Run {blue sam test skill help} for additional help.`);
return;
}
const snipsfile = new Snipsfile();
snipsfile.parse();
const skill = head(snipsfile.skills.filter((s: Skill) => { return s.name === skillName; }));
if (!skill) {
cli.stream.error(chalk`Skill {blue ${skillName}} not found in Snipsfile.`);
return ;
}
const binding = head(skill.bindings.filter((b: Binding) => { return b.intentName === intentName; }));
if (!binding) {
cli.stream.error(chalk`Skill {blue ${skillName}} does not seem to handle intents named {blue ${intentName}}.`);
return ;
}
cli.stream.print(chalk`Testing skill {blue ${skillName}} with intent {blue ${intentName}}...`);
cli.stream.print('');
const child = Exec.exec(skill.language, binding.action, slots);
child.stdout.on('data', (data) => {
cli.stream.print('Output:');
cli.stream.print('======================================');
cli.stream.print(data);
cli.stream.print('======================================');
});
child.stdout.on('end', (code) => {
let message = 'Testing complete';
if (code !== undefined) {
message += ` with exit code ${code}`;
}
cli.stream.print(message);
});
};