瀏覽代碼

Refactor utils

Loris Leiva 1 年之前
父節點
當前提交
83b780abd2
共有 7 個文件被更改,包括 45 次插入81 次删除
  1. 14 7
      index.ts
  2. 0 43
      utils/directoryTraverse.ts
  3. 1 1
      utils/getInputs.ts
  4. 28 0
      utils/objectHelpers.ts
  5. 2 3
      utils/renderTemplates.ts
  6. 0 27
      utils/sortDependencies.ts
  7. 0 0
      utils/stringHelpers.ts

+ 14 - 7
index.ts

@@ -4,10 +4,9 @@ import chalk from "chalk";
 import * as fs from "node:fs";
 import * as path from "node:path";
 
-import { postOrderDirectoryTraverse } from "./utils/directoryTraverse";
 import { logBanner, logEnd, logStart } from "./utils/getLogs";
 import { RenderContext, getRenderContext } from "./utils/getRenderContext";
-import { renderTemplate } from "./utils/renderTemplate";
+import { renderTemplate } from "./utils/renderTemplates";
 
 init().catch((e) => {
   console.error(e);
@@ -41,11 +40,7 @@ function createOrEmptyTargetDirectory(ctx: RenderContext) {
   if (!fs.existsSync(ctx.targetDirectory)) {
     fs.mkdirSync(ctx.targetDirectory);
   } else if (ctx.shouldOverride) {
-    postOrderDirectoryTraverse(
-      ctx.targetDirectory,
-      (dir) => fs.rmdirSync(dir),
-      (file) => fs.unlinkSync(file)
-    );
+    emptyDirectory(ctx.targetDirectory);
   } else {
     const message = ctx.language.errors.cannotOverrideDirectory.replace(
       "$targetDirectory",
@@ -55,3 +50,15 @@ function createOrEmptyTargetDirectory(ctx: RenderContext) {
     process.exit(1);
   }
 }
+
+function emptyDirectory(directory: string) {
+  for (const filename of fs.readdirSync(directory)) {
+    if (filename === ".git") continue;
+    const fullpath = path.resolve(directory, filename);
+    if (fs.lstatSync(fullpath).isDirectory()) {
+      fs.rmdirSync(fullpath);
+    } else {
+      fs.unlinkSync(fullpath);
+    }
+  }
+}

+ 0 - 43
utils/directoryTraverse.ts

@@ -1,43 +0,0 @@
-import * as fs from "node:fs";
-import * as path from "node:path";
-
-export function preOrderDirectoryTraverse(
-  dir: string,
-  dirCallback: (dir: string) => void,
-  fileCallback: (file: string) => void
-) {
-  for (const filename of fs.readdirSync(dir)) {
-    if (filename === ".git") {
-      continue;
-    }
-    const fullpath = path.resolve(dir, filename);
-    if (fs.lstatSync(fullpath).isDirectory()) {
-      dirCallback(fullpath);
-      // in case the dirCallback removes the directory entirely
-      if (fs.existsSync(fullpath)) {
-        preOrderDirectoryTraverse(fullpath, dirCallback, fileCallback);
-      }
-      continue;
-    }
-    fileCallback(fullpath);
-  }
-}
-
-export function postOrderDirectoryTraverse(
-  dir: string,
-  dirCallback: (dir: string) => void,
-  fileCallback: (file: string) => void
-) {
-  for (const filename of fs.readdirSync(dir)) {
-    if (filename === ".git") {
-      continue;
-    }
-    const fullpath = path.resolve(dir, filename);
-    if (fs.lstatSync(fullpath).isDirectory()) {
-      postOrderDirectoryTraverse(fullpath, dirCallback, fileCallback);
-      dirCallback(fullpath);
-      continue;
-    }
-    fileCallback(fullpath);
-  }
-}

+ 1 - 1
utils/getInputs.ts

@@ -4,7 +4,7 @@ import { parseArgs } from "node:util";
 import prompts from "prompts";
 
 import { Language } from "./getLanguage";
-import { kebabCase } from "./strings";
+import { kebabCase } from "./stringHelpers";
 
 export const allClients = ["js", "rust"] as const;
 export type Client = (typeof allClients)[number];

+ 28 - 0
utils/deepMerge.ts → utils/objectHelpers.ts

@@ -21,3 +21,31 @@ export function deepMerge(target: object, obj: object): object {
 
   return target;
 }
+
+export function sortDependencies<T extends object = object>(packageJson: T): T {
+  const sorted = {};
+
+  const depTypes = [
+    "dependencies",
+    "devDependencies",
+    "peerDependencies",
+    "optionalDependencies",
+  ];
+
+  for (const depType of depTypes) {
+    if (packageJson[depType]) {
+      sorted[depType] = {};
+
+      Object.keys(packageJson[depType])
+        .sort()
+        .forEach((name) => {
+          sorted[depType][name] = packageJson[depType][name];
+        });
+    }
+  }
+
+  return {
+    ...packageJson,
+    ...sorted,
+  };
+}

+ 2 - 3
utils/renderTemplate.ts → utils/renderTemplates.ts

@@ -2,16 +2,15 @@ import * as fs from "node:fs";
 import * as path from "node:path";
 import nunjucks, { ConfigureOptions } from "nunjucks";
 
-import { deepMerge } from "./deepMerge";
 import { RenderContext } from "./getRenderContext";
-import { sortDependencies } from "./sortDependencies";
+import { deepMerge, sortDependencies } from "./objectHelpers";
 import {
   camelCase,
   kebabCase,
   pascalCase,
   snakeCase,
   titleCase,
-} from "./strings";
+} from "./stringHelpers";
 
 /**
  * Renders a template folder/file to the provided destination,

+ 0 - 27
utils/sortDependencies.ts

@@ -1,27 +0,0 @@
-export function sortDependencies<T extends object = object>(packageJson: T): T {
-  const sorted = {};
-
-  const depTypes = [
-    "dependencies",
-    "devDependencies",
-    "peerDependencies",
-    "optionalDependencies",
-  ];
-
-  for (const depType of depTypes) {
-    if (packageJson[depType]) {
-      sorted[depType] = {};
-
-      Object.keys(packageJson[depType])
-        .sort()
-        .forEach((name) => {
-          sorted[depType][name] = packageJson[depType][name];
-        });
-    }
-  }
-
-  return {
-    ...packageJson,
-    ...sorted,
-  };
-}

+ 0 - 0
utils/strings.ts → utils/stringHelpers.ts