Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
fix: add option to keep default dev tools (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
phuctm97 authored Aug 2, 2022
1 parent 20e19b4 commit 7de52e9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"hgcheck",
"hgignore",
"intellij",
"minh",
"mkdocs",
"nbundle",
"nolookalikes",
"npmignore",
"phuc",
"pnpm"
]
}
17 changes: 12 additions & 5 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -34,18 +36,23 @@ 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.");

await installDependencies(projectDirectory);

console.log();

if (await tryGitInit(projectDirectory)) {
console.log("Initialized a git repository.\n");
}

const packageManager = "yarn";
const useYarn = packageManager === "yarn";

Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -68,6 +72,7 @@ async function run() {
}
await create(path.resolve(projectDirectory), {
typescript: opts.ts || opts.typescript,
devtool: opts.devtool,
});
});
await program.parseAsync();
Expand Down
61 changes: 37 additions & 24 deletions src/utils/format-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,55 @@ async function createAppName(): Promise<string> {
throw new Error("Couldn't generate app name.");
}

export async function formatProject(projectDirectory: string): Promise<void> {
export interface FormatProjectOptions {
devtool?: boolean;
}

export async function formatProject(
projectDirectory: string,
options?: FormatProjectOptions
): Promise<void> {
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");
}

0 comments on commit 7de52e9

Please sign in to comment.