@tapestry-mud/cli 0.3.0 → 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/bin/tapestry.js CHANGED
@@ -2,6 +2,7 @@
2
2
  'use strict';
3
3
 
4
4
  const { Command } = require('commander');
5
+ const { version } = require('../package.json');
5
6
  const { init } = require('../src/commands/init');
6
7
  const { createPack } = require('../src/commands/create-pack');
7
8
  const { install } = require('../src/commands/install');
@@ -29,7 +30,7 @@ const program = new Command();
29
30
  program
30
31
  .name('tapestry')
31
32
  .description('Tapestry Package Manager')
32
- .version('0.1.0');
33
+ .version(version);
33
34
 
34
35
  program
35
36
  .command('init')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tapestry-mud/cli",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "CLI for the Tapestry MUD engine",
5
5
  "bin": {
6
6
  "tapestry": "./bin/tapestry.js"
@@ -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
- if (!res.ok) {
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();
@@ -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
- if (!res.ok) {
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);
@@ -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
- if (!res.ok) {
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}`);
@@ -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
- if (!res.ok) {
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 };
@@ -14,14 +14,13 @@ function ask(rl, question) {
14
14
 
15
15
  function askPassword(rl, prompt) {
16
16
  return new Promise((resolve) => {
17
- process.stdout.write(prompt);
18
17
  const orig = rl._writeToOutput.bind(rl);
19
- rl._writeToOutput = () => {};
20
- rl.question('', (password) => {
18
+ rl.question(prompt, (password) => {
21
19
  rl._writeToOutput = orig;
22
20
  process.stdout.write('\n');
23
21
  resolve(password);
24
22
  });
23
+ rl._writeToOutput = () => {};
25
24
  });
26
25
  }
27
26