Sails 捆绑了 Whelk,让您可以将 JavaScript 函数作为 Shell 脚本运行。这对于运行排定的作业(cron、Heroku 调度器)、工作进程以及任何其他需要访问您的 Sails 应用程序模型、配置和助手函数的自定义、一次性脚本非常有用。
要添加新脚本,只需在应用程序的 scripts/
文件夹中创建一个文件。
sails generate script hello
然后,要运行它,请使用
sails run hello
如果您需要在没有全局访问
sails
命令行界面的情况下运行脚本(例如在 Procfile 中),请使用node ./node_modules/sails/bin/sails run hello
。
这是一个更复杂的范例,您可能会在真实的应用程序中看到
// scripts/send-email-proof-reminders.js
module.exports = {
description: 'Send a reminder to any recent users who haven\'t confirmed their email address yet.',
inputs: {
template: {
description: 'The name of another email template to use as an optional override.',
type: 'string',
defaultsTo: 'reminder-to-confirm-email'
}
},
fn: async function (inputs, exits) {
await User.stream({
emailStatus: 'pending',
emailConfirmationReminderAlreadySent: false,
createdAt: { '>': Date.now() - 1000*60*60*24*3 }
})
.eachRecord(async (user, proceed)=>{
await sails.helpers.sendTemplateEmail.with({
template: inputs.template,
templateData: {
user: user
},
to: user.emailAddress
});
return proceed();
});//∞
return exits.success();
}
};
然后您可以运行
sails run send-email-proof-reminders
有关使用方式的更多详细信息,请参阅 whelk
README。