@cyklang/cli 0.5.0 → 0.5.2

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 CHANGED
@@ -13,4 +13,25 @@ export declare class Cmd extends Command {
13
13
  * @param options
14
14
  */
15
15
  updateAuthAccess(tableName: string, prefix: string, id: string, options: any): Promise<void>;
16
+ /**
17
+ * method executeCommand
18
+ * @param command
19
+ * @returns
20
+ */
21
+ executeCommand(command: string): Promise<{
22
+ stdout: string;
23
+ stderr: string;
24
+ }>;
25
+ /**
26
+ * method runShellCommand
27
+ * @param command
28
+ */
29
+ runShellCommand(command: string, args: string[]): Promise<void>;
30
+ /**
31
+ *
32
+ * @param command
33
+ * @param args
34
+ * @returns
35
+ */
36
+ spawnCommand(command: string, args: string[]): Promise<void>;
16
37
  }
package/build/Cmd.js CHANGED
@@ -10,6 +10,8 @@ const dotenv_1 = __importDefault(require("dotenv"));
10
10
  const loglevel_1 = __importDefault(require("loglevel"));
11
11
  const NodeCrypto_1 = require("./NodeCrypto");
12
12
  const DBClient_1 = require("./DBClient");
13
+ const child_process_1 = require("child_process");
14
+ const kolorist_1 = require("kolorist");
13
15
  const logger = loglevel_1.default.getLogger('Cmd.ts');
14
16
  logger.setLevel('debug');
15
17
  class Cmd extends commander_1.Command {
@@ -24,7 +26,7 @@ class Cmd extends commander_1.Command {
24
26
  const structure = new core_1.Structure();
25
27
  if (process.env.DBREMOTE_URL === undefined)
26
28
  throw 'DBREMOTE_URL undefined';
27
- logger.debug('Cmd.DBREMOTE_URL ' + process.env.DBREMOTE_URL);
29
+ logger.info('Cyk server: ' + process.env.DBREMOTE_URL);
28
30
  const nodeCrypto = new NodeCrypto_1.NodeCrypto();
29
31
  this.dbRemote = new core_1.DBRemote(structure.scope, process.env.DBREMOTE_URL, nodeCrypto);
30
32
  const login = await this.dbRemote.signin(process.env.USER_NAME, undefined, process.env.USER_PASSWORD);
@@ -82,5 +84,61 @@ class Cmd extends commander_1.Command {
82
84
  throw err;
83
85
  }
84
86
  }
87
+ /**
88
+ * method executeCommand
89
+ * @param command
90
+ * @returns
91
+ */
92
+ executeCommand(command) {
93
+ return new Promise((resolve, reject) => {
94
+ (0, child_process_1.exec)(command, (error, stdout, stderr) => {
95
+ if (error) {
96
+ reject(error);
97
+ return;
98
+ }
99
+ resolve({ stdout, stderr });
100
+ });
101
+ });
102
+ }
103
+ /**
104
+ * method runShellCommand
105
+ * @param command
106
+ */
107
+ async runShellCommand(command, args) {
108
+ try {
109
+ await this.spawnCommand(command, args);
110
+ // console.log(command)
111
+ // const result = await this.executeCommand(command);
112
+ // console.log(result.stdout);
113
+ // if (result.stderr && result.stderr.trim() !== '')
114
+ // console.error(result.stderr);
115
+ }
116
+ catch (error) {
117
+ console.error('Error running "' + command + '":', error);
118
+ throw error;
119
+ }
120
+ }
121
+ /**
122
+ *
123
+ * @param command
124
+ * @param args
125
+ * @returns
126
+ */
127
+ spawnCommand(command, args) {
128
+ console.log();
129
+ console.log([command, ...args, '...'].join(' '));
130
+ const child_process = (0, child_process_1.spawn)(command, args, { stdio: 'inherit' });
131
+ return new Promise((resolve, reject) => {
132
+ child_process.on('close', (code) => {
133
+ if (code === 0) {
134
+ console.log((0, kolorist_1.green)('*') + ' ' + [command, ...args].join(' '));
135
+ resolve();
136
+ }
137
+ else {
138
+ reject(new Error([command, ...args].join(' ') + ' --> Error ' + code));
139
+ }
140
+ });
141
+ });
142
+ }
85
143
  }
