-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpublish.js
173 lines (141 loc) · 5 KB
/
publish.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
162
163
164
165
166
167
168
169
170
171
172
173
//in order to publish
// - must generate proper npm json for the package.
// - download existing json if any and compare to make sure there is a version change
// - must npm pack tar
// - must copy tar named after version to s3 bucket.
// - must copy json to file and send to to s3 bucket named packagenamehere.json
var path = require('path');
var packageroot = require('packageroot');
var packagenpmjson = require('packagenpmjson');
var knox = require('knox');
var fs = require('fs');
var info = require('./info');// request existing json for package.
var log = require('./log');
var exec = require('./exec');
var jsonmerge = require('./jsonmerge');
var s3error = require('./s3error');
var npm = require('npm');
module.exports = function(config,cb){
var cwd = config.dir||process.cwd();
packageroot(cwd,function(err,root){
// get the target package.json
var packagejson = require(path.join(root,'package.json'));
if(err) return cb(err);
log('info','preparing publish ',packagejson.name,' in ',root,' to s3');
var s = Date.now();
exec.pack(root,function(err,tarpath,stat){
log('info','packed tar to publish - ',Date.now()-s,'ms');
//
// generate npm style json.
//
//[npm registry url],[path to package root],[path to tar produced by npm pack]
s = Date.now();
packagenpmjson('{REPO ADDRESS}',root,tarpath,function(err,data){
log('info','generated package json to publish ',Date.now()-s,'ms');
if(err) return cb(err);
//
// get the json of the version already published.
//
s = Date.now();
if(!packagejson.s3bucket) {
if(config.defaultBucket){
log('warn','package.json did not define an s3bucket using default ',config.defaultBucket);
packagejson.s3bucket = config.defaultBucket;
} else {
return cb(new Error('s3bucket or default bucket required for publish'));
}
}
info(config,packagejson.s3bucket+'/'+packagejson.name,function(err,json){
log('info','got package information friom s3 ',Date.now()-s,'ms');
if(err && err.code !== 'E_NOENT') return cb(err);
if(json) {
// is this version of this module already published?
if(json.versions[packagejson.version]){
if(config.force) {
log('warn','this version is already published but you are forcing me to overwrite it!');
} else {
var e = new Error('verison already published. incrememnt version or --force to force overwrite.');
e.code = 'E_DUP';
return cb(e);
}
}
//
// merge json
//
json = jsonmerge(json,data);
} else {
json = data;
}
//
// put in s3
//
var client = knox.createClient({
key:config.key,
secret:config.secret,
bucket:packagejson.s3bucket
});
s = Date.now();
var c = 2
,errors = []
,done = function(err,data){
if(err) errors.push(err);
c--;
if(!c) {
//
// im uploaded to s3!
//
fs.unlink(tarpath,function(err){
log('info','published package information and tar to s3',Date.now()-s,'ms');
if(err) log('warn','couldnt delete packed tar',err);
cb(errors.length?errors:false,json,'/'+path.basename(tarpath));
});
}
};
jsonstr = JSON.stringify(json);
//
// put json
//
console.log('PUT JSON ',packagejson.s3bucket);
console.log('PUT JSON ',packagejson.name);
var req = client.put(packagejson.name+'.json',{
'Content-Length': jsonstr.length
, 'Content-Type': 'application/json'
});
req.on('response',function(res){
var body = '';
res.on('data',function(data){
body += data.toString();
}).on('end',function(){
var json = false;
if(res.statusCode === 200){
err = false;
} else {
err = body;
}
done(s3error(err),body,res);
});
});
req.end(jsonstr);
//
// put tar
//
client.putFile(tarpath,'/'+path.basename(tarpath),function(err,res){
// fu
var body = '';
res.on('data',function(data){
body += data.toString();
}).on('end',function(){
var json = false;
if(res.statusCode === 200){
err = false;
} else {
err = body;
}
done(s3error(err),body,res);
});
});
});
});
});
});
}