Add "mix:run" command (#698)

Allows running of scripts defined in the package.json of a Mix package, through Artisan.
This commit is contained in:
Ben Thomson 2022-09-17 11:40:00 +08:00 committed by GitHub
parent 87a0a470d3
commit e84933775d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 194 additions and 89 deletions

View File

@ -3,34 +3,96 @@ name: Tests
on:
push:
branches:
- 1.0
- 1.1
- 1.2
- '1.2'
- develop
pull_request:
jobs:
frontendTests:
runs-on: ubuntu-latest
name: JavaScript
strategy:
max-parallel: 2
matrix:
operatingSystem: [ubuntu-latest, windows-latest]
fail-fast: false
runs-on: ${{ matrix.operatingSystem }}
name: ${{ matrix.operatingSystem }} / JavaScript
env:
nodeVersion: 16
phpVersion: '8.0'
extensions: curl, fileinfo, gd, mbstring, openssl, pdo, pdo_sqlite, sqlite3, xml, zip
key: winter-cms-cache-develop
steps:
- name: Checkout changes
uses: actions/checkout@v2
- name: Cancel previous incomplete runs
uses: styfle/cancel-workflow-action@0.8.0
with:
fetch-depth: 0
access_token: ${{ github.token }}
- name: Checkout changes
uses: actions/checkout@v3
- name: Setup extension cache
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ env.phpVersion }}
extensions: ${{ env.extensions }}
key: ${{ env.key }}
- name: Cache extensions
uses: actions/cache@v3
with:
path: ${{ steps.extcache.outputs.dir }}
key: ${{ steps.extcache.outputs.key }}
restore-keys: ${{ steps.extcache.outputs.key }}
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.phpVersion }}
extensions: ${{ env.extensions }}
- name: Install Node
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 12
node-version: ${{ env.nodeVersion }}
- name: Install Node dependencies
working-directory: ./modules/system/tests/js
run: npm install
- name: Echo branches
run: echo "${{ github.ref }} | ${{ github.head_ref }} | ${{ github.ref_name }} | ${{ github.base_ref }}"
- name: Switch library dependency (develop)
if: github.ref == 'refs/heads/develop' || github.base_ref == 'develop'
run: php ./.github/workflows/utilities/library-switcher "dev-develop as 1.2"
- name: Switch library dependency (1.2)
if: github.head_ref == '1.2' || github.ref == 'refs/heads/1.2' || github.base_ref == '1.2'
run: php ./.github/workflows/utilities/library-switcher "1.2.x-dev as 1.2"
- name: Setup dependency cache
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Composer dependencies
run: composer install --no-interaction --no-progress --no-scripts
- name: Reset modules
run: |
git reset --hard
git clean -fd
- name: Run post-update Composer scripts
run: php artisan package:discover
- name: Run tests
working-directory: ./modules/system/tests/js
run: npm run test
run: |
php artisan mix:install
php artisan mix:run module-system test
phpUnitTests:
strategy:
@ -108,7 +170,9 @@ jobs:
run: composer install --no-interaction --no-progress --no-scripts
- name: Reset modules
run: git reset --hard
run: |
git reset --hard
git clean -fd
- name: Run post-update Composer scripts
run: php artisan package:discover

View File

