Skip to content

Commit 636bd8f

Browse files
authored
Merge pull request #4 from hkdobrev/module-chunks
Break logical chunks in small file modules
2 parents 95b616d + 0a52cbf commit 636bd8f

File tree

6 files changed

+56
-38
lines changed

6 files changed

+56
-38
lines changed

index.js

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

3-
const cli = require('commander');
4-
const process = require('process');
5-
const cosmiconfig = require('cosmiconfig');
6-
const execa = require('execa');
7-
const findBinary = require('./find-binary');
8-
const pkg = require('./package.json');
3+
const cliInit = require('./src/cliInit');
4+
const configLoad = require('./src/configLoad');
5+
const runAll = require('./src/runAll');
96

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

src/cliInit.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/configLoad.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+
};
File renamed without changes.

src/runAll.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const runIfFileChanged = require('./runIfFileChanged');
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/runIfFileChanged.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('./findBinary');
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)