@cyklang/cli 0.5.4 → 0.5.5

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.
@@ -1,113 +1,195 @@
1
- import { Cmd } from "./Cmd";
1
+ import { Cmd, readPackageJson, runShellCommand } from "./Cmd";
2
2
  import * as fs from "fs"
3
3
  import * as path from "path"
4
- import {green, red, bold} from 'kolorist'
4
+ import { green, red, bold } from 'kolorist'
5
5
 
6
6
  import loglevel from 'loglevel'
7
7
  import { uploadModule } from "./ModuleCommand";
8
+ import { uninstallPackage } from "./UninstallCommand";
9
+ import { DBManager, DBRemote } from "@cyklang/core";
10
+ import { FileDescriptor, uploadAsset, uploadDirectory } from "./AssetCommand";
8
11
  const logger = loglevel.getLogger('InstallCommand.ts')
9
12
  logger.setLevel('debug')
10
13
 
11
14
  export class InstallCommand extends Cmd {
15
+ /**
16
+ * constructor
17
+ * @param name
18
+ */
12
19
  constructor(name: string) {
13
20
  super(name)
14
- this.description('npm install and upload xml modules to Cyk server')
21
+ this.description('npm install and run install script')
15
22
  .argument('<node_package>', 'Node Package installed as dev dependency')
16
23
  .action(async (node_package: any, options: any) => {
17
24
  await this.install(node_package, options)
18
25
  })
19
26
  }
20
27
 
28
+ /**
29
+ * method install
30
+ * @param node_package
31
+ * @param options
32
+ */
21
33
  async install(node_package: string, options: any) {
22
34
  try {
23
35
  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
- }
36
+ if (this.dbManager === undefined || !this.dbRemote) throw 'dbManager or dbRemote undefined'
68
37
 
38
+ await installPackage(node_package, this.dbManager, this.dbRemote)
69
39
  }
70
40
  catch (err) {
71
41
  logger.error(err)
72
42
  }
73
43
  }
