Skip to content

Commit

Permalink
Base project
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenes committed Jan 6, 2023
1 parent 8e48f49 commit 9bf01fa
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Lib/BaseCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class BaseCommand {
constructor(value) {
this.name = value.name;
this.description = value.description;
}

getName() {
return this.name;
}

getDescription() {
return this.description;
}
}

module.exports = BaseCommand;
65 changes: 65 additions & 0 deletions Lib/kurami.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const fs = require('fs');
const path = require('path');

class Kurami {
constructor() {
this.path = path.resolve() + '/Command'
}

getCommandsInfo() {
const commands = fs.readdirSync(this.path)

let cleanCommandsList = this.cleanCommandsList(commands)

return cleanCommandsList.map(commandFile => {
console.log(path.join(this.path, commandFile))
const command = require(path.join(this.path, commandFile))
return {
name: new command().getName(),
description: new command().getDescription(),
fileName: commandFile
}
})
}

cleanCommandsList(commands) {
let cleanCommandsList = []

commands.map(file => {
if (file !== 'BaseCommand.js') {
cleanCommandsList.push(file)
}
})

return cleanCommandsList
}

run(args) {
const commands = this.getCommandsInfo()

if (args[0] === "help" || args.length === 0) {
this.runHelp()
}

if (args.length > 0 && args[0] !== "help") {
const command = commands.find(command => command.name === args[0])

if (command) {
const commandFile = require(path.join(this.path, command.fileName))
new commandFile().run()
} else {
console.log('Command not found')
}
}
}

runHelp() {
console.log('Kurami CLI')
console.log('Available commands:')
this.getCommandsInfo().map(command => {
console.log(`- ${command.name} - ${command.description}`)
})
}
}

module.exports = new Kurami();
Empty file added bin/kurami.js
Empty file.
Empty file added index.js
Empty file.
16 changes: 16 additions & 0 deletions test/Command/TestCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { BaseCommand } = require('kurami');

class TestCommand extends BaseCommand {
constructor() {
super({
name: 'test:test',
description: 'Test command'
});
}

async run() {
console.log('Test command');
}
}

module.exports = TestCommand;
3 changes: 3 additions & 0 deletions test/kurami.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"commandsPath": "./Command"
}
40 changes: 40 additions & 0 deletions test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "test",
"version": "1.0.0",
"description": "test project for show how work the command system",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"kurami": "kurami"
},
"author": "Nenes",
"license": "UNLICENSED",
"dependencies": {
"kurami": "file:./.."
}
}

0 comments on commit 9bf01fa

Please sign in to comment.