-
Notifications
You must be signed in to change notification settings - Fork 348
/
check-published-plugin.js
executable file
·161 lines (151 loc) · 4.47 KB
/
check-published-plugin.js
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
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var Q = require('Q');
var semver = require('semver');
var cordovalib = require('cordova').cordova_lib;
var TARGET_OWNERS = {
'agrieve': 1,
'maxw': 1,
'mmocny': 1,
'ian': 1,
'kamrik': 1
};
function collectPluginInfos(dirs) {
// Stop plugman from spewing JSON
console.log = function() {};
var ret = [];
dirs.forEach(function(dir) {
if (!fs.existsSync(path.join(dir, 'plugin.xml'))) {
console.warn('Skipping: ' + dir + ' (no plugin.xml found)');
return;
}
var pluginInfo = new cordovalib.PluginInfo(dir);
ret.push(cordovalib.plugman.raw.info([pluginInfo.id])
.then(function(infoResult) {
return cordovalib.plugman.raw.owner(['ls', pluginInfo.id])
.then(function(npmOwners) {
return {
pluginInfo: pluginInfo,
npmInfo: infoResult,
npmOwners: npmOwners.map(function(o) { return o.name })
};
});
}, function(e) {
console.warn('Failed to get info for: ' + pluginInfo.id);
console.warn(e);
return {
pluginInfo: pluginInfo,
npmInfo: null,
npmOwners: null
};
}));
});
return Q.all(ret).finally(function() {
delete console.log;
});
}
function validatePlugins(pluginInfos) {
var hadWarning = false;
pluginInfos.forEach(function(info) {
var npmInfo = info.npmInfo;
var pluginInfo = info.pluginInfo;
info.ownersToRemove = [];
info.ownersToAdd = [];
if (npmInfo) {
var warnings = [];
if (npmInfo.name != pluginInfo.id) {
warnings.push('Plugin ID is different on NPM: ' + npmInfo.name);
}
var latestVer = npmInfo['dist-tags']['latest'];
var expectedLocalVer = semver.inc(latestVer, 'patch') + '-dev';
if (pluginInfo.version != latestVer && pluginInfo.version != expectedLocalVer) {
warnings.push('Published version is ' + latestVer + ' but local version is ' + pluginInfo.version);
}
var expectedAttachment = pluginInfo.id + '-' + latestVer + '.tgz';
var attachments = npmInfo._attachments;
if (!attachments[expectedAttachment] || attachments[expectedAttachment].length < 1000) {
warnings.push('Looks to be missing .tgz!');
}
info.npmOwners.forEach(function(owner) {
if (!TARGET_OWNERS[owner]) {
warnings.push('Has extra owner "' + owner + '"');
info.ownersToRemove.push(owner);
}
});
Object.keys(TARGET_OWNERS).forEach(function(owner) {
if (info.npmOwners.indexOf(owner) == -1) {
warnings.push('Missing owner "' + owner + '"');
info.ownersToAdd.push(owner);
}
});
if (warnings.length) {
warnings.forEach(function(msg) {
console.warn(pluginInfo.id + ': ' + msg);
});
hadWarning = true;
} else {
console.log(pluginInfo.id + '@' + latestVer + ': Everything checks out.');
}
} else {
console.log(pluginInfo.id + ': Is not published yet.');
}
});
return Q(hadWarning);
}
function fixPlugins(pluginInfos) {
var ret = Q();
pluginInfos.forEach(function(info) {
var pluginId = info.pluginInfo.id;
info.ownersToRemove.forEach(function(owner) {
ret = ret.then(function() {
console.log('plugman owner rm ' + owner + ' ' + pluginId);
return cordovalib.plugman.raw.owner(['rm', owner, pluginId]);
});
});
info.ownersToAdd.forEach(function(owner) {
ret = ret.then(function() {
console.log('plugman owner add ' + owner + ' ' + pluginId);
return cordovalib.plugman.raw.owner(['add', owner, pluginId]);
});
});
});
return ret;
}
function usage() {
console.log('usage: ' + process.argv[1] + ' [--fix] pluginDir1 pluginDir2 ...');
process.exit(1);
}
function main() {
var dirs = [];
var fix = false;
process.argv.slice(2).forEach(function(arg) {
if (/^-/.exec(arg)) {
if (arg == '--fix') {
fix = true;
} else if (arg != '--help') {
console.error('Unknown flag: ' + arg);
usage();
} else {
usage();
}
} else {
dirs.push(arg);
}
});
if (dirs.length === 0) {
usage();
}
collectPluginInfos(dirs)
.then(function(pluginInfos) {
return validatePlugins(pluginInfos)
.then(function(hadWarning) {
if (fix) {
return fixPlugins(pluginInfos);
} else if (hadWarning) {
console.log('To fix owner problems, run with --fix');
}
});
}).done();
}
main();