From 946bdf2929232271984d752c5cf760ae6c5ab43e Mon Sep 17 00:00:00 2001 From: thinkholic Date: Sun, 23 Jun 2024 23:49:16 +0200 Subject: [PATCH] initial commit --- .github/workflows/publish.yml | 30 ++++++++++++++ .gitignore | 16 ++++++++ LICENSE | 19 +++++++++ README.md | 74 +++++++++++++++++++++++++++++++++++ __tests__/cli.test.js | 14 +++++++ index.js | 63 +++++++++++++++++++++++++++++ package.json | 41 +++++++++++++++++++ 7 files changed, 257 insertions(+) create mode 100644 .github/workflows/publish.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 __tests__/cli.test.js create mode 100755 index.js create mode 100644 package.json diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..ee3dfad --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,30 @@ +name: Publish to npm + +on: + push: + branches: + - main + tags: + - 'v*.*.*' # Publish only on version tags + +jobs: + publish: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: '14' + cache: 'npm' + + - name: Install dependencies + run: npm install + + - name: Publish to npm + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + run: npm publish diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95057e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Node.js dependencies +node_modules +npm-debug.log +yarn-error.log +package-lock.json +yarn.lock + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# macOS files +.DS_Store diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9cf1062 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +MIT License + +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..cdf634e --- /dev/null +++ b/README.md @@ -0,0 +1,74 @@ +# Hydra-JS CLI + +## Development + +### Prerequisites + +- Node.js (v14 or later) +- npm (v7 or later) + +### Setup + +```bash +git clone git@github.com:hydra-js/cli.git hydra-cli +cd hydra-cli +npm install +``` + +### Running locally + +To run the CLI tool locally without installing it globally, use: + +```bash +node index.js init [appName] +``` + +### Running locally with `npm link` + +> This section provides instructions on how to use `npm link` for local development, enabling you to test the CLI tool as if it were installed globally. + + +To link the CLI tool locally for development and testing, use `npm link`: + +1. Run `npm link` in the project directory. This creates a symlink globally: + +```bash +npm link +``` + +2. Now you can run the `hydra` command from anywhere on your system: + +```bash +hydra init [appName] +``` + +This allows you to test changes to the CLI tool without needing to reinstall it each time you make modifications. + +### Publishing + +To publish the package to npm: + +1. Ensure you are logged in to npm: + +```bash +npm login +``` + +2. Make sure the NPM_TOKEN secret is added to your GitHub repository. + +3. Create a new version tag and push it to GitHub: + +```bash +git tag v1.0.0 +git push origin v1.0.0 +``` + +The GitHub Actions workflow will automatically publish the package to npm when a new tag is pushed. + +### Contributing + +Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas. + +### License + +This project is licensed under the MIT License. diff --git a/__tests__/cli.test.js b/__tests__/cli.test.js new file mode 100644 index 0000000..0fd6b70 --- /dev/null +++ b/__tests__/cli.test.js @@ -0,0 +1,14 @@ +const { execSync } = require('child_process'); + +describe('CLI', () => { + test('init command', () => { + // Run the init command + const output = execSync('node ../index.js init my-hydra-app').toString(); + + // Verify output + expect(output).toContain('Project my-hydra-app generated successfully.'); + expect(output).toContain('Repository cloned successfully.'); + expect(output).toContain('Subdirectory copied successfully.'); + expect(output).toContain('Cleanup completed.'); + }); +}); diff --git a/index.js b/index.js new file mode 100755 index 0000000..fb316ee --- /dev/null +++ b/index.js @@ -0,0 +1,63 @@ +#!/usr/bin/env node + +const { Command } = require('commander'); +const simpleGit = require('simple-git'); +const path = require('path'); +const fs = require('fs-extra'); + +const program = new Command(); +const git = simpleGit(); + +const pkg = require('./package.json'); + +const templateRepoUrl = 'https://github.com/hydra-js/monorepo'; +const subdirectory = 'packages/server-nodejs'; + +program.name('hydra').description(pkg.description).version(pkg.version); + +program + .command('init [appName]') + .description( + 'Initialize a Hydra App' + ) + .action(async (appName = 'my-hydra-app') => { + const tempRepoPath = path.join(process.cwd(), appName, '__tmp'); + const appPath = path.join(process.cwd(), appName); + + if (fs.existsSync(appPath)) { + console.error(`Error: Directory ${appName} already exists.`); + process.exit(1); + } + + try { + console.log(`Cloning repository from ${templateRepoUrl}...`); + await git.clone(templateRepoUrl, tempRepoPath); + console.log('Repository cloned successfully.'); + + const sourcePath = path.join(tempRepoPath, subdirectory); + + if (!fs.existsSync(sourcePath)) { + throw new Error( + `Subdirectory ${subdirectory} does not exist in the repository.` + ); + } + + console.log(`Copying ${subdirectory} to ${appName}...`); + await fs.copy(sourcePath, appPath); + console.log('Subdirectory copied successfully.'); + + // @TODO: Make necessory changes + + console.log('Cleaning up temporary files...'); + await fs.remove(tempRepoPath); + console.log('Cleanup completed.'); + + console.log(`Project ${appName} generated successfully.`); + } catch (err) { + console.error('Failed to generate project:', err); + await fs.remove(tempRepoPath); + process.exit(1); + } + }); + +program.parse(process.argv); diff --git a/package.json b/package.json new file mode 100644 index 0000000..088ed26 --- /dev/null +++ b/package.json @@ -0,0 +1,41 @@ +{ + "name": "@hydra-js/cli", + "version": "1.0.0-alpha.1", + "description": "Hydra-JS Command-line Toolkit", + "bin": { + "hydra": "./index.js" + }, + "scripts": { + "start": "node index.js", + "test": "jest" + }, + "keywords": [ + "hydra", + "hydra-js", + "cli" + ], + "repository": { + "type": "git", + "url": "git@github.com:hydra-js/cli.git" + }, + "contributors": [ + { + "name": "heimdallrj", + "email": "mailtokasun@gmail.com", + "url": "https://github.com/heimdallrj" + } + ], + "license": "MIT", + "dependencies": { + "commander": "^12.1.0", + "fs-extra": "^11.2.0", + "simple-git": "^3.24.0" + }, + "publishConfig": { + "access": "public" + }, + "devDependencies": { + "@types/jest": "^29.5.12", + "jest": "^29.7.0" + } +}