Skip to content

Commit

Permalink
Added reordered params and script handling for PackageJson
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko committed Jun 7, 2024
1 parent eb95c3c commit 1500557
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 25 deletions.
35 changes: 34 additions & 1 deletion modules/system/classes/CompilableAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function getPackageCount(): int
/**
* Returns all packages registered.
*/
public function getPackages(bool $includeIgnored = false, string $type = 'mix'): array
public function getPackages(string $type, bool $includeIgnored = false): array
{
$packages = $this->packages[$type] ?? [];

Expand All @@ -215,6 +215,39 @@ public function getPackages(bool $includeIgnored = false, string $type = 'mix'):
return $packages;
}

/**
* Returns if package(s) is registered.
*/
public function hasPackage(string $name, bool $includeIgnored = false): bool
{
foreach ($this->packages ?? [] as $packages) {
foreach ($packages as $packageName => $config) {
if ($name === $packageName && (!$config['ignored'] || $includeIgnored)) {
return true;
}
}
}

return false;
}

/**
* Returns package(s).
*/
public function getPackage(string $name, bool $includeIgnored = false): array
{
$results = [];
foreach ($this->packages ?? [] as $packages) {
foreach ($packages as $packageName => $config) {
if ($name === $packageName && (!$config['ignored'] || $includeIgnored)) {
$results[] = $packages;
}
}
}

return $results;
}

/**
* Registers an entity as a package for compilation.
*
Expand Down
34 changes: 34 additions & 0 deletions modules/system/classes/PackageJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,40 @@ public function removeDependency(string $package): static
return $this;
}

/**
* Returns if a script exists
*/
public function hasScript(string $name): bool
{
return isset($this->data['scripts'][$name]);
}

/**
* Returns the value of a script by name
*/
public function getScript(string $name): ?string
{
return $this->data['scripts'][$name] ?? null;
}

/**
* Adds a script
*/
public function addScript(string $name, string $script): static
{
$this->data['scripts'][$name] = $script;
return $this;
}

/**
* Removes a script by name
*/
public function removeScript(string $name): static
{
unset($this->data['scripts'][$name]);
return $this;
}

/**
* Returns the package.json contents as an array
*/
Expand Down
24 changes: 7 additions & 17 deletions modules/system/console/NpmRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Symfony\Component\Process\Process;
use System\Classes\CompilableAssets;
use System\Classes\PackageJson;
use Winter\Storm\Console\Command;
use Winter\Storm\Support\Facades\File;

class NpmRun extends Command
{
Expand Down Expand Up @@ -44,21 +44,22 @@ public function handle(): int
$compilableAssets = CompilableAssets::instance();
$compilableAssets->fireCallbacks();

$packages = $compilableAssets->getPackages();
$name = $this->argument('package');
$script = $this->argument('script');

if (!in_array($name, array_keys($packages))) {
if ($compilableAssets->hasPackage($name)) {
$this->error(
sprintf('Package "%s" is not a registered package.', $name)
);
return 1;
}

$package = $packages[$name];
$packageJson = $this->readPackageJson($package);
$package = $compilableAssets->getPackage($name);

if (!isset($packageJson['scripts'][$script])) {
// Assume that packages with matching names have matching package.json files
$packageJson = new PackageJson($package[0]['package'] ?? null);

if (!$packageJson->hasScript($script)) {
$this->error(
sprintf('Script "%s" is not defined in package "%s".', $script, $name)
);
Expand Down Expand Up @@ -95,15 +96,4 @@ public function handle(): int
}
});
}

