Skip to content

Commit

Permalink
Use bun shell over shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
W2Wizard committed May 11, 2024
1 parent dfc1bfb commit 2def8b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
62 changes: 38 additions & 24 deletions scripts/new.sh → scripts/new.ts
Original file line number Diff line number Diff line change
@@ -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 <project_name>"
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";
//=============================================================================
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 <project_name>");
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.");

0 comments on commit 2def8b4

Please sign in to comment.