@cyklang/cli 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -1,27 +1,148 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
3
26
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
28
  };
6
29
  Object.defineProperty(exports, "__esModule", { value: true });
7
- const loglevel_1 = __importDefault(require("loglevel"));
8
30
  const commander_1 = require("commander");
31
+ const fs = __importStar(require("fs"));
32
+ const loglevel_1 = __importDefault(require("loglevel"));
33
+ const path_1 = __importDefault(require("path"));
34
+ const AssetCommand_1 = require("./AssetCommand");
35
+ const Cmd_1 = require("./Cmd");
36
+ const InstallCommand_1 = require("./InstallCommand");
9
37
  const ModuleCommand_1 = require("./ModuleCommand");
10
- const UserCommand_1 = require("./UserCommand");
38
+ const OpenCommand_1 = require("./OpenCommand");
11
39
  const QueryCommand_1 = require("./QueryCommand");
40
+ const RunCommand_1 = require("./RunCommand");
12
41
  const TableCommand_1 = require("./TableCommand");
42
+ const TableDataCommand_1 = require("./TableDataCommand");
13
43
  const TestCommand_1 = require("./TestCommand");
14
- const RunCommand_1 = require("./RunCommand");
15
- const InstallCommand_1 = require("./InstallCommand");
16
- const AssetCommand_1 = require("./AssetCommand");
17
- const OpenCommand_1 = require("./OpenCommand");
18
44
  const UninstallCommand_1 = require("./UninstallCommand");
19
- const TableDataCommand_1 = require("./TableDataCommand");
45
+ const UserCommand_1 = require("./UserCommand");
20
46
  const logger = loglevel_1.default.getLogger("index.ts");
21
47
  logger.setLevel("debug");
48
+ /**
49
+ * function getOrchestrator
50
+ */
51
+ function getOrchestrator() {
52
+ let result;
53
+ const cwd = process.cwd();
54
+ const dockerComposePath = path_1.default.join(cwd, 'docker-compose.yml');
55
+ const k8sPath = path_1.default.join(cwd, 'k8s');
56
+ if (fs.existsSync(dockerComposePath) && fs.statSync(dockerComposePath).isFile()) {
57
+ result = 'docker';
58
+ }
59
+ else if (fs.existsSync(k8sPath) && fs.statSync(k8sPath).isDirectory()) {
60
+ result = 'kubernetes';
61
+ }
62
+ return result;
63
+ }
64
+ /**
65
+ * function startServer
66
+ */
67
+ async function startServer() {
68
+ try {
69
+ const orchestrator = getOrchestrator();
70
+ if (orchestrator === 'docker') {
71
+ await (0, Cmd_1.spawnCommand)('docker', ['compose', 'up', '-d']);
72
+ }
73
+ if (orchestrator === 'kubernetes') {
74
+ const portForwardPath = path_1.default.join(process.cwd(), 'k8s', 'port-forward');
75
+ if (!fs.existsSync(portForwardPath) || !fs.statSync(portForwardPath).isFile())
76
+ throw portForwardPath + ' file not found';
77
+ const config = JSON.parse(fs.readFileSync(portForwardPath).toString());
78
+ if (!config.postgres || !config.nodejs)
79
+ throw portForwardPath + ' : file format incorrect';
80
+ (0, Cmd_1.spawnCommand)('kubectl', ['port-forward', 'svc/postgres', config.postgres + ':5432']);
81
+ logger.info('Postgresql proxy at localhost:' + config.postgres);
82
+ (0, Cmd_1.spawnCommand)('kubectl', ['port-forward', 'svc/nodejs', config.nodejs + ':3000']);
83
+ logger.info('Nodejs proxy at localhost:' + config.nodejs);
84
+ }
85
+ if (!orchestrator) {
86
+ logger.error('No cyk server is defined in current folder ' + process.cwd());
87
+ }
88
+ }
89
+ catch (err) {
90
+ console.error(err);
91
+ }
92
+ }
93
+ /**
94
+ * function stopServer
95
+ */
96
+ async function stopServer() {
97
+ try {
98
+ const orchestrator = getOrchestrator();
99
+ if (orchestrator === 'docker') {
100
+ await (0, Cmd_1.spawnCommand)('docker', ['compose', 'down']);
101
+ }
102
+ if (orchestrator === 'kubernetes') {
103
+ await (0, Cmd_1.spawnCommand)('kubectl', ['delete', '-f', path_1.default.join(process.cwd(), 'k8s')]);
104
+ logger.info('Kubernetes deployments and services deleted');
105
+ }
106
+ if (!orchestrator) {
107
+ logger.error('No cyk server is defined in current folder ' + process.cwd());
108
+ }
109
+ }
110
+ catch (err) {
111
+ console.error(err);
112
+ }
113
+ }
114
+ /**
115
+ * class StartCommand
116
+ */
117
+ class StartCommand extends commander_1.Command {
118
+ constructor(name) {
119
+ super(name);
120
+ this.description('start cyk server defined in current project folder')
121
+ .action(async (options) => {
122
+ await startServer();
123
+ });
124
+ }
125
+ }
126
+ /**
127
+ * class StopCommand
128
+ */
129
+ class StopCommand extends commander_1.Command {
130
+ constructor(name) {
131
+ super(name);
132
+ this.description('stop cyk server defined in current project folder')
133
+ .action(async (options) => {
134
+ await stopServer();
135
+ });
136
+ }
137
+ }
138
+ /**
139
+ * Main program
140
+ */
22
141
  const program = new commander_1.Command();
