From 546a6c52c1b55646f264905955555b6309811477 Mon Sep 17 00:00:00 2001 From: Emmanuel Lampe Date: Tue, 15 Feb 2022 01:26:01 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Initial=20project=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current state of the project with the cli tool --- .gitignore | 1 + LICENSE.md | 7 +++++ README.md | 49 +++++++++++++++++++++++++++++++ package.json | 18 ++++++++++++ src/index.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 8 ++++++ 6 files changed, 164 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 package.json create mode 100644 src/index.js create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..2a50043 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Copyright (c) 2022 Emmanuel Lampe + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d0a3a75 --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# keshi + +A simple CLI for running commands concurrently. + +Made for laravel but works with everything. + +## Installation + +`yarn global add keshi-cli` + +`npm install keshi-cli --global` + +## Usage + +For starting everything you just need to run the command `keshi`. + +Keshi will then searches for a `keshi.default.js` with tasks, if it doesn't exist, the default built in commands will be used. + +### Default tasks + +```js +export default { + tasks: { + laravel: 'php artisan serve', + vue: 'yarn dev', + }, +}; +``` + +### Running the command + +`keshi` + +## Goal + +I originally developed this tool for this purpose, so that I don't have to open multiple terminals each time when programming Laravel apps with VueJS. + +## Security + +If you discover any security related issues, please email mail@emmanuel-lampe.de instead of using the issue tracker. + +## Credits + +- [Emmanuel Lampe](https://github.com/rexlmanu) +- [All Contributors](../../contributors) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/package.json b/package.json new file mode 100644 index 0000000..cfc3bfa --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "keshi", + "version": "1.0.0", + "type": "module", + "bin": { + "keshi": "./src/index.js" + }, + "license": "MIT", + "dependencies": { + "chalk": "^5.0.0" + }, + "author": { + "name": "Emmanuel Lampe", + "email": "mail@emmanuel-lampe.de", + "url": "https://github.com/rexlManu" + }, + "description": "A simple CLI for running commands concurrently" +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..4581eb8 --- /dev/null +++ b/src/index.js @@ -0,0 +1,81 @@ +#!/usr/bin/env node + +import path from 'path'; +import fs from 'fs'; +import { spawn } from 'child_process'; +import chalk from 'chalk'; + +const cwd = process.cwd(); +const defaultConfig = { + tasks: { + laravel: 'php artisan serve', + vue: 'yarn dev', + }, +}; + +const runningTasks = []; + +let config = defaultConfig; + +// check if a windvel.config.js file exists +const configFile = path.join(cwd, 'keshi.config.js'); +if (fs.existsSync(configFile)) { + // check if on windows + const filePrefix = process.platform === 'win32' ? 'file://' : ''; + const module = (await import(`${filePrefix}${configFile}`)).default; + config = { ...defaultConfig, ...module }; +} else { + console.log(chalk.yellow('Using default config.')); + console.log( + chalk.yellow('No keshi.config.js file found in the current directory.'), + ); +} + +// check if tasks are empty +if (config.tasks.length === 0) { + console.log(chalk.red('No tasks found in keshi.config.js')); + process.exit(1); +} + +// spawn a new process for each task and add them to the runningTasks array +Object.keys(config.tasks).forEach((taskName) => { + const task = config.tasks[taskName]; + const taskProcess = spawn(task, { + shell: true, + }); + console.log(chalk.green(`Spawned task: ${taskName} (${task})`)); + const prefix = chalk.white(`[${chalk.cyan(taskName)}] `); + runningTasks.push(taskProcess); + // log the task output + taskProcess.stdout.on('data', (data) => { + // split data at newlines + const lines = data.toString().split('\n'); + lines.forEach((line) => { + console.log(prefix + line); + }); + }); + taskProcess.stderr.on('data', (data) => { + // split data at newlines + const lines = data.toString().split('\n'); + lines.forEach((line) => { + console.log(prefix + line); + }); + }); + + // when a task ends, remove it from the runningTasks array + taskProcess.on('exit', () => { + const index = runningTasks.indexOf(taskProcess); + if (index > -1) { + runningTasks.splice(index, 1); + } + }); +}); + +// when the process gets terminated, kill all running tasks +process.on('SIGINT', () => { + console.log(chalk.red('\nTerminating all running tasks...')); + runningTasks.forEach((task) => { + task.kill('SIGINT'); + }); + process.exit(1); +}); diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..6338a7d --- /dev/null +++ b/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +chalk@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.0.0.tgz#bd96c6bb8e02b96e08c0c3ee2a9d90e050c7b832" + integrity sha512-/duVOqst+luxCQRKEo4bNxinsOQtMP80ZYm7mMqzuh5PociNL0PvmHFvREJ9ueYL2TxlHjBcmLCdmocx9Vg+IQ==