@huaiyou/hooks-git 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @huaiyou/hooks-git
2
2
 
3
+ ## 2.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Fix init command - move package.json reading before dependency check
8
+
9
+ ## 2.1.1
10
+
11
+ ### Patch Changes
12
+
13
+ - Fix init command to install required dependencies (husky, lint-staged, commitlint)
14
+
3
15
  ## 2.1.0
4
16
 
5
17
  ### Minor Changes
package/dist/cli.cjs CHANGED
@@ -14,7 +14,7 @@ const cac__default = /*#__PURE__*/_interopDefaultCompat(cac);
14
14
  const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
15
15
  const picocolors__default = /*#__PURE__*/_interopDefaultCompat(picocolors);
16
16
 
17
- const version = "2.1.0";
17
+ const version = "2.1.2";
18
18
 
19
19
  const logger = {
20
20
  /**
@@ -55,6 +55,16 @@ const runCommand = async (command, args) => {
55
55
  throw error;
56
56
  }
57
57
  };
58
+ async function getPackageManager() {
59
+ const cwd = process.cwd();
60
+ const hasPnpmLock = fs__default.existsSync(node_path.resolve(cwd, "pnpm-lock.yaml"));
61
+ const hasYarnLock = fs__default.existsSync(node_path.resolve(cwd, "yarn.lock"));
62
+ const hasPackageLock = fs__default.existsSync(node_path.resolve(cwd, "package-lock.json"));
63
+ if (hasPnpmLock) return "pnpm";
64
+ if (hasYarnLock) return "yarn";
65
+ if (hasPackageLock) return "npm";
66
+ return "npm";
67
+ }
58
68
  const updatePackageJson = async (callback) => {
59
69
  const pkgPath = node_path.resolve(process.cwd(), "package.json");
60
70
  if (!fs__default.existsSync(pkgPath)) {
@@ -72,11 +82,31 @@ const init = async () => {
72
82
  logger.warn("Not a git repository. Initializing...");
73
83
  await runCommand("git", ["init"]);
74
84
  }
75
- logger.info("Installing Husky...");
85
+ logger.info("Installing dependencies...");
86
+ const pm = await getPackageManager();
87
+ const pkgPath = node_path.resolve(process.cwd(), "package.json");
88
+ const pkg = await fs__default.readJson(pkgPath);
89
+ const devDepsNeeded = [];
90
+ if (!pkg.devDependencies?.husky && !pkg.dependencies?.husky) {
91
+ devDepsNeeded.push("husky");
92
+ }
93
+ if (!pkg.devDependencies?.["lint-staged"] && !pkg.dependencies?.["lint-staged"]) {
94
+ devDepsNeeded.push("lint-staged");
95
+ }
96
+ if (!pkg.devDependencies?.["@commitlint/cli"] && !pkg.dependencies?.["@commitlint/cli"]) {
97
+ devDepsNeeded.push("@commitlint/cli");
98
+ }
99
+ if (!pkg.devDependencies?.["@commitlint/config-conventional"] && !pkg.dependencies?.["@commitlint/config-conventional"]) {
100
+ devDepsNeeded.push("@commitlint/config-conventional");
101
+ }
102
+ if (devDepsNeeded.length > 0) {
103
+ logger.info(`Installing devDependencies: ${devDepsNeeded.join(", ")}`);
104
+ await runCommand(pm, ["add", "-D", ...devDepsNeeded]);
105
+ }
76
106
  try {
77
- await runCommand("npx", ["husky", "install"]);
107
+ await runCommand(pm === "yarn" ? "npx" : pm, ["husky", "install"]);
78
108
  } catch (e) {
79
- logger.error("Failed to install husky. Make sure dependencies are installed.");
109
+ logger.error("Failed to initialize husky.");
80
110
  }
81
111
  logger.info("Adding hooks...");
82
112
  const huskyDir = node_path.resolve(process.cwd(), ".husky");
@@ -89,8 +119,6 @@ const init = async () => {
89
119
  `;
90
120
  await fs__default.outputFile(preCommitPath, preCommitContent, { mode: 493 });
91
121
  logger.info("Creating configuration files...");
92
- const pkgPath = node_path.resolve(process.cwd(), "package.json");
93
- const pkg = await fs__default.readJson(pkgPath);
94
122
  const isModule = pkg.type === "module";
95
123
  const isTsProject = fs__default.existsSync(node_path.resolve(process.cwd(), "tsconfig.json"));
96
124
  const commitlintFile = isTsProject ? "commitlint.config.ts" : isModule ? "commitlint.config.cjs" : "commitlint.config.js";
package/dist/cli.mjs CHANGED
@@ -6,7 +6,7 @@ import { consola } from 'consola';
6
6
  import { execa } from 'execa';
7
7
  import picocolors from 'picocolors';
8
8
 
9
- const version = "2.1.0";
9
+ const version = "2.1.2";
10
10
 
11
11
  const logger = {
12
12
  /**
@@ -47,6 +47,16 @@ const runCommand = async (command, args) => {
47
47
  throw error;
48
48
  }
49
49
  };
50
+ async function getPackageManager() {
51
+ const cwd = process.cwd();
52
+ const hasPnpmLock = fs.existsSync(resolve(cwd, "pnpm-lock.yaml"));
53
+ const hasYarnLock = fs.existsSync(resolve(cwd, "yarn.lock"));
54
+ const hasPackageLock = fs.existsSync(resolve(cwd, "package-lock.json"));
55
+ if (hasPnpmLock) return "pnpm";
56
+ if (hasYarnLock) return "yarn";
57
+ if (hasPackageLock) return "npm";
58
+ return "npm";
59
+ }
50
60
  const updatePackageJson = async (callback) => {
51
61
  const pkgPath = resolve(process.cwd(), "package.json");
52
62
  if (!fs.existsSync(pkgPath)) {
@@ -64,11 +74,31 @@ const init = async () => {
64
74
  logger.warn("Not a git repository. Initializing...");
65
75
  await runCommand("git", ["init"]);
66
76
  }
67
- logger.info("Installing Husky...");
77
+ logger.info("Installing dependencies...");
78
+ const pm = await getPackageManager();
79
+ const pkgPath = resolve(process.cwd(), "package.json");
80
+ const pkg = await fs.readJson(pkgPath);
81
+ const devDepsNeeded = [];
82
+ if (!pkg.devDependencies?.husky && !pkg.dependencies?.husky) {
83
+ devDepsNeeded.push("husky");
84
+ }
85
+ if (!pkg.devDependencies?.["lint-staged"] && !pkg.dependencies?.["lint-staged"]) {
86
+ devDepsNeeded.push("lint-staged");
87
+ }
88
+ if (!pkg.devDependencies?.["@commitlint/cli"] && !pkg.dependencies?.["@commitlint/cli"]) {
89
+ devDepsNeeded.push("@commitlint/cli");
90
+ }
91
+ if (!pkg.devDependencies?.["@commitlint/config-conventional"] && !pkg.dependencies?.["@commitlint/config-conventional"]) {
92
+ devDepsNeeded.push("@commitlint/config-conventional");
93
+ }
94
+ if (devDepsNeeded.length > 0) {
95
+ logger.info(`Installing devDependencies: ${devDepsNeeded.join(", ")}`);
96
+ await runCommand(pm, ["add", "-D", ...devDepsNeeded]);
97
+ }
68
98
  try {
69
- await runCommand("npx", ["husky", "install"]);
99
+ await runCommand(pm === "yarn" ? "npx" : pm, ["husky", "install"]);
70
100
  } catch (e) {
71
- logger.error("Failed to install husky. Make sure dependencies are installed.");
101
+ logger.error("Failed to initialize husky.");
72
102
  }
73
103
  logger.info("Adding hooks...");
74
104
  const huskyDir = resolve(process.cwd(), ".husky");
@@ -81,8 +111,6 @@ const init = async () => {
81
111
  `;
82
112
  await fs.outputFile(preCommitPath, preCommitContent, { mode: 493 });
83
113
  logger.info("Creating configuration files...");
84
- const pkgPath = resolve(process.cwd(), "package.json");
85
- const pkg = await fs.readJson(pkgPath);
86
114
  const isModule = pkg.type === "module";
87
115
  const isTsProject = fs.existsSync(resolve(process.cwd(), "tsconfig.json"));
88
116
  const commitlintFile = isTsProject ? "commitlint.config.ts" : isModule ? "commitlint.config.cjs" : "commitlint.config.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huaiyou/hooks-git",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Git hooks configuration with Husky, Commitlint and Lint-staged",
5
5
  "bin": {
6
6
  "hy-hooks-git": "./dist/cli.mjs"