UNPKG

6.41 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 mySpawnSync = function mySpawnSync(cmd) {
48 // eslint-disable-next-line no-console
49 console.log("".concat(_safe.default.magenta("[sharyn/cli]"), " ").concat(_safe.default.gray(cmd)).replace(/\.\/node_modules\/\.bin\//g, ''));
50 return (0, _child_process.spawnSync)(cmd, {
51 shell: true,
52 stdio: 'inherit'
53 });
54};
55
56var sequence = function sequence(arr) {
57 return arr.join(' && ');
58};
59
60var taskName = process.argv[2];
61var result = (0, _libLang.swit)(taskName, [['dev', function () {
62 var firstCommands = [];
63 hasDocker && firstCommands.push(_commands.DOCKER_UP);
64 hasDocker && _shared.knexConfigPath && firstCommands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
65 hasDocker && _shared.knexConfigPath && hasSeeds && firstCommands.push(_commands.dbSeed);
66 firstCommands.push(_commands.rmBundle);
67 mySpawnSync(sequence(firstCommands));
68 mySpawn(_commands.serverWatch);
69 mySpawn(_commands.clientWatch);
70}], ['dev-ssr-only', function () {
71 var commands = [];
72 hasDocker && commands.push(_commands.DOCKER_UP);
73 hasDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
74 hasDocker && _shared.knexConfigPath && hasSeeds && commands.push(_commands.dbSeed);
75 commands.push(_commands.serverWatchSsrOnly);
76 mySpawnSync(sequence(commands));
77}], ['dev-no-ssr', function () {
78 var firstCommands = [];
79 hasDocker && firstCommands.push(_commands.DOCKER_UP);
80 hasDocker && _shared.knexConfigPath && firstCommands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
81 hasDocker && _shared.knexConfigPath && hasSeeds && firstCommands.push(_commands.dbSeed);
82 firstCommands.push(_commands.rmBundle);
83 mySpawnSync(sequence(firstCommands));
84 mySpawn(_commands.serverWatchNoSsr);
85 mySpawn(_commands.clientWatch);
86}], ['local-prod', function () {
87 var commands = [];
88 hasDocker && commands.push(_commands.DOCKER_UP);
89 hasDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
90 hasDocker && _shared.knexConfigPath && hasSeeds && commands.push(_commands.dbSeed);
91 commands.push(_commands.rmLibAndBundle, _commands.clientBuild, _commands.babel);
92 hasHeroku ? commands.push(_commands.herokuLocalProd) : commands.push(_commands.nodeLocalProd);
93 mySpawnSync(sequence(commands));
94}], ['build-prod', function () {
95 return mySpawnSync(sequence([_commands.rmLibAndBundle, _commands.clientBuild, _commands.babel]));
96}], ['lint', function () {
97 return mySpawnSync(sequence([_commands.lint, _commands.typecheck]));
98}], ['test', function () {
99 var commands = [_commands.rmBundle, _commands.testUnit, _commands.clientBuild];
100 var testDbIdInitial = getDbTestProcessId('db-test');
101 var testRedisIdInitial = getDbTestProcessId('redis-test');
102 hasDocker && testDbIdInitial && commands.push((0, _commands.dockerDownTest)(testDbIdInitial));
103 hasDocker && testRedisIdInitial && commands.push((0, _commands.dockerDownTest)(testRedisIdInitial));
104 hasDocker && commands.push(_commands.DOCKER_UP_TEST);
105 hasDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG_TEST, _commands.dbMigrTest);
106 commands.push(_commands.testE2E);
107 var cmdResult = mySpawnSync(sequence(commands));
108 var testDbIdFinal = getDbTestProcessId('db-test');
109 var testRedisIdFinal = getDbTestProcessId('redis-test');
110 var cleanupCommands = [];
111 hasDocker && testDbIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testDbIdFinal));
112 hasDocker && testRedisIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testRedisIdFinal));
113 mySpawnSync(sequence(cleanupCommands));
114 return cmdResult;
115}], ['lint-test', function () {
116 var commands = [_commands.lint, _commands.typecheck, _commands.rmBundle, _commands.testUnit, _commands.clientBuild];
117 var testDbIdInitial = getDbTestProcessId('db-test');
118 var testRedisIdInitial = getDbTestProcessId('redis-test');
119 hasDocker && testDbIdInitial && commands.push((0, _commands.dockerDownTest)(testDbIdInitial));
120 hasDocker && testRedisIdInitial && commands.push((0, _commands.dockerDownTest)(testRedisIdInitial));
121 hasDocker && commands.push(_commands.DOCKER_UP_TEST);
122 hasDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG_TEST, _commands.dbMigrTest);
123 commands.push(_commands.testE2E);
124 var cmdResult = mySpawnSync(sequence(commands));
125 var testDbIdFinal = getDbTestProcessId('db-test');
126 var testRedisIdFinal = getDbTestProcessId('redis-test');
127 var cleanupCommands = [];
128 hasDocker && testDbIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testDbIdFinal));
129 hasDocker && testRedisIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testRedisIdFinal));
130 mySpawnSync(sequence(cleanupCommands));
131 return cmdResult;
132}], ['migrate-db', function () {
133 return mySpawnSync(_commands.dbMigr);
134}], ['stats', function () {
135 return mySpawnSync(_commands.stats);
136}]], function () {
137 // eslint-disable-next-line no-console
138 console.error("".concat(taskName, " is not a valid @sharyn/cli command."));
139 process.exit(1);
140});
141
142if (result) {
143 process.exit(result.status);
144}
\No newline at end of file