Skip to content

Commit

Permalink
fix: fix to allow dialog without providing skill id for hosted skills (
Browse files Browse the repository at this point in the history
  • Loading branch information
kakhaUrigashvili authored Jun 24, 2020
1 parent ec32913 commit 13fa8db
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 95 deletions.
86 changes: 47 additions & 39 deletions lib/commands/dialog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const optionModel = require('@src/commands/option-model');
const CliError = require('@src/exceptions/cli-error');
const DialogReplayFile = require('@src/model/dialog-replay-file');
const ResourcesConfig = require('@src/model/resources-config');
const Manifest = require('@src/model/manifest');
const jsonView = require('@src/view/json-view');
const CONSTANTS = require('@src/utils/constants');
const profileHelper = require('@src/utils/profile-helper');
const Messenger = require('@src/view/messenger');
Expand Down Expand Up @@ -37,28 +37,28 @@ class DialogCommand extends AbstractCommand {

handle(cmd, cb) {
let dialogMode;
try {
dialogMode = this._dialogModeFactory(this._getDialogConfig(cmd));
} catch (err) {
Messenger.getInstance().error(err);
return cb(err);
}

const spinner = new SpinnerView();
spinner.start('Checking if skill is ready to simulate...');
helper.validateDialogArgs(dialogMode, (dialogArgsValidationError) => {
if (dialogArgsValidationError) {
spinner.terminate(SpinnerView.TERMINATE_STYLE.FAIL, 'Failed to validate command options');
Messenger.getInstance().error(dialogArgsValidationError);
return cb(dialogArgsValidationError);
this._getDialogConfig(cmd, (err, config) => {
if (err) {
Messenger.getInstance().error(err);
return cb(err);
}
spinner.terminate();
dialogMode.start((controllerError) => {
if (controllerError) {
Messenger.getInstance().error(controllerError);
return cb(controllerError);
dialogMode = this._dialogModeFactory(config);
const spinner = new SpinnerView();
spinner.start('Checking if skill is ready to simulate...');
helper.validateDialogArgs(dialogMode, (dialogArgsValidationError) => {
if (dialogArgsValidationError) {
spinner.terminate(SpinnerView.TERMINATE_STYLE.FAIL, 'Failed to validate command options');
Messenger.getInstance().error(dialogArgsValidationError);
return cb(dialogArgsValidationError);
}
cb();
spinner.terminate();
dialogMode.start((controllerError) => {
if (controllerError) {
Messenger.getInstance().error(controllerError);
return cb(controllerError);
}
cb();
});
});
});
}
Expand All @@ -68,51 +68,59 @@ class DialogCommand extends AbstractCommand {
* @param {Object} cmd encapsulates arguments provided to the dialog command.
* @return { skillId, locale, stage, profile, debug, replayFile, smapiClient, userInputs <for replay mode> }
*/
_getDialogConfig(cmd) {
_getDialogConfig(cmd, callback) {
const debug = Boolean(cmd.debug);
let { skillId, locale, stage, profile } = cmd;
profile = profileHelper.runtimeProfile(profile);
stage = stage || CONSTANTS.SKILL.STAGE.DEVELOPMENT;
let firstLocaleFromManifest;
let userInputs;
const smapiClient = new SmapiClient({ profile, doDebug: debug });
if (cmd.replay) {
const dialogReplayConfig = new DialogReplayFile(cmd.replay);
skillId = dialogReplayConfig.getSkillId();
if (!stringUtils.isNonBlankString(skillId)) {
throw 'Replay file must contain skillId';
return process.nextTick(callback(new CliError('Replay file must contain skillId')));
}
locale = dialogReplayConfig.getLocale();
if (!stringUtils.isNonBlankString(locale)) {
throw 'Replay file must contain locale';
return process.nextTick(callback(new CliError('Replay file must contain locale')));
}
try {
userInputs = this._validateUserInputs(dialogReplayConfig.getUserInput());
} catch (err) {
return callback(err);
}
userInputs = this._validateUserInputs(dialogReplayConfig.getUserInput());
} else {
if (!stringUtils.isNonBlankString(skillId)) {
try {
new ResourcesConfig(path.join(process.cwd(), CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG));
skillId = ResourcesConfig.getInstance().getSkillId(profile);
} catch (err) {
throw new CliError('Failed to read project resource file. Please run the command within a ask-cli project.');
return process.nextTick(callback(new CliError('Failed to read project resource file. '
+ 'Please run the command within a ask-cli project.')));
}
if (!stringUtils.isNonBlankString(skillId)) {
throw `Failed to obtain skill-id from project resource file ${CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG}`;
return process.nextTick(callback(new CliError('Failed to obtain skill-id from project '
+ `resource file ${CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG}`)));
}
const skillPackageSrc = ResourcesConfig.getInstance().getSkillMetaSrc(profile);
const manifestPath = path.join(skillPackageSrc, CONSTANTS.FILE_PATH.SKILL_PACKAGE.MANIFEST);
new Manifest(manifestPath);
[firstLocaleFromManifest] = Object.keys(Manifest.getInstance().getPublishingLocales());
}

}
smapiClient.skill.manifest.getManifest(skillId, stage, (err, res) => {
if (err) {
return callback(err);
}
if (res.statusCode >= 300) {
const error = jsonView.toString(res.body);
return callback(error);
}
[firstLocaleFromManifest] = Object.keys(res.body.manifest.publishingInformation.locales);
if (!locale && !process.env.ASK_DEFAULT_DEVICE_LOCALE && firstLocaleFromManifest) {
Messenger.getInstance().info(`Defaulting locale to the first value from the skill manifest: ${firstLocaleFromManifest}`);
}
locale = locale || process.env.ASK_DEFAULT_DEVICE_LOCALE || firstLocaleFromManifest;
if (!stringUtils.isNonBlankString(locale)) {
throw 'Locale has not been specified.';
}
}
const smapiClient = new SmapiClient({ profile, doDebug: debug });
return { skillId, locale, stage, profile, debug, replay: cmd.replay, smapiClient, userInputs };
callback(null, { skillId, locale, stage, profile, debug, replay: cmd.replay, smapiClient, userInputs });
});
}

_dialogModeFactory(config) {
Expand All @@ -127,7 +135,7 @@ class DialogCommand extends AbstractCommand {
userInputs.forEach((input) => {
const trimmedInput = input.trim();
if (!stringUtils.isNonBlankString(trimmedInput)) {
throw "Replay file's userInput cannot contain empty string.";
throw new CliError("Replay file's userInput cannot contain empty string.");
}
validatedInputs.push(trimmedInput);
});
Expand Down
Loading

0 comments on commit 13fa8db

Please sign in to comment.