File tree Expand file tree Collapse file tree 6 files changed +56
-40
lines changed
Expand file tree Collapse file tree 6 files changed +56
-40
lines changed Original file line number Diff line number Diff line change 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 ( ) ) ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments