Skip to content

Commit 042cb00

Browse files
Make it compatible with the NPM Scope
1 parent 80760a9 commit 042cb00

6 files changed

Lines changed: 95 additions & 3 deletions

File tree

‎Converter/NpmPackageConverter.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function getMapKeys()
2727

2828
return array(
2929
'name' => array('name', function ($value) use ($assetType) {
30-
return $assetType->formatComposerName($value);
30+
return $assetType->formatComposerName(NpmPackageUtil::convertName($value));
3131
}),
3232
'type' => array('type', function () use ($assetType) {
3333
return $assetType->getComposerType();
@@ -80,4 +80,14 @@ protected function getMapExtras()
8080
'publishConfig' => 'npm-asset-publish-config',
8181
);
8282
}
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
protected function convertDependency($dependency, $version, array &$vcsRepos, array $composer)
88+
{
89+
$dependency = NpmPackageUtil::convertName($dependency);
90+
91+
return parent::convertDependency($dependency, $version, $vcsRepos, $composer);
92+
}
8393
}

‎Converter/NpmPackageUtil.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@
1818
*/
1919
abstract class NpmPackageUtil
2020
{
21+
/**
22+
* Convert the npm package name.
23+
*
24+
* @param string $name The npm package name
25+
*
26+
* @return string
27+
*/
28+
public static function convertName($name)
29+
{
30+
if (0 === strpos($name, '@') && false !== $pos = strpos($name, '/')) {
31+
$name = ltrim(str_replace('/', '--', $name), '@');
32+
}
33+
34+
return $name;
35+
}
36+
2137
/**
2238
* Convert the author section.
2339
*

‎Repository/AbstractAssetsRepository.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function whatProvides(Pool $pool, $name, $bypassFilters = false)
130130
try {
131131
$repoName = Util::convertAliasName($name);
132132
$packageName = Util::cleanPackageName($repoName);
133-
$packageUrl = str_replace('%package%', $packageName, $this->lazyProvidersUrl);
133+
$packageUrl = $this->buildPackageUrl($packageName);
134134
$cacheName = $packageName.'-'.sha1($packageName).'-package.json';
135135
$data = $this->fetchFile($packageUrl, $cacheName);
136136
$repo = $this->createVcsRepositoryConfig($data, Util::cleanPackageName($name));
@@ -155,6 +155,18 @@ public function getMinimalPackages()
155155
return array();
156156
}
157157

158+
/**
159+
* Build the package url.
160+
*
161+
* @param string $packageName The package name
162+
*
163+
* @return string
164+
*/
165+
protected function buildPackageUrl($packageName)
166+
{
167+
return str_replace('%package%', $packageName, $this->lazyProvidersUrl);
168+
}
169+
158170
/**
159171
* Finds what provides in cache or return empty array if the
160172
* name is not a asset package.

‎Repository/NpmRepository.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Composer\Package\CompletePackageInterface;
1616
use Composer\Package\Loader\ArrayLoader;
1717
use Composer\Repository\ArrayRepository;
18+
use Fxp\Composer\AssetPlugin\Converter\NpmPackageUtil;
1819
use Fxp\Composer\AssetPlugin\Exception\InvalidCreateRepositoryException;
1920

2021
/**
@@ -64,6 +65,14 @@ public function search($query, $mode = 0, $type = null)
6465
return array();
6566
}
6667

68+
/**
69+
* {@inheritdoc}
70+
*/
71+
protected function buildPackageUrl($packageName)
72+
{
73+
return parent::buildPackageUrl(NpmPackageUtil::convertName($packageName));
74+
}
75+
6776
/**
6877
* {@inheritdoc}
6978
*/

‎Tests/Converter/NpmPackageConverterTest.php‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function setUp()
2929
/* @var AssetTypeInterface $type */
3030
$type = $this->type;
3131
$this->converter = new NpmPackageConverter($type);
32-
$this->asset = (array) json_decode(file_get_contents(__DIR__.'/../Fixtures/package/npm.json'), true);
32+
$this->asset = $this->loadPackage();
3333
}
3434

3535
public function testConvert()
@@ -164,6 +164,26 @@ public function testConvert()
164164
$this->assertArrayNotHasKey('archive', $composer);
165165
}
166166

167+
public function testConvertWithScope()
168+
{
169+
$this->asset = $this->loadPackage('npm-scope.json');
170+
$composer = $this->converter->convert($this->asset);
171+
172+
$this->assertArrayHasKey('name', $composer);
173+
$this->assertSame('ASSET/scope--test', $composer['name']);
174+
175+
$this->assertArrayHasKey('require', $composer);
176+
$this->assertSame(array(
177+
'ASSET/scope--library1' => '>= 1.0.0',
178+
'ASSET/scope2--library2' => '>= 1.0.0',
179+
), $composer['require']);
180+
181+
$this->assertArrayHasKey('require-dev', $composer);
182+
$this->assertSame(array(
183+
'ASSET/scope3--dev-library1' => '>= 1.0.0',
184+
), $composer['require-dev']);
185+
}
186+
167187
public function getConvertDistData()
168188
{
169189
return array(
@@ -184,4 +204,16 @@ public function testConvertDist($value, $result)
184204
{
185205
$this->assertSame($result, NpmPackageUtil::convertDist($value));
186206
}
207+
208+
/**
209+
* Load the package.
210+
*
211+
* @param string $package The package file name
212+
*
213+
* @return array
214+
*/
215+
private function loadPackage($package = 'npm.json')
216+
{
217+
return (array) json_decode(file_get_contents(__DIR__.'/../Fixtures/package/'.$package), true);
218+
}
187219
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@scope/test",
3+
"description": "Test NPM Scope",
4+
"version": "1.0.0-pre",
5+
"license": "MIT",
6+
"dependencies": {
7+
"@scope/library1": ">= 1.0.0",
8+
"@scope2/library2": ">= 1.0.0"
9+
},
10+
"devDependencies": {
11+
"@scope3/dev-library1": ">= 1.0.0"
12+
}
13+
}

0 commit comments

Comments
 (0)