/**
* 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)
: [];
}
}
6 changes: 3 additions & 3 deletions modules/system/console/asset/AssetCompile.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function compileHandle(string $type): int
$compilableAssets = CompilableAssets::instance();
$compilableAssets->fireCallbacks();

$registeredPackages = $compilableAssets->getPackages(type: $type);
$registeredPackages = $compilableAssets->getPackages($type);
$requestedPackages = $this->option('package') ?: [];

// Calling commands in unit tests can cause the option casting to not work correctly,
Expand All @@ -61,7 +61,7 @@ public function compileHandle(string $type): int
// Filter the registered packages to only include requested packages
if (count($requestedPackages) && count($registeredPackages)) {
// Get an updated list of packages including any newly added packages
$registeredPackages = $compilableAssets->getPackages(type: $type);
$registeredPackages = $compilableAssets->getPackages($type);

// Filter the registered packages to only deal with the requested packages
foreach (array_keys($registeredPackages) as $name) {
Expand Down Expand Up @@ -120,7 +120,7 @@ public function watchHandle(string $type): int
$compilableAssets = CompilableAssets::instance();
$compilableAssets->fireCallbacks();

$packages = $compilableAssets->getPackages(type: $type);
$packages = $compilableAssets->getPackages($type);
$name = $this->argument('package');

if (!in_array($name, array_keys($packages))) {
Expand Down
2 changes: 1 addition & 1 deletion modules/system/console/asset/AssetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function handle(): int
$compilableAssets = CompilableAssets::instance();
$compilableAssets->fireCallbacks();

$packages = $compilableAssets->getPackages(true);
$packages = $compilableAssets->getPackages($this->assetType, true);

if (isset($packages[$package])) {
$this->warn('Package `' . $package . '` has already been configured');
Expand Down
4 changes: 2 additions & 2 deletions modules/system/console/asset/AssetInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function getRequestedAndRegisteredPackages(): array
$compilableAssets = CompilableAssets::instance();
$compilableAssets->fireCallbacks();

$registeredPackages = $compilableAssets->getPackages(type: $this->assetType);
$registeredPackages = $compilableAssets->getPackages($this->assetType);
$requestedPackages = $this->option('package') ?: [];

// Normalize the requestedPackages option
Expand Down Expand Up @@ -164,7 +164,7 @@ protected function getRequestedAndRegisteredPackages(): array
}

// Get an updated list of packages including any newly added packages
$registeredPackages = $compilableAssets->getPackages();
$registeredPackages = $compilableAssets->getPackages($this->assetType);

// Filter the registered packages to only deal with the requested packages
foreach (array_keys($registeredPackages) as $name) {
Expand Down
2 changes: 1 addition & 1 deletion modules/system/console/asset/AssetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function handle(): int
$compilableAssets = CompilableAssets::instance();
$compilableAssets->fireCallbacks();

$packages = $compilableAssets->getPackages(true, $this->assetType);
$packages = $compilableAssets->getPackages($this->assetType, true);

if (count($packages) === 0) {
$this->info('No packages have been registered.');
Expand Down
70 changes: 70 additions & 0 deletions modules/system/tests/classes/PackageJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,76 @@ public function testRemoveDependency(): void
$this->assertArrayNotHasKey('test', $packageJson->getContents()['dependencies']);
}

/**
* Test checking if a script exists in a package.json
*
* @return void
*/
public function testHasScript(): void
{
$packageJson = new PackageJson(__DIR__ . '/../fixtures/npm/package-test.json');

$this->assertTrue($packageJson->hasScript('foo'));
$this->assertTrue($packageJson->hasScript('example'));
$this->assertTrue($packageJson->hasScript('test'));

$this->assertFalse($packageJson->hasScript('bar'));
$this->assertFalse($packageJson->hasScript('winter'));
$this->assertFalse($packageJson->hasScript('testing'));
}

/**
* Test getting the value of a script by name
*
* @return void
*/
public function testGetScript(): void
{
$packageJson = new PackageJson(__DIR__ . '/../fixtures/npm/package-test.json');

$this->assertEquals('bar ./test', $packageJson->getScript('foo'));
$this->assertEquals('example test', $packageJson->getScript('example'));
$this->assertEquals('testing', $packageJson->getScript('test'));
}

/**
* Test getting the value of a script by name
*
* @return void
*/
public function testAddScript(): void
{
$packageJson = new PackageJson(__DIR__ . '/../fixtures/npm/package-test.json');

$packageJson->addScript('winter', 'winter ./testing');

$this->assertTrue($packageJson->hasScript('winter'));
$this->assertEquals('winter ./testing', $packageJson->getScript('winter'));

$contents = $packageJson->getContents();

$this->assertTrue(isset($contents['scripts']['winter']));
$this->assertEquals('winter ./testing', $contents['scripts']['winter']);
}

/**
* Test removing scripts from package.json
*
* @return void
*/
public function testRemoveScript(): void
{
$packageJson = new PackageJson(__DIR__ . '/../fixtures/npm/package-test.json');

$this->assertTrue($packageJson->hasScript('foo'));
$packageJson->removeScript('foo');
$this->assertFalse($packageJson->hasScript('foo'));

$this->assertTrue($packageJson->hasScript('example'));
$packageJson->removeScript('example');
$this->assertFalse($packageJson->hasScript('example'));
}

/**
* Test saving, when saving with a file path set on init and passing a file path on save. Fails when no path given
*
Expand Down
5 changes: 5 additions & 0 deletions modules/system/tests/fixtures/npm/package-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
},
"devDependencies": {
"test-dev": "^3.0.2"
},
"scripts": {
"test": "testing",
"example": "example test",
"foo": "bar ./test"
}
}

0 comments on commit 1500557

Please sign in to comment.