@cyklang/cli 0.5.1 → 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 {
@@ -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;
@@ -3,8 +3,13 @@ export declare class InstallCommand extends Cmd {
3
3
  constructor(name: string);
4
4
  install(node_package: string, options: any): Promise<void>;
5
5
  /**
6
- * method verifyPackageJson
6
+ * method installNodePackage
7
7
  * @param node_package
8
8
  */
9
- verifyPackageJson(node_package: string): Promise<void>;
9
+ installNodePackage(node_package: string): Promise<void>;
10
+ /**
11
+ * method lookupInstallScript
12
+ * @param node_package
13
+ */
14
+ lookupInstallScript(node_package: string): Promise<string | undefined>;
10
15
  }
@@ -30,6 +30,7 @@ exports.InstallCommand = void 0;
30
30
  const Cmd_1 = require("./Cmd");
31
31
  const fs = __importStar(require("fs"));
32
32
  const path = __importStar(require("path"));
33
+ const kolorist_1 = require("kolorist");
33
34
  const loglevel_1 = __importDefault(require("loglevel"));
34
35
  const ModuleCommand_1 = require("./ModuleCommand");
35
36
  const logger = loglevel_1.default.getLogger('InstallCommand.ts');
@@ -37,8 +38,8 @@ logger.setLevel('debug');
37
38
  class InstallCommand extends Cmd_1.Cmd {
38
39
  constructor(name) {
39
40
  super(name);
40
- this.description('upload Cyk modules to a Cyk server from a Node package downloaded by "npm install" ')
41
- .argument('<node_package>', 'Node Package installed as dependency')
41
+ this.description('npm install and upload xml modules to Cyk server')
42
+ .argument('<node_package>', 'Node Package installed as dev dependency')
42
43
  .action(async (node_package, options) => {
43
44
  await this.install(node_package, options);
44
45
  });
@@ -48,15 +49,45 @@ class InstallCommand extends Cmd_1.Cmd {
48
49
  await this.prologue(options);
49
50
  if (this.dbManager === undefined)
50
51
  throw 'dbManager undefined';
51
- await this.verifyPackageJson(node_package);
52
- const xmlFolder = path.join('node_modules', node_package, 'xml');
53
- if (!fs.existsSync(xmlFolder) || !fs.statSync(xmlFolder).isDirectory()) {
54
- throw 'xml folder not found ( ' + xmlFolder + ' )';
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, []);
55
63
  }
56
- const files = fs.readdirSync(xmlFolder);
57
- for (let ind = 0; ind < files.length; ind++) {
58
- const file = files[ind];
59
- await (0, ModuleCommand_1.uploadModule)(path.join(xmlFolder, file), this.dbManager);
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
+ }
60
91
  }
61
92
  }
62
93
  catch (err) {
@@ -64,23 +95,39 @@ class InstallCommand extends Cmd_1.Cmd {
64
95
  }
65
96
  }
66
97
  /**
67
- * method verifyPackageJson
98
+ * method installNodePackage
68
99
  * @param node_package
69
100
  */
70
- async verifyPackageJson(node_package) {
71
- let packageJson;
101
+ async installNodePackage(node_package) {
72
102
  try {
73
- // Lecture du contenu du fichier package.json en tant que chaîne de caractères
74
- const packageJsonContent = fs.readFileSync('package.json', 'utf8');
75
- // Analyse du contenu en tant qu'objet JSON
76
- packageJson = JSON.parse(packageJsonContent);
103
+ await this.runShellCommand('npm', ['install', '--save-dev', node_package]);
77
104
  }
78
105
  catch (error) {
79
- console.error('Error loading package.json file :', error);
106
+ console.error('Error installing ' + node_package + ' :', error);
80
107
  throw error;
81
108
  }
82
- if (!packageJson.dependencies || !packageJson.dependencies[node_package]) {
83
- throw node_package + ' not installed in the current project. Install it first with "npm install"';
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;
84
131
  }
85
132
  }
86
133
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyklang/cli",
3
- "version": "0.5.1",
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
 
@@ -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
  }
@@ -1,6 +1,7 @@
1
1
  import { Cmd } from "./Cmd";
2
2
  import * as fs from "fs"
3
3
  import * as path from "path"
4
+ import {green, red, bold} from 'kolorist'
4
5
 
5
6
  import loglevel from 'loglevel'
6
7
  import { uploadModule } from "./ModuleCommand";
@@ -10,8 +11,8 @@ logger.setLevel('debug')
10
11
  export class InstallCommand extends Cmd {
11
12
  constructor(name: string) {
12
13
  super(name)
13
- this.description('upload Cyk modules to a Cyk server from a Node package downloaded by "npm install" ')
14
- .argument('<node_package>', 'Node Package installed as dependency')
14
+ this.description('npm install and upload xml modules to Cyk server')
15
+ .argument('<node_package>', 'Node Package installed as dev dependency')
15
16
  .action(async (node_package: any, options: any) => {
16
17
  await this.install(node_package, options)
17
18
  })
@@ -21,18 +22,50 @@ export class InstallCommand extends Cmd {
21
22
  try {
22
23
  await this.prologue(options)
23
24
  if (this.dbManager === undefined) throw 'dbManager undefined'
24
- await this.verifyPackageJson(node_package)
25
25
 
26
- const xmlFolder = path.join('node_modules', node_package, 'xml')
27
- if (!fs.existsSync(xmlFolder) || !fs.statSync(xmlFolder).isDirectory()) {
28
- throw 'xml folder not found ( ' + xmlFolder + ' )'
29
- }
26
+ /**
27
+ * install the Node Package
28
+ */
29
+ await this.installNodePackage(node_package)
30
+
31
+ const installScript = await this.lookupInstallScript(node_package)
30
32
 
31
- const files = fs.readdirSync(xmlFolder)
32
- for (let ind = 0; ind < files.length; ind++) {
33
- const file = files[ind]
34
- await uploadModule(path.join(xmlFolder, file), this.dbManager)
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
+ }
35
67
  }
68
+
36
69
  }
37
70
  catch (err) {
38
71
  logger.error(err)
@@ -40,32 +73,41 @@ export class InstallCommand extends Cmd {
40
73
  }
41
74
 
42
75
  /**
43
- * method verifyPackageJson
76
+ * method installNodePackage
44
77
  * @param node_package
45
78
  */
46
- async verifyPackageJson(node_package: string) {
47
-
48
- let packageJson: any
79
+ async installNodePackage(node_package: string) {
49
80
 
50
81
  try {
51
- // Lecture du contenu du fichier package.json en tant que chaîne de caractères
52
- const packageJsonContent = fs.readFileSync('package.json', 'utf8');
53
-
54
- // Analyse du contenu en tant qu'objet JSON
55
- packageJson = JSON.parse(packageJsonContent);
56
-
82
+ await this.runShellCommand('npm', ['install', '--save-dev', node_package])
57
83
  } catch (error) {
58
- console.error('Error loading package.json file :', error);
84
+ console.error('Error installing ' + node_package + ' :', error);
59
85
  throw error
60
86
  }
87
+ }
61
88
 
62
- if (!packageJson.dependencies || !packageJson.dependencies[node_package]) {
63
- throw node_package + ' not installed in the current project. Install it first with "npm install"'
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
64
110
  }
65
-
66
-
67
-
68
111
  }
69
112
 
70
-
71
113
  }