-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnpm-wrap.cc
151 lines (126 loc) · 3.57 KB
/
npm-wrap.cc
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* ========================================================================== *
*
* Wraps NPM exposing certain routines to Nix as builtins.
*
* -------------------------------------------------------------------------- */
#include <regex>
#include <nix/util.hh>
#include <nix/primops.hh>
#include <nix/json-to-value.hh>
#include <nix/eval-inline.hh>
#include "progs.hh"
using namespace nix;
/* -------------------------------------------------------------------------- */
std::string
nix::npmResolve( const std::string spec )
{
static const std::regex patt = std::regex( "^.*'(https://[^']+)'.*$" );
std::string uri = chomp( runNpm( { "show", spec, "dist.tarball" } ) );
auto lines = tokenizeString<Strings>( uri, "\n" );
if ( 1 < lines.size() )
{
std::smatch match;
std::regex_match( lines.back(), match, patt );
return match[1].str();
}
else if ( hasPrefix( uri, "file:" ) )
{
return uri.substr( 5 );
}
else
{
return uri;
}
}
/* -------------------------------------------------------------------------- */
static void
prim_npmResolve(
EvalState & state, const PosIdx pos, Value ** args, Value & v
)
{
const std::string spec( state.forceStringNoCtx( * args[0], pos ) );
v.mkString( npmResolve( spec ) );
}
static RegisterPrimOp primop_npm_resolve( {
.name = "npmResolve",
.args = { "spec" },
.doc = R"(
Resolve a package specifier <IDENT>[@<DESCRIPTOR>] to a URI.
)",
.fun = prim_npmResolve,
} );
/* -------------------------------------------------------------------------- */
static void
prim_npmShow(
EvalState & state, const PosIdx pos, Value ** args, Value & v
)
{
std::string spec( state.forceStringNoCtx( * args[0], pos ) );
try {
parseJSON( state, runNpm( { "show", "--json", spec } ), v );
} catch( JSONParseError & e ) {
e.addTrace(state.positions[pos], "while decoding a JSON string");
throw;
}
}
static RegisterPrimOp primop_npm_show( {
.name = "npmShow",
.args = { "spec" },
.doc = R"(
Resolve a package specifier <IDENT>[@<DESCRIPTOR>] to a package metadata.
)",
.fun = prim_npmShow,
} );
/* -------------------------------------------------------------------------- */
static void
prim_npmLock(
EvalState & state, const PosIdx pos, Value ** args, Value & v
)
{
PathSet context;
std::string path(
* state.coerceToString( pos, * args[0], context, false, false )
);
auto result = runTreeFor( { path } );
try
{
parseJSON( state, result, v );
}
catch( JSONParseError & e )
{
e.addTrace( state.positions[pos], "while decoding a JSON string" );
throw;
}
}
static RegisterPrimOp primop_npm_lock( {
.name = "npmLock",
.args = { "path" },
.doc = R"(
Produce a virtual package-lock.json for package at *path*.
)",
.fun = prim_npmLock,
} );
/* -------------------------------------------------------------------------- */
static void
prim_semverSat(
EvalState & state, const PosIdx pos, Value ** args, Value & v
)
{
std::string range ( state.forceStringNoCtx( * args[0], pos ) );
std::string version ( state.forceStringNoCtx( * args[1], pos ) );
std::string rsl = runSemver( { "-r", range, version } );
v.mkBool( ! rsl.empty() );
}
static RegisterPrimOp primop_semver_sat( {
.name = "semverSat",
.args = { "range", "version" },
.doc = R"(
Does *version* fall within the semver *range*?
)",
.fun = prim_semverSat,
} );
/* -------------------------------------------------------------------------- *
*
*
*
* ========================================================================== */