Skip to content

Commit

Permalink
Add option --install-npm to build.php to install from NPM directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven65 committed Sep 24, 2024
1 parent 61294a7 commit b6921d7
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions build.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,41 @@
/* Parameters:
--no-composer Does not install vendors. Just create the autoloader.
--cleanup Remove removeables.
--allow-gulp Allow gulp to be used.
--install-npm Install NPM package instead
*/

// Any command needed to run and build plugin assets when newly cheched out of repo.
$buildCommands = [];

//Add composer build, if flag --no-composer is undefined.
//Dump autloader.
//Dump autloader.
//Only if composer.json exists.
if (file_exists('composer.json')) {
if (is_array($argv) && !in_array('--no-composer', $argv)) {
$buildCommands[] = 'composer install --prefer-dist --no-progress --no-dev';
if(file_exists('composer.json')) {
if(is_array($argv) && !in_array('--no-composer', $argv)) {
$buildCommands[] = 'composer install --prefer-dist --no-progress --no-dev';
}
$buildCommands[] = 'composer dump-autoload';
}

//Run npm if package.json is found
if (file_exists('package.json') && file_exists('package-lock.json')) {
$buildCommands[] = 'npm ci --no-progress --no-audit';
} elseif (file_exists('package.json') && !file_exists('package-lock.json')) {
$buildCommands[] = 'npm install --no-progress --no-audit';
}

//Run build if package-lock.json is found
if (file_exists('package-lock.json') && !file_exists('gulp.js')) {
$buildCommands[] = 'npx --yes browserslist@latest --update-db';
$buildCommands[] = 'npm run build';
} elseif (
file_exists('package-lock.json') &&
file_exists('gulp.js') &&
is_array($argv) &&
in_array('--allow-gulp', $argv)
) {
$buildCommands[] = 'gulp';
if(file_exists('package.json') && file_exists('package-lock.json')) {
if(is_array($argv) && !in_array('--install-npm', $argv)) {
$buildCommands[] = 'npm ci --no-progress --no-audit';
} else {
$npmPackage = json_decode(file_get_contents('package.json'));
$buildCommands[] = "npm install $npmPackage->name";
$buildCommands[] = "rm -rf ./dist";
$buildCommands[] = "mv node_modules/$npmPackage->name/dist ./";
}
} elseif(file_exists('package.json') && !file_exists('package-lock.json')) {
if(is_array($argv) && !in_array('--install-npm', $argv)) {
$buildCommands[] = 'npm install --no-progress --no-audit';
} else {
$npmPackage = json_decode(file_get_contents('package.json'));
$buildCommands[] = "npm install $npmPackage->name";
$buildCommands[] = "rm -rf ./dist";
$buildCommands[] = "mv node_modules/$npmPackage->name/dist ./";
}
}

// Files and directories not suitable for prod to be removed.
Expand All @@ -60,13 +61,12 @@
'package.json',
'phpunit.xml.dist',
'README.md',
'gulpfile.js',
'./node_modules/',
'./source/sass/',
'./source/js/',
'LICENSE',
'babel.config.js',
'yarn.lock',
'yarn.lock'
];

$dirName = basename(dirname(__FILE__));
Expand All @@ -86,7 +86,7 @@
}

// Remove files and directories if '--cleanup' argument is supplied to save local developers from disasters.
if (is_array($argv) && in_array('--cleanup', $argv)) {
if(is_array($argv) && in_array('--cleanup', $argv)) {
foreach ($removables as $removable) {
if (file_exists($removable)) {
print "Removing $removable from $dirName\n";
Expand All @@ -111,7 +111,7 @@ function executeCommand($command)

$proc = popen($fullCommand, 'r');

$liveOutput = '';
$liveOutput = '';
$completeOutput = '';

while (!feof($proc)) {
Expand Down

0 comments on commit b6921d7

Please sign in to comment.