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