Skip to content

Commit

Permalink
feat: Add beforePushScript
Browse files Browse the repository at this point in the history
  • Loading branch information
finom committed Oct 18, 2016
1 parent 8b1db06 commit f125fd7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ npm install --save-dev deploy-to-git

## Configuring

Configuration for the tool needs to be placed at ``config.deployToGit`` object inside ``package.json``. All fields are required.
Configuration for the tool needs to be placed at ``config.deployToGit`` object inside ``package.json``.

- ``repository`` - a repository
- ``branch`` - a branch of the repository where you want to push the artifacts
- ``folder`` - a folder where artifacts are generated by the build and where the repository is cloned
- ``script`` - a script which runs the build
- ``commit`` - commit text
- ``user`` - commitee information for Git - an object with keys ``name`` and ``email``
- ``beforePushScript`` (optional) - a command that should be run after a commit (e. g. add needed git tags).

Substrings started with ``$`` are replaced by corresponding environment variables.

Expand Down
46 changes: 35 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@
const { execSync } = require('child_process');

const variablePrefix = 'npm_package_config_deployToGit_';
const fields = ['repository', 'branch', 'folder', 'commit', 'script', 'user_name', 'user_email'];
const fields = {
repository: true,
branch: true,
folder: true,
commit: true,
script: true,
user_name: true,
user_email: true,
beforePushScript: false
}
const cwd = process.cwd();
const config = {};

for (const field of fields) {
for (const [field, isRequired] of Object.entries(fields)) {
const configVar = process.env[`${variablePrefix}${field}`];

if (!configVar) {
if (!configVar && isRequired) {
throw Error(`deployOnGit requires "${field}" field in package config`);
}

config[field] = configVar.replace(/\$([a-zA-Z0-9_]+)/g, (match, envVarName) => {
const envVar = process.env[envVarName];
if(configVar) {
config[field] = configVar.replace(/\$([a-zA-Z0-9_]+)/g, (match, envVarName) => {
const envVar = process.env[envVarName];

if (!envVar) {
throw Error(`Environment variable "${envVarName}" presented at string "${configVar}" is missing`);
}
if (!envVar) {
throw Error(`Environment variable "${envVarName}" presented at string "${configVar}" is missing`);
}

return envVar;
});
return envVar;
});
}
}

console.log('Starting deploy to Git...');
Expand All @@ -45,11 +56,24 @@ execSync(`
git commit --allow-empty -m "${config.commit}" 2>&1
`, { cwd });

if(config.beforePushScript) {
console.log('Running beforePushScript...');

try {
execSync(`
cd ${config.folder} &&
${config.beforePushScript}
`, { cwd });
} catch(e) {
throw Error('Failed to run beforePushScript.');
}
}

console.log('Pushing...');
try {
execSync(`
cd ${config.folder} &&
git push ${config.repository} ${config.branch} 2>&1
git push --tags ${config.repository} ${config.branch} 2>&1
`, { cwd });
} catch (e) {
throw Error('Failed to push.');
Expand Down

0 comments on commit f125fd7

Please sign in to comment.