@cyklang/cli 0.5.3 → 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,114 +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 by npm
28
- * and postinstall script will invoke this 'cyk install' command
29
- */
30
- // await this.installNodePackage(node_package)
31
-
32
- const installScript = await this.lookupInstallScript(node_package)
33
-
34
- if (installScript) {
35
- /**
36
- * execute installScript
37
- */
38
- const installPath = path.join('node_modules',node_package,'scripts',installScript)
39
- await this.runShellCommand(installPath, [])
40
- }
41
- else {
42
- /**
43
- * default install : upload modules and execute _init.xml
44
- */
45
- console.log()
46
- console.log('upload modules to server ...')
47
- const xmlFolder = path.join('node_modules', node_package, 'xml')
48
- if (!fs.existsSync(xmlFolder) || !fs.statSync(xmlFolder).isDirectory()) {
49
- throw 'xml folder not found ( ' + xmlFolder + ' )'
50
- }
51
- /**
52
- * upload modules to the server
53
- */
54
- let initModule: string | undefined
55
- const files = fs.readdirSync(xmlFolder)
56
- for (let ind = 0; ind < files.length; ind++) {
57
- const file = files[ind]
58
- await uploadModule(path.join(xmlFolder, file), this.dbManager)
59
- if (file.endsWith('_init.xml')) {
60
- initModule = file
61
- }
62
- }
63
- console.log(green('*') + ' upload modules to server' )
64
- if (initModule) {
65
- const initPath = path.join(xmlFolder, initModule)
66
- await this.runShellCommand('npx', ['cyk', 'run', initPath])
67
- }
68
- }
36
+ if (this.dbManager === undefined || !this.dbRemote) throw 'dbManager or dbRemote undefined'
69
37
 
38
+ await installPackage(node_package, this.dbManager, this.dbRemote)
70
39
  }
71
40
  catch (err) {
72
41
  logger.error(err)
73
42
  }
74
43
  }
44
+ }
45
+
46
+ export async function installPackage(node_package: string, dbManager: DBManager, dbRemote: DBRemote) {
75
47
 
76
48
  /**
77
- * method installNodePackage
78
- * @param node_package
49
+ * install the Node Package
79
50
  */
80
- async installNodePackage(node_package: string) {
51
+ await installNodePackage(node_package, dbManager)
81
52
 
82
- try {
83
- await this.runShellCommand('npm', ['install', '--save-dev', node_package])
84
- } catch (error) {
85
- console.error('Error installing ' + node_package + ' :', error);
86
- throw error
87
- }
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, [])
61
+ }
62
+ else {
63
+ /**
64
+ * default install : upload modules and execute _init.xml
65
+ */
66
+ await defaultInstallScript(node_package, dbManager, dbRemote)
88
67
  }
89
68
 
69
+ }
70
+
71
+ /**
72
+ * method defaultInstallScript
73
+ * @param node_package
74
+ */
75
+ async function defaultInstallScript(node_package: string, dbManager: DBManager, dbRemote: DBRemote) {
76
+
77
+ console.log()
78
+ console.log('Default install script')
90
79
  /**
91
- * method lookupInstallScript
92
- * @param node_package
80
+ * upload package assets
93
81
  */
94
- async lookupInstallScript(node_package: string): Promise<string | undefined> {
95
- let result: string | undefined
96
- try {
97
- const scriptsFolder = path.join('node_modules', node_package, 'scripts')
98
- if (fs.existsSync(scriptsFolder) && fs.statSync(scriptsFolder).isDirectory()) {
99
- const files = fs.readdirSync(scriptsFolder)
100
- for (let ind = 0; ind < files.length; ind++) {
101
- const file = files[ind]
102
- if (file.startsWith('install.')) {
103
- result = file
104
- }
105
- }
106
- }
107
- 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)
108
111
  }
109
- catch (err) {
110
- 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)
111
116
  }
117
+
112
118
  }
119
+ console.log(green('*') + ' upload assets to server')
120
+ }
113
121
 
122
+ /**
123
+ * function uploadModules
124
+ * @param node_package
125
+ * @param dbManager
126
+ */
127
+ async function uploadPackageModules(node_package: string, dbManager: DBManager) {
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
+ }
114
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.2')
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'))