Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite npm's scripts in JavaScript #184

Merged
merged 1 commit into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
});
}