-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom console commands #1225
Comments
Hi @thyvo ! You want to create your If it's the case, you case easily create a new scripts in your app eg. |
Hi @lauriejim, I already tried this. Unfortunately when I use |
@thyvo OKay I see. Actually you will have to start your server, there is no other way (like |
@lauriejim Hmm thats to bad, can I pass some parameters to |
It's not possible. I will have to work on it when we start to make unit testing on Strapi. But it's not now. |
I manged to get it to work doing the following:
and created a new envoirement called commandline that runs strapi on port 1338 for the duration that the script runs. |
I create a new card on Product Board (see https://portal.productboard.com/strapi/c/39-extensible-cli). Feel free to give us more insights. Since we're considering to implement the feature, I'm closing this issue but you can still comment below this message and ping me if needed. |
Is there any updates here? |
Managed to run CLI commands without running http server. For current version it will look like this cli/init.js var strapi = require('strapi')
strapi().load().then(
async (strapiInstance) => {
let data = await strapiInstance.query('permission', 'users-permissions').find()
// do something
console.log(data)
// exit with 0 code
strapiInstance.stop(0)
}
) in package.json
and run my command by If someone knows better solution send it here, please:) |
Just a bit of addition to run multiple scripts using the same command. I named my directory commands/init.js
in package.json Now I am able to execute multiple scripts from commands/my-custom-script-A.js
commands/my-custom-script-B.js
|
I am working at adding caporal to handle usable console commands and make an extenstion for comfortable console commands |
In slight addition to @amorev you may want to use the following boilerplate for current Strapi v4: var strapi = require('@strapi/strapi');
strapi()
.load()
.then(async (strapiInstance) => {
// ...Your custom code...
// Explicitly destroy server and connections.
strapiInstance.server.destroy();
strapiInstance.stop(0);
}); Also kindly note the availability of |
Doing the same for Strapi 4.16.2 in Node v18.16.0 with Typescript support could look like this: In import strapi, { type Strapi } from '@strapi/strapi';
const main = async () => {
const appContext = await strapi.compile();
const app: Strapi = await strapi(appContext).load();
console.log('app', app);
app.server.destroy();
app.stop(0);
};
main(); In "scripts": {
...
"import": "ts-node ./src/scripts/import"
},
"devDependencies": {
...
"ts-node": "^10.9.2"
}, |
What is the expected behavior?
I would like to run some migration/bulk update scripts from the cli using the strapi models/services but I could not find a way to make it work without running a full featured server and using some api endpoints.
From my php days I used the laravel framework and they had the Artisan Console feature:
https://laravel.com/docs/5.6/artisan#writing-commands
It would be nice to have this simular feature in strapi where you can just do:
strapi run <command>
and it would run the command in
For now, is there a way that I can run the some scripts using the strapi features without starting a full server?
The text was updated successfully, but these errors were encountered: