1
- var exec = require ( "child_process" ) . exec ,
2
- fs = require ( "fs" ) ,
3
- mkdirp = require ( "mkdirp" ) ,
4
- semver = require ( "semver" ) ,
1
+ var semver = require ( "semver" ) ,
5
2
template = require ( "./template" ) ,
6
3
wordpress = require ( "./wordpress" ) ,
7
4
pluginsDb = require ( "./pluginsdb" ) ,
5
+ service = require ( "./service.js" ) ,
8
6
config = require ( "./config" ) ;
9
7
10
8
11
9
12
10
13
11
14
- function dirname ( path ) {
15
- path = path . split ( "/" ) ;
16
- path . pop ( ) ;
17
- return path . join ( "/" ) ;
18
- }
19
-
20
- function createError ( message , code , data ) {
21
- var error = new Error ( message ) ;
22
- if ( code ) {
23
- error . code = code ;
24
- }
25
- if ( data ) {
26
- error . data = data ;
27
- }
28
- return error ;
29
- }
30
-
31
-
32
-
33
-
34
-
35
- var reGithubSsh = / ^ g i t @ g i t h u b \. c o m : ( [ ^ / ] + ) \/ ( .+ ) \. g i t $ / ,
36
- reGithubHttp = / ^ \w + ?: \/ \/ \w + @ g i t h u b \. c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \. g i t $ / ,
37
- reGithubGit = / ^ g i t : \/ \/ g i t h u b \. c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \. g i t $ / ,
38
- reGithubSite = / ^ h t t p s ? : \/ \/ g i t h u b \. c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) ( \/ .* ) ? $ / ;
39
-
40
- function getRepoDetails ( repo ) {
41
- var userName , repoName , partialPath ,
42
- matches =
43
- reGithubSsh . exec ( repo ) ||
44
- reGithubHttp . exec ( repo ) ||
45
- reGithubGit . exec ( repo ) ||
46
- reGithubSite . exec ( repo ) ;
47
-
48
- if ( matches ) {
49
- userName = matches [ 1 ] ;
50
- repoName = matches [ 2 ] ;
51
- partialPath = "/" + userName + "/" + repoName ;
52
- return {
53
- userName : userName ,
54
- repoName : repoName ,
55
- url : "http://github.com" + partialPath ,
56
- git : "git://github.com" + partialPath + ".git" ,
57
- downloadUrl : function ( version ) {
58
- return "https://github.com" + partialPath + "/zipball/" + version
59
- } ,
60
- path : config . repoDir + partialPath
61
- } ;
62
- }
63
-
64
- return null ;
65
- }
66
-
67
-
68
-
69
-
70
-
71
- function fetchPlugin ( repoDetails , fn ) {
72
- // make sure the user's directory exists first
73
- mkdirp ( dirname ( repoDetails . path ) , 0755 , function ( error ) {
74
- if ( error ) {
75
- return fn ( error ) ;
76
- }
77
-
78
- createOrUpdateRepo ( repoDetails , fn ) ;
79
- } ) ;
80
- }
81
-
82
- function createOrUpdateRepo ( repoDetails , fn ) {
83
- fs . stat ( repoDetails . path , function ( error ) {
84
- // repo already exists
85
- if ( ! error ) {
86
- return updateRepo ( repoDetails , fn ) ;
87
- }
88
-
89
- // error other than repo not existing
90
- if ( error . code !== "ENOENT" ) {
91
- return fn ( error ) ;
92
- }
93
-
94
- createRepo ( repoDetails , fn ) ;
95
- } ) ;
96
- }
97
-
98
- function createRepo ( repoDetails , fn ) {
99
- exec ( "git clone " + repoDetails . git + " " + repoDetails . path , function ( error , stdout , stderr ) {
100
- if ( error ) {
101
- return fn ( error ) ;
102
- }
103
-
104
- // TODO: handle stderr
105
-
106
- getVersions ( repoDetails , fn ) ;
107
- } ) ;
108
- }
109
-
110
- function updateRepo ( repoDetails , fn ) {
111
- getVersions ( repoDetails , function ( error , prevVersions ) {
112
- if ( error ) {
113
- return fn ( error ) ;
114
- }
115
-
116
- exec ( "git fetch -t" , { cwd : repoDetails . path } ,
117
- function ( error , stdout , stderr ) {
118
- if ( error ) {
119
- return fn ( error ) ;
120
- }
121
-
122
- // TODO: handle stderr
123
-
124
- getVersions ( repoDetails , function ( error , versions ) {
125
- if ( error ) {
126
- return fn ( error ) ;
127
- }
128
-
129
- fn ( null , versions . filter ( function ( version ) {
130
- return prevVersions . indexOf ( version ) === - 1 ;
131
- } ) ) ;
132
- } ) ;
133
- } ) ;
134
- } ) ;
135
- }
136
-
137
-
138
-
139
-
140
-
141
- function getVersions ( repoDetails , fn ) {
142
- exec ( "git tag" , { cwd : repoDetails . path } , function ( error , stdout , stderr ) {
143
- if ( error ) {
144
- return fn ( error ) ;
145
- }
146
-
147
- var tags = stdout . split ( "\n" ) ;
148
- tags . pop ( ) ;
149
- tags = tags . filter ( function ( version ) {
150
- // we allow a v prefix, but nothing else
151
- if ( version . charAt ( 0 ) === "v" ) {
152
- version = version . substring ( 1 ) ;
153
- }
154
-
155
- // tag is not a clean version number
156
- if ( semver . clean ( version ) !== version ) {
157
- return false ;
158
- }
159
-
160
- return semver . valid ( version ) ;
161
- } ) ;
162
- fn ( null , tags ) ;
163
- } ) ;
164
- }
165
-
166
-
167
-
168
-
169
-
170
- function validateVersion ( repoDetails , version , fn ) {
171
- getPackageJson ( repoDetails , version , function ( error , package ) {
12
+ function validateVersion ( repo , version , fn ) {
13
+ repo . getPackageJson ( version , function ( error , package ) {
172
14
if ( error ) {
173
15
return fn ( error ) ;
174
16
}
@@ -180,31 +22,6 @@ function validateVersion( repoDetails, version, fn ) {
180
22
} ) ;
181
23
}
182
24
183
- function getPackageJson ( repoDetails , version , fn ) {
184
- exec ( "git show " + version + ":package.json" , { cwd : repoDetails . path } , function ( error , stdout , stderr ) {
185
- // this will also result in an error being passed, so we check stderr first
186
- if ( stderr && stderr . substring ( 0 , 41 ) === "fatal: Path 'package.json' does not exist" ) {
187
- return fn ( createError ( "No package.json for " + version + "." , "NO_PACKAGE_JSON" , {
188
- version : version
189
- } ) ) ;
190
- }
191
-
192
- if ( error ) {
193
- return fn ( error ) ;
194
- }
195
-
196
- try {
197
- var package = JSON . parse ( stdout ) ;
198
- } catch ( error ) {
199
- return fn ( createError ( "Could not parse package.json for " + version + "." , "INVALID_PACKAGE_JSON" , {
200
- version : version
201
- } ) ) ;
202
- }
203
-
204
- return fn ( null , package ) ;
205
- } ) ;
206
- }
207
-
208
25
function validatePackageJson ( package , version ) {
209
26
var errors = [ ] ;
210
27
@@ -273,14 +90,14 @@ function generatePage( package, fn ) {
273
90
274
91
275
92
276
- function processPlugin ( repo , fn ) {
277
- var repoDetails = getRepoDetails ( repo . url ) ;
93
+ function processPlugin ( data , fn ) {
94
+ var repo = service . getRepo ( data ) ;
278
95
279
- if ( ! repoDetails ) {
280
- return fn ( createError ( "Could not parse '" + repoUrl + "'." , "URL_PARSE " ) ) ;
96
+ if ( ! repo ) {
97
+ return fn ( new Error ( "Could not parse request. " ) ) ;
281
98
}
282
99
283
- fetchPlugin ( repoDetails , function ( error , versions ) {
100
+ repo . getNewVersions ( function ( error , versions ) {
284
101
if ( error ) {
285
102
return fn ( error ) ;
286
103
}
@@ -348,7 +165,7 @@ function processPlugin( repo, fn ) {
348
165
}
349
166
350
167
versions . forEach ( function ( version ) {
351
- validateVersion ( repoDetails , version , function ( error , data ) {
168
+ validateVersion ( repo , version , function ( error , data ) {
352
169
if ( error ) {
353
170
// TODO: log failure for retry
354
171
return progress ( ) ;
@@ -378,30 +195,28 @@ function processPlugin( repo, fn ) {
378
195
function _addPluginVersion ( version , package , fn ) {
379
196
// find out who owns this plugin
380
197
// if there is no owner, then set the user as the owner
381
- pluginsDb . getOrSetOwner ( package . name , repoDetails . userName , function ( error , owner ) {
198
+ pluginsDb . getOrSetOwner ( package . name , repo . userName , function ( error , owner ) {
382
199
if ( error ) {
383
200
// TODO: log failure for retry
384
201
return fn ( error ) ;
385
202
}
386
203
387
204
// the plugin is owned by someone else
388
- if ( owner !== repoDetails . userName ) {
205
+ if ( owner !== repo . userName ) {
389
206
// TODO: report error to user
390
- return fn ( createError ( "Plugin owned by someone else." , "NOT_OWNER" , {
391
- owner : owner
392
- } ) ) ;
207
+ return fn ( new Error ( "Plugin owned by someone else." ) ) ;
393
208
}
394
209
395
- pluginsDb . addVersion ( repoDetails , package , function ( error ) {
210
+ pluginsDb . addVersion ( repo , package , function ( error ) {
396
211
if ( error ) {
397
212
// TODO: log failure for retry
398
213
return fn ( error ) ;
399
214
}
400
215
401
216
// add additional metadata and generate the plugin page
402
217
var pluginData = Object . create ( package ) ;
403
- pluginData . _downloadUrl = repoDetails . downloadUrl ( version ) ;
404
- pluginData . url = repoDetails . url ;
218
+ pluginData . _downloadUrl = repo . downloadUrl ( version ) ;
219
+ pluginData . url = repo . siteUrl ;
405
220
generatePage ( pluginData , function ( error , page ) {
406
221
if ( error ) {
407
222
// TODO: log failure for retry
0 commit comments