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(*): script init package #54

Merged
merged 3 commits into from
Jul 10, 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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
}
],
"editor.formatOnSave": true,
"[handlebars]": {
"editor.formatOnSave": false
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"type-check": "turbo run type-check",
"check:spell": "cspell '**' --gitignore --no-progress",
"postinstall": "husky install"
"postinstall": "husky install",
"generate:package": "turbo gen package-template"
},
"dependencies": {
"@emotion/react": "^11.11.4"
Expand All @@ -22,6 +23,7 @@
"next": "14.2.4",
"prettier": "^3.2.5",
"turbo": "^2.0.4",
"@turbo/gen": "^1.12.4",
"typescript": "^5.4.5"
},
"packageManager": "pnpm@8.15.6",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

const generator = (plop: PlopTypes.NodePlopAPI): void => {
plop.setGenerator('package-template', {
description: 'package-template',
prompts: [
{
type: 'input',
name: 'packageName',
message: 'Package name:',
validate: (value: string) => {
if (/.+/.test(value)) {
return true;
}
return 'package name is required';
},
},
{
type: 'checkbox',
name: 'devDependencies',
message: 'Select the devDependencies you want to include:',
choices: [
{ name: '@sambad/sds', value: '@sambad/sds' },
{ name: '@sambad/eslint-config', value: '@sambad/eslint-config' },
{
name: '@sambad/typescript-config',
value: '@sambad/typescript-config',
},
],
},
{
type: 'list',
name: 'eslintConfigOption',
message: 'Enter eslint config option:',
choices: [
{
name: 'library',
value: 'library',
},
{
name: 'next',
value: 'next',
},
{
name: 'react-internal',
value: 'react-internal',
},
],
when: (answers) => answers.devDependencies.includes('@sambad/eslint-config'),
},
{
type: 'list',
name: 'typescriptConfigOption',
message: 'Enter typescript config option:',
choices: [
{
name: 'base',
value: 'base',
},
{
name: 'nextjs',
value: 'nextjs',
},
{
name: 'react-library',
value: 'react-library',
},
],
when: (answers) => answers.devDependencies.includes('@sambad/typescript-config'),
},
],

actions: (answer) => {
const actions: Array<PlopTypes.ActionType> = [
// package.json ์ƒ์„ฑ
{
type: 'add',
path: `{{ turbo.paths.root }}/packages/core//{{ dashCase packageName }}/package.json`,
templateFile: 'templates/package.hbs',
abortOnFail: true,
},
// src ํด๋” ์ƒ์„ฑ
{
type: 'add',
path: `{{ turbo.paths.root }}/packages/core/{{ dashCase packageName }}/src/.gitkeep`,
},
];

if (!answer || !answer.devDependencies.length) {
return actions;
}

// devDependencies ์ถ”๊ฐ€
const devDeps: Record<string, string> = {};
if (answer.devDependencies.includes('@sambad/sds')) {
devDeps['@sambad/sds'] = 'workspace:*';
}

if (answer.devDependencies.includes('@sambad/eslint-config')) {
devDeps['@sambad/eslint-config'] = 'workspace:*';
devDeps['@types/eslint'] = '^8.56.5';
devDeps['eslint'] = '^8.57.0';

actions.push({
type: 'add',
path: '{{ turbo.paths.root }}/packages/core/{{ dashCase packageName }}/.eslintrc.js',
templateFile: 'templates/eslintrc.hbs',
});
}

if (answer.devDependencies.includes('@sambad/typescript-config')) {
devDeps['@sambad/typescript-config'] = 'workspace:*';
devDeps['typescript'] = '^5.3.3';

actions.push({
type: 'add',
path: '{{ turbo.paths.root }}/packages/core/{{ dashCase packageName }}/tsconfig.json',
templateFile: 'templates/tsconfig.hbs',
});
}

actions.push({
type: 'modify',
path: '{{ turbo.paths.root }}/packages/core/{{ dashCase packageName }}/package.json',
transform: (fileContents) => {
const packageJson = JSON.parse(fileContents);
packageJson.devDependencies = {
...(packageJson.devDependencies || {}),
...devDeps,
};
return JSON.stringify(packageJson, null, 2);
},
});

return actions;
},
});
};

export default generator;
10 changes: 10 additions & 0 deletions turbo/generators/templates/eslintrc.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import("eslint").Linter.Config} */
module.exports = {
root: true,
extends: ["@sambad/eslint-config/{{ eslintConfigOption }}.js"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.lint.json",
tsconfigRootDir: __dirname,
},
};
9 changes: 9 additions & 0 deletions turbo/generators/templates/package.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@sambad/{{ dashCase packageName }}",
"version": "0.0.0",
"private": true,
"exports": {},
"script": {},
"devDependencies": {
}
}
8 changes: 8 additions & 0 deletions turbo/generators/templates/tsconfig.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@sambad/typescript-config/{{ typescriptConfigOption }}.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}