Completed
Pull Request — master (#29)
by Paulo Rodrigues
04:54
created

InstallCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
ccs 4
cts 4
cp 1
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rj\FrontendBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Process\Process;
9
10
class InstallCommand extends Command
11
{
12 18
    protected function configure()
13
    {
14
        $this
15 18
            ->setName('rj_frontend:install')
16 18
            ->setDescription('Install npm and bower dependencies')
17
        ;
18 18
    }
19
20 9
    protected function execute(InputInterface $input, OutputInterface $output)
21
    {
22 9
        if (!$this->commandExists('npm')) {
23 7
            return $output->writeln(
24 7
'<error>npm is not installed</error>
25
26
node.js is probably not installed on your system. For node.js installation
27
instructions, refer to https://nodejs.org/en/download/package-manager
28
'
29
            );
30
        }
31
32 2
        if (!$this->commandExists('bower')) {
33 1
            return $output->writeln(
34 1
'<error>bower is not installed</error>
35
36
You can install bower using npm:
37
npm install -g bower
38
'
39
            );
40
        }
41
42 1
        $output->writeln('<info>Running `npm install`</info>');
43 1
        $this->runProcess($output, 'npm install');
44
45 1
        $output->writeln('<info>Running `bower install`</info>');
46 1
        $this->runProcess($output, 'bower install');
47 1
    }
48
49
    protected function runProcess($output, $command)
50
    {
51
        $process = new Process($command);
52
53
        $process->run(function ($type, $buffer) use ($output) {
54
            if (Process::ERR === $type) {
55
                $output->writeln("<error>$buffer</error>");
56
            } else {
57
                $output->writeln($buffer);
58
            }
59
        });
60
61
        if (!$process->isSuccessful()) {
62
            throw new \RuntimeException($process->getErrorOutput());
63
        }
64
    }
65
66
    protected function commandExists($command)
67
    {
68
        $process = new Process("$command -v");
69
        $process->run();
70
71
        if (!$process->isSuccessful()) {
72
            return !preg_match('/: not found/', $process->getErrorOutput());
73
        }
74
75
        return true;
76
    }
77
}
78