在 vorpal 动作期间调用另一个函数
calling another function during vorpal action
我刚刚开始使用 Vorpal.js 来帮助构建 CLI 工具,该工具最终将针对 API 端点执行 HTTP 请求。
我让它提出问题并验证数据输入,但是一旦我有了输入,我就让卢布调用另一个方法,该方法从请求的答案中收缩端点 url。
这是我的代码:
function apiEndpoint (env) {
if (env === 'INT') {
return API_ENDPOINT_INT;
} else if (env === 'TEST') {
return API_ENDPOINT_TEST;
} else if (env === 'LIVE') {
return API_ENDPOINT_LIVE
}
}
function constructRequestURL (params) {
let API_ENDPOINT = apiEndpoint(params.env);
let requestURL = `${API_GATEWAY}${API_ENDPOINT}?asset_uri=${params.asset_uri}&${site_name}&${asset_type}&${event_type}`
return requestURL;
}
vorpal
.command('render')
.option('--dave')
.action(function (args, cb) {
const self = this;
this.prompt(questions)
.then (function (result) {
// self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
let requestURL = constructRequestURL(result);
self.log(`Request URL: ${requestURL}`);
cb();
})
})
vorpal
.delimiter('AMP ReRenderer$')
.show();
这条注释行有效
// self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
然而调用 constructUrl
函数时什么也没有发生,cli 工具只是存在。
我想这可能是范围问题?
没有。它可能正在默默地失败。始终确保您 catch
兑现承诺。
vorpal
.command('render')
.option('--dave')
.action(function (args, cb) {
const self = this;
this.prompt(questions)
.then (function (result) {
// self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
let requestURL = constructRequestURL(result);
self.log(`Request URL: ${requestURL}`);
cb();
}).catch(err => {
self.log(err);
cb();
});
})
我刚刚开始使用 Vorpal.js 来帮助构建 CLI 工具,该工具最终将针对 API 端点执行 HTTP 请求。
我让它提出问题并验证数据输入,但是一旦我有了输入,我就让卢布调用另一个方法,该方法从请求的答案中收缩端点 url。
这是我的代码:
function apiEndpoint (env) {
if (env === 'INT') {
return API_ENDPOINT_INT;
} else if (env === 'TEST') {
return API_ENDPOINT_TEST;
} else if (env === 'LIVE') {
return API_ENDPOINT_LIVE
}
}
function constructRequestURL (params) {
let API_ENDPOINT = apiEndpoint(params.env);
let requestURL = `${API_GATEWAY}${API_ENDPOINT}?asset_uri=${params.asset_uri}&${site_name}&${asset_type}&${event_type}`
return requestURL;
}
vorpal
.command('render')
.option('--dave')
.action(function (args, cb) {
const self = this;
this.prompt(questions)
.then (function (result) {
// self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
let requestURL = constructRequestURL(result);
self.log(`Request URL: ${requestURL}`);
cb();
})
})
vorpal
.delimiter('AMP ReRenderer$')
.show();
这条注释行有效
// self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
然而调用 constructUrl
函数时什么也没有发生,cli 工具只是存在。
我想这可能是范围问题?
没有。它可能正在默默地失败。始终确保您 catch
兑现承诺。
vorpal
.command('render')
.option('--dave')
.action(function (args, cb) {
const self = this;
this.prompt(questions)
.then (function (result) {
// self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
let requestURL = constructRequestURL(result);
self.log(`Request URL: ${requestURL}`);
cb();
}).catch(err => {
self.log(err);
cb();
});
})