Skip to content

Commit f125fd7

Browse files
committed
feat: Add beforePushScript
1 parent 8b1db06 commit f125fd7

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ npm install --save-dev deploy-to-git
77

88
## Configuring
99

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

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

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

index.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,37 @@
22
const { execSync } = require('child_process');
33

44
const variablePrefix = 'npm_package_config_deployToGit_';
5-
const fields = ['repository', 'branch', 'folder', 'commit', 'script', 'user_name', 'user_email'];
5+
const fields = {
6+
repository: true,
7+
branch: true,
8+
folder: true,
9+
commit: true,
10+
script: true,
11+
user_name: true,
12+
user_email: true,
13+
beforePushScript: false
14+
}
615
const cwd = process.cwd();
716
const config = {};
817

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

12-
if (!configVar) {
21+
if (!configVar && isRequired) {
1322
throw Error(`deployOnGit requires "${field}" field in package config`);
1423
}
1524

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

19-
if (!envVar) {
20-
throw Error(`Environment variable "${envVarName}" presented at string "${configVar}" is missing`);
21-
}
29+
if (!envVar) {
30+
throw Error(`Environment variable "${envVarName}" presented at string "${configVar}" is missing`);
31+
}
2232

23-
return envVar;
24-
});
33+
return envVar;
34+
});
35+
}
2536
}
2637

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

59+
if(config.beforePushScript) {
60+
console.log('Running beforePushScript...');
61+
62+
try {
63+
execSync(`
64+
cd ${config.folder} &&
65+
${config.beforePushScript}
66+
`, { cwd });
67+
} catch(e) {
68+
throw Error('Failed to run beforePushScript.');
69+
}
70+
}
71+
4872
console.log('Pushing...');
4973
try {
5074
execSync(`
5175
cd ${config.folder} &&
52-
git push ${config.repository} ${config.branch} 2>&1
76+
git push --tags ${config.repository} ${config.branch} 2>&1
5377
`, { cwd });
5478
} catch (e) {
5579
throw Error('Failed to push.');

0 commit comments

Comments
 (0)