Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(root): add generators #820

Merged
merged 3 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pnpm-lock.yaml
*.code-snippets
**/migrations/**
**/*.hbs

# For CI
.pnpm-store/
3 changes: 3 additions & 0 deletions knip.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const config: KnipConfig = {
},
ignoreDependencies: ['prettier-plugin-*', 'sharp'],
workspaces: {
'.': {
entry: ['turbo/generators/config.ts']
},
'apps/docs': {
entry: ['mdx.config.ts', 'src/components/demos/**/*.tsx'],
postcss: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@tszhong0411/eslint-config": "workspace:*",
"@tszhong0411/prettier-config": "workspace:*",
"@tszhong0411/tsconfig": "workspace:*",
"@turbo/gen": "^2.0.11",
"@types/node": "^20.14.2",
"@vitest/coverage-v8": "^1.6.0",
"@vitest/ui": "^1.6.0",
Expand Down
892 changes: 826 additions & 66 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions turbo/generators/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type PlopTypes } from '@turbo/gen'

import { packageGenerator } from './templates/package/generator'

const generators = [packageGenerator]

const generator = (plop: PlopTypes.NodePlopAPI): void => {
for (const gen of generators) {
gen(plop)
}
}

export default generator
3 changes: 3 additions & 0 deletions turbo/generators/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
9 changes: 9 additions & 0 deletions turbo/generators/templates/package/eslint.config.mjs.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import tszhong0411 from '@tszhong0411/eslint-config'

export default tszhong0411({
project: './tsconfig.json',
tsconfigRootDir: import.meta.dirname,
{{#each enabledESLintRuleSets}}
{{this}}: true,
{{/each}}
})
135 changes: 135 additions & 0 deletions turbo/generators/templates/package/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { type PlopTypes } from '@turbo/gen'
import { execSync } from 'node:child_process'

export const packageGenerator = (plop: PlopTypes.NodePlopAPI): void => {
plop.setGenerator('package', {
description: 'Create a new package',
prompts: [
{
type: 'input',
name: 'name',
message: 'What is the name of the package (without scope)?',
validate: (input: string) => {
if (!/^[\da-z-]+$/.test(input)) {
return 'Package name can only contain lowercase letters, numbers and dashes.'
}

return true
}
},
{
type: 'input',
name: 'description',
message: 'What is the description of the package?',
validate: (input: string) => {
if (input.trim().length === 0) {
return 'Description is required.'
}

return true
}
},
{
type: 'input',
name: 'version',
message: 'What is the initial version of the package?',
default: '0.0.0',
validate: (input: string) => {
if (!/^\d+\.\d+\.\d+$/.test(input)) {
return 'Version should be in the format of x.y.z.'
}

return true
}
},
{
type: 'confirm',
name: 'shouldPublish',
message: 'Will it be published on npm registry?',
default: false
},
{
type: 'confirm',
name: 'shouldCompile',
message: 'Do you need to compile the package?',
default: false
},
{
type: 'checkbox',
name: 'enabledESLintRuleSets',
choices: ['react', 'next', 'playwright', 'testingLibrary']
}
],
actions: (answers) => {
const actions: PlopTypes.Actions = [
// Add package.json
{
type: 'add',
path: 'packages/{{ name }}/package.json',
templateFile: 'templates/package/package.json.hbs'
},
// Remove the last line break from package.json
{
type: 'modify',
path: 'packages/{{ name }}/package.json',
pattern: /\n$/,
template: ''
},
// Add ESLint configuration
{
type: 'add',
path: 'packages/{{ name }}/eslint.config.mjs',
templateFile: 'templates/package/eslint.config.mjs.hbs'
},
// Add tsconfig.json
{
type: 'add',
path: 'packages/{{ name }}/tsconfig.json',
templateFile: 'templates/package/tsconfig.json.hbs'
},
// Add index.ts
{
type: 'add',
path: 'packages/{{ name }}/src/index.ts',
template: "console.log('Hello, world!')"
}
]

if (!answers?.shouldPublish) {
// Append the package to ignore list in changesets' configuration
actions.push({
type: 'append',
path: '.changeset/config.json',
pattern: / {2}"ignore": \[(?<insertion>)/g,
template: ' "@tszhong0411/{{ name }}",'
})
}

if (answers?.shouldCompile) {
// Add tsup configuration
actions.push({
type: 'add',
path: 'packages/{{ name }}/tsup.config.ts',
templateFile: 'templates/package/tsup.config.ts.hbs'
})
}

// Install dependencies and format the code
actions.push(() => {
const packageName = (answers as { name: string }).name

execSync('pnpm install', {
stdio: 'inherit'
})

execSync(`pnpm prettier --write packages/${packageName}/**`, {
stdio: 'inherit'
})

return `@tszhong0411/${packageName} is created successfully!`
})

return actions
}
})
}
58 changes: 58 additions & 0 deletions turbo/generators/templates/package/package.json.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
{{#unless shouldPublish}}
"private": true,
{{/unless}}
"name": "@tszhong0411/{{ name }}",
"version": "{{ version }}",
"description": "{{ description }}",
"license": "GPL-3.0",
{{#if shouldPublish}}
"author": "tszhong0411 <me@honghong.me> (https://github.com/tszhong0411/)",
"homepage": "https://github.com/tszhong0411/honghong.me#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/tszhong0411/honghong.me.git"
},
"bugs": {
"url": "https://github.com/tszhong0411/honghong.me/issues"
},
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
{{/if}}
"type": "module",
{{#if shouldCompile}}
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
{{else}}
"main": "./src/index.ts",
{{/if}}
"scripts": {
"clean": "rm -rf .turbo",
"lint": "eslint . --max-warnings 0",
"lint:fix": "eslint --fix .",
"type-check": "tsc --noEmit",
{{#if shouldCompile}}
"build": "tsup",
"dev": "tsup --watch"
{{/if}}
},
"devDependencies": {
"@tszhong0411/eslint-config": "workspace:*",
"@tszhong0411/tsconfig": "workspace:*"
},
"lint-staged": {
"*.{cjs,mjs,js,jsx,cts,mts,ts,tsx,json}": "eslint --fix",
"**/*": "prettier --write --ignore-unknown"
}
}
6 changes: 6 additions & 0 deletions turbo/generators/templates/package/tsconfig.json.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"exclude": ["node_modules"],
"extends": "@tszhong0411/tsconfig/base.json",
"include": ["**/*.ts"]
}
8 changes: 8 additions & 0 deletions turbo/generators/templates/package/tsup.config.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: ['src/index.ts'],
dts: true,
format: ['esm'],
target: 'esnext'
})