86
144
  exports.Cmd = Cmd;
@@ -0,0 +1,15 @@
1
+ import { Cmd } from "./Cmd";
2
+ export declare class InstallCommand extends Cmd {
3
+ constructor(name: string);
4
+ install(node_package: string, options: any): Promise<void>;
5
+ /**
6
+ * method installNodePackage
7
+ * @param node_package
8
+ */
9
+ installNodePackage(node_package: string): Promise<void>;
10
+ /**
11
+ * method lookupInstallScript
12
+ * @param node_package
13
+ */
14
+ lookupInstallScript(node_package: string): Promise<string | undefined>;
15
+ }
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.InstallCommand = void 0;
30
+ const Cmd_1 = require("./Cmd");
31
+ const fs = __importStar(require("fs"));
32
+ const path = __importStar(require("path"));
33
+ const kolorist_1 = require("kolorist");
34
+ const loglevel_1 = __importDefault(require("loglevel"));
35
+ const ModuleCommand_1 = require("./ModuleCommand");
36
+ const logger = loglevel_1.default.getLogger('InstallCommand.ts');
37
+ logger.setLevel('debug');
38
+ class InstallCommand extends Cmd_1.Cmd {
39
+ constructor(name) {
40
+ super(name);
41
+ this.description('npm install and upload xml modules to Cyk server')
42
+ .argument('<node_package>', 'Node Package installed as dev dependency')
43
+ .action(async (node_package, options) => {
44
+ await this.install(node_package, options);
45
+ });
46
+ }
47
+ async install(node_package, options) {
48
+ try {
49
+ await this.prologue(options);
50
+ if (this.dbManager === undefined)
51
+ throw 'dbManager undefined';
52
+ /**
53
+ * install the Node Package
54
+ */
55
+ await this.installNodePackage(node_package);
56
+ const installScript = await this.lookupInstallScript(node_package);
57
+ if (installScript) {
58
+ /**
59
+ * execute installScript
60
+ */
61
+ const installPath = path.join('node_modules', node_package, 'scripts', installScript);
62
+ await this.runShellCommand(installPath, []);
63
+ }
64
+ else {
65
+ /**
66
+ * default install : upload modules and execute _init.xml
67
+ */
68
+ console.log();
69
+ console.log('upload modules to server ...');
70
+ const xmlFolder = path.join('node_modules', node_package, 'xml');
71
+ if (!fs.existsSync(xmlFolder) || !fs.statSync(xmlFolder).isDirectory()) {
72
+ throw 'xml folder not found ( ' + xmlFolder + ' )';
73
+ }
74
+ /**
75
+ * upload modules to the server
76
+ */
77
+ let initModule;
78
+ const files = fs.readdirSync(xmlFolder);
79
+ for (let ind = 0; ind < files.length; ind++) {
80
+ const file = files[ind];
81
+ await (0, ModuleCommand_1.uploadModule)(path.join(xmlFolder, file), this.dbManager);
82
+ if (file.endsWith('_init.xml')) {
83
+ initModule = file;
84
+ }
85
+ }
86
+ console.log((0, kolorist_1.green)('*') + ' upload modules to server');
87
+ if (initModule) {
88
+ const initPath = path.join(xmlFolder, initModule);
89
+ await this.runShellCommand('npx', ['cyk', 'run', initPath]);
90
+ }
91
+ }
92
+ }
93
+ catch (err) {
94
+ logger.error(err);
95
+ }
96
+ }
97
+ /**
98
+ * method installNodePackage
99
+ * @param node_package
100
+ */
101
+ async installNodePackage(node_package) {
102
+ try {
103
+ await this.runShellCommand('npm', ['install', '--save-dev', node_package]);
104
+ }
105
+ catch (error) {
106
+ console.error('Error installing ' + node_package + ' :', error);
107
+ throw error;
108
+ }
109
+ }
110
+ /**
111
+ * method lookupInstallScript
112
+ * @param node_package
113
+ */
114
+ async lookupInstallScript(node_package) {
115
+ let result;
116
+ try {
117
+ const scriptsFolder = path.join('node_modules', node_package, 'scripts');
118
+ if (fs.existsSync(scriptsFolder) && fs.statSync(scriptsFolder).isDirectory()) {
119
+ const files = fs.readdirSync(scriptsFolder);
120
+ for (let ind = 0; ind < files.length; ind++) {
121
+ const file = files[ind];
122
+ if (file.startsWith('install.')) {
123
+ result = file;
124
+ }
125
+ }
126
+ }
127
+ return result;
128
+ }
129
+ catch (err) {
130
+ throw err;
131
+ }
132
+ }
133
+ }
134
+ exports.InstallCommand = InstallCommand;
@@ -1,5 +1,12 @@
1
1
  #!/usr/bin/env ts-node
