简体   繁体   English

如何从 symfony 项目制作“npm run build”

[英]How to make a 'npm run build' from a symfony project

I currently have a symfony project that uses Foundation for Emails to create responsive emails.我目前有一个 symfony 项目,它使用 Foundation for Emails 创建响应式电子邮件。

The Foundation framework uses the command 'npm run build' to tranform files. Foundation 框架使用命令“npm run build”来转换文件。 I tried doing a service parse my content using the Process class but I must be using it wrong as it does not execute 'npm run build'.我尝试使用 Process 类进行服务解析我的内容,但我一定是使用错误,因为它不执行“npm run build”。 Here is my faulty code :这是我的错误代码:

<?php
/**
 * Created by PhpStorm.
 * User: jeremie
 * Date: 28/12/17
 * Time: 16:59
 */

namespace Acme\Open4XMLParserBundle\Services;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;


/**
 * @todo : code this
 */
class FoundationParser
{
    protected $foundationLocation;
    protected $process;

    /**
     * FoundationParser constructor.
     * @param $foundationLocation
     */
    public function __construct($foundationLocation)
    {
        $this->foundationLocation = $foundationLocation;
        $this->process = new Process('npm run build', $this->foundationLocation);
    }

    /**
     * Run npm run build if needed
     */
    public function initFoundation()
    {
        //make sure that 'npm run build' is running and execute it if not
        if (!$this->process->isRunning()) {
            $this->process->start();
        }
    }

    public function saveFoundationContent($foundationContent, $filename)
    {
        //save the file in foundation/src/pages
        $fileSystem = new Filesystem();
        $fileLocation = $this->foundationLocation . '/src/pages/' . $filename;
        if (!$fileSystem->exists($fileLocation)) {
            $fileSystem->dumpFile($fileLocation, $foundationContent);
            $fileSystem->chmod($fileLocation, 0664);
        }
    }

    public function retrieveValidHtml($fileName)
    {
        $fileLocation = $this->foundationLocation . '/dist/' . $fileName;
        while (true) {
            try {
                $result = file_get_contents($fileLocation);
                if ($result !== false){
                    return $result;
                }
            } catch (\Exception $e) {

            }
        }
    }
}

And I use my service like this :我像这样使用我的服务:

$foundationParser = $this->container->get('open4xmlparser.foundationparser');
$foundationParser->initFoundation();
$foundationParser->saveFoundationContent($foundationContent, 'test.html');
$response = new Response($foundationParser->retrieveValidHtml('test.html'));
$response->headers->set('Content-Type', 'text/html');
$response->send();

And it tells me that 'test.html' does not exist.它告诉我“test.html”不存在。 Any idea on how to do what I want?关于如何做我想做的任何想法?

What I finally decided to do is a Symfony command that launch my program in an infinite loop(it is never supposed to stop).我最终决定做的是一个 Symfony 命令,它在无限循环中启动我的程序(它永远不应该停止)。 Instead of using a service I executed 'npm run build' directly in the while loop.我没有使用服务,而是直接在 while 循环中执行了“npm run build”。

protected function execute(InputInterface $input, OutputInterface $output)
{
    $output->writeln([
        '',
        'Running program',
        '===============',
        ''
    ]);
    $runBuild = new Process('npm run build', $this->getContainer()->getParameter('foundation_location'));

    while (true) {

        if (!$runBuild->isRunning()){
            $output->writeln([
                '',
                'Executing npm run build',
                ''
            ]);
            $runBuild->start();
        }
    }
}

You seem to have different paths in the saveFoundationContent and retrieveValidHtml for the target file.您似乎在目标文件的saveFoundationContentretrieveValidHtml有不同的路径。

// saveFoundationContent()
$fileLocation = $this->foundationLocation . '/src/pages/' . $filename;

// retrieveValidHtml()
$fileLocation = $this->foundationLocation . '/dist/' . $fileName;

Obviously, retrieveValidHtml() cannot find the file in the location.显然, retrieveValidHtml()在该位置找不到文件。

Tip: Store the subdirectory path as a class variable (or constant):提示:将子目录路径存储为类变量(或常量):

class FoundationParser
{
    private $subdir = "/dist/"; // or "/src/pages/";

    … 

    public function retrieveValidHtml($fileName)
    {
        $fileLocation = sprintf("%s%s%s", $this->foundationLocation, $this->subdir, $fileName);
        …
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM