Skip to content

Commit

Permalink
replicate create-vite behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
vaaski committed Jul 19, 2024
1 parent 4ad843d commit 7093acd
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 372 deletions.
175 changes: 3 additions & 172 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,175 +1,6 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

.DS_Store
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
dist

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
templates/vite
8 changes: 8 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@
"rules": {
"recommended": true
}
},
"formatter": {
"indentStyle": "tab"
},
"javascript": {
"formatter": {
"semicolons": "asNeeded"
}
}
}
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
"description": "Quickly create a local development environment for your TypeScript project.",
"author": "vaaski <[email protected]>",
"license": "MIT",
"files": [
"dist",
"templates"
],
"files": ["dist", "templates"],
"scripts": {
"start": "bun run src/index.ts",
"dev": "bun run --watch src/index.ts",
"build": "bun run scripts/build.ts",
"test": "echo no tests yet"
"test": "echo no tests yet",
"format": "biome format --write src scripts"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
Expand Down
16 changes: 8 additions & 8 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { platform } from "node:os"
const BANNER = "#!/usr/bin/env bun\n"

try {
await unlink("./dist/index.js")
await unlink("./dist/index.js")
} catch {}

const result = await Bun.build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
minify: true,
target: "bun",
entrypoints: ["./src/index.ts"],
outdir: "./dist",
minify: true,
target: "bun",
})

if (!result.success) {
console.log(result.logs)
process.exit(1)
console.log(result.logs)
process.exit(1)
}

const file = Bun.file("./dist/index.js")
Expand All @@ -25,5 +25,5 @@ const output = BANNER + text

await Bun.write("./dist/index.js", output)
if (platform() !== "win32") {
Bun.spawn(["chmod", "+x", "./dist/index.js"])
Bun.spawn(["chmod", "+x", "./dist/index.js"])
}
43 changes: 43 additions & 0 deletions scripts/vite-templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { mkdir, readdir, rename, rm } from "node:fs/promises"
import { dirname, join } from "node:path"
import { fileURLToPath } from "node:url"

import { spawnerInstance } from "../src/util"

const __dirname = dirname(fileURLToPath(import.meta.url))

const OUTPUT_FOLDER = join(__dirname, "../templates/vite")
const tempFolder = join(__dirname, "../.temp")

await rm(tempFolder, { recursive: true, force: true })
await mkdir(tempFolder, { recursive: true })

const spawner = spawnerInstance({
cwd: tempFolder,
stdio: ["inherit", "inherit", "inherit"],
})

await spawner(["bunx", "degit", "vitejs/vite/packages/create-vite"]).exited

const content = await readdir(tempFolder)
const nonTemplates = content.filter((file) => !file.startsWith("template-"))
const templates = content.filter((file) => file.startsWith("template-"))

for (const item of nonTemplates) {
const path = join(tempFolder, item)
await rm(path, { recursive: true, force: true })
}

await rm(OUTPUT_FOLDER, { recursive: true, force: true })
await mkdir(OUTPUT_FOLDER, { recursive: true })

for (const item of templates) {
const path = join(tempFolder, item)
const newName = item.replace("template-", "")
const newPath = join(OUTPUT_FOLDER, newName)

console.log({ path, item, newName, newPath })
await rename(path, newPath)
}

await rm(tempFolder, { recursive: true, force: true })
67 changes: 35 additions & 32 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
import { blue, bold, gray, magenta, yellow } from "kolorist"

export type TemplateBuiltin = {
name: string
folder: string
color: (str: string | number) => string
editorEntry: string
entry: string[]
alias?: string
name: string
folder: string
color: (str: string | number) => string
editorEntry: string
entry: string[]
alias?: string
}
export type TemplateVite = Omit<TemplateBuiltin, "folder" | "entry" | "editorEntry"> & {
vite: true
export type TemplateVite = Omit<
TemplateBuiltin,
"folder" | "entry" | "editorEntry"
> & {
vite: true
}

export type Template = TemplateBuiltin | TemplateVite

export const templates: Template[] = [
{
name: "typescript",
alias: "ts",
folder: "typescript",
color: blue,
editorEntry: "index.ts",
entry: ["bun", "run", "--watch", "index.ts"],
},
{
name: "javascript",
alias: "js",
folder: "javascript",
color: yellow,
editorEntry: "index.js",
entry: ["bun", "run", "--watch", "index.js"],
},
{
name: "vite",
alias: "v",
color: magenta,
vite: true,
},
{
name: "TypeScript",
alias: "ts",
folder: "typescript",
color: blue,
editorEntry: "index.ts",
entry: ["bun", "run", "--watch", "index.ts"],
},
{
name: "JavaScript",
alias: "js",
folder: "javascript",
color: yellow,
editorEntry: "index.js",
entry: ["bun", "run", "--watch", "index.js"],
},
{
name: "vite",
alias: "v",
color: magenta,
vite: true,
},
]

export const helpMessage = `
Expand All @@ -52,5 +55,5 @@ ${gray("Deletion options, pick either or get prompted:")}
Available templates:
${templates
.map((t) => ` - ${t.color(t.name)}${t.alias ? `/${t.color(t.alias)}` : ""}`)
.join("\n")}`
.map((t) => ` - ${t.color(t.name)}${t.alias ? `/${t.color(t.alias)}` : ""}`)
.join("\n")}`
Loading

0 comments on commit 7093acd

Please sign in to comment.