2
+ import { DBManager } from "@cyklang/core";
2
3
  import { Command } from 'commander';
3
4
  export declare class ModuleCommand extends Command {
4
5
  constructor(name: string);
5
6
  }
7
+ /**
8
+ *
9
+ * @param file
10
+ * @param dbManager
11
+ */
12
+ export declare function uploadModule(file: string, dbManager: DBManager): Promise<void>;
@@ -27,7 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ModuleCommand = void 0;
30
+ exports.uploadModule = exports.ModuleCommand = void 0;
31
31
  const core_1 = require("@cyklang/core");
32
32
  const fs = __importStar(require("fs"));
33
33
  const loglevel_1 = __importDefault(require("loglevel"));
@@ -135,30 +135,7 @@ class ModuleU extends Cmd_1.Cmd {
135
135
  else {
136
136
  for (let ind = 0; ind < files.length; ind++) {
137
137
  const file = files[ind];
138
- const dbname = getModuleDBName(file);
139
- const source = fs.readFileSync(file).toString();
140
- const tag = (0, core_1.parseXML)(dbname, source);
141
- const dbModuleExist = await this.dbManager.dbModuleExist(dbname);
142
- if (dbModuleExist === undefined) {
143
- // insert
144
- const dbModule = new core_1.DBModule();
145
- dbModule.source = source;
146
- dbModule.dbname = dbname;
147
- dbModule.access = tag.attributes.ACCESS;
148
- dbModule.description = tag.attributes.DESCRIPTION;
149
- logger.debug('module insert ' + dbname + ' access ' + dbModule.access + ' description ' + dbModule.description);
150
- const id = await this.dbManager.dbModuleInsert(dbModule);
151
- logger.info('module ' + dbname + ' added with ID ' + id);
152
- }
153
- else {
154
- // update
155
- dbModuleExist.source = source;
156
- dbModuleExist.access = tag.attributes.ACCESS;
157
- dbModuleExist.description = tag.attributes.DESCRIPTION;
158
- logger.debug('module update ' + dbname + ' access ' + dbModuleExist.access + ' description ' + dbModuleExist.description);
159
- await this.dbManager.dbModuleUpdate(dbModuleExist);
160
- logger.info('module ' + dbname + ' updated');
161
- }
138
+ await uploadModule(file, this.dbManager);
162
139
  }
163
140
  }
164
141
  }
@@ -167,6 +144,38 @@ class ModuleU extends Cmd_1.Cmd {
167
144
  }
168
145
  }
169
146
  }
