forked from willfarrell/alfred-pkgman-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Npm.php
51 lines (42 loc) · 1.36 KB
/
Npm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace WillFarrell\AlfredPkgMan;
require_once('Cache.php');
require_once('Repo.php');
class Npm extends Repo
{
protected $id = 'npm';
protected $url = 'https://npms.io';
protected $search_url = 'https://api.npms.io/v2/search?q=';
public function search($query)
{
$query = str_replace(' ', '+', $query);
if (!$this->hasMinQueryLength($query)) {
return $this->xml();
}
$this->pkgs = $this->cache->get_query_json(
$this->id,
$query,
"{$this->search_url}{$query}&size={$this->max_return}"
);
foreach ($this->pkgs->results as $pkg) {
$p = $pkg->package;
$name = $p->name;
$this->cache->w->result(
$this->id,
$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}{$query}");
return $this->xml();
}
}
// Test code, uncomment to debug this script from the command-line
// $repo = new Npm();
// echo $repo->search('gr');