@cyklang/cli
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -16,2 +16,23 @@ import { DBManager, DBRemote, SigninResponse } from "@cyklang/core"; | ||
updateAuthAccess(tableName: string, prefix: string, id: string, options: any): Promise<void>; | ||
/** | ||
* method executeCommand | ||
* @param command | ||
* @returns | ||
*/ | ||
executeCommand(command: string): Promise<{ | ||
stdout: string; | ||
stderr: string; | ||
}>; | ||
/** | ||
* method runShellCommand | ||
* @param command | ||
*/ | ||
runShellCommand(command: string, args: string[]): Promise<void>; | ||
/** | ||
* | ||
* @param command | ||
* @param args | ||
* @returns | ||
*/ | ||
spawnCommand(command: string, args: string[]): Promise<void>; | ||
} |
@@ -13,2 +13,4 @@ "use strict"; | ||
const DBClient_1 = require("./DBClient"); | ||
const child_process_1 = require("child_process"); | ||
const kolorist_1 = require("kolorist"); | ||
const logger = loglevel_1.default.getLogger('Cmd.ts'); | ||
@@ -86,3 +88,59 @@ logger.setLevel('debug'); | ||
} | ||
/** | ||
* method executeCommand | ||
* @param command | ||
* @returns | ||
*/ | ||
executeCommand(command) { | ||
return new Promise((resolve, reject) => { | ||
(0, child_process_1.exec)(command, (error, stdout, stderr) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
resolve({ stdout, stderr }); | ||
}); | ||
}); | ||
} | ||
/** | ||
* method runShellCommand | ||
* @param command | ||
*/ | ||
async runShellCommand(command, args) { | ||
try { | ||
await this.spawnCommand(command, args); | ||
// console.log(command) | ||
// const result = await this.executeCommand(command); | ||
// console.log(result.stdout); | ||
// if (result.stderr && result.stderr.trim() !== '') | ||
// console.error(result.stderr); | ||
} | ||
catch (error) { | ||
console.error('Error running "' + command + '":', error); | ||
throw error; | ||
} | ||
} | ||
/** | ||
* | ||
* @param command | ||
* @param args | ||
* @returns | ||
*/ | ||
spawnCommand(command, args) { | ||
console.log(); | ||
console.log([command, ...args, '...'].join(' ')); | ||
const child_process = (0, child_process_1.spawn)(command, args, { stdio: 'inherit' }); | ||
return new Promise((resolve, reject) => { | ||
child_process.on('close', (code) => { | ||
if (code === 0) { | ||
console.log((0, kolorist_1.green)('*') + ' ' + [command, ...args].join(' ')); | ||
resolve(); | ||
} | ||
else { | ||
reject(new Error([command, ...args].join(' ') + ' --> Error ' + code)); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
exports.Cmd = Cmd; |
@@ -6,6 +6,11 @@ import { Cmd } from "./Cmd"; | ||
/** | ||
* method verifyPackageJson | ||
* method installNodePackage | ||
* @param node_package | ||
*/ | ||
verifyPackageJson(node_package: string): Promise<void>; | ||
installNodePackage(node_package: string): Promise<void>; | ||
/** | ||
* method lookupInstallScript | ||
* @param node_package | ||
*/ | ||
lookupInstallScript(node_package: string): Promise<string | undefined>; | ||
} |
@@ -33,2 +33,3 @@ "use strict"; | ||
const path = __importStar(require("path")); | ||
const kolorist_1 = require("kolorist"); | ||
const loglevel_1 = __importDefault(require("loglevel")); | ||
@@ -41,4 +42,4 @@ const ModuleCommand_1 = require("./ModuleCommand"); | ||
super(name); | ||
this.description('upload Cyk modules to a Cyk server from a Node package downloaded by "npm install" ') | ||
.argument('<node_package>', 'Node Package installed as dependency') | ||
this.description('npm install and upload xml modules to Cyk server') | ||
.argument('<node_package>', 'Node Package installed as dev dependency') | ||
.action(async (node_package, options) => { | ||
@@ -53,11 +54,41 @@ await this.install(node_package, options); | ||
throw 'dbManager undefined'; | ||
await this.verifyPackageJson(node_package); | ||
const xmlFolder = path.join('node_modules', node_package, 'xml'); | ||
if (!fs.existsSync(xmlFolder) || !fs.statSync(xmlFolder).isDirectory()) { | ||
throw 'xml folder not found ( ' + xmlFolder + ' )'; | ||
/** | ||
* install the Node Package | ||
*/ | ||
await this.installNodePackage(node_package); | ||
const installScript = await this.lookupInstallScript(node_package); | ||
if (installScript) { | ||
/** | ||
* execute installScript | ||
*/ | ||
const installPath = path.join('node_modules', node_package, 'scripts', installScript); | ||
await this.runShellCommand(installPath, []); | ||
} | ||
const files = fs.readdirSync(xmlFolder); | ||
for (let ind = 0; ind < files.length; ind++) { | ||
const file = files[ind]; | ||
await (0, ModuleCommand_1.uploadModule)(path.join(xmlFolder, file), this.dbManager); | ||
else { | ||
/** | ||
* default install : upload modules and execute _init.xml | ||
*/ | ||
console.log(); | ||
console.log('upload modules to server ...'); | ||
const xmlFolder = path.join('node_modules', node_package, 'xml'); | ||
if (!fs.existsSync(xmlFolder) || !fs.statSync(xmlFolder).isDirectory()) { | ||
throw 'xml folder not found ( ' + xmlFolder + ' )'; | ||
} | ||
/** | ||
* upload modules to the server | ||
*/ | ||
let initModule; | ||
const files = fs.readdirSync(xmlFolder); | ||
for (let ind = 0; ind < files.length; ind++) { | ||
const file = files[ind]; | ||
await (0, ModuleCommand_1.uploadModule)(path.join(xmlFolder, file), this.dbManager); | ||
if (file.endsWith('_init.xml')) { | ||
initModule = file; | ||
} | ||
} | ||
console.log((0, kolorist_1.green)('*') + ' upload modules to server'); | ||
if (initModule) { | ||
const initPath = path.join(xmlFolder, initModule); | ||
await this.runShellCommand('npx', ['cyk', 'run', initPath]); | ||
} | ||
} | ||
@@ -70,22 +101,38 @@ } | ||
/** | ||
* method verifyPackageJson | ||
* method installNodePackage | ||
* @param node_package | ||
*/ | ||
async verifyPackageJson(node_package) { | ||
let packageJson; | ||
async installNodePackage(node_package) { | ||
try { | ||
// Lecture du contenu du fichier package.json en tant que chaîne de caractères | ||
const packageJsonContent = fs.readFileSync('package.json', 'utf8'); | ||
// Analyse du contenu en tant qu'objet JSON | ||
packageJson = JSON.parse(packageJsonContent); | ||
await this.runShellCommand('npm', ['install', '--save-dev', node_package]); | ||
} | ||
catch (error) { | ||
console.error('Error loading package.json file :', error); | ||
console.error('Error installing ' + node_package + ' :', error); | ||
throw error; | ||
} | ||
if (!packageJson.dependencies || !packageJson.dependencies[node_package]) { | ||
throw node_package + ' not installed in the current project. Install it first with "npm install"'; | ||
} | ||
/** | ||
* method lookupInstallScript | ||
* @param node_package | ||
*/ | ||
async lookupInstallScript(node_package) { | ||
let result; | ||
try { | ||
const scriptsFolder = path.join('node_modules', node_package, 'scripts'); | ||
if (fs.existsSync(scriptsFolder) && fs.statSync(scriptsFolder).isDirectory()) { | ||
const files = fs.readdirSync(scriptsFolder); | ||
for (let ind = 0; ind < files.length; ind++) { | ||
const file = files[ind]; | ||
if (file.startsWith('install.')) { | ||
result = file; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
catch (err) { | ||
throw err; | ||
} | ||
} | ||
} | ||
exports.InstallCommand = InstallCommand; |
{ | ||
"name": "@cyklang/cli", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "cyklang CLI", | ||
@@ -25,2 +25,3 @@ "main": "build/index.js", | ||
"inquirer": "^8.2.5", | ||
"kolorist": "^1.8.0", | ||
"loglevel": "^1.8.0", | ||
@@ -27,0 +28,0 @@ "mime-types": "^2.1.35", |
@@ -7,2 +7,5 @@ import { DBManager, DBRemote, SigninResponse, Structure } from "@cyklang/core"; | ||
import { DBClient } from "./DBClient"; | ||
import { exec, spawn } from 'child_process'; | ||
import {red, green, bold} from 'kolorist' | ||
const logger = loglevel.getLogger('Cmd.ts') | ||
@@ -88,3 +91,3 @@ logger.setLevel('debug') | ||
if (! this.dbManager) throw 'prologue() has not been called before' | ||
if (!this.dbManager) throw 'prologue() has not been called before' | ||
@@ -99,2 +102,60 @@ const dbClient = new DBClient(this.dbManager) | ||
} | ||
/** | ||
* method executeCommand | ||
* @param command | ||
* @returns | ||
*/ | ||
executeCommand(command: string): Promise<{ stdout: string, stderr: string }> { | ||
return new Promise((resolve, reject) => { | ||
exec(command, (error, stdout, stderr) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
resolve({ stdout, stderr }); | ||
}); | ||
}); | ||
} | ||
/** | ||
* method runShellCommand | ||
* @param command | ||
*/ | ||
async runShellCommand(command: string, args: string[]) { | ||
try { | ||
await this.spawnCommand(command, args) | ||
// console.log(command) | ||
// const result = await this.executeCommand(command); | ||
// console.log(result.stdout); | ||
// if (result.stderr && result.stderr.trim() !== '') | ||
// console.error(result.stderr); | ||
} catch (error) { | ||
console.error('Error running "' + command + '":', error); | ||
throw error | ||
} | ||
} | ||
/** | ||
* | ||
* @param command | ||
* @param args | ||
* @returns | ||
*/ | ||
spawnCommand(command: string, args: string[]): Promise<void> { | ||
console.log() | ||
console.log([command, ...args, '...'].join(' ')) | ||
const child_process = spawn(command, args, { stdio: 'inherit' }) | ||
return new Promise((resolve, reject) => { | ||
child_process.on('close', (code) => { | ||
if (code === 0) { | ||
console.log(green('*') + ' ' + [command, ...args].join(' ')) | ||
resolve() | ||
} | ||
else { | ||
reject(new Error([command, ...args].join(' ') + ' --> Error ' + code)) | ||
} | ||
}) | ||
}) | ||
} | ||
} |
import { Cmd } from "./Cmd"; | ||
import * as fs from "fs" | ||
import * as path from "path" | ||
import {green, red, bold} from 'kolorist' | ||
@@ -13,4 +14,4 @@ import loglevel from 'loglevel' | ||
super(name) | ||
this.description('upload Cyk modules to a Cyk server from a Node package downloaded by "npm install" ') | ||
.argument('<node_package>', 'Node Package installed as dependency') | ||
this.description('npm install and upload xml modules to Cyk server') | ||
.argument('<node_package>', 'Node Package installed as dev dependency') | ||
.action(async (node_package: any, options: any) => { | ||
@@ -25,14 +26,46 @@ await this.install(node_package, options) | ||
if (this.dbManager === undefined) throw 'dbManager undefined' | ||
await this.verifyPackageJson(node_package) | ||
const xmlFolder = path.join('node_modules', node_package, 'xml') | ||
if (!fs.existsSync(xmlFolder) || !fs.statSync(xmlFolder).isDirectory()) { | ||
throw 'xml folder not found ( ' + xmlFolder + ' )' | ||
/** | ||
* install the Node Package | ||
*/ | ||
await this.installNodePackage(node_package) | ||
const installScript = await this.lookupInstallScript(node_package) | ||
if (installScript) { | ||
/** | ||
* execute installScript | ||
*/ | ||
const installPath = path.join('node_modules',node_package,'scripts',installScript) | ||
await this.runShellCommand(installPath, []) | ||
} | ||
else { | ||
/** | ||
* default install : upload modules and execute _init.xml | ||
*/ | ||
console.log() | ||
console.log('upload modules to server ...') | ||
const xmlFolder = path.join('node_modules', node_package, 'xml') | ||
if (!fs.existsSync(xmlFolder) || !fs.statSync(xmlFolder).isDirectory()) { | ||
throw 'xml folder not found ( ' + xmlFolder + ' )' | ||
} | ||
/** | ||
* upload modules to the server | ||
*/ | ||
let initModule: string | undefined | ||
const files = fs.readdirSync(xmlFolder) | ||
for (let ind = 0; ind < files.length; ind++) { | ||
const file = files[ind] | ||
await uploadModule(path.join(xmlFolder, file), this.dbManager) | ||
if (file.endsWith('_init.xml')) { | ||
initModule = file | ||
} | ||
} | ||
console.log(green('*') + ' upload modules to server' ) | ||
if (initModule) { | ||
const initPath = path.join(xmlFolder, initModule) | ||
await this.runShellCommand('npx', ['cyk', 'run', initPath]) | ||
} | ||
} | ||
const files = fs.readdirSync(xmlFolder) | ||
for (let ind = 0; ind < files.length; ind++) { | ||
const file = files[ind] | ||
await uploadModule(path.join(xmlFolder, file), this.dbManager) | ||
} | ||
} | ||
@@ -45,30 +78,39 @@ catch (err) { | ||
/** | ||
* method verifyPackageJson | ||
* method installNodePackage | ||
* @param node_package | ||
*/ | ||
async verifyPackageJson(node_package: string) { | ||
async installNodePackage(node_package: string) { | ||
let packageJson: any | ||
try { | ||
// Lecture du contenu du fichier package.json en tant que chaîne de caractères | ||
const packageJsonContent = fs.readFileSync('package.json', 'utf8'); | ||
// Analyse du contenu en tant qu'objet JSON | ||
packageJson = JSON.parse(packageJsonContent); | ||
await this.runShellCommand('npm', ['install', '--save-dev', node_package]) | ||
} catch (error) { | ||
console.error('Error loading package.json file :', error); | ||
console.error('Error installing ' + node_package + ' :', error); | ||
throw error | ||
} | ||
} | ||
if (!packageJson.dependencies || !packageJson.dependencies[node_package]) { | ||
throw node_package + ' not installed in the current project. Install it first with "npm install"' | ||
/** | ||
* method lookupInstallScript | ||
* @param node_package | ||
*/ | ||
async lookupInstallScript(node_package: string): Promise<string | undefined> { | ||
let result: string | undefined | ||
try { | ||
const scriptsFolder = path.join('node_modules', node_package, 'scripts') | ||
if (fs.existsSync(scriptsFolder) && fs.statSync(scriptsFolder).isDirectory()) { | ||
const files = fs.readdirSync(scriptsFolder) | ||
for (let ind = 0; ind < files.length; ind++) { | ||
const file = files[ind] | ||
if (file.startsWith('install.')) { | ||
result = file | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
catch (err) { | ||
throw err | ||
} | ||
} | ||
} |
384135
5860
10
3
+ Addedkolorist@^1.8.0
+ Addedkolorist@1.8.0(transitive)