phpnpmexecnodespurifycss

how to execute nodejs programs in php


I am working on project that uses purify-css

 https://github.com/purifycss/purifycss

in which I have installed purify-css on the server with npm, now I want to execute the command shown below through PHP and I am using exec() method.

   purifycss css/final_css.css test1.html --min --out bootstrap-purify-1.css --info --rejected

but command is not working.If I use this same command directly in terminal then it is working fine. All other commands like "mkdir" and all others are working fine but this command in PHP exec() function is not working.I don't know why?. Thanks in advance. Point out if you notice something.


Solution

  • It depends on the directory where you run that command from. If you call exec or other function in php to execute a linux command, that will run in the directory where the php file is. You can execute pwd command to see in which directory your command runs and if is not the root of the project you can change it

    exec('cd /path/to/dir && purifycss ...');
    

    I also recommend you to use this tool https://symfony.com/doc/current/components/process.html which is tested and cover more use cases than you can do with exec function.

    As a final remark, I strongly recommend to create a gulp command and execute that command from terminal. Or you can use webpack or grunt if you are more familiar with those. I don't find a use case where the php app should know how to generate it's own styles and js files. That is a job for deployment script.