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 _env = require("@sharyn/env");
|
17 |
|
18 | var _shared = require("./shared");
|
19 |
|
20 | var _commands = require("./commands");
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var hasDocker = (0, _checkSetup.hasFile)('docker-compose.yml');
|
26 | var hasHeroku = (0, _checkSetup.hasFile)('Procfile');
|
27 | var hasSeeds = (0, _checkSetup.hasFile)('/src/_db/seeds');
|
28 |
|
29 | var getDbTestProcessId = function getDbTestProcessId(containerName) {
|
30 | var result = (0, _child_process.execSync)("docker ps -q --filter=\"name=".concat(containerName, "\"")).toString();
|
31 | var ids = result.split(_os.EOL).filter(function (x) {
|
32 | return x;
|
33 | });
|
34 |
|
35 | if (ids.length > 1) {
|
36 | throw Error("Multiple running processes found for ".concat(containerName));
|
37 | }
|
38 |
|
39 | return ids[0];
|
40 | };
|
41 |
|
42 | var mySpawn = function mySpawn(cmd) {
|
43 |
|
44 | console.log("".concat(_safe.default.magenta("[sharyn/cli]"), " ").concat(_safe.default.gray(cmd)).replace(/\.\/node_modules\/\.bin\//g, ''));
|
45 | return (0, _child_process.spawn)(cmd, {
|
46 | shell: true,
|
47 | stdio: 'inherit'
|
48 | });
|
49 | };
|
50 |
|
51 | var mySpawnSync = function mySpawnSync(cmd) {
|
52 |
|
53 | console.log("".concat(_safe.default.magenta("[sharyn/cli]"), " ").concat(_safe.default.gray(cmd)).replace(/\.\/node_modules\/\.bin\//g, ''));
|
54 | return (0, _child_process.spawnSync)(cmd, {
|
55 | shell: true,
|
56 | stdio: 'inherit'
|
57 | });
|
58 | };
|
59 |
|
60 | var sequence = function sequence(arr) {
|
61 | return arr.join(' && ');
|
62 | };
|
63 |
|
64 | var taskName = process.argv[2];
|
65 | var useDocker = hasDocker && (process.argv.length > 2 ? !(process.argv[3] === '--no-docker') : true);
|
66 | var result = (0, _swit.default)(taskName, [['dev', function () {
|
67 | var firstCommands = [];
|
68 | useDocker && firstCommands.push(_commands.DOCKER_UP);
|
69 | useDocker && _shared.knexConfigPath && firstCommands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
|
70 | useDocker && _shared.knexConfigPath && hasSeeds && firstCommands.push(_commands.dbSeed);
|
71 | firstCommands.push(_commands.rmBundle);
|
72 | mySpawnSync(sequence(firstCommands));
|
73 | mySpawn(_commands.serverWatch);
|
74 | mySpawn(_commands.clientWatch);
|
75 | }], ['dev-ssr-only', function () {
|
76 | var commands = [];
|
77 | useDocker && commands.push(_commands.DOCKER_UP);
|
78 | useDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
|
79 | useDocker && _shared.knexConfigPath && hasSeeds && commands.push(_commands.dbSeed);
|
80 | commands.push(_commands.serverWatchSsrOnly);
|
81 | mySpawnSync(sequence(commands));
|
82 | }], ['dev-no-ssr', function () {
|
83 | var firstCommands = [];
|
84 | useDocker && firstCommands.push(_commands.DOCKER_UP);
|
85 | useDocker && _shared.knexConfigPath && firstCommands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
|
86 | useDocker && _shared.knexConfigPath && hasSeeds && firstCommands.push(_commands.dbSeed);
|
87 | firstCommands.push(_commands.rmBundle);
|
88 | mySpawnSync(sequence(firstCommands));
|
89 | mySpawn(_commands.serverWatchNoSsr);
|
90 | mySpawn(_commands.clientWatch);
|
91 | }], ['local-prod', function () {
|
92 | var commands = [];
|
93 | useDocker && commands.push(_commands.DOCKER_UP);
|
94 | useDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr);
|
95 | useDocker && _shared.knexConfigPath && hasSeeds && commands.push(_commands.dbSeed);
|
96 | commands.push(_commands.rmLibAndBundle, _commands.clientBuild, _commands.babel);
|
97 | hasHeroku ? commands.push(_commands.herokuLocalProd) : commands.push(_commands.nodeLocalProd);
|
98 | mySpawnSync(sequence(commands));
|
99 | }], ['build-prod', function () {
|
100 | return mySpawnSync(sequence([_commands.rmLibAndBundle, _commands.clientBuild, _commands.babel]));
|
101 | }], ['lint', function () {
|
102 | return mySpawnSync(sequence([_commands.lint, _commands.typecheck]));
|
103 | }], ['test', function () {
|
104 | var commands = [_commands.rmBundle, _commands.testUnit, _commands.clientBuild];
|
105 | var testDbIdInitial = getDbTestProcessId('db-test');
|
106 | var testRedisIdInitial = getDbTestProcessId('redis-test');
|
107 | useDocker && testDbIdInitial && commands.push((0, _commands.dockerDownTest)(testDbIdInitial));
|
108 | useDocker && testRedisIdInitial && commands.push((0, _commands.dockerDownTest)(testRedisIdInitial));
|
109 | useDocker && commands.push(_commands.DOCKER_UP_TEST);
|
110 | useDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG_TEST, _commands.dbMigrTest);
|
111 | commands.push(_commands.testE2E);
|
112 | var cmdResult = mySpawnSync(sequence(commands));
|
113 | var testDbIdFinal = getDbTestProcessId('db-test');
|
114 | var testRedisIdFinal = getDbTestProcessId('redis-test');
|
115 | var cleanupCommands = [];
|
116 | useDocker && testDbIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testDbIdFinal));
|
117 | useDocker && testRedisIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testRedisIdFinal));
|
118 | cleanupCommands.length > 0 && mySpawnSync(sequence(cleanupCommands));
|
119 | _env.TESTING_SOUND && mySpawnSync(_commands.SAY_DONE);
|
120 | return cmdResult;
|
121 | }], ['lint-test', function () {
|
122 | var commands = [_commands.lint, _commands.typecheck, _commands.rmBundle, _commands.testUnit, _commands.clientBuild];
|
123 | var testDbIdInitial = getDbTestProcessId('db-test');
|
124 | var testRedisIdInitial = getDbTestProcessId('redis-test');
|
125 | useDocker && testDbIdInitial && commands.push((0, _commands.dockerDownTest)(testDbIdInitial));
|
126 | useDocker && testRedisIdInitial && commands.push((0, _commands.dockerDownTest)(testRedisIdInitial));
|
127 | useDocker && commands.push(_commands.DOCKER_UP_TEST);
|
128 | useDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG_TEST, _commands.dbMigrTest);
|
129 | commands.push(_commands.testE2E);
|
130 | var cmdResult = mySpawnSync(sequence(commands));
|
131 | var testDbIdFinal = getDbTestProcessId('db-test');
|
132 | var testRedisIdFinal = getDbTestProcessId('redis-test');
|
133 | var cleanupCommands = [];
|
134 | useDocker && testDbIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testDbIdFinal));
|
135 | useDocker && testRedisIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testRedisIdFinal));
|
136 | cleanupCommands.length > 0 && mySpawnSync(sequence(cleanupCommands));
|
137 | _env.TESTING_SOUND && mySpawnSync(_commands.SAY_DONE);
|
138 | return cmdResult;
|
139 | }], ['lint-test-storybook', function () {
|
140 | var commands = [_commands.lint, _commands.typecheck, _commands.rmBundle, _commands.testUnit, _commands.clientBuild];
|
141 | var testDbIdInitial = getDbTestProcessId('db-test');
|
142 | var testRedisIdInitial = getDbTestProcessId('redis-test');
|
143 | useDocker && testDbIdInitial && commands.push((0, _commands.dockerDownTest)(testDbIdInitial));
|
144 | useDocker && testRedisIdInitial && commands.push((0, _commands.dockerDownTest)(testRedisIdInitial));
|
145 | useDocker && commands.push(_commands.DOCKER_UP_TEST);
|
146 | useDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG_TEST, _commands.dbMigrTest);
|
147 | commands.push(_commands.testE2E);
|
148 | var cmdResult = mySpawnSync(sequence(commands));
|
149 | var testDbIdFinal = getDbTestProcessId('db-test');
|
150 | var testRedisIdFinal = getDbTestProcessId('redis-test');
|
151 | var postTestsCommands = [];
|
152 | useDocker && testDbIdFinal && postTestsCommands.push((0, _commands.dockerDownTest)(testDbIdFinal));
|
153 | useDocker && testRedisIdFinal && postTestsCommands.push((0, _commands.dockerDownTest)(testRedisIdFinal));
|
154 |
|
155 | if ((cmdResult === null || cmdResult === void 0 ? void 0 : cmdResult.status) === 0) {
|
156 | postTestsCommands.push(_commands.BUILD_STORYBOOK);
|
157 | postTestsCommands.push(_commands.GIT_ADD_STORYBOOK);
|
158 | }
|
159 |
|
160 | postTestsCommands.length > 0 && mySpawnSync(sequence(postTestsCommands));
|
161 | _env.TESTING_SOUND && mySpawnSync(_commands.SAY_DONE);
|
162 | return cmdResult;
|
163 | }], ['deploy-staging', function () {
|
164 | var commands = [_commands.PUSH_ORIGIN_MASTER, _commands.PUSH_HEROKU_STAGING_MASTER];
|
165 | _env.HEROKU_DEPLOYMENT_SOUND && commands.push(_commands.SAY_DONE);
|
166 | mySpawnSync(sequence(commands));
|
167 | }], ['promote', function () {
|
168 | var commands = [_commands.HEROKU_PIPELINE_PROMOTE];
|
169 | _env.HEROKU_DEPLOYMENT_SOUND && commands.push(_commands.SAY_DONE);
|
170 | mySpawnSync(sequence(commands));
|
171 | }], ['deploy-prod', function () {
|
172 | var commands = [_commands.PUSH_ORIGIN_MASTER, _commands.PUSH_HEROKU_STAGING_MASTER, _commands.HEROKU_PIPELINE_PROMOTE];
|
173 | _env.HEROKU_DEPLOYMENT_SOUND && commands.push(_commands.SAY_DONE);
|
174 | mySpawnSync(sequence(commands));
|
175 | }], ['migrate-db', function () {
|
176 | return mySpawnSync(_commands.dbMigr);
|
177 | }], ['stats', function () {
|
178 | return mySpawnSync(_commands.stats);
|
179 | }], ['sound', function () {
|
180 | return mySpawnSync(_commands.SAY_DONE);
|
181 | }], ['storybook', function () {
|
182 | return mySpawnSync(_commands.startStorybook);
|
183 | }], ['build-storybook', function () {
|
184 | return mySpawnSync(_commands.BUILD_STORYBOOK);
|
185 | }]], function () {
|
186 |
|
187 | console.error("".concat(taskName, " is not a valid @sharyn/cli command."));
|
188 | process.exit(1);
|
189 | });
|
190 |
|
191 | if (result) {
|
192 | process.exit(result.status);
|
193 | } |
\ | No newline at end of file |