Skip to content
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

Closed
1 task done
tomvanoosterwyck opened this issue May 18, 2018 · 13 comments
Closed
1 task done

Custom console commands #1225

tomvanoosterwyck opened this issue May 18, 2018 · 13 comments
Assignees
Labels
issue: feature request Issue suggesting a new feature severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve

Comments

@tomvanoosterwyck
Copy link

tomvanoosterwyck commented May 18, 2018

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

src
  |-- commands
        |-- <command>.js

For now, is there a way that I can run the some scripts using the strapi features without starting a full server?


  • I'm sure that this issue hasn't already been referenced.
@lauriejim lauriejim added issue: feature request Issue suggesting a new feature severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve labels May 18, 2018
@lauriejim
Copy link
Contributor

Hi @thyvo !

You want to create your migration/bulk update scripts on your application only ?

If it's the case, you case easily create a new scripts in your app eg./scripts/migrations.js and add in package.json scripts object node ./scripts/migrations.js. and then run npm run migration to execute your script.

@lauriejim lauriejim added type: question 🙋 and removed issue: feature request Issue suggesting a new feature labels May 18, 2018
@tomvanoosterwyck
Copy link
Author

Hi @lauriejim,

I already tried this. Unfortunately when I use const strapi = require('strapi') I won't have any access to my custom models and services, it just gives me an empty strapi object

@lauriejim lauriejim added issue: feature request Issue suggesting a new feature severity: medium If it breaks the basic use of the product but can be worked around and removed severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve type: question 🙋 labels May 18, 2018
@lauriejim
Copy link
Contributor

@thyvo OKay I see. Actually you will have to start your server, there is no other way (like server.js file to start and use strapi.stop() when it's done)

@tomvanoosterwyck
Copy link
Author

@lauriejim Hmm thats to bad, can I pass some parameters to strapi start so it runs on a different port for the duration of the script thats running?

@lauriejim
Copy link
Contributor

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.

@tomvanoosterwyck
Copy link
Author

I manged to get it to work doing the following:

var strapi = require('strapi')

strapi.start({
  environment: 'commandline'
}, function (err) {
  Location.remove({},function(err){
    console.log('dropping locations')
    strapi.stop()
  })
})

and created a new envoirement called commandline that runs strapi on port 1338 for the duration that the script runs.
(for anyone that wants to run scripts without creating additional routes)

@lauriejim lauriejim added severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve and removed severity: medium If it breaks the basic use of the product but can be worked around labels Jul 1, 2018
@lauriejim lauriejim assigned Aurelsicoko and unassigned lauriejim Aug 22, 2018
@Aurelsicoko
Copy link
Member

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.

@amorev
Copy link

amorev commented Jun 21, 2020

Is there any updates here?

@amorev
Copy link

amorev commented Jun 21, 2020

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

 "cli": "node cli/init.js",

and run my command by npm run cli

If someone knows better solution send it here, please:)

@asimkhan73301
Copy link

Just a bit of addition to run multiple scripts using the same command. I named my directory commands and done the following configurations.

commands/init.js

const path = require('path');
require(path.join(__dirname, process.argv[2]));

in package.json
"command": "node commands/init.js"

Now I am able to execute multiple scripts from commands directory using npm run command filename.js

commands/my-custom-script-A.js

console.log('my custom script A')

commands/my-custom-script-B.js

console.log('my custom script B')
npm run command my-custom-script-A
npm run command my-custom-script-B

@amorev
Copy link

amorev commented Jun 27, 2020

I am working at adding caporal to handle usable console commands and make an extenstion for comfortable console commands

@FlxAlbroscheit
Copy link

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 strapi console for eval`ing code in realtime.

@popovicsandras
Copy link

popovicsandras commented Jan 13, 2024

Doing the same for Strapi 4.16.2 in Node v18.16.0 with Typescript support could look like this:

In src/scripts/import/index.ts

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 package.json:
(don't forget to add ts-node with yarn add --dev ts-node)

  "scripts": {
    ...
    "import": "ts-node ./src/scripts/import"
  },
  "devDependencies": {
    ...
    "ts-node": "^10.9.2"
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: feature request Issue suggesting a new feature severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve
Projects
None yet
Development

No branches or pull requests

7 participants