-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
export {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const p = require("path"); | ||
const h = require("./"); | ||
function help(code) { | ||
console.log(`Usage: | ||
husky install [dir] (default: .husky) | ||
husky uninstall | ||
husky set|add <file> [cmd]`); | ||
process.exit(code); | ||
} | ||
const [, , cmd, ...args] = process.argv; | ||
const ln = args.length; | ||
const [x, y] = args; | ||
const hook = (fn) => () => !ln || ln > 2 ? help(2) : fn(x, y); | ||
const cmds = { | ||
install: () => (ln > 1 ? help(2) : h.install(x)), | ||
uninstall: h.uninstall, | ||
set: hook(h.set), | ||
add: hook(h.add), | ||
['-v']: () => console.log(require(p.join(__dirname, '../package.json')).version), | ||
}; | ||
try { | ||
cmds[cmd] ? cmds[cmd]() : help(0); | ||
} | ||
catch (e) { | ||
console.error(e instanceof Error ? `husky - ${e.message}` : e); | ||
process.exit(1); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export declare function install(dir?: string): void; | ||
export declare function set(file: string, cmd: string): void; | ||
export declare function add(file: string, cmd: string): void; | ||
export declare function uninstall(): void; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.uninstall = exports.add = exports.set = exports.install = void 0; | ||
const cp = require("child_process"); | ||
const fs = require("fs"); | ||
const p = require("path"); | ||
const l = (msg) => console.log(`husky - ${msg}`); | ||
const git = (args) => cp.spawnSync('git', args, { stdio: 'inherit' }); | ||
function install(dir = '.husky') { | ||
if (process.env.HUSKY === '0') { | ||
l('HUSKY env variable is set to 0, skipping install'); | ||
return; | ||
} | ||
if (git(['rev-parse']).status !== 0) { | ||
l(`git command not found, skipping install`); | ||
return; | ||
} | ||
const url = 'https://typicode.github.io/husky/#/?id=custom-directory'; | ||
if (!p.resolve(process.cwd(), dir).startsWith(process.cwd())) { | ||
throw new Error(`.. not allowed (see ${url})`); | ||
} | ||
if (!fs.existsSync('.git')) { | ||
throw new Error(`.git can't be found (see ${url})`); | ||
} | ||
try { | ||
fs.mkdirSync(p.join(dir, '_'), { recursive: true }); | ||
fs.writeFileSync(p.join(dir, '_/.gitignore'), '*'); | ||
fs.copyFileSync(p.join(__dirname, '../husky.sh'), p.join(dir, '_/husky.sh')); | ||
const { error } = git(['config', 'core.hooksPath', dir]); | ||
if (error) { | ||
throw error; | ||
} | ||
} | ||
catch (e) { | ||
l('Git hooks failed to install'); | ||
throw e; | ||
} | ||
l('Git hooks installed'); | ||
} | ||
exports.install = install; | ||
function set(file, cmd) { | ||
const dir = p.dirname(file); | ||
if (!fs.existsSync(dir)) { | ||
throw new Error(`can't create hook, ${dir} directory doesn't exist (try running husky install)`); | ||
} | ||
fs.writeFileSync(file, `#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
${cmd} | ||
`, { mode: 0o0755 }); | ||
l(`created ${file}`); | ||
} | ||
exports.set = set; | ||
function add(file, cmd) { | ||
if (fs.existsSync(file)) { | ||
fs.appendFileSync(file, `${cmd}\n`); | ||
l(`updated ${file}`); | ||
} | ||
else { | ||
set(file, cmd); | ||
} | ||
} | ||
exports.add = add; | ||
function uninstall() { | ||
git(['config', '--unset', 'core.hooksPath']); | ||
} | ||
exports.uninstall = uninstall; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.