@cyklang/cli 0.7.0 → 0.7.3
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/Cmd.d.ts +7 -0
- package/build/Cmd.js +26 -1
- package/build/index.js +34 -1
- package/package.json +1 -1
- package/src/Cmd.ts +25 -3
- package/src/index.ts +46 -3
package/build/Cmd.d.ts
CHANGED
|
@@ -35,6 +35,13 @@ export declare function runShellCommand(command: string, args: string[]): Promis
|
|
|
35
35
|
* @returns
|
|
36
36
|
*/
|
|
37
37
|
export declare function spawnCommand(command: string, args: string[]): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* function spawnCommandData
|
|
40
|
+
* @param command
|
|
41
|
+
* @param args
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
export declare function spawnCommandData(command: string, args: string[]): Promise<string>;
|
|
38
45
|
/**
|
|
39
46
|
* function readPackageJson
|
|
40
47
|
* @returns
|
package/build/Cmd.js
CHANGED
|
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.readPackageJson = exports.spawnCommand = exports.runShellCommand = exports.Cmd = void 0;
|
|
29
|
+
exports.readPackageJson = exports.spawnCommandData = exports.spawnCommand = exports.runShellCommand = exports.Cmd = void 0;
|
|
30
30
|
const core_1 = require("@cyklang/core");
|
|
31
31
|
const commander_1 = require("commander");
|
|
32
32
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
@@ -168,6 +168,31 @@ function spawnCommand(command, args) {
|
|
|
168
168
|
});
|
|
169
169
|
}
|
|
170
170
|
exports.spawnCommand = spawnCommand;
|
|
171
|
+
/**
|
|
172
|
+
* function spawnCommandData
|
|
173
|
+
* @param command
|
|
174
|
+
* @param args
|
|
175
|
+
* @returns
|
|
176
|
+
*/
|
|
177
|
+
function spawnCommandData(command, args) {
|
|
178
|
+
let result = '';
|
|
179
|
+
const child_process = (0, child_process_1.spawn)(command, args);
|
|
180
|
+
return new Promise((resolve, reject) => {
|
|
181
|
+
child_process.stdout.on('data', (data) => {
|
|
182
|
+
result += String(data);
|
|
183
|
+
});
|
|
184
|
+
child_process.on('close', (code) => {
|
|
185
|
+
if (code === 0) {
|
|
186
|
+
// console.log(green('*') + ' ' + [command, ...args].join(' '))
|
|
187
|
+
resolve(result);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
reject(new Error([command, ...args].join(' ') + ' --> Error ' + code));
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
exports.spawnCommandData = spawnCommandData;
|
|
171
196
|
/**
|
|
172
197
|
* function readPackageJson
|
|
173
198
|
* @returns
|
package/build/index.js
CHANGED
|
@@ -71,6 +71,24 @@ async function startServer() {
|
|
|
71
71
|
await (0, Cmd_1.spawnCommand)('docker', ['compose', 'up', '-d']);
|
|
72
72
|
}
|
|
73
73
|
if (orchestrator === 'kubernetes') {
|
|
74
|
+
// apply resource files
|
|
75
|
+
await (0, Cmd_1.spawnCommand)('kubectl', ['apply', '-f', path_1.default.join(process.cwd(), 'k8s')]);
|
|
76
|
+
// wait for the services to be available
|
|
77
|
+
while (true) {
|
|
78
|
+
const services = await (0, Cmd_1.spawnCommandData)('kubectl', ['get', 'svc', '-o', 'name']);
|
|
79
|
+
if (services.includes('service/nodejs') && services.includes('service/postgres')) {
|
|
80
|
+
const availNodejs = await serviceAvailable('nodejs');
|
|
81
|
+
const availPostgres = await serviceAvailable('postgres');
|
|
82
|
+
if (availNodejs && availPostgres)
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
// wait for 1 second
|
|
86
|
+
logger.info('Wait for nodejs and postgres services...');
|
|
87
|
+
await new Promise((resolve) => setTimeout(() => {
|
|
88
|
+
resolve();
|
|
89
|
+
}, 1000));
|
|
90
|
+
}
|
|
91
|
+
// port-forward
|
|
74
92
|
const portForwardPath = path_1.default.join(process.cwd(), 'k8s', 'port-forward');
|
|
75
93
|
if (!fs.existsSync(portForwardPath) || !fs.statSync(portForwardPath).isFile())
|
|
76
94
|
throw portForwardPath + ' file not found';
|
|
@@ -90,6 +108,21 @@ async function startServer() {
|
|
|
90
108
|
console.error(err);
|
|
91
109
|
}
|
|
92
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* function serviceAvailable
|
|
113
|
+
* @param service
|
|
114
|
+
* @returns
|
|
115
|
+
*/
|
|
116
|
+
async function serviceAvailable(service) {
|
|
117
|
+
const descNodejs = await (0, Cmd_1.spawnCommandData)('kubectl', ['describe', 'service', service]);
|
|
118
|
+
const lines = descNodejs.split('\n');
|
|
119
|
+
for (const line of lines) {
|
|
120
|
+
if (line.includes('Endpoints')) {
|
|
121
|
+
return !line.includes('<none>');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
93
126
|
/**
|
|
94
127
|
* function stopServer
|
|
95
128
|
*/
|
|
@@ -140,7 +173,7 @@ class StopCommand extends commander_1.Command {
|
|
|
140
173
|
*/
|
|
141
174
|
const program = new commander_1.Command();
|
|
142
175
|
program.name('cyk').description('cyklang CLI')
|
|
143
|
-
.version('0.
|
|
176
|
+
.version('0.7.3');
|
|
144
177
|
program.addCommand(new StartCommand('start'));
|
|
145
178
|
program.addCommand(new StopCommand('stop'));
|
|
146
179
|
program.addCommand(new InstallCommand_1.InstallCommand('install'));
|
package/package.json
CHANGED
package/src/Cmd.ts
CHANGED
|
@@ -115,9 +115,6 @@ export class Cmd extends Command {
|
|
|
115
115
|
});
|
|
116
116
|
});
|
|
117
117
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
118
|
}
|
|
122
119
|
|
|
123
120
|
|
|
@@ -162,6 +159,31 @@ export function spawnCommand(command: string, args: string[]): Promise<void> {
|
|
|
162
159
|
})
|
|
163
160
|
}
|
|
164
161
|
|
|
162
|
+
/**
|
|
163
|
+
* function spawnCommandData
|
|
164
|
+
* @param command
|
|
165
|
+
* @param args
|
|
166
|
+
* @returns
|
|
167
|
+
*/
|
|
168
|
+
export function spawnCommandData(command: string, args: string[]): Promise<string> {
|
|
169
|
+
let result = ''
|
|
170
|
+
const child_process = spawn(command, args)
|
|
171
|
+
return new Promise((resolve, reject) => {
|
|
172
|
+
child_process.stdout.on('data', (data) => {
|
|
173
|
+
result += String(data)
|
|
174
|
+
})
|
|
175
|
+
child_process.on('close', (code) => {
|
|
176
|
+
if (code === 0) {
|
|
177
|
+
// console.log(green('*') + ' ' + [command, ...args].join(' '))
|
|
178
|
+
resolve(result)
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
reject(new Error([command, ...args].join(' ') + ' --> Error ' + code))
|
|
182
|
+
}
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
|
|
165
187
|
/**
|
|
166
188
|
* function readPackageJson
|
|
167
189
|
* @returns
|
package/src/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import * as fs from 'fs'
|
|
|
4
4
|
import loglevel from 'loglevel'
|
|
5
5
|
import path from 'path'
|
|
6
6
|
import { AssetCommand } from './AssetCommand'
|
|
7
|
-
import { Cmd, spawnCommand } from './Cmd'
|
|
7
|
+
import { Cmd, spawnCommand, spawnCommandData } from './Cmd'
|
|
8
8
|
import { InstallCommand } from './InstallCommand'
|
|
9
9
|
import { ModuleCommand } from "./ModuleCommand"
|
|
10
10
|
import { OpenCommand } from './OpenCommand'
|
|
@@ -48,11 +48,37 @@ async function startServer() {
|
|
|
48
48
|
await spawnCommand('docker', ['compose', 'up', '-d'])
|
|
49
49
|
}
|
|
50
50
|
if (orchestrator === 'kubernetes') {
|
|
51
|
+
|
|
52
|
+
// apply resource files
|
|
53
|
+
await spawnCommand('kubectl', ['apply', '-f', path.join(process.cwd(), 'k8s')])
|
|
54
|
+
|
|
55
|
+
// wait for the services to be available
|
|
56
|
+
|
|
57
|
+
while (true) {
|
|
58
|
+
|
|
59
|
+
const services = await spawnCommandData('kubectl', ['get', 'svc', '-o', 'name'])
|
|
60
|
+
|
|
61
|
+
if (services.includes('service/nodejs') && services.includes('service/postgres')) {
|
|
62
|
+
const availNodejs = await serviceAvailable('nodejs')
|
|
63
|
+
const availPostgres = await serviceAvailable('postgres')
|
|
64
|
+
if (availNodejs && availPostgres) break
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// wait for 1 second
|
|
68
|
+
logger.info('Wait for nodejs and postgres services...')
|
|
69
|
+
await new Promise<void>((resolve) =>
|
|
70
|
+
setTimeout(() => {
|
|
71
|
+
resolve();
|
|
72
|
+
}, 1000)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// port-forward
|
|
51
77
|
const portForwardPath = path.join(process.cwd(), 'k8s', 'port-forward')
|
|
52
78
|
if (!fs.existsSync(portForwardPath) || !fs.statSync(portForwardPath).isFile())
|
|
53
79
|
throw portForwardPath + ' file not found'
|
|
54
80
|
const config = JSON.parse(fs.readFileSync(portForwardPath).toString())
|
|
55
|
-
if (!
|
|
81
|
+
if (!config.postgres || !config.nodejs)
|
|
56
82
|
throw portForwardPath + ' : file format incorrect'
|
|
57
83
|
spawnCommand('kubectl', ['port-forward', 'svc/postgres', config.postgres + ':5432'])
|
|
58
84
|
logger.info('Postgresql proxy at localhost:' + config.postgres)
|
|
@@ -68,6 +94,23 @@ async function startServer() {
|
|
|
68
94
|
}
|
|
69
95
|
}
|
|
70
96
|
|
|
97
|
+
/**
|
|
98
|
+
* function serviceAvailable
|
|
99
|
+
* @param service
|
|
100
|
+
* @returns
|
|
101
|
+
*/
|
|
102
|
+
async function serviceAvailable(service: string): Promise<boolean> {
|
|
103
|
+
|
|
104
|
+
const descNodejs = await spawnCommandData('kubectl', ['describe', 'service', service])
|
|
105
|
+
const lines = descNodejs.split('\n')
|
|
106
|
+
for(const line of lines) {
|
|
107
|
+
if (line.includes('Endpoints')) {
|
|
108
|
+
return ! line.includes('<none>')
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return false
|
|
112
|
+
}
|
|
113
|
+
|
|
71
114
|
/**
|
|
72
115
|
* function stopServer
|
|
73
116
|
*/
|
|
@@ -121,7 +164,7 @@ class StopCommand extends Command {
|
|
|
121
164
|
*/
|
|
122
165
|
const program = new Command()
|
|
123
166
|
program.name('cyk').description('cyklang CLI')
|
|
124
|
-
.version('0.
|
|
167
|
+
.version('0.7.3')
|
|
125
168
|
program.addCommand(new StartCommand('start'))
|
|
126
169
|
program.addCommand(new StopCommand('stop'))
|
|
127
170
|
program.addCommand(new InstallCommand('install'))
|