From 7de52e9c9d68018eb0a3ff915c6a6ec2c3cf30de Mon Sep 17 00:00:00 2001 From: Minh-Phuc Tran <25026967+phuctm97@users.noreply.github.com> Date: Tue, 2 Aug 2022 17:06:31 +0800 Subject: [PATCH] fix: add option to keep default dev tools (#25) --- README.md | 7 +++-- cspell.json | 2 ++ src/create.ts | 17 ++++++++--- src/index.ts | 5 +++ src/utils/format-project.ts | 61 ++++++++++++++++++++++--------------- 5 files changed, 60 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index de61c1f..099bd30 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,10 @@ yarn create notion-app --ts `create-notion-app` comes with the following options: -| Option | Description | -| -------------------------- | ---------------------------------- | -| **-t, --ts, --typescript** | Initialize as a TypeScript project | +| Option | Description | +| -------------------------- | ------------------------------------------------------------------------- | +| **-t, --ts, --typescript** | Initialize as a TypeScript project | +| **-d, --devtool** | Use default devtools (prettier, husky, lint-staged, commitlint, & cspell) | ## Contributing diff --git a/cspell.json b/cspell.json index 3cdfcce..1bf18e6 100644 --- a/cspell.json +++ b/cspell.json @@ -5,10 +5,12 @@ "hgcheck", "hgignore", "intellij", + "minh", "mkdocs", "nbundle", "nolookalikes", "npmignore", + "phuc", "pnpm" ] } diff --git a/src/create.ts b/src/create.ts index eeea0c2..fc3208d 100644 --- a/src/create.ts +++ b/src/create.ts @@ -7,11 +7,13 @@ import { formatProject, installDependencies, tryGitInit, + toJson, } from "./utils"; import path from "path"; export interface CreateOptions { typescript?: boolean; + devtool?: boolean; } export default async function create( @@ -34,7 +36,16 @@ export default async function create( branch: "main", }); - await formatProject(projectDirectory); + await formatProject(projectDirectory, options); + + if ((await tryGitInit(projectDirectory)) && options.devtool) { + const pkgJsonPath = path.join(projectDirectory, "package.json"); + const pkg = JSON.parse(await fs.readFile(pkgJsonPath, "utf8")); + if (pkg.devDependencies.husky) { + pkg.scripts.prepare = "husky install"; + await fs.writeFile(pkgJsonPath, toJson(pkg), "utf8"); + } + } console.log("Installing packages. This might take up to a few minutes."); @@ -42,10 +53,6 @@ export default async function create( console.log(); - if (await tryGitInit(projectDirectory)) { - console.log("Initialized a git repository.\n"); - } - const packageManager = "yarn"; const useYarn = packageManager === "yarn"; diff --git a/src/index.ts b/src/index.ts index 69ed77c..0ca1408 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,6 +52,10 @@ async function run() { "initialize as a TypeScript project" ).hideHelp() ) + .option( + "-d, --devtool", + "use default devtools (prettier, husky, lint-staged, commitlint, & cspell)" + ) .action(async (optionalProjectDirectory, opts) => { let projectDirectory = optionalProjectDirectory; if (!projectDirectory) { @@ -68,6 +72,7 @@ async function run() { } await create(path.resolve(projectDirectory), { typescript: opts.ts || opts.typescript, + devtool: opts.devtool, }); }); await program.parseAsync(); diff --git a/src/utils/format-project.ts b/src/utils/format-project.ts index b9042e4..a12723e 100644 --- a/src/utils/format-project.ts +++ b/src/utils/format-project.ts @@ -17,42 +17,55 @@ async function createAppName(): Promise { throw new Error("Couldn't generate app name."); } -export async function formatProject(projectDirectory: string): Promise { +export interface FormatProjectOptions { + devtool?: boolean; +} + +export async function formatProject( + projectDirectory: string, + options?: FormatProjectOptions +): Promise { await Promise.all( [ - ".github", - ".vscode", - ".husky", - "commitlint.config.js", - "cspell.json", "LICENSE", - "lint-staged.config.js", - ".prettierignore", "yarn.lock", + ...(options?.devtool + ? [] + : [ + ".github", + ".vscode", + ".husky", + "commitlint.config.js", + "cspell.json", + "lint-staged.config.js", + ".prettierignore", + ]), ].map((file) => fs.rm(path.join(projectDirectory, file), { recursive: true }) ) ); + + if (options?.devtool) { + const cspellJsonPath = path.join(projectDirectory, "cspell.json"); + const cspell = JSON.parse(await fs.readFile(cspellJsonPath, "utf8")); + cspell.words = cspell.words.filter( + (w: string) => !["minh", "phuc"].includes(w) + ); + await fs.writeFile(cspellJsonPath, toJson(cspell), "utf8"); + } + const name = path.basename(projectDirectory); const pkgJsonPath = path.join(projectDirectory, "package.json"); const pkg = JSON.parse(await fs.readFile(pkgJsonPath, "utf8")); pkg.name = await createAppName(); pkg.productName = capitalCase(name); - pkg.devDependencies = Object.fromEntries( - Object.entries(pkg.devDependencies).filter(([key]) => - key.startsWith("@nbundle/") - ) - ); if (pkg.scripts.prepare) delete pkg.scripts.prepare; - await fs.writeFile( - pkgJsonPath, - toJson({ - name: pkg.name, - version: pkg.version, - productName: pkg.productName, - description: pkg.description, - ...pkg, - }), - "utf8" - ); + if (!options?.devtool) { + pkg.devDependencies = Object.fromEntries( + Object.entries(pkg.devDependencies).filter(([key]) => + key.startsWith("@nbundle/") + ) + ); + } + await fs.writeFile(pkgJsonPath, toJson(pkg), "utf8"); }