Skip to content

Commit

Permalink
Rewrite npm's scripts in JavaScript (#184)
Browse files Browse the repository at this point in the history
This removes the dependency on `sh` for a Node env. `sh` is not available on Windows
  • Loading branch information
aminya committed May 14, 2021
1 parent 45e9234 commit 42acef3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 64 deletions.
6 changes: 3 additions & 3 deletions .npm/bin/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env node

var spawn = require('child_process').spawn;
var path = require('path');
const { getExePath } = require('../get-exe');

var command_args = process.argv.slice(2);

var child = spawn(
path.join(__dirname, 'lefthook'),
getExePath(),
command_args,
{ stdio: [process.stdin, process.stdout, process.stderr] });
{ stdio: "inherit" });

child.on('close', function (code) {
if (code !== 0) {
Expand Down
51 changes: 0 additions & 51 deletions .npm/bin/lefthook

This file was deleted.

36 changes: 36 additions & 0 deletions .npm/get-exe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const path = require("path")

function getExePath() {
// Detect OS
// https://nodejs.org/api/process.html#process_process_platform
let goOS = process.platform;
let extension = '';
if (['win32', 'cygwin'].includes(process.platform)) {
goOS = 'windows';
extension = '.exe';
}

// Detect architecture
// https://nodejs.org/api/process.html#process_process_arch
let goArch = process.arch;
switch (process.arch) {
case 'x64': {
goArch = 'amd64';
break;
}
case 'x32':
case 'ia32': {
goArch = '386';
break;
}
}

const dir = path.join(__dirname, 'bin');
const executable = path.join(
dir,
`lefthook_${goOS}_${goArch}`,
`lefthook${extension}`
);
return executable;
}
exports.getExePath = getExePath;
17 changes: 7 additions & 10 deletions .npm/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
const { spawn } = require("child_process");
const { join } = require("path");
if (!process.env.CI) {
const { spawnSync } = require('child_process');
const { getExePath } = require('./get-exe');

const isCI = process.env.CI;

if (!isCI) {
binpath = join(__dirname, 'bin', 'lefthook');

result = spawn(binpath, ["install", "-f"], {
cwd: process.env.INIT_CWD,
stdio: [process.stdin, process.stdout, process.stderr]
// run install
spawnSync(getExePath(), ['install', '-f'], {
cwd: process.env.INIT_CWD || process.cwd,
stdio: 'inherit',
});
}

0 comments on commit 42acef3

Please sign in to comment.