Skip to content

Commit

Permalink
Refactor create-webstone-app (#388)
Browse files Browse the repository at this point in the history
* refactor: move build to tsup

* feat: new template

* refactor: remove listr2 and execa

* feat: init new script

* refactor: move plugin structure

* feat: add new entry

* refactor: further adjustments

* chore: add changeset

* fix: small fixes

* feat: init copy function

* feat: use create-svelte

* feat: update dev deps

* fix: deepmerge

* refactor: remove plugin template

* feat: copy files

* fix: last updates for package.json

* refactor: conditionally adjust plugin

* refactor: remove unusded code

* chore: package for distribution

* feat: parse args

* refactor: remove command-line-args

* Update packages/create-webstone-app/templates/plugin-structure/build-cli.js

Co-authored-by: Mike Nikles <[email protected]>

* test: add tests

* test: add new tests

* test: remove old tests

* test: add test

---------

Co-authored-by: Mike Nikles <[email protected]>
  • Loading branch information
Cahllagerfeld and mikenikles authored Aug 21, 2023
1 parent ec0ea0e commit 01f44e5
Show file tree
Hide file tree
Showing 52 changed files with 818 additions and 1,648 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-onions-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-webstone-app": minor
---

refactor create-webstone-app to use new flow & new plugin template
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"terminal.integrated.profiles.osx": {
"devboxCompatibleShell": {
"path": "/bin/zsh",
"args": []
}
},
"terminal.integrated.defaultProfile.osx": "devboxCompatibleShell"
}
2 changes: 1 addition & 1 deletion devbox.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"packages": ["nodejs@latest", "nodePackages.pnpm@latest"],
"packages": ["nodejs@latest", "nodePackages_latest.pnpm@latest"],
"shell": {
"init_hook": ["sh ./.config/devbox/init.sh"],
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions devbox.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"lockfile_version": "1",
"packages": {
"nodePackages.pnpm@latest": {
"last_modified": "2023-07-23T03:35:12Z",
"resolved": "github:NixOS/nixpkgs/af8cd5ded7735ca1df1a1174864daab75feeb64a#nodePackages.pnpm",
"nodePackages_latest.pnpm@latest": {
"last_modified": "2023-08-08T03:07:33Z",
"resolved": "github:NixOS/nixpkgs/844ffa82bbe2a2779c86ab3a72ff1b4176cec467#nodePackages_latest.pnpm",
"source": "devbox-search",
"version": "8.6.9"
"version": "8.6.11"
},
"nodejs@latest": {
"last_modified": "2023-07-23T03:35:12Z",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Start your next web application with Webstone Plugins and configure it as you go.",
"private": true,
"engines": {
"node": ">=v20.5.1"
"node": ">=v20"
},
"scripts": {
"build": "pnpm --recursive --parallel build",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "The Webstone command line interface",
"types": "build/types/types.d.ts",
"engines": {
"node": ">=v20.5.1"
"node": ">=v20"
},
"bin": {
"webstone": "./bin",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-webstone-app/bin.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
import("./dist/index.js");
import("./dist/bin.js");
44 changes: 22 additions & 22 deletions packages/create-webstone-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@
"name": "create-webstone-app",
"version": "0.7.0",
"description": "Start your next web application with Webstone and configure it as you go.",
"keywords": [
"svelte",
"sveltekit",
"boilerplate",
"starterkit",
"web app",
"graphql"
],
"license": "MIT",
"author": "Mike Nikles, @mikenikles",
"type": "module",
"exports": {
".": "./dist/index.js"
},
"types": "./dist/index.d.ts",
"bin": "./bin.js",
"scripts": {
"build": "node ./scripts/build.js build",
"clean": "rm -fr ./dist",
"dev": "node ./scripts/build.js dev",
"prepublishOnly": "pnpm build",
"prepare": "pnpm build",
"test": "pnpm test:unit",
"prepublishOnly": "pnpm build",
"test": "node --loader tsx --test src/*.spec.ts",
"test:unit": "c8 --all --include=src --reporter=html pnpm test:unit:only",
"test:unit:only": "NODE_OPTIONS='--loader tsx' uvu tests"
},
"keywords": [
"svelte",
"sveltekit",
"boilerplate",
"starterkit",
"web app",
"graphql"
],
"license": "MIT",
"devDependencies": {
"@types/command-line-args": "^5.2.0",
"@types/node": "20.4.10",
"esbuild": "^0.19.1",
"esbuild-node-externals": "^1.8.0",
"typescript": "^5.1.6"
},
"dependencies": {
"chalk": "5.3.0",
"command-line-args": "^5.2.1",
"create-svelte": "^5.0.5",
"enquirer": "2.4.1",
"execa": "7.2.0",
"fs-extra": "11.1.1",
"json5": "^2.2.3",
"listr2": "6.6.1"
"ts-deepmerge": "^6.2.0"
},
"devDependencies": {
"@types/node": "20.4.10",
"tsup": "^6.7.0",
"type-fest": "^4.2.0",
"typescript": "^5.1.6"
}
}
49 changes: 17 additions & 32 deletions packages/create-webstone-app/scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
import esbuild from "esbuild";
import { nodeExternalsPlugin } from "esbuild-node-externals";

/**
* @type {Object.<string, esbuild.BuildOptions>}
*/
const config = {
format: "esm",
target: "esnext",
plugins: [nodeExternalsPlugin()],
banner: {
js: "#!/usr/bin/env node",
},
bundle: true,
entryPoints: ["./src/index.ts"],
logLevel: "info",
minify: true,
outfile: "./dist/index.js",
platform: "node",
};
import { defineConfig, build } from "tsup";

/**
* @type {"build" | "dev"}
*/

const mode = process.argv[2];
if (!mode) {
console.error("Usage: node ./scripts/esbuild.js build|dev");

// check if mode is valid
if (!["build", "dev"].includes(mode)) {
console.log("Usage: node ./scripts/build.js build|dev");
process.exit(1);
}

switch (mode) {
case "build":
await esbuild.build(config).catch(() => process.exit(1));
break;
case "dev": {
const context = await esbuild.context(config).catch(() => process.exit(1));
await context.watch();
break;
}
}
const config = defineConfig({
entry: ["src/index.ts", "src/bin.ts"],
target: "esnext",
format: "esm",
treeshake: true,
minify: true,
dts: true,
watch: mode === "dev" ? ["src"] : false,
});

await build(config);
99 changes: 99 additions & 0 deletions packages/create-webstone-app/src/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import fs from "fs-extra";
import chalk from "chalk";
import enquirer from "enquirer";
import { displayNextSteps, displayWelcome } from "./helpers";
import { createWebstone } from "./index";
import { parseArgs } from "node:util";

// argparsing
const { values: argValues } = parseArgs({
allowPositionals: true,
options: {
type: {
type: "string",
alias: "t",
},
"extend-cli": {
type: "boolean",
},
},
});

let extendCLI = argValues["extend-cli"] || false;
let type: "app" | "plugin" | null = null;
if (argValues.type && ["app", "plugin"].includes(argValues.type)) {
type = argValues.type as "app" | "plugin";
}

const { version } = JSON.parse(
fs.readFileSync(new URL("../package.json", import.meta.url), "utf-8"),
);

let cwd =
process.argv[2] && !process.argv[2].startsWith("--") ? process.argv[2] : ".";

displayWelcome();
console.log(chalk.bold(`create-webstone v${version}`));

if (cwd === ".") {
const dir: { dir: string } = await enquirer.prompt({
type: "text",
name: "dir",
message:
"Where should we create your project? (Hit enter to use current directory)",
initial: ".",
});

cwd = dir.dir;
}

if (fs.existsSync(cwd)) {
if (fs.readdirSync(cwd).length > 0) {
const forceCreate: { forceCreate: boolean } = await enquirer.prompt({
type: "confirm",
name: "forceCreate",
message: `The ./${cwd} directory is not empty. Do you want to continue?`,
initial: false,
});

if (!forceCreate.forceCreate) {
console.log(
chalk.red(
`Exiting, please empty the ./${cwd} directory or choose a different one to create the Webstone app.`,
),
);
process.exit(1);
}
}
}

if (!type) {
const promptType: { type: "Webstone App" | "Webstone Plugin" } =
await enquirer.prompt({
type: "select",
name: "type",
message: "What type of Webstone project do you want to create?",
choices: ["Webstone App", "Webstone Plugin"],
});

const typeMap = {
"Webstone App": "app",
"Webstone Plugin": "plugin",
} as const;

type = typeMap[promptType.type];
}

if (type === "plugin" && !extendCLI) {
const extendCLIAnswer: { extendCLI: boolean } = await enquirer.prompt({
type: "confirm",
name: "extendCLI",
message: "Does your plugin extend the Webstone CLI?",
initial: false,
});
extendCLI = extendCLIAnswer.extendCLI;
}

await createWebstone(cwd, { type, extendCLI });

displayNextSteps(cwd);
Loading

0 comments on commit 01f44e5

Please sign in to comment.