Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

Commit

Permalink
Replaced the runtime argument with CLI options
Browse files Browse the repository at this point in the history
- **Preparing for v2**: The runtime variable was replaced with CLI options
  • Loading branch information
burgil committed Oct 20, 2024
1 parent 8aabdb8 commit 6c6e4a0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 73 deletions.
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Supe Project Creator v1.7.0
# Supe Project Creator v1.7.1

A simple tool for creating simple web projects with batteries included.

Expand Down Expand Up @@ -82,13 +82,13 @@ To start using Supe Project Creator, simply run the following command:

### Create an AI Demo Project in the Current Working Directory:
```bash
deno jsr:@supeprojects/[email protected].0 --demo -n cat-dog-detector -r deno
deno jsr:@supeprojects/[email protected].1 --demo -n cat-dog-detector -r deno
cd cat-dog-detector
```

### Create a Clean Project in the Current Working Directory:
```bash
deno jsr:@supeprojects/[email protected].0 -n my-supe-project --runtime deno
deno jsr:@supeprojects/[email protected].1 -n my-supe-project --runtime deno
cd my-supe-project
```

Expand Down Expand Up @@ -139,7 +139,7 @@ This behavior is, of course, configurable in `hotreload/config.ts`, allowing you
To view the available options for Supe Project Creator, run the following command:

```bash
deno jsr:@supeprojects/[email protected].0 --help
deno jsr:@supeprojects/[email protected].1 --help
```

- `-h`, `--help`: Displays help information.
Expand Down Expand Up @@ -185,18 +185,18 @@ Utilizing SPC programmatically allows you to:
#### Programmatic Use Example

```ts
import SPC from 'jsr:@supeprojects/[email protected].0';
import SPC from 'jsr:@supeprojects/[email protected].1';

console.log("Testing SPC programmatically...");

// Display the help menu
SPC([], 'deno');
SPC([]);

// Create a new clean project named "my-example-project"
SPC(['--name', 'my-example-project'], 'deno');
SPC(['--name', 'my-example-project']);

// Create a project with specific runtime and additional options (May soon be replaced with an argument instead of a variable)
SPC(['--name', 'my-custom-project'], 'deno');
SPC(['--name', 'my-custom-project']);

// Generate a project and immediately start the development server - Not implemented yet! TODO: Implement --start script
// SPC(['--name', 'my-fast-project', '--start']);
Expand All @@ -205,7 +205,6 @@ SPC(['--name', 'my-custom-project'], 'deno');
### Programmatic Use Parameters

- `argv: string[]`: An array of command-line arguments that control the behavior of the project creation process. This includes options like `--name` for naming the project, and in the future, it might have a `--runtime` option to specify the environment (e.g., `bun`, `deno`, or `node`) or `--bun`, `--deno`, and `--node`.
- `runtime: 'bun' | 'deno' | 'node'`: The runtime environment for the project creation process. You need to specify which environment to use by passing one of these values.

For more detailed information on available options and flags, refer to the [CLI Options](#cli-options) section of the documentation.

Expand All @@ -218,17 +217,17 @@ With these commands, you can easily set up and utilize the Supe Project Creator
To add the `Supe Project Creator` package in Deno, you can import it directly without any installation if you use the `jsr:` prefix in your import:

```ts
import * as SPC from "jsr:@supeprojects/[email protected].0";
import * as SPC from "jsr:@supeprojects/[email protected].1";

// Example usage
console.log("Testing SPC in Deno...");
SPC([], 'deno'); // Shows the help menu
SPC([]); // Shows the help menu
```

Alternatively, if you prefer to add it, use the following command:

```bash
deno add jsr:@supeprojects/[email protected].0
deno add jsr:@supeprojects/[email protected].1
```

Then, you can import it like this:
Expand All @@ -238,7 +237,7 @@ import * as SPC from "@supeprojects/supe-project-creator";

// Example usage
console.log("Testing SPC in Deno...");
SPC([], 'deno'); // Shows the help menu
SPC([]); // Shows the help menu
```

#### Bun
Expand All @@ -256,7 +255,7 @@ import * as SPC from "@supeprojects/supe-project-creator";

