Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
heimdallrj committed Jun 23, 2024
0 parents commit 946bdf2
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Hydra-JS CLI

## Development

### Prerequisites

- Node.js (v14 or later)
- npm (v7 or later)

### Setup

```bash
git clone [email protected]: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.
14 changes: 14 additions & 0 deletions __tests__/cli.test.js
Original file line number Diff line number Diff line change
@@ -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.');
});
});
63 changes: 63 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]:hydra-js/cli.git"
},
"contributors": [
{
"name": "heimdallrj",
"email": "[email protected]",
"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"
}
}

0 comments on commit 946bdf2

Please sign in to comment.