147
+ /**
148
+ *
149
+ * @param file
150
+ * @param dbManager
151
+ */
152
+ async function uploadModule(file, dbManager) {
153
+ const dbname = getModuleDBName(file);
154
+ const source = fs.readFileSync(file).toString();
155
+ const tag = (0, core_1.parseXML)(dbname, source);
156
+ const dbModuleExist = await dbManager.dbModuleExist(dbname);
157
+ if (dbModuleExist === undefined) {
158
+ // insert
159
+ const dbModule = new core_1.DBModule();
160
+ dbModule.source = source;
161
+ dbModule.dbname = dbname;
162
+ dbModule.access = tag.attributes.ACCESS;
163
+ dbModule.description = tag.attributes.DESCRIPTION;
164
+ // logger.debug('module insert ' + dbname + ' access ' + dbModule.access + ' description ' + dbModule.description)
165
+ const id = await dbManager.dbModuleInsert(dbModule);
166
+ logger.info('module ' + dbname + ' added with ID ' + id);
167
+ }
168
+ else {
169
+ // update
170
+ dbModuleExist.source = source;
171
+ dbModuleExist.access = tag.attributes.ACCESS;
172
+ dbModuleExist.description = tag.attributes.DESCRIPTION;
173
+ // logger.debug('module update ' + dbname + ' access ' + dbModuleExist.access + ' description ' + dbModuleExist.description)
174
+ await dbManager.dbModuleUpdate(dbModuleExist);
175
+ logger.info('module ' + dbname + ' updated');
176
+ }
177
+ }
178
+ exports.uploadModule = uploadModule;
170
179
  class ModuleDownload extends Cmd_1.Cmd {
171
180
  constructor(name, description) {
172
181
  super(name);
@@ -1,5 +1,5 @@
1
1
  import { Cmd } from "./Cmd";
2
2
  export declare class OpenCommand extends Cmd {
3
- constructor();
3
+ constructor(name: string);
4
4
  openBrowser(module: string, options: any): Promise<void>;
5
5
  }
@@ -9,8 +9,8 @@ const loglevel_1 = __importDefault(require("loglevel"));
9
9
  const logger = loglevel_1.default.getLogger('OpenCommand.ts');
10
10
  logger.setLevel('debug');
11
11
  class OpenCommand extends Cmd_1.Cmd {
12
- constructor() {
13
- super('open');
12
+ constructor(name) {
13
+ super(name);
14
14
  this.description('open browser').version('0.1')
15
15
  .argument('<module>', 'module to execute')
16
16
  .action(async (module, options) => {
@@ -1,5 +1,5 @@
1
1
  import { Cmd } from "./Cmd";
2
2
  export declare class RunCommand extends Cmd {
3
- constructor();
3
+ constructor(name: string);
4
4
  runFiles(files: string[], options: any): Promise<void>;
5
5
  }
@@ -36,8 +36,8 @@ const Cmd_1 = require("./Cmd");
36
36
  loglevel_1.default.setLevel('debug');
37
37
  const logger = loglevel_1.default.getLogger('RunCommand.ts');
38
38
  class RunCommand extends Cmd_1.Cmd {
39
- constructor() {
40
- super('run');
39
+ constructor(name) {
40
+ super(name);
41
41
  this.description('run module').version('0.2')
42
42
  .argument('<files...>', 'local module file(s) to run')
43
43
  .action(async (files, options) => {
package/build/index.js CHANGED
@@ -12,7 +12,7 @@ const QueryCommand_1 = require("./QueryCommand");
12
12
  const TableCommand_1 = require("./TableCommand");
13
13
  const TestCommand_1 = require("./TestCommand");
14
14
  const RunCommand_1 = require("./RunCommand");
15
- const InitCommand_1 = require("./InitCommand");
15
+ const InstallCommand_1 = require("./InstallCommand");
16
16
  const AssetCommand_1 = require("./AssetCommand");
17
17
  const OpenCommand_1 = require("./OpenCommand");
18
18
  const logger = loglevel_1.default.getLogger("index.ts");
@@ -20,7 +20,8 @@ logger.setLevel("debug");
20
20
  const program = new commander_1.Command();
21
21
  program.name('cyk').description('cyklang CLI')
22
22
  .version('0.2');
23
- program.addCommand(new InitCommand_1.InitCommand('init'));
23
+ program.addCommand(new InstallCommand_1.InstallCommand('install'));
24
+ program.addCommand(new InstallCommand_1.InstallCommand('i'));
24
25
  program.addCommand(new AssetCommand_1.AssetCommand('asset', 'manage assets'));
25
26
  program.addCommand(new AssetCommand_1.AssetCommand('a', 'manage assets'));
26
27
  program.addCommand(new ModuleCommand_1.ModuleCommand('module'));
@@ -32,6 +33,8 @@ program.addCommand(new QueryCommand_1.QueryCommand('q'));
32
33
  program.addCommand(new TableCommand_1.TableCommand('table'));
33
34
  program.addCommand(new TableCommand_1.TableCommand('t'));
34
35
  program.addCommand(new TestCommand_1.TestCommand());
35
- program.addCommand(new RunCommand_1.RunCommand());
36
- program.addCommand(new OpenCommand_1.OpenCommand());
36
+ program.addCommand(new RunCommand_1.RunCommand('run'));
37
+ program.addCommand(new RunCommand_1.RunCommand('r'));
38
+ program.addCommand(new OpenCommand_1.OpenCommand('open'));
39
+ program.addCommand(new OpenCommand_1.OpenCommand('o'));
37
40
  program.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyklang/cli",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "cyklang CLI",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -22,6 +22,7 @@
22
22
  "dotenv": "^16.0.1",
23
23
  "form-data": "^4.0.0",
24
24
  "inquirer": "^8.2.5",
25
+ "kolorist": "^1.8.0",
25
26
  "loglevel": "^1.8.0",
26
27
  "mime-types": "^2.1.35",
27
28
  "open": "^9.1.0",
package/src/Cmd.ts CHANGED
@@ -4,6 +4,9 @@ import dotenv from 'dotenv'
4
4
  import loglevel from 'loglevel'
5
5
  import { NodeCrypto } from "./NodeCrypto";
6
6
  import { DBClient } from "./DBClient";
7
+ import { exec, spawn } from 'child_process';
8
+ import {red, green, bold} from 'kolorist'
9
+
7
10
  const logger = loglevel.getLogger('Cmd.ts')
8
11
  logger.setLevel('debug')
9
12
 
@@ -23,7 +26,7 @@ export class Cmd extends Command {
23
26
 
24
27
  const structure = new Structure()
25
28
  if (process.env.DBREMOTE_URL === undefined) throw 'DBREMOTE_URL undefined'
26
- logger.debug('Cmd.DBREMOTE_URL ' + process.env.DBREMOTE_URL)
29
+ logger.info('Cyk server: ' + process.env.DBREMOTE_URL)
27
30
  const nodeCrypto = new NodeCrypto()
28
31
  this.dbRemote = new DBRemote(structure.scope, process.env.DBREMOTE_URL, nodeCrypto)
29
32
  const login = await this.dbRemote.signin(process.env.USER_NAME, undefined, process.env.USER_PASSWORD)
@@ -84,7 +87,7 @@ export class Cmd extends Command {
84
87
  </db.update>
85
88
  `
86
89
 
87
- if (! this.dbManager) throw 'prologue() has not been called before'
90
+ if (!this.dbManager) throw 'prologue() has not been called before'
88
91
 
89
92
  const dbClient = new DBClient(this.dbManager)
90
93
  dbClient.execInstructions(inst)
@@ -94,4 +97,62 @@ export class Cmd extends Command {
94
97
  throw err
95
98
  }
96
99
  }
100
+
101
+ /**
102
+ * method executeCommand
103
+ * @param command
104
+ * @returns
105
+ */
106
+ executeCommand(command: string): Promise<{ stdout: string, stderr: string }> {
107
+ return new Promise((resolve, reject) => {
108
+ exec(command, (error, stdout, stderr) => {
109
+ if (error) {
110
+ reject(error);
111
+ return;
112
+ }
113
+ resolve({ stdout, stderr });
114
+ });
115
+ });
116
+ }
117
+
118
+ /**
119
+ * method runShellCommand
120
+ * @param command
121
+ */
122
+ async runShellCommand(command: string, args: string[]) {
123
+ try {
124
+ await this.spawnCommand(command, args)
125
+ // console.log(command)
126
+ // const result = await this.executeCommand(command);
127
+ // console.log(result.stdout);
128
+ // if (result.stderr && result.stderr.trim() !== '')
129
+ // console.error(result.stderr);
130
+ } catch (error) {
131
+ console.error('Error running "' + command + '":', error);
132
+ throw error
133
+ }
134
+ }
135
+
136
+ /**
137
+ *
138
+ * @param command
139
+ * @param args
140
+ * @returns
141
+ */
142
+ spawnCommand(command: string, args: string[]): Promise<void> {
143
+ console.log()
144
+ console.log([command, ...args, '...'].join(' '))
145
+ const child_process = spawn(command, args, { stdio: 'inherit' })
146
+ return new Promise((resolve, reject) => {
147
+ child_process.on('close', (code) => {
148
+ if (code === 0) {
149
+ console.log(green('*') + ' ' + [command, ...args].join(' '))
150
+ resolve()
151
+ }
152
+ else {
153
+ reject(new Error([command, ...args].join(' ') + ' --> Error ' + code))
154
+ }
155
+ })
156
+ })
157
+ }
97
158
  }
@@ -0,0 +1,113 @@
1
+ import { Cmd } from "./Cmd";
2
+ import * as fs from "fs"
3
+ import * as path from "path"
4
+ import {green, red, bold} from 'kolorist'
5
+
6
+ import loglevel from 'loglevel'
7
+ import { uploadModule } from "./ModuleCommand";
8
+ const logger = loglevel.getLogger('InstallCommand.ts')
9
+ logger.setLevel('debug')
10
+
11
+ export class InstallCommand extends Cmd {
12
+ constructor(name: string) {
13
+ super(name)
14
+ this.description('npm install and upload xml modules to Cyk server')
15
+ .argument('<node_package>', 'Node Package installed as dev dependency')
16
+ .action(async (node_package: any, options: any) => {
17
+ await this.install(node_package, options)
18
+ })
19
+ }
20
+
21
+ async install(node_package: string, options: any) {
22
+ try {
23
+ await this.prologue(options)
24
+ if (this.dbManager === undefined) throw 'dbManager undefined'
25
+
26
+ /**
27
+ * install the Node Package
28
+ */
29
+ await this.installNodePackage(node_package)
30
+
31
+ const installScript = await this.lookupInstallScript(node_package)
32
+
33
+ if (installScript) {
34
+ /**
35
+ * execute installScript
36
+ */
37
+ const installPath = path.join('node_modules',node_package,'scripts',installScript)
38
+ await this.runShellCommand(installPath, [])
39
+ }
40
+ else {
41
+ /**
42
+ * default install : upload modules and execute _init.xml
43
+ */
44
+ console.log()
45
+ console.log('upload modules to server ...')
46
+ const xmlFolder = path.join('node_modules', node_package, 'xml')
47
+ if (!fs.existsSync(xmlFolder) || !fs.statSync(xmlFolder).isDirectory()) {
48
+ throw 'xml folder not found ( ' + xmlFolder + ' )'
49
+ }
50
+ /**
51
+ * upload modules to the server
52
+ */
53
+ let initModule: string | undefined
54
+ const files = fs.readdirSync(xmlFolder)
55
+ for (let ind = 0; ind < files.length; ind++) {
56
+ const file = files[ind]
57
+ await uploadModule(path.join(xmlFolder, file), this.dbManager)
58
+ if (file.endsWith('_init.xml')) {
59
+ initModule = file
60
+ }
61
+ }
62
+ console.log(green('*') + ' upload modules to server' )
63
+ if (initModule) {
64
+ const initPath = path.join(xmlFolder, initModule)
65
+ await this.runShellCommand('npx', ['cyk', 'run', initPath])
66
+ }
67
+ }
68
+
69
+ }
70
+ catch (err) {
71
+ logger.error(err)
72
+ }
73
+ }
74
+
75
+ /**
76
+ * method installNodePackage
77
+ * @param node_package
78
+ */
79
+ async installNodePackage(node_package: string) {
80
+
81
+ try {
82
+ await this.runShellCommand('npm', ['install', '--save-dev', node_package])
83
+ } catch (error) {
84
+ console.error('Error installing ' + node_package + ' :', error);
85
+ throw error
86
+ }
87
+ }
88
+
89
+ /**
90
+ * method lookupInstallScript
91
+ * @param node_package
92
+ */
93
+ async lookupInstallScript(node_package: string): Promise<string | undefined> {
94
+ let result: string | undefined
95
+ try {
96
+ const scriptsFolder = path.join('node_modules', node_package, 'scripts')
97
+ if (fs.existsSync(scriptsFolder) && fs.statSync(scriptsFolder).isDirectory()) {
98
+ const files = fs.readdirSync(scriptsFolder)
99
+ for (let ind = 0; ind < files.length; ind++) {
100
+ const file = files[ind]
101
+ if (file.startsWith('install.')) {
102
+ result = file
103
+ }
104
+ }
105
+ }
106
+ return result
107
+ }
108
+ catch (err) {
109
+ throw err
110
+ }
111
+ }
112
+
113
+ }
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ts-node
2
- import { DBModule, parseXML } from "@cyklang/core"
2
+ import { DBManager, DBModule, parseXML } from "@cyklang/core"
3
3
  import * as fs from "fs"
4
4
  import loglevel from 'loglevel'
5
5
  import path from "path"
@@ -115,30 +115,7 @@ class ModuleU extends Cmd {
115
115
  else {
116
116
  for (let ind = 0; ind < files.length; ind++) {
117
117
  const file = files[ind]
118
- const dbname = getModuleDBName(file)
119
- const source = fs.readFileSync(file).toString()
120
- const tag = parseXML(dbname, source)
121
- const dbModuleExist = await this.dbManager.dbModuleExist(dbname)
122
- if (dbModuleExist === undefined) {
123
- // insert
124
- const dbModule = new DBModule()
125
- dbModule.source = source
126
- dbModule.dbname = dbname
127
- dbModule.access = tag.attributes.ACCESS
128
- dbModule.description = tag.attributes.DESCRIPTION
129
- logger.debug('module insert ' + dbname + ' access ' + dbModule.access + ' description ' + dbModule.description)
130
- const id = await this.dbManager.dbModuleInsert(dbModule)
131
- logger.info('module ' + dbname + ' added with ID ' + id)
132
- }
133
- else {
134
- // update
135
- dbModuleExist.source = source
136
- dbModuleExist.access = tag.attributes.ACCESS
137
- dbModuleExist.description = tag.attributes.DESCRIPTION
138
- logger.debug('module update ' + dbname + ' access ' + dbModuleExist.access + ' description ' + dbModuleExist.description)
139
- await this.dbManager.dbModuleUpdate(dbModuleExist)
140
- logger.info('module ' + dbname + ' updated')
141
- }
118
+ await uploadModule(file, this.dbManager)
142
119
  }
143
120
  }
144
121
  }
@@ -148,6 +125,38 @@ class ModuleU extends Cmd {
148
125
  }
149
126
  }
150
127
 
128
+ /**
129
+ *
130
+ * @param file
131
+ * @param dbManager
132
+ */
133
+ export async function uploadModule(file: string, dbManager: DBManager) {
134
+ const dbname = getModuleDBName(file)
135
+ const source = fs.readFileSync(file).toString()
136
+ const tag = parseXML(dbname, source)
137
+ const dbModuleExist = await dbManager.dbModuleExist(dbname)
138
+ if (dbModuleExist === undefined) {
139
+ // insert
140
+ const dbModule = new DBModule()
141
+ dbModule.source = source
142
+ dbModule.dbname = dbname
143
+ dbModule.access = tag.attributes.ACCESS
144
+ dbModule.description = tag.attributes.DESCRIPTION
145
+ // logger.debug('module insert ' + dbname + ' access ' + dbModule.access + ' description ' + dbModule.description)
146
+ const id = await dbManager.dbModuleInsert(dbModule)
147
+ logger.info('module ' + dbname + ' added with ID ' + id)
148
+ }
149
+ else {
150
+ // update
151
+ dbModuleExist.source = source
152
+ dbModuleExist.access = tag.attributes.ACCESS
153
+ dbModuleExist.description = tag.attributes.DESCRIPTION
154
+ // logger.debug('module update ' + dbname + ' access ' + dbModuleExist.access + ' description ' + dbModuleExist.description)
155
+ await dbManager.dbModuleUpdate(dbModuleExist)
156
+ logger.info('module ' + dbname + ' updated')
157
+ }
158
+ }
159
+
151
160
  interface DownloadOptions {
152
161
  id: string | undefined
153
162
  env: string | undefined
@@ -5,8 +5,8 @@ const logger = loglevel.getLogger('OpenCommand.ts')
5
5
  logger.setLevel('debug')
6
6
 
7
7
  export class OpenCommand extends Cmd {
8
- constructor() {
9
- super('open')
8
+ constructor(name: string) {
9
+ super(name)
10
10
  this.description('open browser').version('0.1')
11
11
  .argument('<module>', 'module to execute')
12
12
  .action(async (module: any, options: any) => {
package/src/RunCommand.ts CHANGED
@@ -8,8 +8,8 @@ loglevel.setLevel('debug')
8
8
  const logger = loglevel.getLogger('RunCommand.ts');
9
9
 
10
10
  export class RunCommand extends Cmd {
11
- constructor() {
12
- super('run')
11
+ constructor(name: string) {
12
+ super(name)
13
13
  this.description('run module').version('0.2')
14
14
  .argument('<files...>', 'local module file(s) to run')
15
15
  .action(async (files: any, options: any) => {
package/src/index.ts CHANGED
@@ -7,7 +7,7 @@ import { QueryCommand } from './QueryCommand'
7
7
  import { TableCommand } from './TableCommand'
8
8
  import { TestCommand } from './TestCommand'
9
9
  import { RunCommand } from './RunCommand'
10
- import { InitCommand } from './InitCommand'
10
+ import { InstallCommand } from './InstallCommand'
11
11
  import { AssetCommand } from './AssetCommand'
12
12
  import { OpenCommand } from './OpenCommand'
13
13
  const logger = loglevel.getLogger("index.ts")
@@ -17,7 +17,8 @@ const program = new Command()
17
17
  program.name('cyk').description('cyklang CLI')
18
18
  .version('0.2')
19
19
 
20
- program.addCommand(new InitCommand('init'))
20
+ program.addCommand(new InstallCommand('install'))
21
+ program.addCommand(new InstallCommand('i'))
21
22
  program.addCommand(new AssetCommand('asset', 'manage assets'))
22
23
  program.addCommand(new AssetCommand('a', 'manage assets'))
23
24
  program.addCommand(new ModuleCommand('module'))
@@ -29,7 +30,9 @@ program.addCommand(new QueryCommand('q'))
29
30
  program.addCommand(new TableCommand('table'))
30
31
  program.addCommand(new TableCommand('t'))
31
32
  program.addCommand(new TestCommand())
32
- program.addCommand(new RunCommand())
33
- program.addCommand(new OpenCommand())
33
+ program.addCommand(new RunCommand('run'))
34
+ program.addCommand(new RunCommand('r'))
35
+ program.addCommand(new OpenCommand('open'))
36
+ program.addCommand(new OpenCommand('o'))
34
37
 
35
38
  program.parse()