Adding installer script to un ignore the package-lock.json file from npm

parent 410fab4d
...@@ -67,6 +67,7 @@ class InstallCommand extends Command ...@@ -67,6 +67,7 @@ class InstallCommand extends Command
\Modules\Core\Console\Installers\Scripts\ModuleAssets::class, \Modules\Core\Console\Installers\Scripts\ModuleAssets::class,
\Modules\Core\Console\Installers\Scripts\ThemeAssets::class, \Modules\Core\Console\Installers\Scripts\ThemeAssets::class,
\Modules\Core\Console\Installers\Scripts\UnignoreComposerLock::class, \Modules\Core\Console\Installers\Scripts\UnignoreComposerLock::class,
\Modules\Core\Console\Installers\Scripts\UnignorePackageLock::class,
\Modules\Core\Console\Installers\Scripts\SetInstalledFlag::class, \Modules\Core\Console\Installers\Scripts\SetInstalledFlag::class,
])->install($this); ])->install($this);
......
<?php
namespace Modules\Core\Console\Installers\Scripts;
use Illuminate\Console\Command;
use Modules\Core\Console\Installers\SetupScript;
class UnignorePackageLock implements SetupScript
{
const PACKAGE_LOCK = 'package-lock.json';
/**
* Fire the install script
*
* @param Command $command
* @return mixed
*/
public function fire(Command $command)
{
$gitignorePath = base_path('.gitignore');
if (!$this->gitignoreContainsPackageLock($gitignorePath)) {
return;
}
$removePackageLock = $command->confirm('Do you want to remove package-lock.json from .gitignore ?', true);
if ($removePackageLock) {
$out = $this->getGitignoreLinesButPackageLock($gitignorePath);
$this->writeNewGitignore($gitignorePath, $out);
}
}
/**
* @param $gitignorePath
* @return bool
*/
private function gitignoreContainsPackageLock($gitignorePath)
{
return file_exists($gitignorePath) && strpos(file_get_contents($gitignorePath), self::PACKAGE_LOCK) !== false;
}
/**
* @param $gitignorePath
* @return array
*/
private function getGitignoreLinesButPackageLock($gitignorePath)
{
$data = file($gitignorePath);
$out = [];
foreach ($data as $line) {
if (trim($line) !== self::PACKAGE_LOCK) {
$out[] = $line;
}
}
return $out;
}
/**
* @param $gitignorePath
* @param $out
*/
private function writeNewGitignore($gitignorePath, $out)
{
$fp = fopen($gitignorePath, 'wb+');
flock($fp, LOCK_EX);
foreach ($out as $line) {
fwrite($fp, $line);
}
flock($fp, LOCK_UN);
fclose($fp);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment