Skip to content

Commit

Permalink
Use NPMs.io for Yo searches
Browse files Browse the repository at this point in the history
Fixes #161
Fixes #121
  • Loading branch information
jeffbyrnes committed Mar 26, 2022
1 parent 519e763 commit 6d0b46c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
Binary file modified Package Managers.alfredworkflow
Binary file not shown.
4 changes: 2 additions & 2 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<key>bundleid</key>
<string>com.farrell.pkgman.alfredworkflow</string>
<key>category</key>
<string>Development</string>
<string>Internet</string>
<key>connections</key>
<dict>
<key>0376C25E-AF4F-4F76-B9BC-AD9E998CE4A3</key>
Expand Down Expand Up @@ -3262,7 +3262,7 @@ The Python Package Index is very slow due to a lack on API and pagination. A min
<string>PATH</string>
</array>
<key>version</key>
<string>4.2.1</string>
<string>4.2.3</string>
<key>webaddress</key>
<string>https://github.com/willfarrell/alfred-pkgman-workflow</string>
</dict>
Expand Down
42 changes: 22 additions & 20 deletions src/Yo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,47 @@ class Yo extends Repo
protected $id = 'yo';
protected $kind = 'generators';
protected $has_db = true;
protected $url = 'https://npms.io';
protected $search_url = 'https://api.npms.io/v2/search?q=keywords:yeoman-generator+';

public function search($query)
{
$query = str_replace(' ', '+', $query);
if (!$this->hasMinQueryLength($query)) {
return $this->asJson();
}

$this->pkgs = $this->cache->get_query_json(
$this->id,
$query,
"{$this->search_url}{$query}&size={$this->max_return}"
);

foreach ($this->pkgs->results as $pkg) {
// make params
$pkg = $pkg->package;
if ($this->check($pkg, $query)) {
$title = $pkg->name;

// add author to title
if (isset($pkg->author->name)) {
$title .= " by {$pkg->author->name}";
}

$this->cache->w->result(
$pkg->name,
$this->makeArg($pkg->name, $pkg->website),
$title,
$pkg->description,
"icon-cache/{$this->id}.png"
);
}
$p = $pkg->package;
$name = $p->name;
$uid = "{$this->id}-{$name}-{$p->version}";

$this->cache->w->result(
$uid,
$this->makeArg($name, $p->links->npm, "{$p->name}: {$p->version}"),
$name,
$p->description,
"icon-cache/{$this->id}.png"
);

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

$this->noResults($query, $this->search_url);
$this->noResults($query, "{$this->url}/search?q=keywords:yeoman-generator+{$query}");

return $this->asJson();
}
}

// Test code, uncomment to debug this script from the command-line
// $repo = new Yo();
// echo $repo->search('ang');
// echo $repo->search('ang');

0 comments on commit 6d0b46c

Please sign in to comment.