UNPKG

sharyn

Version:

Combines all the other packages under one.

145 lines (128 loc) 6.52 kB
#! /usr/bin/env node "use strict"; var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault"); var _os = require("os"); var _child_process = require("child_process"); var _swit = _interopRequireDefault(require("@sharyn/util/swit")); var _safe = _interopRequireDefault(require("colors/safe")); var _checkSetup = require("@sharyn/check-setup"); var _shared = require("./shared"); var _commands = require("./commands"); // flow-disable-next-line // flow-disable-next-line var hasDocker = (0, _checkSetup.hasFile)('docker-compose.yml'); var hasHeroku = (0, _checkSetup.hasFile)('Procfile'); var hasSeeds = (0, _checkSetup.hasFile)('/src/_db/seeds'); var getDbTestProcessId = function getDbTestProcessId(containerName) { var result = (0, _child_process.execSync)("docker ps -q --filter=\"name=".concat(containerName, "\"")).toString(); var ids = result.split(_os.EOL).filter(function (x) { return x; }); if (ids.length > 1) { throw Error("Multiple running processes found for ".concat(containerName)); } return ids[0]; }; var mySpawn = function mySpawn(cmd) { // eslint-disable-next-line no-console console.log("".concat(_safe.default.magenta("[sharyn/cli]"), " ").concat(_safe.default.gray(cmd)).replace(/\.\/node_modules\/\.bin\//g, '')); return (0, _child_process.spawn)(cmd, { shell: true, stdio: 'inherit' }); }; var mySpawnSync = function mySpawnSync(cmd) { // eslint-disable-next-line no-console console.log("".concat(_safe.default.magenta("[sharyn/cli]"), " ").concat(_safe.default.gray(cmd)).replace(/\.\/node_modules\/\.bin\//g, '')); return (0, _child_process.spawnSync)(cmd, { shell: true, stdio: 'inherit' }); }; var sequence = function sequence(arr) { return arr.join(' && '); }; var taskName = process.argv[2]; var result = (0, _swit.default)(taskName, [['dev', function () { var firstCommands = []; hasDocker && firstCommands.push(_commands.DOCKER_UP); hasDocker && _shared.knexConfigPath && firstCommands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr); hasDocker && _shared.knexConfigPath && hasSeeds && firstCommands.push(_commands.dbSeed); firstCommands.push(_commands.rmBundle); mySpawnSync(sequence(firstCommands)); mySpawn(_commands.serverWatch); mySpawn(_commands.clientWatch); }], ['dev-ssr-only', function () { var commands = []; hasDocker && commands.push(_commands.DOCKER_UP); hasDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr); hasDocker && _shared.knexConfigPath && hasSeeds && commands.push(_commands.dbSeed); commands.push(_commands.serverWatchSsrOnly); mySpawnSync(sequence(commands)); }], ['dev-no-ssr', function () { var firstCommands = []; hasDocker && firstCommands.push(_commands.DOCKER_UP); hasDocker && _shared.knexConfigPath && firstCommands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr); hasDocker && _shared.knexConfigPath && hasSeeds && firstCommands.push(_commands.dbSeed); firstCommands.push(_commands.rmBundle); mySpawnSync(sequence(firstCommands)); mySpawn(_commands.serverWatchNoSsr); mySpawn(_commands.clientWatch); }], ['local-prod', function () { var commands = []; hasDocker && commands.push(_commands.DOCKER_UP); hasDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG, _commands.dbMigr); hasDocker && _shared.knexConfigPath && hasSeeds && commands.push(_commands.dbSeed); commands.push(_commands.rmLibAndBundle, _commands.clientBuild, _commands.babel); hasHeroku ? commands.push(_commands.herokuLocalProd) : commands.push(_commands.nodeLocalProd); mySpawnSync(sequence(commands)); }], ['build-prod', function () { return mySpawnSync(sequence([_commands.rmLibAndBundle, _commands.clientBuild, _commands.babel])); }], ['lint', function () { return mySpawnSync(sequence([_commands.lint, _commands.typecheck])); }], ['test', function () { var commands = [_commands.rmBundle, _commands.testUnit, _commands.clientBuild]; var testDbIdInitial = getDbTestProcessId('db-test'); var testRedisIdInitial = getDbTestProcessId('redis-test'); hasDocker && testDbIdInitial && commands.push((0, _commands.dockerDownTest)(testDbIdInitial)); hasDocker && testRedisIdInitial && commands.push((0, _commands.dockerDownTest)(testRedisIdInitial)); hasDocker && commands.push(_commands.DOCKER_UP_TEST); hasDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG_TEST, _commands.dbMigrTest); commands.push(_commands.testE2E); var cmdResult = mySpawnSync(sequence(commands)); var testDbIdFinal = getDbTestProcessId('db-test'); var testRedisIdFinal = getDbTestProcessId('redis-test'); var cleanupCommands = []; hasDocker && testDbIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testDbIdFinal)); hasDocker && testRedisIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testRedisIdFinal)); cleanupCommands.length > 0 && mySpawnSync(sequence(cleanupCommands)); return cmdResult; }], ['lint-test', function () { var commands = [_commands.lint, _commands.typecheck, _commands.rmBundle, _commands.testUnit, _commands.clientBuild]; var testDbIdInitial = getDbTestProcessId('db-test'); var testRedisIdInitial = getDbTestProcessId('redis-test'); hasDocker && testDbIdInitial && commands.push((0, _commands.dockerDownTest)(testDbIdInitial)); hasDocker && testRedisIdInitial && commands.push((0, _commands.dockerDownTest)(testRedisIdInitial)); hasDocker && commands.push(_commands.DOCKER_UP_TEST); hasDocker && _shared.knexConfigPath && commands.push(_commands.DOCKER_WAIT_PG_TEST, _commands.dbMigrTest); commands.push(_commands.testE2E); var cmdResult = mySpawnSync(sequence(commands)); var testDbIdFinal = getDbTestProcessId('db-test'); var testRedisIdFinal = getDbTestProcessId('redis-test'); var cleanupCommands = []; hasDocker && testDbIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testDbIdFinal)); hasDocker && testRedisIdFinal && cleanupCommands.push((0, _commands.dockerDownTest)(testRedisIdFinal)); cleanupCommands.length > 0 && mySpawnSync(sequence(cleanupCommands)); return cmdResult; }], ['migrate-db', function () { return mySpawnSync(_commands.dbMigr); }], ['stats', function () { return mySpawnSync(_commands.stats); }]], function () { // eslint-disable-next-line no-console console.error("".concat(taskName, " is not a valid @sharyn/cli command.")); process.exit(1); }); if (result) { process.exit(result.status); }