@ -6,7 +6,7 @@
"@babel/plugin-transform-runtime",
[
"module-resolver", {
"root": ["."],
"root": ["./tests/js"],
"alias": {
"helpers": "./helpers",
"snowboard": "../../modules/system/assets/js/snowboard",

View File

@ -274,6 +274,7 @@ class ServiceProvider extends ModuleServiceProvider
$this->registerConsoleCommand('mix.list', \System\Console\MixList::class);
$this->registerConsoleCommand('mix.compile', \System\Console\MixCompile::class);
$this->registerConsoleCommand('mix.watch', \System\Console\MixWatch::class);
$this->registerConsoleCommand('mix.run', \System\Console\MixRun::class);
}
/*

View File

@ -0,0 +1,102 @@
<?php namespace System\Console;
use File;
use Symfony\Component\Process\Process;
use System\Classes\MixAssets;
use Winter\Storm\Console\Command;
class MixRun extends Command
{
/**
* @var string|null The default command name for lazy loading.
*/
protected static $defaultName = 'mix:run';
/**
* @var string The name and signature of this command.
*/
protected $signature = 'mix:run
{package : Defines the package where the script is located.}
{script : The name of the script to run, as defined in the package.json "scripts" config.}
{additionalArgs?* : Arguments to pass through to the script being run.}
{--f|production : Runs the script in "production" mode.}
{--s|silent : Silent mode.}';
/**
* @var string The console command description.
*/
protected $description = 'Runs a script in a given package.';
/**
* Execute the console command.
*/
public function handle(): int
{
$mixedAssets = MixAssets::instance();
$mixedAssets->fireCallbacks();
$packages = $mixedAssets->getPackages();
$name = $this->argument('package');
$script = $this->argument('script');
if (!in_array($name, array_keys($packages))) {
$this->error(
sprintf('Package "%s" is not a registered package.', $name)
);
return 1;
}
$package = $packages[$name];
$packageJson = $this->readPackageJson($package);
if (!isset($packageJson['scripts'][$script])) {
$this->error(
sprintf('Script "%s" is not defined in package "%s".', $script, $name)
);
return 1;
}
$this->info(sprintf('Running script "%s" in package "%s"', $script, $name));
$command = ($this->argument('additionalArgs')) ?? [];
if (count($command)) {
array_unshift($command, 'npm', 'run', $script, '--');
} else {
array_unshift($command, 'npm', 'run', $script);
}
$process = new Process(
$command,
base_path($package['path']),
['NODE_ENV' => $this->option('production', false) ? 'production' : 'development'],
null,
null
);
try {
$process->setTty(true);
} catch (\Throwable $e) {
// This will fail on unsupported systems
}
$exitCode = $process->run(function ($status, $stdout) {
if (!$this->option('silent')) {
$this->getOutput()->write($stdout);
}
});
return $exitCode;
}
/**
* Reads the package.json file for the given package.
*/
protected function readPackageJson(array $package): array
{
$packageJsonPath = base_path($package['package']);
return File::exists($packageJsonPath)
? json_decode(File::get($packageJsonPath), true)
: [];
}
}

View File

@ -23,15 +23,25 @@
"url": "https://github.com/wintercms/winter/issues"
},
"homepage": "https://wintercms.com/",
"scripts": {
"test": "jest --config tests/js/jest.config.js tests/js/cases"
},
"dependencies": {
"js-cookie": "^3.0.1"
},
"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/plugin-transform-runtime": "^7.16.4",
"@babel/preset-env": "^7.16.4",
"@babel/register": "^7.16.0",
"@babel/runtime": "^7.16.3",
"babel-plugin-module-resolver": "^4.1.0",
"eslint": "^8.6.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-vue": "^8.5.0",
"jest": "^27.4.3",
"jsdom": "^18.1.1",
"laravel-mix": "^6.0.34",
"laravel-mix-polyfill": "^3.0.1"
}

View File

@ -1,3 +0,0 @@
# Ignore packages
package-lock.json
node_modules

View File

@ -1,32 +0,0 @@
# Snowboard Framework Testing Suite
The files in this directory form the testing suite for the new Winter JavaScript framework - Snowboard. You must install
all Node dependencies in order to run the testing suite:
```bash
cd tests/js
npm install
```
You can then run the tests by using the following command:
```bash
npm run test
```
Please note that the tests are run against the "built" versions of Snowboard and its plugins, in order to test
the exact same functionality that would be delivered to the end user. We do this by leveraging the `JSDOM` library to
simulate an entire HTML document.
Therefore, you must compile a new build if you make any changes and wish to run the tests:
```bash
php artisan mix:compile --package snowboard
```
You can also watch the framework for any changes, which will automatically run a build after a change is made. This will
make development and testing in parallel much quicker:
```bash
php artisan mix:watch snowboard
```

View File

@ -1,37 +0,0 @@
{
"name": "@wintercms/tests",
"description": "Test cases for the Winter JavaScript framework",
"private": true,
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/wintercms/winter.git"
},
"contributors": [
{
"name": "Ben Thomson",
"email": "git@alfreido.com"
},
{
"name": "Winter CMS Maintainers",
"url": "https://wintercms.com/"
}
],
"license": "MIT",
"bugs": {
"url": "https://github.com/wintercms/winter/issues"
},
"homepage": "https://wintercms.com/",
"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/plugin-transform-runtime": "^7.16.4",
"@babel/preset-env": "^7.16.4",
"@babel/register": "^7.16.0",
"@babel/runtime": "^7.16.3",
"babel-plugin-module-resolver": "^4.1.0",
"jest": "^27.4.3",
"jsdom": "^18.1.1"
}
}