23
142
  program.name('cyk').description('cyklang CLI')
24
143
  .version('0.6.0');
144
+ program.addCommand(new StartCommand('start'));
145
+ program.addCommand(new StopCommand('stop'));
25
146
  program.addCommand(new InstallCommand_1.InstallCommand('install'));
26
147
  program.addCommand(new InstallCommand_1.InstallCommand('i'));
27
148
  program.addCommand(new UninstallCommand_1.UninstallCommand('uninstall'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyklang/cli",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "cyklang CLI",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -1,24 +1,129 @@
1
1
  #!/usr/bin/env node
2
- import loglevel from 'loglevel'
3
2
  import { Command } from 'commander'
3
+ import * as fs from 'fs'
4
+ import loglevel from 'loglevel'
5
+ import path from 'path'
6
+ import { AssetCommand } from './AssetCommand'
7
+ import { Cmd, spawnCommand } from './Cmd'
8
+ import { InstallCommand } from './InstallCommand'
4
9
  import { ModuleCommand } from "./ModuleCommand"
5
- import { UserCommand } from './UserCommand'
10
+ import { OpenCommand } from './OpenCommand'
6
11
  import { QueryCommand } from './QueryCommand'
12
+ import { RunCommand } from './RunCommand'
7
13
  import { TableCommand } from './TableCommand'
14
+ import { TableDataCommand } from './TableDataCommand'
8
15
  import { TestCommand } from './TestCommand'
9
- import { RunCommand } from './RunCommand'
10
- import { InstallCommand } from './InstallCommand'
11
- import { AssetCommand } from './AssetCommand'
12
- import { OpenCommand } from './OpenCommand'
13
16
  import { UninstallCommand } from './UninstallCommand'
14
- import { TableDataCommand } from './TableDataCommand'
17
+ import { UserCommand } from './UserCommand'
18
+
15
19
  const logger = loglevel.getLogger("index.ts")
16
20
  logger.setLevel("debug")
17
21
 
22
+ type Orchestrator = 'kubernetes' | 'docker' | undefined
23
+
24
+ /**
25
+ * function getOrchestrator
26
+ */
27
+ function getOrchestrator(): Orchestrator {
28
+ let result: Orchestrator
29
+ const cwd = process.cwd()
30
+ const dockerComposePath = path.join(cwd, 'docker-compose.yml')
31
+ const k8sPath = path.join(cwd, 'k8s')
32
+ if (fs.existsSync(dockerComposePath) && fs.statSync(dockerComposePath).isFile()) {
33
+ result = 'docker'
34
+ }
35
+ else if (fs.existsSync(k8sPath) && fs.statSync(k8sPath).isDirectory()) {
36
+ result = 'kubernetes'
37
+ }
38
+ return result
39
+ }
40
+
41
+ /**
42
+ * function startServer
43
+ */
44
+ async function startServer() {
45
+ try {
46
+ const orchestrator = getOrchestrator()
47
+ if (orchestrator === 'docker') {
48
+ await spawnCommand('docker', ['compose', 'up', '-d'])
49
+ }
50
+ if (orchestrator === 'kubernetes') {
51
+ const portForwardPath = path.join(process.cwd(), 'k8s', 'port-forward')
52
+ if (!fs.existsSync(portForwardPath) || !fs.statSync(portForwardPath).isFile())
53
+ throw portForwardPath + ' file not found'
54
+ const config = JSON.parse(fs.readFileSync(portForwardPath).toString())
55
+ if (! config.postgres || ! config.nodejs)
56
+ throw portForwardPath + ' : file format incorrect'
57
+ spawnCommand('kubectl', ['port-forward', 'svc/postgres', config.postgres + ':5432'])
58
+ logger.info('Postgresql proxy at localhost:' + config.postgres)
59
+ spawnCommand('kubectl', ['port-forward', 'svc/nodejs', config.nodejs + ':3000'])
60
+ logger.info('Nodejs proxy at localhost:' + config.nodejs)
61
+ }
62
+ if (!orchestrator) {
63
+ logger.error('No cyk server is defined in current folder ' + process.cwd())
64
+ }
65
+ }
66
+ catch (err) {
67
+ console.error(err)
68
+ }
69
+ }
70
+
71
+ /**
72
+ * function stopServer
73
+ */
74
+ async function stopServer() {
75
+ try {
76
+ const orchestrator = getOrchestrator()
77
+ if (orchestrator === 'docker') {
78
+ await spawnCommand('docker', ['compose', 'down'])
79
+ }
80
+ if (orchestrator === 'kubernetes') {
81
+ await spawnCommand('kubectl', ['delete', '-f', path.join(process.cwd(), 'k8s')])
82
+ logger.info('Kubernetes deployments and services deleted')
83
+ }
84
+ if (!orchestrator) {
85
+ logger.error('No cyk server is defined in current folder ' + process.cwd())
86
+ }
87
+ }
88
+ catch (err) {
89
+ console.error(err)
90
+ }
91
+ }
92
+
93
+ /**
94
+ * class StartCommand
95
+ */
96
+ class StartCommand extends Command {
97
+ constructor(name: string) {
98
+ super(name)
99
+ this.description('start cyk server defined in current project folder')
100
+ .action(async (options: any) => {
101
+ await startServer()
102
+ })
103
+ }
104
+ }
105
+
106
+ /**
107
+ * class StopCommand
108
+ */
109
+ class StopCommand extends Command {
110
+ constructor(name: string) {
111
+ super(name)
112
+ this.description('stop cyk server defined in current project folder')
113
+ .action(async (options: any) => {
114
+ await stopServer()
115
+ })
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Main program
121
+ */
18
122
  const program = new Command()
19
123
  program.name('cyk').description('cyklang CLI')
20
124
  .version('0.6.0')
21
-
125
+ program.addCommand(new StartCommand('start'))
126
+ program.addCommand(new StopCommand('stop'))
22
127
  program.addCommand(new InstallCommand('install'))
23
128
  program.addCommand(new InstallCommand('i'))
24
129
  program.addCommand(new UninstallCommand('uninstall'))