Skip to content

Commit 84b38db

Browse files
committed
Moved GitHub-specific logic into service.js (didn't split it templates yet).
1 parent 0d3dd53 commit 84b38db

File tree

2 files changed

+190
-201
lines changed

2 files changed

+190
-201
lines changed

src/main.js

Lines changed: 16 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,16 @@
1-
var exec = require( "child_process" ).exec,
2-
fs = require( "fs" ),
3-
mkdirp = require( "mkdirp" ),
4-
semver = require( "semver" ),
1+
var semver = require( "semver" ),
52
template = require( "./template" ),
63
wordpress = require( "./wordpress" ),
74
pluginsDb = require( "./pluginsdb" ),
5+
service = require( "./service.js" ),
86
config = require( "./config" );
97

108

119

1210

1311

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 = /^git@github\.com:([^/]+)\/(.+)\.git$/,
36-
reGithubHttp = /^\w+?:\/\/\w+@github\.com\/([^/]+)\/([^/]+)\.git$/,
37-
reGithubGit = /^git:\/\/github\.com\/([^/]+)\/([^/]+)\.git$/,
38-
reGithubSite = /^https?:\/\/github\.com\/([^/]+)\/([^/]+)(\/.*)?$/;
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 ) {
17214
if ( error ) {
17315
return fn( error );
17416
}
@@ -180,31 +22,6 @@ function validateVersion( repoDetails, version, fn ) {
18022
});
18123
}
18224

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-
20825
function validatePackageJson( package, version ) {
20926
var errors = [];
21027

@@ -273,14 +90,14 @@ function generatePage( package, fn ) {
27390

27491

27592

276-
function processPlugin( repo, fn ) {
277-
var repoDetails = getRepoDetails( repo.url );
93+
function processPlugin( data, fn ) {
94+
var repo = service.getRepo( data );
27895

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." ) );
28198
}
28299

283-
fetchPlugin( repoDetails, function( error, versions ) {
100+
repo.getNewVersions(function( error, versions ) {
284101
if ( error ) {
285102
return fn( error );
286103
}
@@ -348,7 +165,7 @@ function processPlugin( repo, fn ) {
348165
}
349166

350167
versions.forEach(function( version ) {
351-
validateVersion( repoDetails, version, function( error, data ) {
168+
validateVersion( repo, version, function( error, data ) {
352169
if ( error ) {
353170
// TODO: log failure for retry
354171
return progress();
@@ -378,30 +195,28 @@ function processPlugin( repo, fn ) {
378195
function _addPluginVersion( version, package, fn ) {
379196
// find out who owns this plugin
380197
// 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 ) {
382199
if ( error ) {
383200
// TODO: log failure for retry
384201
return fn( error );
385202
}
386203

387204
// the plugin is owned by someone else
388-
if ( owner !== repoDetails.userName ) {
205+
if ( owner !== repo.userName ) {
389206
// 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." ) );
393208
}
394209

395-
pluginsDb.addVersion( repoDetails, package, function( error ) {
210+
pluginsDb.addVersion( repo, package, function( error ) {
396211
if ( error ) {
397212
// TODO: log failure for retry
398213
return fn( error );
399214
}
400215

401216
// add additional metadata and generate the plugin page
402217
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;
405220
generatePage( pluginData, function( error, page ) {
406221
if ( error ) {
407222
// TODO: log failure for retry

0 commit comments

Comments
 (0)