Skip to content

Commit 57cb3b9

Browse files
authored
Add generated by informations in hook script (#400)
1 parent d4f7531 commit 57cb3b9

File tree

3 files changed

+96
-9
lines changed

3 files changed

+96
-9
lines changed

src/installer/__tests__/__snapshots__/getScript.ts.snap

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33
exports[`hookScript should match snapshot (OS X/Linux) 1`] = `
44
"#!/bin/sh
55
# husky
6-
# v1.1.4 darwin
6+
7+
# Hook created by Husky
8+
# Version: 1.1.4
9+
# At: <locale date string>
10+
# See: https://github.com/typicode/husky#readme
11+
12+
# From npm package
13+
# Name: foo-package
14+
# Directory: /home/typicode/projects/foo-package
15+
# Homepage: https://github.com/foo/foo-package
716
817
scriptPath=\\"node_modules/husky/run.js\\"
918
hookName=\`basename \\"$0\\"\`
@@ -30,7 +39,16 @@ fi
3039
exports[`hookScript should match snapshot (Windows) 1`] = `
3140
"#!/bin/sh
3241
# husky
33-
# v1.1.4 win32
42+
43+
# Hook created by Husky
44+
# Version: 1.1.4
45+
# At: <locale date string>
46+
# See: https://github.com/typicode/husky#readme
47+
48+
# From npm package
49+
# Name: foo-package
50+
# Directory: /home/typicode/projects/foo-package
51+
# Homepage: https://github.com/foo/foo-package
3452
3553
scriptPath=\\"node_modules/husky/run.js\\"
3654
hookName=\`basename \\"$0\\"\`

src/installer/__tests__/getScript.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
import getScript from '../getScript'
22

3+
// Mock Date to have deterministic test
4+
Date.now = jest.fn(() => 1482363367071)
5+
36
const rootDir = '/home/typicode/project'
47
const huskyDir = '/home/typicode/project/node_modules/husky'
58

69
// On OS X/Linux runNodePath gets resolved to the following value
710
// In order to make the test deterministic on AppVeyor, the value is hardcoded
811
const runNodePath = '/home/typicode/project/node_modules/run-node/run-node'
912

13+
// Faking env variable
14+
process.env = {
15+
...process.env,
16+
PWD: '/home/typicode/projects/foo-package',
17+
npm_package_homepage: 'https://github.com/foo/foo-package',
18+
npm_package_name: 'foo-package'
19+
}
20+
21+
// Mock Date.toLocaleString
22+
global.Date = class extends Date {
23+
constructor() {
24+
super()
25+
}
26+
27+
public toLocaleString() {
28+
return '<locale date string>'
29+
}
30+
} as DateConstructor
31+
1032
describe('hookScript', () => {
1133
it('should match snapshot (OS X/Linux)', () => {
1234
const script = getScript(rootDir, huskyDir, runNodePath, 'darwin')

src/installer/getScript.ts

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import * as path from 'path'
44
import * as slash from 'slash'
55

66
interface IContext {
7+
createdAt: string
8+
homepage: string
79
node: string
10+
pkgDirectory?: string
11+
pkgHomepage?: string
12+
pkgName?: string
813
platform: string
9-
script: string
14+
runScriptPath: string
1015
version: string
1116
}
1217

@@ -17,11 +22,30 @@ export const huskyIdentifier = '# husky'
1722
const huskyrc = '~/.huskyrc'
1823

1924
// Render script
20-
const render = ({ node, platform, script, version }: IContext) => `#!/bin/sh
25+
const render = ({
26+
createdAt,
27+
homepage,
28+
node,
29+
pkgDirectory,
30+
pkgHomepage,
31+
pkgName,
32+
platform,
33+
runScriptPath,
34+
version
35+
}: IContext) => `#!/bin/sh
2136
${huskyIdentifier}
22-
# v${version} ${platform}
2337
24-
scriptPath="${script}.js"
38+
# Hook created by Husky
39+
# Version: ${version}
40+
# At: ${createdAt}
41+
# See: ${homepage}
42+
43+
# From npm package
44+
# Name: ${pkgName}
45+
# Directory: ${pkgDirectory}
46+
# Homepage: ${pkgHomepage}
47+
48+
scriptPath="${runScriptPath}.js"
2549
hookName=\`basename "$0"\`
2650
gitParams="$*"
2751
@@ -64,11 +88,34 @@ export default function(
6488
// On Windows do not rely on run-node
6589
const node = platform === 'win32' ? 'node' : runNodePath
6690

67-
const { version } = JSON.parse(
91+
// Env variable
92+
const pkgName = process && process.env && process.env.npm_package_name
93+
const pkgHomepage = process && process.env && process.env.npm_package_homepage
94+
const pkgDirectory = process && process.env && process.env.PWD
95+
96+
// Husky package.json
97+
const { homepage, version } = JSON.parse(
6898
fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf-8')
6999
)
70100

71-
const script = slash(path.join(path.relative(rootDir, huskyDir), 'run'))
101+
// Path to run.js
102+
const runScriptPath = slash(
103+
path.join(path.relative(rootDir, huskyDir), 'run')
104+
)
105+
106+
// created at
107+
const createdAt = new Date().toLocaleString()
72108

73-
return render({ node, platform, script, version })
109+
// Render script
110+
return render({
111+
createdAt,
112+
homepage,
113+
node,
114+
pkgDirectory,
115+
pkgHomepage,
116+
pkgName,
117+
platform,
118+
runScriptPath,
119+
version
120+
})
74121
}

0 commit comments

Comments
 (0)