UNPKG

1.51 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const { resolve } = require('path');
4
5const program = require('commander');
6
7const { readYaml } = require('./utils');
8
9// -- Constants --------------- --- -- -
10
11const PROD_COMPOSE_FILE = 'dc.prod.yml';
12const COMPOSE_FILE = process.env.COMPOSE_FILE || PROD_COMPOSE_FILE;
13const COMPOSE_PROJECT_NAME = process.env.COMPOSE_PROJECT_NAME;
14
15// -- getNetworkNames --------------- --- -- -
16
17/**
18 * Returns the names of the non-external networks declared in the default or given Compose file.
19 * @param {string} [composeFile] - The name of the Compose file to use. Defaults to the value of the
20 * `COMPOSE_FILE` environment variable, or `dc.prod.yml`.
21 * @returns {Promise.<string[]>}
22 */
23const getNetworkNames = async ({ composefile, prepend }) => {
24 const composeFile = await readYaml(resolve(composefile));
25 const networks = composeFile.networks;
26 let names = Object.keys(networks);
27
28 names = names.filter((name) => {
29 return networks[name] && !networks[name].external
30 });
31
32 if (prepend) {
33 names = names.map((name) => `${COMPOSE_PROJECT_NAME}_${name}`);
34 }
35
36 return names;
37};
38
39// -- CLI --------------- --- -- -
40
41program
42 .version('0.1.0')
43 .option('--composefile [path]', `Specify the Compose file to use [${COMPOSE_FILE}].`, COMPOSE_FILE)
44 .option('--prepend', 'Prepend the compose-project-name.')
45 .parse(process.argv);
46
47getNetworkNames(program)
48 .then((names) => console.log(names.join(' ')))
49 .catch((error) => {
50 console.error(error);
51 process.exit(1);
52 });