// Example usage
console.log("Testing SPC in Bun...");
SPC([], 'bun'); // Shows the help menu
SPC([]); // Shows the help menu
```

#### Node.js
Expand All @@ -274,7 +273,7 @@ import * as SPC from "@supeprojects/supe-project-creator";

// Example usage
console.log("Testing SPC in Node...");
SPC([], 'node'); // Shows the help menu
SPC([]); // Shows the help menu
```

## License
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@supeprojects/supe-project-creator",
"version": "1.7.0",
"version": "1.7.1",
"license": "MIT",
"exports": "./index.ts",
"tasks": {
Expand Down
61 changes: 6 additions & 55 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ const CLI_COMMENT: string = "Hello World!"; // Keep this here to avoid falsely d
* ```
*
* @param {string[]} argv - Command line arguments provided by the user. These control the behavior of the project creation.
* @param {'bun' | 'deno' | 'node'} runtime - The runtime environment for the project creation process.
* @returns {void}
*/
export default function SupeProjectCreator(argv: string[], runtime: 'bun' | 'deno' | 'node'): void {
export default function SupeProjectCreator(argv: string[]): void {
// Variables:
let CleanProject = true;
let projectName = '';
const supeVersion = '1.7.0';
const supeVersion = '1.7.1';
const supeVersionDate = '2024-10-16';
let runtime = '';
runtime = 'deno'; // WIP
if (argv.length === 0) argv.push('--help');

if (runtime === 'node') {
Expand Down Expand Up @@ -931,62 +932,12 @@ const timer = setInterval(() => {
console.log('\x1b[36m%s\x1b[0m', ` ${runtime === 'bun' ? 'bun start' : ''}${runtime === 'deno' ? 'deno run start' : ''}${runtime === 'node' ? 'npm start' : ''}`);
}

// Function to render options
function renderOptions(options: string[], currentIndex: number) {
console.clear();
console.log('Use arrow keys to navigate, press Enter to select\n');
options.forEach((option, index) => {
if (index === currentIndex) {
console.log(`\x1b[36m> ${capitalizeFirstChar(option)}\x1b[0m`); // Highlight current option
} else {
console.log(` ${capitalizeFirstChar(option)}`);
}
});
}

// Function to handle keypress events
function handleKeyPress(key: string, options: ('bun' | 'deno' | 'node')[], currentIndex: number): number {
let newCurrentIndex = currentIndex;
if (key === '\u001B\u005B\u0041') { // Up arrow
newCurrentIndex = newCurrentIndex > 0 ? newCurrentIndex - 1 : options.length - 1;
} else if (key === '\u001B\u005B\u0042') { // Down arrow
newCurrentIndex = newCurrentIndex < options.length - 1 ? newCurrentIndex + 1 : 0;
} else if (key === '\r') { // Enter key
console.clear();
console.log(`You selected: ${options[newCurrentIndex]}`);
const argv: string[] = process.argv.slice(2); // Parse command line arguments
SupeProjectCreator(argv, options[newCurrentIndex]);
process.exit();
} else if (key === '\u0003') { // Ctrl+C to exit
process.exit();
}
renderOptions(options, newCurrentIndex);
return newCurrentIndex;
}

function capitalizeFirstChar(str: string) {
if (!str) return str;
return str.charAt(0).toUpperCase() + str.slice(1);
}

// P/CLI - A half package half command line interface hybrid with cross-runtime support
const DEBUG = true;
if (import.meta.main) {
if (DEBUG) console.log("Running from CLI");
// TODO: Remove selection and replace with CLI arguments
// Enable raw mode to capture input
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
// Options to navigate
const options: ('bun' | 'deno' | 'node')[] = ['bun', 'deno', 'node'];
let currentIndex = 0;
// Initial rendering of the options
renderOptions(options, currentIndex);
// Capture and handle keypresses
process.stdin.on('data', (key: string) => {
currentIndex = handleKeyPress(key, options, currentIndex);
});
const argv: string[] = process.argv.slice(2); // Parse command line arguments
SupeProjectCreator(argv);
} else {
if (DEBUG) console.log("Imported as a module");
}
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@supeprojects/supe-project-creator",
"version": "1.7.0",
"version": "1.7.1",
"exports": "./index.ts",
"license": "MIT"
}
2 changes: 1 addition & 1 deletion spc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import SPC from './index.ts';

console.log("Testing SPC programmatically...");

SPC(['-n', 'yo'], 'deno');
SPC(['-n', 'example-project']);

0 comments on commit 6c6e4a0

Please sign in to comment.