diff --git a/.gitignore b/.gitignore index f620e8b..6e2a675 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ yarn-error.log* # turbo .turbo tsconfig.tsbuildinfo + +# Cloudflare +wrangler.dev.toml diff --git a/package-lock.json b/package-lock.json index cd8d6f2..f9397f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,11 @@ "version": "0.0.0", "workspaces": [ "config/*", - "packages/*" + "packages/*", + "scripts" ], "devDependencies": { + "esbuild-register": "^3.3.3", "prettier": "latest", "prettier-plugin-tailwindcss": "^0.1.11", "turbo": "latest" @@ -2258,6 +2260,12 @@ "@jridgewell/sourcemap-codec": "^1.4.10" } }, + "node_modules/@ltd/j-toml": { + "version": "1.30.0", + "resolved": "https://registry.npmjs.org/@ltd/j-toml/-/j-toml-1.30.0.tgz", + "integrity": "sha512-RMylvTbZiU9B5bx+bZmPtacKobjMJOJBDxn3TPraEe4bz/gG0ZpVKCedDqLgrDYIlCSxUutoFan1IJ2EHxvo3Q==", + "dev": true + }, "node_modules/@miniflare/cache": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/@miniflare/cache/-/cache-2.4.0.tgz", @@ -2903,6 +2911,15 @@ "@types/estree": "*" } }, + "node_modules/@types/fs-extra": { + "version": "9.0.13", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.13.tgz", + "integrity": "sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", @@ -2928,6 +2945,16 @@ "integrity": "sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ==", "dev": true }, + "node_modules/@types/inquirer": { + "version": "8.2.1", + "resolved": "https://registry.npmjs.org/@types/inquirer/-/inquirer-8.2.1.tgz", + "integrity": "sha512-wKW3SKIUMmltbykg4I5JzCVzUhkuD9trD6efAmYgN2MrSntY0SMRQzEnD3mkyJ/rv9NLbTC7g3hKKE86YwEDLw==", + "dev": true, + "dependencies": { + "@types/through": "*", + "rxjs": "^7.2.0" + } + }, "node_modules/@types/json-buffer": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@types/json-buffer/-/json-buffer-3.0.0.tgz", @@ -2953,6 +2980,21 @@ "@types/node": "*" } }, + "node_modules/@types/lodash": { + "version": "4.14.182", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.182.tgz", + "integrity": "sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==", + "dev": true + }, + "node_modules/@types/lodash.kebabcase": { + "version": "4.1.7", + "resolved": "https://registry.npmjs.org/@types/lodash.kebabcase/-/lodash.kebabcase-4.1.7.tgz", + "integrity": "sha512-qzrcpK5uiADZ9OyZaegalM0b9Y3WetoBQ04RAtP3xZFGC5ul1UxmbjZ3j6suCh0BDkvgQmoMh8t5e9cVrdJYMw==", + "dev": true, + "dependencies": { + "@types/lodash": "*" + } + }, "node_modules/@types/mdast": { "version": "3.0.10", "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-3.0.10.tgz", @@ -3025,6 +3067,15 @@ "integrity": "sha512-TgfOX+mGY/NyNxJLIbDWrO9DjGoVSW9+aB8H2yy1fy32jsvxijhmyJI9fDFgvz3YP4lvJaq9DzdR/M1bOgVc9g==", "dev": true }, + "node_modules/@types/through": { + "version": "0.0.30", + "resolved": "https://registry.npmjs.org/@types/through/-/through-0.0.30.tgz", + "integrity": "sha512-FvnCJljyxhPM3gkRgWmxmDZyAQSiBQQWLI0A0VFL0K7W1oRUrPJSqNO0NvTnLkBcotdlp3lKvaT0JrnyRDkzOg==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/unist": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.6.tgz", @@ -5398,6 +5449,15 @@ "node": ">=12" } }, + "node_modules/esbuild-register": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/esbuild-register/-/esbuild-register-3.3.3.tgz", + "integrity": "sha512-eFHOkutgIMJY5gc8LUp/7c+LLlDqzNi9T6AwCZ2WKKl3HmT+5ef3ZRyPPxDOynInML0fgaC50yszPKfPnjC0NQ==", + "dev": true, + "peerDependencies": { + "esbuild": ">=0.12 <1" + } + }, "node_modules/esbuild-sunos-64": { "version": "0.14.22", "resolved": "https://registry.npmjs.org/esbuild-sunos-64/-/esbuild-sunos-64-0.14.22.tgz", @@ -8235,6 +8295,12 @@ "integrity": "sha1-gteb/zCmfEAF/9XiUVMArZyk168=", "dev": true }, + "node_modules/lodash.kebabcase": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.kebabcase/-/lodash.kebabcase-4.1.1.tgz", + "integrity": "sha512-N8XRTIMMqqDgSy4VLKPnJ/+hpGZN+PHQiJnSenYqPaVV/NCqEogTnAdZLQiGKhxX+JCs8waWq2t1XHWKOmlY8g==", + "dev": true + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", @@ -10737,6 +10803,10 @@ "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", "dev": true }, + "node_modules/project-scripts": { + "resolved": "scripts", + "link": true + }, "node_modules/promise-inflight": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/promise-inflight/-/promise-inflight-1.0.1.tgz", @@ -14260,6 +14330,24 @@ "typescript": "^4.6.4", "wrangler": "^2.0.5" } + }, + "scripts": { + "name": "project-scripts", + "version": "0.0.0", + "license": "MIT", + "devDependencies": { + "@ltd/j-toml": "^1.30.0", + "@types/fs-extra": "^9.0.13", + "@types/inquirer": "^8.2.1", + "@types/lodash.kebabcase": "^4.1.7", + "eslint": "^8.15.0", + "eslint-config-custom": "*", + "fs-extra": "^10.1.0", + "inquirer": "^8.2.4", + "lodash.kebabcase": "^4.1.1", + "tsconfig": "*", + "typescript": "^4.6.4" + } } }, "dependencies": { @@ -15839,6 +15927,12 @@ "@jridgewell/sourcemap-codec": "^1.4.10" } }, + "@ltd/j-toml": { + "version": "1.30.0", + "resolved": "https://registry.npmjs.org/@ltd/j-toml/-/j-toml-1.30.0.tgz", + "integrity": "sha512-RMylvTbZiU9B5bx+bZmPtacKobjMJOJBDxn3TPraEe4bz/gG0ZpVKCedDqLgrDYIlCSxUutoFan1IJ2EHxvo3Q==", + "dev": true + }, "@miniflare/cache": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/@miniflare/cache/-/cache-2.4.0.tgz", @@ -16340,6 +16434,15 @@ "@types/estree": "*" } }, + "@types/fs-extra": { + "version": "9.0.13", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.13.tgz", + "integrity": "sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", @@ -16365,6 +16468,16 @@ "integrity": "sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ==", "dev": true }, + "@types/inquirer": { + "version": "8.2.1", + "resolved": "https://registry.npmjs.org/@types/inquirer/-/inquirer-8.2.1.tgz", + "integrity": "sha512-wKW3SKIUMmltbykg4I5JzCVzUhkuD9trD6efAmYgN2MrSntY0SMRQzEnD3mkyJ/rv9NLbTC7g3hKKE86YwEDLw==", + "dev": true, + "requires": { + "@types/through": "*", + "rxjs": "^7.2.0" + } + }, "@types/json-buffer": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@types/json-buffer/-/json-buffer-3.0.0.tgz", @@ -16390,6 +16503,21 @@ "@types/node": "*" } }, + "@types/lodash": { + "version": "4.14.182", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.182.tgz", + "integrity": "sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==", + "dev": true + }, + "@types/lodash.kebabcase": { + "version": "4.1.7", + "resolved": "https://registry.npmjs.org/@types/lodash.kebabcase/-/lodash.kebabcase-4.1.7.tgz", + "integrity": "sha512-qzrcpK5uiADZ9OyZaegalM0b9Y3WetoBQ04RAtP3xZFGC5ul1UxmbjZ3j6suCh0BDkvgQmoMh8t5e9cVrdJYMw==", + "dev": true, + "requires": { + "@types/lodash": "*" + } + }, "@types/mdast": { "version": "3.0.10", "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-3.0.10.tgz", @@ -16462,6 +16590,15 @@ "integrity": "sha512-TgfOX+mGY/NyNxJLIbDWrO9DjGoVSW9+aB8H2yy1fy32jsvxijhmyJI9fDFgvz3YP4lvJaq9DzdR/M1bOgVc9g==", "dev": true }, + "@types/through": { + "version": "0.0.30", + "resolved": "https://registry.npmjs.org/@types/through/-/through-0.0.30.tgz", + "integrity": "sha512-FvnCJljyxhPM3gkRgWmxmDZyAQSiBQQWLI0A0VFL0K7W1oRUrPJSqNO0NvTnLkBcotdlp3lKvaT0JrnyRDkzOg==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/unist": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.6.tgz", @@ -18132,6 +18269,13 @@ "optional": true, "peer": true }, + "esbuild-register": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/esbuild-register/-/esbuild-register-3.3.3.tgz", + "integrity": "sha512-eFHOkutgIMJY5gc8LUp/7c+LLlDqzNi9T6AwCZ2WKKl3HmT+5ef3ZRyPPxDOynInML0fgaC50yszPKfPnjC0NQ==", + "dev": true, + "requires": {} + }, "esbuild-sunos-64": { "version": "0.14.22", "resolved": "https://registry.npmjs.org/esbuild-sunos-64/-/esbuild-sunos-64-0.14.22.tgz", @@ -20239,6 +20383,12 @@ "integrity": "sha1-gteb/zCmfEAF/9XiUVMArZyk168=", "dev": true }, + "lodash.kebabcase": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.kebabcase/-/lodash.kebabcase-4.1.1.tgz", + "integrity": "sha512-N8XRTIMMqqDgSy4VLKPnJ/+hpGZN+PHQiJnSenYqPaVV/NCqEogTnAdZLQiGKhxX+JCs8waWq2t1XHWKOmlY8g==", + "dev": true + }, "lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", @@ -21946,6 +22096,22 @@ "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", "dev": true }, + "project-scripts": { + "version": "file:scripts", + "requires": { + "@ltd/j-toml": "^1.30.0", + "@types/fs-extra": "^9.0.13", + "@types/inquirer": "^8.2.1", + "@types/lodash.kebabcase": "^4.1.7", + "eslint": "^8.15.0", + "eslint-config-custom": "*", + "fs-extra": "^10.1.0", + "inquirer": "^8.2.4", + "lodash.kebabcase": "^4.1.1", + "tsconfig": "*", + "typescript": "^4.6.4" + } + }, "promise-inflight": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/promise-inflight/-/promise-inflight-1.0.1.tgz", diff --git a/package.json b/package.json index e86f860..42318d1 100644 --- a/package.json +++ b/package.json @@ -4,16 +4,20 @@ "private": true, "workspaces": [ "config/*", - "packages/*" + "packages/*", + "scripts" ], "scripts": { "build": "npx --yes turbo run build", "dev": "npx --yes turbo run dev --parallel", + "format": "prettier --write \"**/*.{ts,tsx,js,jsx,md,yml}\"", "lint": "npx --yes turbo run lint", - "typecheck": "npx --yes turbo run typecheck", - "format": "prettier --write \"**/*.{ts,tsx,js,jsx,md,yml}\"" + "new:do": "node -r esbuild-register scripts/new-do.ts", + "setup": "node -r esbuild-register scripts/setup.ts", + "typecheck": "npx --yes turbo run typecheck" }, "devDependencies": { + "esbuild-register": "^3.3.3", "prettier": "latest", "prettier-plugin-tailwindcss": "^0.1.11", "turbo": "latest" diff --git a/scripts/.eslintrc.js b/scripts/.eslintrc.js new file mode 100644 index 0000000..b56159e --- /dev/null +++ b/scripts/.eslintrc.js @@ -0,0 +1,4 @@ +module.exports = { + root: true, + extends: ['custom'], +} diff --git a/scripts/package.json b/scripts/package.json new file mode 100644 index 0000000..d47c247 --- /dev/null +++ b/scripts/package.json @@ -0,0 +1,22 @@ +{ + "name": "project-scripts", + "version": "0.0.0", + "license": "MIT", + "scripts": { + "lint": "eslint .", + "typecheck": "tsc -b" + }, + "devDependencies": { + "@ltd/j-toml": "^1.30.0", + "@types/fs-extra": "^9.0.13", + "@types/inquirer": "^8.2.1", + "@types/lodash.kebabcase": "^4.1.7", + "eslint": "^8.15.0", + "eslint-config-custom": "*", + "fs-extra": "^10.1.0", + "inquirer": "^8.2.4", + "lodash.kebabcase": "^4.1.1", + "tsconfig": "*", + "typescript": "^4.6.4" + } +} diff --git a/scripts/setup.ts b/scripts/setup.ts new file mode 100644 index 0000000..ec74c00 --- /dev/null +++ b/scripts/setup.ts @@ -0,0 +1,80 @@ +import fs from 'fs-extra' +import path from 'path' +import inquirer from 'inquirer' +import { parse, stringify } from '@ltd/j-toml' + +async function run() { + let workerDir = path.resolve(process.cwd(), 'packages/worker') + let exampleWorkerConfig = path.resolve(workerDir, 'wrangler.example.toml') + let devWorkerConfig = path.resolve(workerDir, 'wrangler.dev.toml') + let prodWorkerConfig = path.resolve(workerDir, 'wrangler.toml') + + if (fs.existsSync(devWorkerConfig)) { + let answer = await inquirer.prompt<{ continue: string }>([ + { + name: 'continue', + type: 'confirm', + default: 'n', + message: + '🔴 Project is already configured. Do you want to run setup again? This will overwrite the existing config file.', + }, + ]) + if (answer.continue === 'no') { + return + } + console.error("\n🚨 Overwriting 'wrangler.dev.toml'") + } + + console.error("\n🔨 Copying 'wrangler.example.toml' to 'wrangler.dev.toml'\n") + await fs.copyFile(exampleWorkerConfig, devWorkerConfig) + + let answer = await inquirer.prompt<{ workerName: string; accountId: string }>( + [ + { + name: 'workerName', + message: 'What is the name of your worker?', + type: 'input', + default: 'my-worker', + validate: (input: string) => { + if (input.length === 0) { + return 'Please enter a name for your worker.' + } + return true + }, + }, + { + name: 'accountId', + message: + 'What is your Cloudflare account ID? (You can find this at https://dash.cloudflare.com)', + type: 'input', + validate: (input: string) => { + if (input.length === 0) { + return 'Please enter your Cloudflare account ID.' + } + return true + }, + }, + ], + ) + + console.error('\n🔨 Updating wrangler.toml') + let parsedConfig = parse(fs.readFileSync(prodWorkerConfig, 'utf8'), '1.0') + parsedConfig.name = answer.workerName + parsedConfig.account_id = answer.accountId + + console.error('🔨 Writing wrangler.toml') + await fs.writeFile( + prodWorkerConfig, + // @ts-ignore + stringify(parsedConfig, { newline: '\n' }), + ) + + console.error('\n🎉 Done!') +} + +run().catch(e => { + console.error(e) + process.exit(1) +}) + +export {} diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json new file mode 100644 index 0000000..09d6c83 --- /dev/null +++ b/scripts/tsconfig.json @@ -0,0 +1,20 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "tsconfig/base.json", + "compilerOptions": { + "target": "ES2019", + "allowJs": true, + "skipLibCheck": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "noEmit": true, + "incremental": true, + "esModuleInterop": true, + "module": "esnext", + "resolveJsonModule": true, + "isolatedModules": true, + "moduleResolution": "node" + }, + "include": ["**/*.ts"], + "exclude": ["node_modules", "new-do"] +}