-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
69 lines (53 loc) · 2.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env node
const { execSync } = require('child_process');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { argv } = yargs(hideBin(process.argv));
const variablePrefix = 'npm_package_config_deployToGit_';
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, isRequired] of Object.entries(fields)) {
const configVar = argv[field] || process.env[`${variablePrefix}${field}`];
if (!configVar && isRequired) {
throw Error(`deployOnGit requires "${field}" field in package config`);
}
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`);
}
return envVar;
});
}
}
console.log('Starting deploy to Git...');
console.log(`Cloning the repository to "${config.folder}" folder...`);
execSync(`git clone -b ${config.branch} ${config.repository} ${config.folder}`, { cwd, stdio: 'inherit' });
console.log(`Starting script "${config.script}"...`);
execSync(`${config.script}`, { cwd, stdio: 'inherit' });
console.log('Configuring and committing...');
execSync([
`cd ${config.folder}`,
`git config user.email "${config.user_email}"`,
`git config user.name "${config.user_name}"`,
'git add .',
`git commit --allow-empty -m "${config.commit}"`
].join('&&'), { cwd, stdio: 'inherit' });
if (config.beforePushScript) {
console.log('Running beforePushScript...');
execSync(`cd ${config.folder} && ${config.beforePushScript}`, { cwd, stdio: 'inherit' });
}
console.log('Pushing...');
execSync(`cd ${config.folder} && git push --tags ${config.repository} ${config.branch}`, { cwd, stdio: 'inherit' });
console.log('Deploying to git is finished.');