Issues (5)

src/console/InstallTailwindCss.php (5 issues)

Labels
Severity
1
<?php
2
3
namespace Cloudtux\TailwindCss\console;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Process\Process;
7
8
class InstallTailwindCss extends Command
9
{
10
11
    protected $signature = 'install:tailwindcss';
12
    protected $description = 'Install TailwindCss preset';
13
    private $packageManager;
14
15
    public function __construct()
16
    {
17
        parent::__construct();
18
    }
19
20
    public function handle()
21
    {
22
23
        $this->getPackageManager();
24
        $this->createTailwindJsFile();
25
        $this->updateSassFile();
26
        $this->updateWebpackFile();
27
        $this->installComplete();
28
29
    }
30
31
    private function getPackageManager(){
32
33
        $this->packageManager = $this->choice('How would you like to install?', ['yarn', 'npm'], 0);
34
35
        $this->info("Installing with " . $this->packageManager . ". Please wait...");
36
37
        if($this->packageManager == 'yarn'){
38
            return $this->installUsingYarn();
0 ignored issues
show
Are you sure the usage of $this->installUsingYarn() targeting Cloudtux\TailwindCss\con...Css::installUsingYarn() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
39
        }
40
41
        return $this->installUsingNpm();
0 ignored issues
show
Are you sure the usage of $this->installUsingNpm() targeting Cloudtux\TailwindCss\con...dCss::installUsingNpm() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
42
43
    }
44
45
    private function installUsingYarn(){
46
47
        $process = new Process('yarn add tailwindcss --dev');
48
        $process->run();
49
        return $this->info($process->getOutput());
0 ignored issues
show
Are you sure the usage of $this->info($process->getOutput()) targeting Illuminate\Console\Command::info() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
50
51
    }
52
53
    private function installUsingNpm(){
54
55
        $process = new Process('npm install tailwindcss --save-dev');
56
        $process->run();
57
        return $this->info($process->getOutput());
0 ignored issues
show
Are you sure the usage of $this->info($process->getOutput()) targeting Illuminate\Console\Command::info() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
59
    }
60
61
    private function createTailwindJsFile(){
62
63
        $this->info("Creating tailwind.js in your root directory.");
64
65
        $process = new Process('./node_modules/.bin/tailwind init tailwind.js');
66
        $process->run();
67
        return $this->info($process->getOutput());
0 ignored issues
show
Are you sure the usage of $this->info($process->getOutput()) targeting Illuminate\Console\Command::info() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
69
    }
70
71
    private function updateSassFile(){
72
73
        $appCssFile = base_path('resources/sass/app.scss');
74
75
        if (!file_exists($appCssFile)) {
76
            $this->info("Creating file /resources/sass/app.scss ...");
77
            $process = new Process('touch ' . $appCssFile);
78
            $process->run();
79
        }
80
81
        $this->info("Appending tailwind directives to /resources/sass/app.scss ...");
82
83
        $fileContents = file_get_contents($appCssFile);
84
85
        if (!preg_match("/Tailwind Directives/", $fileContents)) {
86
            $fileContents .= "\n\n//Tailwind Directives\n\n";
87
        }
88
89
        if (!preg_match("/@tailwind preflight;/", $fileContents)) {
90
            $fileContents .= "@tailwind preflight;\n";
91
        }
92
93
        if (!preg_match("/@tailwind components;/", $fileContents)) {
94
            $fileContents .= "@tailwind components;\n";
95
        }
96
97
        if (!preg_match("/@tailwind utilities;/", $fileContents)) {
98
            $fileContents .= "@tailwind utilities;";
99
        }
100
101
        file_put_contents($appCssFile, $fileContents);
102
103
    }
104
105
    private function installComplete(){
106
107
        $command = ($this->packageManager == 'yarn' ? 'yarn run dev' : 'npm run dev');
108
109
        $this->info("Installation is now complete!");
110
        $process = new Process($command);
111
        $process->run();
112
113
    }
114
115
    private function updateWebpackFile(){
116
117
        $webpackFile = base_path('webpack.mix.js');
118
119
        if (!file_exists($webpackFile)) {
120
            $this->info("Creating file webpack.mix.js ...");
121
            $process = new Process('touch ' . $webpackFile);
122
            $process->run();
123
        }
124
125
        $this->info("Appending tailwind to webpack.mix.js ...");
126
127
        $fileContents = file_get_contents($webpackFile);
128
129
        $sassContents = ".sass('resources/sass/app.scss', 'public/css')\n\t";
130
        $sassContents .= ".options({\n\t\t";
131
        $sassContents .= "processCssUrls: false,\n\t\t";
132
        $sassContents .= "postCss: [ tailwindcss('tailwind.js') ],\n\t";
133
        $sassContents .= "});";
134
135
        $prependFileContents = "";
136
137
        if (!preg_match("/require\(\'tailwindcss\'\)/", $fileContents)) {
138
139
            if (preg_match("/.sass(.*)/", $fileContents)) {
140
                $fileContents = preg_replace("/.sass(.*)/", $sassContents, $fileContents);
141
            } else {
142
                $fileContents .= "mix" . $sassContents;
143
            }
144
145
            $prependFileContents = "const tailwindcss = require('tailwindcss');\n";
146
        }
147
148
        file_put_contents($webpackFile, $prependFileContents . $fileContents);
149
150
    }
151
152
}