Skip to content

Commit 77623af

Browse files
committed
Fix Grunt plugin search
Fixes #120 Like Yeoman generators, Grunt plugins are a subset of NPM packages, so we use the NPMs.io API to search them in a similar fashion. Amongst these 3 searches, definitely room to refactor.
1 parent db80cd1 commit 77623af

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

Package Managers.alfredworkflow

-72 Bytes
Binary file not shown.

src/Grunt.php

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,49 @@
55
class Grunt extends Repo
66
{
77
protected $id = 'grunt';
8-
protected $kind = 'plugins';
9-
protected $url = 'http://gruntjs.com';
10-
protected $search_url = 'http://gruntjs.com/plugins/';
8+
protected $kind = 'gruntplugin';
119
protected $has_db = true;
10+
protected $url = 'https://npms.io';
11+
protected $search_url = 'https://api.npms.io/v2/search?q=keywords:gruntplugin+';
1212

1313
public function search($query)
1414
{
15+
$query = str_replace(' ', '+', $query);
1516
if (!$this->hasMinQueryLength($query)) {
1617
return $this->asJson();
1718
}
1819

19-
foreach ($this->pkgs->aaData as $pkg) {
20-
// make params
21-
if ($this->check($pkg, $query, 'name', 'ds')) {
22-
// remove grunt- from title
23-
$title = str_replace('grunt-', '', $pkg->name);
24-
25-
// add author to title
26-
if (isset($pkg->author)) {
27-
$title .= " by {$pkg->author}";
28-
}
29-
$url = "https://www.npmjs.org/package/{$pkg->name}";
30-
31-
// Uncomment to skip deprecated plugins
32-
// if (strpos($plugin->description, "DEPRECATED") !== false) {
33-
// continue;
34-
// }
35-
36-
$this->cache->w->result(
37-
$pkg->name,
38-
$this->makeArg($pkg->name, $url),
39-
$title,
40-
$pkg->ds,
41-
"icon-cache/{$this->id}.png"
42-
);
43-
}
44-
20+
$this->pkgs = $this->cache->get_query_json(
21+
$this->id,
22+
$query,
23+
"{$this->search_url}{$query}&size={$this->max_return}"
24+
);
25+
26+
foreach ($this->pkgs->results as $pkg) {
27+
$p = $pkg->package;
28+
$name = $p->name;
29+
$uid = "{$this->id}-{$name}-{$p->version}";
30+
31+
$this->cache->w->result(
32+
$uid,
33+
$this->makeArg($name, $p->links->npm, "{$p->name}: {$p->version}"),
34+
$name,
35+
$p->description,
36+
"icon-cache/{$this->id}.png"
37+
);
4538

4639
// only search till max return reached
4740
if (count($this->cache->w->results()) === $this->max_return) {
4841
break;
4942
}
5043
}
5144

52-
$this->noResults($query, "{$this->search_url}{$query}");
45+
$this->noResults($query, "{$this->url}/search?q=keywords:gruntplugin+{$query}");
5346

5447
return $this->asJson();
5548
}
5649
}
5750

5851
// Test code, uncomment to debug this script from the command-line
5952
// $repo = new Grunt();
60-
// echo $repo->search('contrib');
53+
// echo $repo->search('contrib');

0 commit comments

Comments
 (0)