Skip to content

Commit

Permalink
feat(create-mud): simplify without create-create-app
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jan 21, 2025
1 parent 567c2ae commit 7c202c8
Show file tree
Hide file tree
Showing 13 changed files with 1,205 additions and 466 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ You can see MUD's features, both those available and those still in development,
## Quickstart

```
pnpm create mud my-project
pnpm create mud@latest
```

Then follow the directions onscreen.
Expand Down
1 change: 1 addition & 0 deletions packages/create-mud/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public/

# End of https://www.gitignore.io/api/node

templates

# Created by `pnpm test`
test-project
6 changes: 2 additions & 4 deletions packages/create-mud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

Create a new MUD project

> created by [create-create-app](https://github.com/uetchy/create-create-app).
## Usage

```bash
npm create mud <name>
```
pnpm create mud@latest
```
4 changes: 4 additions & 0 deletions packages/create-mud/bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

// workaround for https://github.com/pnpm/pnpm/issues/1801
import "../dist/cli.js";
16 changes: 11 additions & 5 deletions packages/create-mud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
"description": "Create a new MUD project",
"license": "MIT",
"author": "Lattice <[email protected]>",
"bin": "dist/cli.js",
"type": "module",
"bin": "bin/cli.js",
"files": [
"dist"
"bin",
"dist",
"templates"
],
"scripts": {
"build": "pnpm run build:js",
"build:js": "tsup && tsx ./scripts/copy-templates.ts",
"build:js": "pnpm clean && tsup && pnpm run copy-templates",
"clean": "pnpm run clean:js",
"clean:js": "shx rm -rf dist",
"copy-templates": "tsx ./scripts/copy-templates.ts",
"dev": "tsup --watch",
"prepublishOnly": "npm run clean && npm run build",
"test": "pnpm run test:vanilla && pnpm run test:react && pnpm run test:react-ecs && pnpm run test:phaser && pnpm run test:threejs",
"test": "pnpm clean && pnpm run copy-templates && pnpm vitest && pnpm run test:vanilla && pnpm run test:react && pnpm run test:react-ecs && pnpm run test:phaser && pnpm run test:threejs",
"test:ci": "pnpm run test",
"test:phaser": "dist/cli.js test-project --template phaser && shx rm -rf test-project",
"test:react": "dist/cli.js test-project --template react && shx rm -rf test-project",
Expand All @@ -24,9 +28,11 @@
"test:vanilla": "dist/cli.js test-project --template vanilla && shx rm -rf test-project"
},
"dependencies": {
"create-create-app": "git+https://github.com/holic/create-create-app#74376c59b48a04aabbe94d9cacfe9cb1cecccd63"
"fast-glob": "^3.3.3",
"yargs-interactive": "^3.0.1"
},
"devDependencies": {
"@types/yargs-interactive": "^2.1.6",
"execa": "^7.0.0"
},
"publishConfig": {
Expand Down
17 changes: 8 additions & 9 deletions packages/create-mud/scripts/copy-templates.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fs from "node:fs/promises";
import path from "node:path";
import { execa } from "execa";
import { glob } from "glob";
import glob from "fast-glob";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

(async () => {
const packageDir = path.resolve(__dirname, "..");
Expand All @@ -19,18 +23,13 @@ import { glob } from "glob";

for (const file of files) {
const sourcePath = path.resolve(rootDir, file);
const destPath = path.resolve(
packageDir,
"dist",
// Rename `.gitignore` to `gitignore`, so that create-create-app can copy it properly.
file.replace(/\.gitignore$/, "gitignore"),
);
const destPath = path.resolve(packageDir, file);

await fs.mkdir(path.dirname(destPath), { recursive: true });

// Replace all MUD package links with mustache placeholder used by create-create-app
// that will be replaced with the latest MUD version number when the template is used.
if (/package.json$/.test(destPath)) {
if (/package\.json$/.test(destPath)) {
const source = await fs.readFile(sourcePath, "utf-8");
await fs.writeFile(
destPath,
Expand All @@ -41,7 +40,7 @@ import { glob } from "glob";
}
// Replace template workspace root `tsconfig.json` files (which have paths relative to monorepo)
// with one that inherits our base tsconfig.
else if (/templates\/[^/]+\/tsconfig.json/.test(destPath)) {
else if (/templates\/[^/]+\/tsconfig\.json/.test(destPath)) {
await fs.copyFile(path.join(__dirname, "tsconfig.base.json"), destPath);
} else {
await fs.copyFile(sourcePath, destPath);
Expand Down
62 changes: 62 additions & 0 deletions packages/create-mud/src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import yargsInteractive from "yargs-interactive";
import glob from "fast-glob";
import packageJson from "../../package.json";
import { templates } from "../common";
import { exists } from "../exists";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function run() {
yargsInteractive()
.usage("$0 [args]")
.interactive({
interactive: { default: true },
name: {
describe: "Name your project",
type: "input",
},
template: {
describe: "Pick a template",
type: "list",
choices: templates,
},
"mud-version": {
type: "input",
describe: "The version of MUD packages to use, defaults to latest",
default: packageJson.version,
},
})
.then(async (args) => {
if (!args.name) throw new Error("No project name provided.");

const targetDir = path.join(process.cwd(), args.name);
if (await exists(targetDir)) {
throw new Error(`Target directory "${targetDir}" already exists.`);
}

const sourceDir = path.join(__dirname, "..", "templates", args.template);
const files = await glob("**/*", { cwd: sourceDir, dot: true });

for (const filename of files) {
const sourceFile = path.join(sourceDir, filename);
const targetFile = path.join(targetDir, filename);

await fs.mkdir(path.dirname(targetFile), { recursive: true });

if (/package\.json$/.test(sourceFile)) {
const source = await fs.readFile(sourceFile, "utf-8");
await fs.writeFile(targetFile, source.replaceAll(/{{mud-version}}/g, args.mudVersion), "utf-8");
} else {
await fs.copyFile(sourceFile, targetFile);
}
}

console.log(`\nDone! Play in the MUD with \`cd ${args.name}\` and \`pnpm run dev\`\n`);
});
}

