UNPKG

5.07 kBJavaScriptView Raw
1#! /usr/bin/env node
2"use strict";
3
4var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
5
6var _os = require("os");
7
8var _child_process = require("child_process");
9
10var _libLang = require("@verekia/lib-lang");
11
12var _safe = _interopRequireDefault(require("colors/safe"));
13
14var _checkSetup = require("@sharyn/check-setup");
15
16var _shared = require("./shared");
17
18var _commands = require("./commands");
19
20// flow-disable-next-line
21var hasDocker = (0, _checkSetup.hasFile)('docker-compose.yml');
22var hasHeroku = (0, _checkSetup.hasFile)('Procfile');
23var hasSeeds = (0, _checkSetup.hasFile)('/src/_db/seeds');
24
25var getDbTestProcessId = function getDbTestProcessId(containerName) {
26 var result = (0, _child_process.execSync)("docker ps -q --filter=\"name=".concat(containerName, "\"")).toString();
27 var ids = result.split(_os.EOL).filter(function (x) {
28 return x;
29 });
30
31 if (ids.length > 1) {
32 throw Error("Multiple running processes found for ".concat(containerName));
33 }
34
35 return ids[0];
36};
37
38var mySpawn = function mySpawn(cmd) {
39 // eslint-disable-next-line no-console
40 console.log("".concat(_safe.default.magenta("[sharyn/cli]"), " ").concat(_safe.default.gray(cmd)).replace(/\.\/node_modules\/\.bin\//g, ''));
41 return (0, _child_process.spawn)(cmd, {
42 shell: true,
43 stdio: 'inherit'
44 });
45};
46
47var sequence = function sequence(arr) {
48 return arr.join(' && ');
49};
50
51var taskName = process.argv[2];
52(0, _libLang.swit)(taskName, [['dev', function () {
53 var firstCommands = [];
54 hasDocker && firstCommands.push(_commands.DOCKER_UP);
55 _shared.knexConfigPath && firstCommands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
56 _shared.knexConfigPath && hasSeeds && firstCommands.push(_commands.dbSeed);
57 firstCommands.push(_commands.rmBundle);
58 mySpawn(sequence(firstCommands)).on('close', function (code) {
59 if (code === 0) {
60 mySpawn(_commands.serverWatch);
61 mySpawn(_commands.clientWatch);
62 }
63 });
64}], ['dev-ssr-only', function () {
65 var commands = [];
66 hasDocker && commands.push(_commands.DOCKER_UP);
67 _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
68 _shared.knexConfigPath && hasSeeds && commands.push(_commands.dbSeed);
69 commands.push(_commands.serverWatchSsrOnly);
70 mySpawn(sequence(commands));
71}], ['dev-no-ssr', function () {
72 var firstCommands = [];
73 hasDocker && firstCommands.push(_commands.DOCKER_UP);
74 _shared.knexConfigPath && firstCommands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
75 _shared.knexConfigPath && hasSeeds && firstCommands.push(_commands.dbSeed);
76 firstCommands.push(_commands.rmBundle);
77 mySpawn(sequence(firstCommands)).on('close', function (code) {
78 if (code === 0) {
79 mySpawn(_commands.serverWatchNoSsr);
80 mySpawn(_commands.clientWatch);
81 }
82 });
83}], ['local-prod', function () {
84 var commands = [];
85 hasDocker && commands.push(_commands.DOCKER_UP);
86 _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
87 _shared.knexConfigPath && hasSeeds && commands.push(_commands.dbSeed);
88 commands.push(_commands.rmLibAndBundle, _commands.clientBuild, _commands.babel);
89 hasHeroku ? commands.push(_commands.herokuLocalProd) : commands.push(_commands.nodeLocalProd);
90 mySpawn(sequence(commands));
91}], ['build-prod', function () {
92 return mySpawn(sequence([_commands.rmLibAndBundle, _commands.clientBuild, _commands.babel]));
93}], ['lint', function () {
94 return mySpawn(sequence([_commands.lint, _commands.typecheck]));
95}], ['test', function () {
96 var commands = [];
97 var testDbId = getDbTestProcessId('db-test');
98 var testRedisId = getDbTestProcessId('redis-test');
99 hasDocker && testDbId && commands.push((0, _commands.dockerDownTest)(testDbId));
100 hasDocker && testRedisId && commands.push((0, _commands.dockerDownTest)(testRedisId));
101 hasDocker && commands.push(_commands.DOCKER_UP_TEST);
102 _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG_TEST, _commands.dbMigrTest);
103 commands.push(_commands.rmBundle, _commands.testUnit, _commands.clientBuild, _commands.testE2E);
104 mySpawn(sequence(commands));
105}], ['lint-test', function () {
106 var commands = [_commands.lint, _commands.typecheck];
107 var testDbId = getDbTestProcessId('db-test');
108 var testRedisId = getDbTestProcessId('redis-test');
109 hasDocker && testDbId && commands.push((0, _commands.dockerDownTest)(testDbId));
110 hasDocker && testRedisId && commands.push((0, _commands.dockerDownTest)(testRedisId));
111 hasDocker && commands.push(_commands.DOCKER_UP_TEST);
112 _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG_TEST, _commands.dbMigrTest);
113 commands.push(_commands.rmBundle, _commands.testUnit, _commands.clientBuild, _commands.testE2E);
114 mySpawn(sequence(commands));
115}], ['migrate-db', function () {
116 return mySpawn(_commands.dbMigr);
117}], ['stats', function () {
118 return mySpawn(_commands.stats);
119}]], function () {
120 // eslint-disable-next-line no-console
121 console.error("".concat(taskName, " is not a valid @sharyn/cli command."));
122 process.exit(1);
123});
\No newline at end of file