UNPKG

3.08 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5const { resolve } = require('path');
6
7const program = require('commander');
8
9const { getServiceSpecs } = require('./utils');
10
11// -- Constants --------------- --- -- -
12
13const COMPOSE_FILE = process.env.COMPOSE_FILE || 'docker-composer.yml';
14
15// -- getServiceNames --------------- --- -- -
16
17/**
18 * Returns the names of the services declared in the default or given Compose file.
19 *
20 * @param {string} [composeFile] - The name of the Compose file to use. Defaults to the value of the
21 * `COMPOSE_FILE` environment variable, or `dc.prod.yml`.
22 * @param {boolean} [front = false] - Filter the names of front services.
23 * @param {string} [test] - Get the test-service names for the given service.
24 * @param {boolean} [testable = false] - Filter the names of testable services.
25 * @param {boolean} [watchable = false] - Filter the names of watchable services.
26 *
27 * @returns {Promise.<string[]>}
28 */
29const getServiceNames = async ({ composefile, front, test, testable, watchable }) => {
30 if (test) {
31 const services = await getServiceSpecs(resolve(composefile));
32 let service = services[test];
33 if (!service) {
34 service = services[`test-${test}`];
35 if (!service) {
36 throw new Error(`There is no '${test}' or 'test-${test}' service.`);
37 }
38 }
39 let names = [`test-${test}`];
40 const hasUnitTests = service.manifest && service.manifest.unitTests && service.manifest.unitTests.enable;
41 if (hasUnitTests && service.manifest.unitTests.dependencies) {
42 names = names.concat(service.manifest.unitTests.dependencies);
43 }
44 return names;
45 }
46 else {
47 const services = await getServiceSpecs(resolve(composefile));
48 let names = Object.keys(services);
49
50 if (front) {
51 names = names.filter((name) => {
52 return services[name] &&
53 services[name].manifest &&
54 services[name].manifest.cargoFrontend;
55 });
56 }
57
58 if (watchable) {
59 names = names.filter((name) => {
60 return services[name] &&
61 services[name].manifest &&
62 services[name].manifest.watchable;
63 });
64 }
65
66 if (testable) {
67 names = names
68 .filter((name) => {
69 return services[name] &&
70 services[name].manifest &&
71 services[name].manifest.unitTests &&
72 services[name].manifest.unitTests.enable;
73 })
74 .map((name) => services[name].manifest.service)
75 }
76
77 return names;
78 }
79};
80
81// -- CLI --------------- --- -- -
82
83program
84 .version('0.1.0')
85 .option('--composefile [path]', `Specify the Compose file to use [${COMPOSE_FILE}].`, COMPOSE_FILE)
86 .option('--front', 'Filter the names of front services.')
87 .option('--test [service]', 'Get the test-service names for the given service.')
88 .option('--testable', 'Filter the names of testable services.')
89 .option('--watchable', 'Filter the names of watchable services.')
90 .parse(process.argv);
91
92getServiceNames(program)
93 .then((names) => console.log(names.join(' ')))
94 .catch((error) => {
95 console.error(error);
96 process.exit(1);
97 });