@tapestry-mud/cli 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const fetch = require('node-fetch');
|
|
4
4
|
const { requireToken } = require('../lib/auth');
|
|
5
|
-
const { DEFAULT_REGISTRY } = require('../lib/registry-client');
|
|
5
|
+
const { DEFAULT_REGISTRY, throwIfError } = require('../lib/registry-client');
|
|
6
6
|
const { createInterface, askPassword } = require('../util/prompt');
|
|
7
7
|
|
|
8
8
|
async function changePassword({ registryUrl = DEFAULT_REGISTRY } = {}) {
|
|
@@ -23,10 +23,7 @@ async function changePassword({ registryUrl = DEFAULT_REGISTRY } = {}) {
|
|
|
23
23
|
},
|
|
24
24
|
body: JSON.stringify({ currentPassword, newPassword }),
|
|
25
25
|
});
|
|
26
|
-
|
|
27
|
-
const body = await res.json().catch(() => ({}));
|
|
28
|
-
throw new Error(body.error || `Change password failed (${res.status})`);
|
|
29
|
-
}
|
|
26
|
+
await throwIfError(res, 'Change password failed');
|
|
30
27
|
console.log('Password changed.');
|
|
31
28
|
} finally {
|
|
32
29
|
rl.close();
|
package/src/commands/login.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const fetch = require('node-fetch');
|
|
4
4
|
const { saveToken } = require('../lib/auth');
|
|
5
|
-
const { DEFAULT_REGISTRY } = require('../lib/registry-client');
|
|
5
|
+
const { DEFAULT_REGISTRY, throwIfError } = require('../lib/registry-client');
|
|
6
6
|
const { createInterface, ask, askPassword } = require('../util/prompt');
|
|
7
7
|
|
|
8
8
|
async function promptCredentials() {
|
|
@@ -27,10 +27,7 @@ async function login({ email, password } = {}, { registryUrl = DEFAULT_REGISTRY
|
|
|
27
27
|
body: JSON.stringify({ email, password }),
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
const body = await res.json().catch(() => ({}));
|
|
32
|
-
throw new Error(body.error || `Login failed (${res.status})`);
|
|
33
|
-
}
|
|
30
|
+
await throwIfError(res, 'Login failed');
|
|
34
31
|
|
|
35
32
|
const { token } = await res.json();
|
|
36
33
|
saveToken(token);
|
package/src/commands/publish.js
CHANGED
|
@@ -9,7 +9,7 @@ const { readYaml } = require('../util/yaml');
|
|
|
9
9
|
const { validate } = require('./validate');
|
|
10
10
|
const { buildTarball, computeIntegrity } = require('../lib/tarball-builder');
|
|
11
11
|
const { requireToken } = require('../lib/auth');
|
|
12
|
-
const { DEFAULT_REGISTRY } = require('../lib/registry-client');
|
|
12
|
+
const { DEFAULT_REGISTRY, throwIfError } = require('../lib/registry-client');
|
|
13
13
|
|
|
14
14
|
async function publish({ cwd = process.cwd(), registryUrl = DEFAULT_REGISTRY } = {}) {
|
|
15
15
|
validate({ cwd });
|
|
@@ -45,10 +45,7 @@ async function publish({ cwd = process.cwd(), registryUrl = DEFAULT_REGISTRY } =
|
|
|
45
45
|
body: form,
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
const body = await res.json().catch(() => ({}));
|
|
50
|
-
throw new Error(body.error || `Publish failed (${res.status})`);
|
|
51
|
-
}
|
|
48
|
+
await throwIfError(res, 'Publish failed');
|
|
52
49
|
|
|
53
50
|
const result = await res.json();
|
|
54
51
|
console.log(` Published ${result.name}@${result.version}`);
|
package/src/commands/register.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const fetch = require('node-fetch');
|
|
4
4
|
const { saveToken } = require('../lib/auth');
|
|
5
|
-
const { DEFAULT_REGISTRY } = require('../lib/registry-client');
|
|
5
|
+
const { DEFAULT_REGISTRY, throwIfError } = require('../lib/registry-client');
|
|
6
6
|
const { createInterface, ask, askPassword } = require('../util/prompt');
|
|
7
7
|
|
|
8
8
|
async function promptRegistration() {
|
|
@@ -28,10 +28,7 @@ async function register({ handle, email, password } = {}, { registryUrl = DEFAUL
|
|
|
28
28
|
body: JSON.stringify({ handle, email, password }),
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
const body = await res.json().catch(() => ({}));
|
|
33
|
-
throw new Error(body.error || `Registration failed (${res.status})`);
|
|
34
|
-
}
|
|
31
|
+
await throwIfError(res, 'Registration failed');
|
|
35
32
|
|
|
36
33
|
const { token } = await res.json();
|
|
37
34
|
saveToken(token);
|
|
@@ -16,6 +16,18 @@ function validatePackageName(name) {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
async function throwIfError(res, context) {
|
|
20
|
+
if (res.status === 429) {
|
|
21
|
+
const retryAfter = res.headers.get('retry-after');
|
|
22
|
+
const hint = retryAfter ? ` Try again in ${Math.ceil(Number(retryAfter) / 60)} min.` : '';
|
|
23
|
+
throw new Error(`Rate limit exceeded.${hint}`);
|
|
24
|
+
}
|
|
25
|
+
if (!res.ok) {
|
|
26
|
+
const body = await res.json().catch(() => ({}));
|
|
27
|
+
throw new Error(body.error || `${context} (${res.status})`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
19
31
|
async function fetchPackageMetadata(name, registryUrl = DEFAULT_REGISTRY) {
|
|
20
32
|
validatePackageName(name);
|
|
21
33
|
const url = `${registryUrl}/v1/packages/${name}`;
|
|
@@ -43,4 +55,4 @@ async function fetchTarball(url) {
|
|
|
43
55
|
return res.buffer();
|
|
44
56
|
}
|
|
45
57
|
|
|
46
|
-
module.exports = { fetchPackageMetadata, fetchTarball, DEFAULT_REGISTRY };
|
|
58
|
+
module.exports = { fetchPackageMetadata, fetchTarball, throwIfError, DEFAULT_REGISTRY };
|