44
+ }
45
+
46
+ export async function installPackage(node_package: string, dbManager: DBManager, dbRemote: DBRemote) {
74
47
 
75
48
  /**
76
- * method installNodePackage
77
- * @param node_package
49
+ * install the Node Package
78
50
  */
79
- async installNodePackage(node_package: string) {
51
+ await installNodePackage(node_package, dbManager)
80
52
 
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
- }
53
+ const installScript = await lookupInstallScript(node_package)
54
+
55
+ if (installScript) {
56
+ /**
57
+ * execute installScript
58
+ */
59
+ const installPath = path.join('node_modules', node_package, 'scripts', installScript)
60
+ await runShellCommand(installPath, [])
87
61
  }
62
+ else {
63
+ /**
64
+ * default install : upload modules and execute _init.xml
65
+ */
66
+ await defaultInstallScript(node_package, dbManager, dbRemote)
67
+ }
68
+
69
+ }
70
+
71
+ /**
72
+ * method defaultInstallScript
73
+ * @param node_package
74
+ */
75
+ async function defaultInstallScript(node_package: string, dbManager: DBManager, dbRemote: DBRemote) {
88
76
 
77
+ console.log()
78
+ console.log('Default install script')
89
79
  /**
90
- * method lookupInstallScript
91
- * @param node_package
80
+ * upload package assets
92
81
  */
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
82
+ await uploadPackageAssets(node_package, dbManager, dbRemote)
83
+
84
+ /**
85
+ * upload package modules to the server
86
+ */
87
+ await uploadPackageModules(node_package, dbManager)
88
+ }
89
+
90
+ /**
91
+ * function uploadPackageAssets
92
+ * @param node_package
93
+ * @param dbManager
94
+ */
95
+ async function uploadPackageAssets(node_package: string, dbManager: DBManager, dbRemote: DBRemote) {
96
+ const assetFolder = path.join('node_modules', node_package, 'asset')
97
+ if (!fs.existsSync(assetFolder) || !fs.statSync(assetFolder).isDirectory()) {
98
+ return
99
+ }
100
+ console.log()
101
+ console.log('upload assets to server ...')
102
+
103
+ if (!dbManager) throw 'dbManager undefined'
104
+ const items = fs.readdirSync(assetFolder)
105
+ for (let ind = 0; ind < items.length; ind++) {
106
+ const item = items[ind]
107
+ const itemPath = path.join(assetFolder, item)
108
+ if (fs.statSync(itemPath).isDirectory()) {
109
+ const dest = "/" + item + "/"
110
+ await uploadDirectory(itemPath, dest, {yes: true}, dbManager, dbRemote)
107
111
  }
108
- catch (err) {
109
- throw err
112
+ else {
113
+ const dest = "/" + item
114
+ const fd: FileDescriptor = {path: path.join(assetFolder, item), mtime: new Date()}
115
+ await uploadAsset(fd, dest, dbManager, dbRemote)
110
116
  }
117
+
111
118
  }
119
+ console.log(green('*') + ' upload assets to server')
120
+ }
121
+
122
+ /**
123
+ * function uploadModules
124
+ * @param node_package
125
+ * @param dbManager
126
+ */
127
+ async function uploadPackageModules(node_package: string, dbManager: DBManager) {
112
128
 
129
+ const moduleFolder = path.join('node_modules', node_package, 'module')
130
+ if (!fs.existsSync(moduleFolder) || !fs.statSync(moduleFolder).isDirectory()) {
131
+ return
132
+ }
133
+ console.log()
134
+ console.log('upload modules to server ...')
135
+ let initModule: string | undefined
136
+ const files = fs.readdirSync(moduleFolder)
137
+ for (let ind = 0; ind < files.length; ind++) {
138
+ const file = files[ind]
139
+ if (!dbManager) throw 'dbManager undefined'
140
+ await uploadModule(path.join(moduleFolder, file), dbManager)
141
+ if (file.endsWith('_init.xml')) {
142
+ initModule = file
143
+ }
144
+ }
145
+ console.log(green('*') + ' upload modules to server')
146
+ if (initModule) {
147
+ const initPath = path.join(moduleFolder, initModule)
148
+ await runShellCommand('npx', ['cyk', 'run', initPath])
149
+ }
150
+ }
151
+
152
+ /**
153
+ * method installNodePackage
154
+ * @param node_package
155
+ */
156
+ async function installNodePackage(node_package: string, dbManager: DBManager) {
157
+
158
+ try {
159
+ const packageJson = await readPackageJson()
160
+ if (packageJson.devDependencies && packageJson.devDependencies[node_package]
161
+ || packageJson.dependencies && packageJson.dependencies[node_package]) {
162
+ console.log(node_package + ' already installed. We uninstall it first')
163
+ if (!dbManager) throw 'dbManager undefined'
164
+ await uninstallPackage(node_package, dbManager)
165
+ }
166
+
167
+ await runShellCommand('npm', ['install', '--save-dev', node_package])
168
+ } catch (error) {
169
+ console.error('Error installing ' + node_package + ' :', error);
170
+ throw error
171
+ }
172
+ }
173
+ /**
174
+ * function lookupInstallScript
175
+ * @param node_package
176
+ */
177
+ async function lookupInstallScript(node_package: string): Promise<string | undefined> {
178
+ let result: string | undefined
179
+ try {
180
+ const scriptsFolder = path.join('node_modules', node_package, 'scripts')
181
+ if (fs.existsSync(scriptsFolder) && fs.statSync(scriptsFolder).isDirectory()) {
182
+ const files = fs.readdirSync(scriptsFolder)
183
+ for (let ind = 0; ind < files.length; ind++) {
184
+ const file = files[ind]
185
+ if (file.startsWith('install.')) {
186
+ result = file
187
+ }
188
+ }
189
+ }
190
+ return result
191
+ }
192
+ catch (err) {
193
+ throw err
194
+ }
113
195
  }
@@ -147,6 +147,7 @@ export async function uploadModule(file: string, dbManager: DBManager) {
147
147
  logger.info('module ' + dbname + ' added with ID ' + id)
148
148
  }
149
149
  else {
150
+ logger.debug('module exists : ' + dbModuleExist.dbname + ', id ' + dbModuleExist.id)
150
151
  // update
151
152
  dbModuleExist.source = source
152
153
  dbModuleExist.access = tag.attributes.ACCESS
@@ -273,3 +274,14 @@ class ModuleDelete extends Cmd {
273
274
  }
274
275
  }
275
276
 
277
+ /**
278
+ * function deleteModule
279
+ * @param dbname
280
+ * @param dbManager
281
+ */
282
+ export async function deleteModule(dbname: string, dbManager: DBManager) {
283
+
284
+ const dbClient = new DBClient(dbManager)
285
+ await dbClient.deleteModule(dbname)
286
+ }
287
+
@@ -0,0 +1,145 @@
1
+ import { Cmd, runShellCommand } 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 { deleteModule } from "./ModuleCommand";
8
+ import { DBManager } from "@cyklang/core";
9
+ const logger = loglevel.getLogger('UninstallCommand.ts')
10
+ logger.setLevel('debug')
11
+
12
+ export class UninstallCommand extends Cmd {
13
+ /**
14
+ * constructor
15
+ * @param name
16
+ */
17
+ constructor(name: string) {
18
+ super(name)
19
+ this.description('run uninstall script and npm uninstall command')
20
+ .argument('<node_package>', 'Node Package installed as dev dependency')
21
+ .action(async (node_package: any, options: any) => {
22
+ await this.uninstall(node_package, options)
23
+ })
24
+ }
25
+
26
+ /**
27
+ * method uninstall
28
+ * @param node_package
29
+ * @param options
30
+ */
31
+ async uninstall(node_package: string, options: any) {
32
+ try {
33
+ await this.prologue(options)
34
+ if (this.dbManager === undefined) throw 'dbManager undefined'
35
+
36
+ await uninstallPackage(node_package, this.dbManager)
37
+ }
38
+ catch (err) {
39
+ logger.error(err)
40
+ }
41
+ }
42
+ }
43
+
44
+ /**
45
+ * function uninstallPackage
46
+ * @param node_package
47
+ */
48
+ export async function uninstallPackage(node_package: string, dbManager: DBManager) {
49
+
50
+ const uninstallScript = await lookupUninstallScript(node_package)
51
+ if (uninstallScript) {
52
+ /**
53
+ * run scripts/uninstall
54
+ */
55
+ const uninstallPath = path.join('node_modules', node_package, 'scripts', uninstallScript)
56
+ await runShellCommand(uninstallPath, [])
57
+ }
58
+ else {
59
+ /**
60
+ * default uninstall :
61
+ */
62
+ await defaultUninstallScript(node_package, dbManager)
63
+ }
64
+
65
+ /**
66
+ * uninstall the node package
67
+ */
68
+ await uninstallNodePackage(node_package)
69
+
70
+ }
71
+ /**
72
+ * method defaultUninstallScript
73
+ * @param node_package
74
+ */
75
+ async function defaultUninstallScript(node_package: string, dbManager: DBManager) {
76
+ console.log()
77
+ console.log('Default uninstall script')
78
+ /**
79
+ * delete package modules from server
80
+ */
81
+ await deletePackageModules(node_package, dbManager)
82
+
83
+ }
84
+
85
+ /**
86
+ * function deletePackageModules
87
+ * @param node_package
88
+ * @param dbManager
89
+ * @returns
90
+ */
91
+ async function deletePackageModules(node_package: string, dbManager: DBManager) {
92
+ const moduleFolder = path.join('node_modules', node_package, 'module')
93
+ if (!fs.existsSync(moduleFolder) || !fs.statSync(moduleFolder).isDirectory()) {
94
+ return
95
+ }
96
+ console.log()
97
+ console.log('delete modules from server ...')
98
+ const files = fs.readdirSync(moduleFolder)
99
+ for (let ind = 0; ind < files.length; ind++) {
100
+ const file = files[ind]
101
+ if (!dbManager) throw 'dbManager undefined'
102
+
103
+ const fileInfo = path.parse(file)
104
+ await deleteModule(fileInfo.name, dbManager)
105
+ }
106
+ console.log(green('*') + ' delete modules from server')
107
+ }
108
+
109
+ /**
110
+ * function uninstallNodePackage
111
+ * @param node_package
112
+ */
113
+ async function uninstallNodePackage(node_package: string) {
114
+
115
+ try {
116
+ await runShellCommand('npm', ['uninstall', '--save-dev', node_package])
117
+ } catch (error) {
118
+ console.error('Error uninstalling ' + node_package + ' :', error);
119
+ throw error
120
+ }
121
+ }
122
+
123
+ /**
124
+ * function lookupUninstallScript
125
+ * @param node_package
126
+ */
127
+ async function lookupUninstallScript(node_package: string): Promise<string | undefined> {
128
+ let result: string | undefined
129
+ try {
130
+ const scriptsFolder = path.join('node_modules', node_package, 'scripts')
131
+ if (fs.existsSync(scriptsFolder) && fs.statSync(scriptsFolder).isDirectory()) {
132
+ const files = fs.readdirSync(scriptsFolder)
133
+ for (let ind = 0; ind < files.length; ind++) {
134
+ const file = files[ind]
135
+ if (file.startsWith('uninstall.')) {
136
+ result = file
137
+ }
138
+ }
139
+ }
140
+ return result
141
+ }
142
+ catch (err) {
143
+ throw err
144
+ }
145
+ }
package/src/index.ts CHANGED
@@ -10,15 +10,17 @@ import { RunCommand } from './RunCommand'
10
10
  import { InstallCommand } from './InstallCommand'
11
11
  import { AssetCommand } from './AssetCommand'
12
12
  import { OpenCommand } from './OpenCommand'
13
+ import { UninstallCommand } from './UninstallCommand'
13
14
  const logger = loglevel.getLogger("index.ts")
14
15
  logger.setLevel("debug")
15
16
 
16
17
  const program = new Command()
17
18
  program.name('cyk').description('cyklang CLI')
18
- .version('0.5.4')
19
+ .version('0.5.5')
19
20
 
20
21
  program.addCommand(new InstallCommand('install'))
21
22
  program.addCommand(new InstallCommand('i'))
23
+ program.addCommand(new UninstallCommand('uninstall'))
22
24
  program.addCommand(new AssetCommand('asset', 'manage assets'))
23
25
  program.addCommand(new AssetCommand('a', 'manage assets'))
24
26
  program.addCommand(new ModuleCommand('module'))