Skip to content

Commit

Permalink
feat: add gitAddCommitPush
Browse files Browse the repository at this point in the history
  • Loading branch information
aadityataparia committed Jun 1, 2019
1 parent d02ad18 commit 5be9670
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 49 deletions.
44 changes: 32 additions & 12 deletions dist/sifrr.dev.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/sifrr.dev.js.map

Large diffs are not rendered by default.

40 changes: 30 additions & 10 deletions dist/sifrr.dev.module.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/sifrr.dev.module.js.map

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions scripts/contributors
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#!/usr/bin/env bash

# From https://github.com/facebook/react/blob/master/scripts/authors
# Generate an AUTHORS file based on the output of git shortlog. It uses ABC
# order, strips out leading spaces and numbers, then filters out specific
# authors.

git shortlog -sen \
| perl -spe 's/^\s+\d+\s+//' \
> misc/CONTRIBUTORS

echo 'Updated contributors'
17 changes: 7 additions & 10 deletions scripts/distribute
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/usr/bin/env bash
#!/usr/bin/env node

set -ex

yarn build
./scripts/contributors

# Add dist files not generated before
git ls-files 'dist/*.js*' | xargs git add
git ls-files 'misc/*' | xargs git add
(git commit -m "chore: add auto generated files" && git push && exit 1) || echo 'Files up to date'
const gitAddCommitPush = require('../src/gitaddcommitpush');
gitAddCommitPush({
preCommand: ['yarn build', 'sh ./scripts/contributors'],
files: ['dist/*.js*', 'misc/*'],
commitMsg: 'chore: add auto generated files'
});
40 changes: 30 additions & 10 deletions src/exec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
const spawn = require('child_process').spawn;
const execa = require('child_process').exec;

function exec(command, options = {}) {
return new Promise((res, rej) => {
execa(command, options, (err, stdout, stderr) => {
if (stdout) process.stdout.write(`out: ${stdout}`);
if (stderr) process.stderr.write(`err: ${stderr}`);
if (err !== null) {
process.stderr.write(`exec error: ${err}`);
rej(err);
}
res({ stdout, stderr });
process.stdout.write(`Running command: ${command} \n`);
if (command.indexOf('sh') === 0) {
options.stdio = options.stdio || 'inherit';
return new Promise((res, rej) => {
const [c, ...args] = command.split(' ');
const runner = spawn(c, args, options);
runner.on('close', (code) => {
if (code !== 0) {
process.stdout.write(`Command exited with code ${code}: ${command} \n`);
rej(code);
} else {
process.stdout.write(`Finished command: ${command} \n`);
res();
}
});
});
});
} else {
return new Promise((res, rej) => {
execa(command, options, (err, stdout, stderr) => {
if (stdout) process.stdout.write(`out: ${stdout} \n`);
if (stderr) process.stderr.write(`err: ${stderr} \n`);
if (err !== null) {
process.stderr.write(`exec error: ${err}`);
rej(err);
}
res({ stdout, stderr });
process.stdout.write(`Finished command: ${command} \n`);
});
});
}
}

module.exports = exec;
29 changes: 29 additions & 0 deletions src/gitaddcommitpush.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const exec = require('./exec');

module.exports = async function({
preCommand = false,
files = '*',
commitMsg = 'chore: add new files',
push = true
} = {}) {
if (preCommand) {
if (Array.isArray(preCommand)) {
for (let i = 0; i < preCommand.length; i++) {
await exec(preCommand[i]);
}
} else {
await exec(preCommand);
}
}
if (Array.isArray(files)) {
for (let i = 0; i < files.length; i++) {
await exec(`git ls-files '${files[i]}' | xargs git add`);
}
} else {
await exec(`git ls-files '${files}' | xargs git add`);
}
await exec(`git commit -m "${commitMsg}"`);
if (push) await exec(`git push`).then(({ stdout }) => {
console.log(stdout, stdout === 'Everything up-to-date');
});
};

0 comments on commit 5be9670

Please sign in to comment.