Is it possible to create custom npm run commands for Laravel? - php

Laravel has the following built-in npm run commands (among others):
npm run install
npm run watch
Is it possible to create custom npm run commands to run custom PHP scripts? For example, I want to create a command called npm run csv that will run a PHP script that imports a bunch of CSV data into a database.
Thanks.
Edit: After asking the question and seeing a lot of the responses, it has become overwhelming obvious that writing a php artisan command is probably the better way to go. As such, that's what I will do.
Thank you all for your responses. As for why I didn't ask that question, it's quite simple: I didn't know that that was a better approach. I'm still new to Laravel and learning. Thanks.

At first, you must write Artisan Console Command. Then you could run it using npm. But this is not a recommended way. you can run any artisan command like:
php artisan inspire
If you want to run this with npm just add this command in the package.json's script. For example:
{
"scripts": {
"inspire": "php artisan inspire"
}
}
Then run the command like this:
npm run inspire

It should be cleaner to create your own artisan command.
See https://laravel.com/docs/5.7/artisan#writing-commands
Then put your csv import code inside the handle() method.
You will just have to run : php artisan import-csv or something like

I love this side of Laravel. From what you are trying to achieve, may I advise on custom artisan commands? :)
https://laravel.com/docs/5.7/artisan
Recommend the reading, it is great for what you are looking :D

There's no need to use NPM to call PHP! Why not just create your own executable?
Using Laravel, there is a command system, so you can make your own artisan commands. But if you want something simpler, you can do this:
Example. I create a file in my project called bin/do_stuff
#!/usr/bin/env php
<?php
echo "Easy as that!\n";
Then make it executable:
chmod +x bin/do_stuff
Then you can run it with ./bin/do_stuff! Not hard at all! Now you can also pass arguments like so:
./bin/do_stuff--option1=value1 --option2
With or without values. To do this, we add the following:
foreach ($argv as $arg)
{
preg_match('/\-\-(\w*)\=?(.+)?/', $arg, $value);
if ($value && isset($value[1]) && $value[1])
{
$options[$value[1]] = isset($value[2]) ? $value[2] : null;
}
}
Great for in cron jobs and back end stuff. Give it a try!

Related

Laravel run package artisan command in controller

I need to run this command when user change something in translation file
php artisan export:messages-flat
I need to add it in may controller
so I'm using this code
\Artisan::call('export:messages-flat');
but it return error saying that
The command "export:messages-flat" does not exist.
but when I
php artisan list
it's in the list
I also try to run other command
\Artisan::call('cache:clear');
and it works
this is the package I'm using link
kindly help me, sorry for may poor english
The package only allows you to run the commands from the CLI. You can see from the source code, they only register the Artisan commands if the application is running from the console.
As an alternative, you can call ExportLocalization::export()->toFlat() according to their documentation.

Magento 2: How to run CLI command from another CLI command class?

I'm working on a custom CLI command & I was wondering what's the best way to call other commands from the PHP code (without shell_exec() or similar).
For example:
When running "php bin/magento my:custom:command", it'll do it's thing & in the end will run "php bin/magento cache:flush".
Any Ideas?
Thanks.
The Magento CLI is built on top of Symfony Console. You can load up and run other commands with this component as such:
$arguments = new ArrayInput(['command' => 'my:custom:command']);
$this->getApplication()->find('my:custom:command')->run($arguments, $output);
$arguments = new ArrayInput(['command' => 'cache:flush']);
$this->getApplication()->find('cache:flush')->run($arguments, $output);
More information here. Although it's unlikely to be a problem for you, please note that the documentation suggests this is not always the best idea:
Most of the times, calling a command from code that is not executed on the command line is not a good idea. The main reason is that the command's output is optimized for the console and not to be passed to other commands.

How to use composer, artisan ...(Laravel) php commands manually

I have one problem I have no ssh access to the server, so I cannot use php artisan, composer and other commands.
As I can quess they do nothing other than modifying files or just copying php src files to specific directories.
In order to understand that process better and because of no access via ssh to the server I am looking for tutroial, manual or an article how can I perform this commands manually.
For example I need to execute
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
What should I do in this case, it would be grate to find some document describes what should I do manually to get the same result.
Laravel provides a handy Facade for artisan commands.
Just use Artisan::call('your-command') from where you need.
Example:
Route::get('artisan-command/{command}', function($command) {
Artisan::call($command);
});
Your URL looks like this: http://yourhost.com/artisan-command/db:seed
More specific for your use-case:
Route::get('vendor-publish/{provider}', function($provider) {
Artisan::call('vendor:publish', ['provider' => $provider]);
});
And the URL: http://yourhost.com/vendor-publish/Tymon\JWTAuth\Providers\JWTAuthServiceProvider
Reference: Artisan Console in the Laravel Docs
You can execute command lines from your app. You can do something like this:
Route::get('execute/my/command', function(){
exec("php path/to/your/project/artisan your-command",$resultLines);
Foreach($resultLines as $resultLine){
echo $resultLine;
}
});
First exec property is your command and second is variable to save the result.
I hope that help you

Call shell commands from Laravel controller?

Is it possible to call Shell commands (e.g for converting images ) from Laravel controller? If yes then how. I have searched on internet. Nothing seems relevant.
You can use the Process component provided by Symfony: http://symfony.com/doc/current/components/process.html
The component is used by Laravel itself, so your don't need to install it via composer separately. Just add use Symfony\Component\Process\Process; to your file.
If you use it(instead of php's exec() function), you'll be able to unit test the code that calls shell commands.
It all depends on what operating system you are using. php already has a few functions to execute shell commands.
Laravel has a build in task runner for ssh commands.
$FilenamePending = "files".csv";
if (File::exists(public_path('downloads/files/wstock/'.$FilenamePending))){
$PathFilesWip= public_path('downloads/files/wstock/'.$FilenamePending);
//$getCommand= "aws s3 cp $PathFiles s3://bucket-name/share/in/test/Transaction/";
$getCommandPending= "aws s3 cp $PathFilesWip s3://store_stocks/";
}else{
$getCommandPending= "";
}
$schedule->exec($getCommandPending) ->timezone('Asia/Kolkata')->dailyAt('00:38')->appendOutputTo(storage_path().'/logs/laravel_output.log');

Running Yii CConsoleCommand on a remote server

I have a problem figuring out how make my command work, because I only have access via ftp.
The action is pretty simple:
public function actionRun($action = "default") {
$this->xml = simplexml_load_file('db.xml');
return $this->{$action}(); // executes the default() method
}
All I need is to somehow execute the php index.php mycommand run, but I'm lost as to how. This command should only be run once in the life of the whole app.
My question is, is it possible to run such a command? Maybe somehow invoke it through php?
Yii command line command are designed to run through Yii.
$ cd protected
$ ./yiic --help
$ ./yiic mycommand
If you only have ftp access, you may be out of luck, and will have to use some workaround, for example running a cron job, or creating a web page that invokes the last command in the code sample I provided.
echo exec('/my_yii_dir/protected/yiic mycommand');

Categories