@thisismanta/semantic-version 1.0.2 → 1.0.4

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.
@@ -29,12 +29,19 @@ const execa_1 = require("execa");
29
29
  main();
30
30
  async function main() {
31
31
  const currentDirectoryPath = process.cwd();
32
- if (fp.basename(fp.dirname(currentDirectoryPath)) !== 'node_modules') {
33
- console.log('Do nothing when running this on semantic-version repository.');
32
+ console.debug('currentDirectoryPath »', currentDirectoryPath);
33
+ const gitDirectoryPath = await findGitDirectoryPath(currentDirectoryPath);
34
+ console.debug('gitDirectoryPath »', gitDirectoryPath);
35
+ if (!gitDirectoryPath) {
36
+ throw new Error('Could not find a Git directory.');
37
+ }
38
+ const packageJSON = JSON.parse(await fs.readFile(fp.join(gitDirectoryPath, 'package.json'), 'utf-8'));
39
+ if (packageJSON.name === '@thisismanta/semantic-version') {
40
+ console.warn('Skip installing Git hooks as it is supposed to be done on a consumer repository.');
34
41
  return;
35
42
  }
36
43
  console.log('Installing Husky');
37
- await (0, execa_1.execaCommand)('npx husky install');
44
+ await (0, execa_1.execaCommand)('npx husky install', { cwd: gitDirectoryPath });
38
45
  // Up 3 levels because of `node_modules/@thisismanta/semantic-version`
39
46
  const huskyDirectoryPath = fp.resolve(currentDirectoryPath, '../../..', '.husky');
40
47
  await fs.access(huskyDirectoryPath);
@@ -43,6 +50,24 @@ async function main() {
43
50
  await upsert(fp.join(huskyDirectoryPath, 'post-commit'), 'npx auto-npm-version');
44
51
  console.log('Done adding Git hooks.');
45
52
  }
53
+ async function findGitDirectoryPath(path) {
54
+ const pathList = path.split(fp.sep);
55
+ while (pathList.length > 1) {
56
+ const testPath = fp.join(pathList.join(fp.sep), '.git');
57
+ try {
58
+ await fs.access(testPath);
59
+ const stat = await fs.lstat(testPath);
60
+ if (stat.isDirectory()) {
61
+ return pathList.join(fp.sep);
62
+ }
63
+ }
64
+ catch {
65
+ // Do nothing
66
+ }
67
+ pathList.pop();
68
+ }
69
+ return null;
70
+ }
46
71
  async function upsert(filePath, text) {
47
72
  try {
48
73
  await fs.access(filePath);
@@ -58,6 +83,6 @@ async function upsert(filePath, text) {
58
83
  .findIndex(line => line.trim().includes(text));
59
84
  if (index === -1) {
60
85
  await fs.appendFile(filePath, '\n' + text + '\n', 'utf-8');
61
- console.log('Added', text, 'to', filePath);
86
+ console.log(`Added "${text}" to ${filePath}`);
62
87
  }
63
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thisismanta/semantic-version",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "author": "Anantachai Saothong <thisismanta@gmail.com>",
5
5
  "license": "ISC",
6
6
  "engines": {