forked from jpnelson/ci-npm-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·119 lines (95 loc) · 2.83 KB
/
index.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
#!/usr/bin/env node
"use strict";
var Path = require("path"),
Fs = require("fs"),
Semver = require("semver"),
RegClient = require("npm-registry-client"),
_ = require("underscore"),
Exec = require('child_process').exec,
Flags = require("minimist")( process.argv.slice(2) ),
client = new RegClient(),
// local vars
uri = Flags.npmregistry || process.env.NPMREGISTRY || "https://registry.npmjs.org/",
user = Flags.npmuser || Flags.NPMUSER || process.env.NPMUSER,
password = Flags.npmpassword || Flags.NPMPASSWORD || process.env.NPMPASSWORD,
email = Flags.npmemail || Flags.NPMEMAIL || process.env.NPMEMAIL,
config = Flags.config || process.cwd(),
opts = {
auth: {
username: user,
password: password,
email: email
}
},
configJSON = require(Path.join(config, "package.json")),
tarball = getTarballName(configJSON),
bodyPath = Path.join(config, tarball);
function getTarballName (configJSON) {
var version = configJSON.version;
var isScoped = (configJSON.name.indexOf("@") === 0);
if (isScoped) {
var scopeAndName = configJSON.name.replace("@", "").split("/");
var scope = scopeAndName[0];
var name = scopeAndName[1];
return scope + "-" + name + "-" + version + ".tgz";
}
return configJSON.name + "-" + version + ".tgz"
}
// create user and publish
var publish = function() {
console.log("trying to publish to npm");
// console.log(Npm.registry.adduser.toString())
client.adduser(uri, opts, function(err, data, raw, res){
if (err) {
console.log(err);
throw err
}
var body = Fs.createReadStream(bodyPath);
// authenticated or created, ready to publish
if (data.ok) {
publishParams = {
access: "public",
body: body,
metadata: configJSON
}
var publishParams = _.extend(publishParams, opts);
client.publish(uri, publishParams, function(err, response){
if (err) {
console.log(err);
throw err
}
console.log(response.ok || "package published!")
})
}
})
}
var pkgeUp = function(){
var body = Fs.createReadStream(bodyPath, "base64");
// require user, password, and email
if (!user || !password || !email) {
console.log("no auth variables set!")
return
}
publish()
}
// encode and add readme data to package
var readmePath = Path.join(config, "README.md")
if (Fs.existsSync(readmePath)) {
var readme = Fs.readFileSync(readmePath, {encoding: "utf8"});
if (!configJSON.readme) {
configJSON.readme = readme
}
}
// require tarball
if (!Fs.existsSync(bodyPath)) {
// console.log("no tarball! can't stream publish")
Exec("npm pack", {cwd: config}, function(err, stdout, stderr){
if (err) {
console.log(err, stderr);
throw err;
}
pkgeUp();
})
} else {
pkgeUp();
}