run();
29 changes: 0 additions & 29 deletions packages/create-mud/src/cli.ts

This file was deleted.

14 changes: 14 additions & 0 deletions packages/create-mud/src/common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { glob } from "glob";
import { templates } from "./common";

describe("templates", () => {
it("matches what is in the file system", async () => {
const availableTemplates = await glob("*", {
maxDepth: 1,
cwd: `${__dirname}/../dist/templates`,
});
expect(templates).toEqual(expect.arrayContaining(availableTemplates));
expect(availableTemplates).toEqual(expect.arrayContaining(templates));
});
});
3 changes: 3 additions & 0 deletions packages/create-mud/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// We define these here to keep them in the order we want to present in the CLI.
// Tests will ensure this list stays up to date.
export const templates = ["react", "react-ecs", "phaser", "threejs", "vanilla"];
10 changes: 10 additions & 0 deletions packages/create-mud/src/exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fs from "node:fs/promises";

export async function exists(path: string) {
try {
await fs.access(path);
return true;
} catch {
return false;
}
}
6 changes: 4 additions & 2 deletions packages/create-mud/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { defineConfig } from "tsup";

export default defineConfig((opts) => ({
entry: ["src/cli.ts"],
entry: ["src/bin/cli.ts"],
target: "esnext",
format: ["esm"],
minify: true,
sourcemap: true,
// don't generate DTS during watch mode because it's slow
Expand All @@ -10,5 +12,5 @@ export default defineConfig((opts) => ({
// don't clean during watch mode to avoid removing
// previously-built DTS files, which other build tasks
// depend on
clean: !opts.watch,
clean: false,
}));
Loading

0 comments on commit 7c202c8

Please sign in to comment.