Skip to content

Commit 473fd74

Browse files
committed
Break logical chunks in small file modules
1 parent 006dafe commit 473fd74

File tree

6 files changed

+56
-40
lines changed

6 files changed

+56
-40
lines changed

index.js

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
11
#!/usr/bin/env node
22

3-
const cli = require('commander');
4-
const execa = require('execa');
5-
const config = require('./src/config');
6-
const findBinary = require('./src/find-binary');
7-
const pkg = require('./package.json');
3+
const cliInit = require('./src/cli-init');
4+
const configLoad = require('./src/config-load');
5+
const runAll = require('./src/run-all');
86

9-
cli
10-
.version(pkg.version, '-v, --version')
11-
.parse(process.argv);
12-
13-
const binary = `${__dirname}/bin/git-run-if-changed.sh`;
14-
15-
function runCommandsIfFileChanged(fileToCheck, commands) {
16-
const commandsList = Array.isArray(commands) ? commands : [commands];
17-
const commandsListResolved = commandsList.map((cmd) => {
18-
const { binaryPath, args: binaryArgs } = findBinary(cmd);
19-
20-
return [binaryPath].concat(binaryArgs).join(' ');
21-
});
22-
const args = [fileToCheck].concat(commandsListResolved);
23-
execa(binary, args).stdout.pipe(process.stdout);
24-
}
25-
26-
Object.entries(config).forEach(([file, commands]) => {
27-
if (commands.length === 0) {
28-
return;
29-
}
30-
31-
runCommandsIfFileChanged(file, commands);
32-
});
7+
cliInit();
8+
runAll(configLoad());

src/cli-init.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const cli = require('commander');
2+
const pkg = require('../package.json');
3+
4+
module.exports = function cliInit() {
5+
cli
6+
.version(pkg.version, '-v, --version')
7+
.parse(process.argv);
8+
};

src/config-load.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const cosmiconfig = require('cosmiconfig');
2+
3+
module.exports = function configLoad() {
4+
const configResult = cosmiconfig('run-if-changed').searchSync();
5+
6+
if (!configResult || configResult.isEmpty) {
7+
process.exit(0);
8+
}
9+
10+
const { config } = configResult;
11+
12+
return config;
13+
};

src/config.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/run-all.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const runIfFileChanged = require('./run-if-file-changed');
2+
3+
function runCommandsForFile(file, commands) {
4+
const commandsList = Array.isArray(commands) ? commands : [commands].filter();
5+
6+
if (commandsList.length === 0) {
7+
return;
8+
}
9+
10+
runIfFileChanged(file, commandsList);
11+
}
12+
13+
module.exports = function runAll(config) {
14+
Object.entries(config).forEach(([file, commands]) => runCommandsForFile(file, commands));
15+
};

src/run-if-file-changed.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const execa = require('execa');
2+
const findBinary = require('./find-binary');
3+
4+
const binary = `${__dirname}/../bin/git-run-if-changed.sh`;
5+
6+
function resolveCommand(command) {
7+
const { binaryPath, args: binaryArgs } = findBinary(command);
8+
9+
return [binaryPath].concat(binaryArgs).join(' ');
10+
}
11+
12+
module.exports = function runIfFileChanged(fileToCheck, commandsList) {
13+
const args = [fileToCheck].concat(commandsList.map(resolveCommand));
14+
execa(binary, args).stdout.pipe(process.stdout);
15+
};

0 commit comments

Comments
 (0)