diff --git a/package.json b/package.json index d975bcf..ab7fab3 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "module": "main.ts", "type": "module", "scripts": { - "new": "/bin/bash ./scripts/new.sh", + "new": "bun --bun run ./scripts/new.ts", "dev": "bun --bun run ./src/main.ts", "api-get": "bun --bun run ./scripts/openapi.ts", "build": "bun --bun build ./src/main.ts --outfile ./build/robopeer.js", diff --git a/scripts/new.sh b/scripts/new.ts similarity index 64% rename from scripts/new.sh rename to scripts/new.ts index 5c7b0bd..ead66d2 100644 --- a/scripts/new.sh +++ b/scripts/new.ts @@ -1,23 +1,19 @@ -#!/bin/bash -#=============================================================================== -# Set up a new project directory with a basic directory structure and files. - -dir="./projects/$1" -if [ -z "$1" ]; then - echo "Usage: new.sh " - exit 1 -fi -if [ -d "$dir" ]; then - echo "Project $1 already exists in $dir" - exit 1 -fi - -test_template=' +// ============================================================================ +// Copyright (C) 2024 W2Wizard +// See README in the root of the project for license details. +// ============================================================================ + +import { $ } from "bun"; + +// ============================================================================ + +const test_template=` //============================================================================= // W2Wizard, Amsterdam @ 2018-2023 // See README and LICENSE files for details. //============================================================================= +import { $ } from "bun"; import { beforeAll, describe, expect, it } from "bun:test"; //============================================================================= @@ -52,12 +48,12 @@ beforeAll(() => { describe("hello_world", () => { it("output equals", () => { const output = runWith("/bin/echo", ["Hello, world!"]); - expect(output).toBe("Hello, world!\n"); + expect(output).toBe("Hello, world!\\n"); }); }); -' +` -script_template='#!/bin/bash +const script_template=`#!/bin/bash #============================================================================== ID=$(xxd -l 16 -ps /dev/urandom | tr -d " \n") @@ -102,10 +98,28 @@ set -e gitCloneCommit build run +` + +// Check if the project name is provided. +if (process.argv.length < 3) { + console.error("Usage: new "); + process.exit(1); +} + +const project = process.argv[2]; +if (project.includes("/")) { + console.error("Invalid project name."); + process.exit(1); +} + +const { exitCode, stderr } = await $`mkdir -p ./projects/${project}`; +if (exitCode !== 0) { + console.error("Unable to create project directory."); + console.error("Reason", stderr); + process.exit(1); +} -' -mkdir -p "$dir" && cd "$dir" -echo "$script_template" > start.sh -echo "$test_template" > index.test.ts -echo "New project $1 created in $dir" -chmod +x start.sh +await $`echo "${script_template}" > ./projects/${project}/start.sh`; +await $`echo "${test_template}" > ./projects/${project}/index.test.ts`; +await $`chmod +x ./projects/${project}/start.sh` +console.log("Project created successfully.");