@abcnews/aunty 12.1.2 → 12.1.3

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abcnews/aunty",
3
- "version": "12.1.2",
3
+ "version": "12.1.3",
4
4
  "description": "A toolkit for working with ABC News projects",
5
5
  "repository": "abcnews/aunty",
6
6
  "license": "MIT",
@@ -2,7 +2,7 @@
2
2
  const { cmd, hvy, ok, opt, sec } = require('../../utils/color');
3
3
 
4
4
  const FORCE_REMINDER = `Use the ${opt('--force')} option to ignore warnings or release without tagging.`;
5
- const VALID_BUMPS = (module.exports.VALID_BUMPS = new Set(['major', 'minor', 'patch']));
5
+ const VALID_BUMPS = (module.exports.VALID_BUMPS = new Set(['major', 'minor', 'patch','prerelease']));
6
6
 
7
7
  const OPTIONS = (module.exports.OPTIONS = {
8
8
  string: ['bump', 'git-remote'],
@@ -8,6 +8,7 @@ const BUNDLE_ANALYZER_CONFIG = (module.exports.BUNDLE_ANALYZER_CONFIG = {
8
8
  });
9
9
 
10
10
  module.exports.MESSAGES = {
11
+ port: port => `Port ${port} already in use attempting to use port ${port + 1}`,
11
12
  analysis: ({ analyzerHost, analyzerPort }) => `http://${analyzerHost}:${analyzerPort}`,
12
13
  serve: ({ bundleAnalysisPath, hot, publicPath }) => `Serve (${hvy(process.env.NODE_ENV)}):${
13
14
  bundleAnalysisPath
@@ -23,9 +23,9 @@ module.exports = command(
23
23
  usage: MESSAGES.usage
24
24
  },
25
25
  async argv => {
26
- const { hasBundleAnalysis, port } = getServeConfig();
26
+ const { hasBundleAnalysis, port } = await getServeConfig();
27
27
  const webpackConfig = getWebpackConfig();
28
- const webpackDevServerConfig = getWebpackDevServerConfig();
28
+ const webpackDevServerConfig = await getWebpackDevServerConfig();
29
29
  const { hot, devMiddleware } = webpackDevServerConfig;
30
30
  const { publicPath } = devMiddleware;
31
31
  const bundleAnalyzerConfig = hasBundleAnalysis
@@ -95,9 +95,11 @@ const addKnownProfileProperties = config => {
95
95
  function resolveProperties(config) {
96
96
  const { pkg, root } = getProjectConfig();
97
97
  const { name } = pkg;
98
+ // Package name may be in `@scope/name` format
99
+ const unscopedName = name.split('/').reverse()[0];
98
100
 
99
101
  config.from = join(root, config.from);
100
- config.to = config.to.replace('<name>', name).replace('<id>', config.id);
102
+ config.to = config.to.replace('<name>', unscopedName).replace('<id>', config.id);
101
103
  config.publicPath = typeof config.resolvePublicPath === 'function' ? config.resolvePublicPath(config) : '/';
102
104
 
103
105
  return config;
@@ -2,6 +2,7 @@
2
2
  const { homedir, hostname } = require('os');
3
3
  const { readFileSync } = require('fs');
4
4
  const { join } = require('path');
5
+ const { Socket } = require('net');
5
6
 
6
7
  // External
7
8
  const { probe } = require('tcp-ping-sync');
@@ -9,6 +10,8 @@ const { probe } = require('tcp-ping-sync');
9
10
  // Ours
10
11
  const { combine } = require('../utils/structures');
11
12
  const { getProjectConfig } = require('./project');
13
+ const { info } = require('../utils/logging');
14
+ const { MESSAGES } = require('../cli/serve/constants');
12
15
 
13
16
  const HOME_DIR = homedir();
14
17
  const SSL_DIR = '.aunty/ssl';
@@ -20,10 +23,14 @@ const DEFAULT_HOST = (module.exports.DEFAULT_HOST = probe(`nucwed${INTERNAL_SUFF
20
23
  : 'localhost');
21
24
  const DEFAULT_PORT = 8000;
22
25
 
23
- module.exports.getServeConfig = () => {
26
+ const serveConfigPromise = getServeConfigPromise();
27
+
28
+ module.exports.getServeConfig = () => serveConfigPromise;
29
+
30
+ async function getServeConfigPromise() {
24
31
  const { serve } = getProjectConfig();
25
32
 
26
- return combine(
33
+ const config = combine(
27
34
  {
28
35
  hasBundleAnalysis: false,
29
36
  host: DEFAULT_HOST,
@@ -35,9 +42,13 @@ module.exports.getServeConfig = () => {
35
42
  addEnvironmentVariables,
36
43
  addUserSSLConfig
37
44
  );
38
- };
45
+ const port = await findPort(config.port, config.port + 100, config.host);
46
+ config.port = port;
39
47
 
40
- const addEnvironmentVariables = config => {
48
+ return config;
49
+ }
50
+
51
+ function addEnvironmentVariables(config) {
41
52
  if (process.env.AUNTY_HOST) {
42
53
  config.host = process.env.AUNTY_HOST;
43
54
  }
@@ -47,12 +58,12 @@ const addEnvironmentVariables = config => {
47
58
  }
48
59
 
49
60
  return config;
50
- };
61
+ }
51
62
 
52
63
  const getSSLPath = (module.exports.getSSLPath = (host, name) => join(HOME_DIR, SSL_DIR, host, name || '.'));
53
64
 
54
65
  /*
55
- Set config.https to cert & key generated with `aunty sign-cert` (if they both exist)
66
+ Set config.https to cert & key generated with `aunty sign-cert` (if they both exist)
56
67
  We expect them to be in: ~/.aunty/ssl/<host>/server.{cert|key}
57
68
  */
58
69
  function addUserSSLConfig(config) {
@@ -67,3 +78,41 @@ function addUserSSLConfig(config) {
67
78
 
68
79
  return config;
69
80
  }
81
+
82
+ async function findPort(port, max = port + 100, host = '0.0.0.0') {
83
+ return new Promise((resolve, reject) => {
84
+ const socket = new Socket();
85
+
86
+ const next = () => {
87
+ socket.destroy();
88
+ info(MESSAGES.port(port));
89
+ if (port <= max) resolve(findPort(port + 1, max, host));
90
+ else reject(new Error('Could not find an available port'));
91
+ };
92
+
93
+ const found = () => {
94
+ socket.destroy();
95
+ resolve(port);
96
+ };
97
+
98
+ // Port is taken if connection can be made
99
+ socket.once('connect', next);
100
+
101
+ // Port is open if connection attempt times out
102
+ socket.setTimeout(500);
103
+ socket.once('timeout', found);
104
+
105
+ // If an error occurs, it's assumed the port is available.
106
+ socket.once('error', e => {
107
+ // If the connection is refused, it's assumed nothing is listening and the port is available.
108
+ if (e.code === 'ECONNREFUSED') {
109
+ found();
110
+ } else {
111
+ // Not sure what to do with other errors, so keep seeking a free port.
112
+ next();
113
+ }
114
+ });
115
+
116
+ socket.connect(port, host);
117
+ });
118
+ }
@@ -7,10 +7,10 @@ const { getBuildConfig } = require('./build');
7
7
  const { getProjectConfig } = require('./project');
8
8
  const { getServeConfig } = require('./serve');
9
9
 
10
- module.exports.getWebpackDevServerConfig = () => {
10
+ module.exports.getWebpackDevServerConfig = async () => {
11
11
  const { root, webpackDevServer: projectWebpackDevServerConfig } = getProjectConfig();
12
12
  const { staticDir } = getBuildConfig();
13
- const { host, hot, https, port } = getServeConfig();
13
+ const { host, hot, https, port } = await getServeConfig();
14
14
 
15
15
  return combine(
16
16
  {