-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·151 lines (132 loc) · 4.18 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
/*
* @Author: Jack
* @Date: 2018-08-01 14:25:34
* @Last Modified by: Taco
* @Last Modified time: 2018-08-03 18:29:41
*/
const yaml = require('js-yaml');
const fs = require('fs');
const path = require('path');
const os = require('os');
const program = require('commander');
const jenkinsApi = require('jenkins-api');
const axios = require('axios');
const chalk = require('chalk');
const packageJson = require('./package.json');
const { viewBuildInfo } = require('./build.js');
let commandName = 'jekins';
let jekinsConfig = null;
let jekinsConfigSample;
let jekins;
let jekinsUrl;
const installPath = __dirname;
try {
if (fs.existsSync(getConfigFile())) {
jekinsConfig = yaml.safeLoad(fs.readFileSync(getConfigFile(), 'utf8'));
}
jekinsConfigSample = yaml.safeLoad(fs.readFileSync(path.join(installPath, './config.yaml'), 'utf8'));
} catch (e) {
console.log(chalk.red(e));
}
function getConfigFile() {
return path.join(os.homedir(), '.jekins.conf.yaml');
}
function getJekins(cmd) {
if (jekins) {
return jekins;
}
const env = getEnv(cmd);
if (!env) {
return;
}
if (!jekinsConfig.env[env]) {
console.error(`Please config env: ${env}`);
}
const envPrefix = encodeURI(jekinsConfig.env[env]);
try {
jekinsUrl = jekinsConfig.url.replace('://', `://${jekinsConfig.user.name}:${jekinsConfig.user.token}@`);
jekinsUrl = `${jekinsUrl}${envPrefix}`;
jekins = jenkinsApi.init(jekinsUrl);
console.log('Initialized: ', jekinsUrl);
return jekins;
} catch (e) {
console.log(e);
}
}
function getEnv(cmd) {
let env;
if (cmd.development) {
env = 'development';
} else if (cmd.beta) {
env = 'beta';
} else if (cmd.production) {
env = 'production';
}
return env;
}
function checkConfigFile() {
if (!jekinsConfig) {
console.log(chalk.red('Config has not been initialized. RUN: '));
console.log(chalk.green(`${commandName} init`));
process.exit(1);
}
}
program
.version(packageJson.version)
.command('build <target>')
.description('trigger new building of <target>')
.option('-p, --production', 'Production Enviroment')
.option('-b, --beta', 'Beta Enviroment')
.option('-d, --development', 'Development Enviroment')
.action(function(target, cmd) {
checkConfigFile();
console.log('building %s', target);
getJekins(cmd).build(target, function(err, data) {
if (err) {
return console.error('Error: ', err);
}
console.log('Finished: ', data.location);
const targetUrl = `${jekinsUrl}/job/${target}`;
const targetApi = `${targetUrl}/api/json`;
let buildId = '';
axios.get(targetApi)
.then(function(response) {
const { lastBuild } = response.data;
buildId = lastBuild.number;
const buildUrl = lastBuild.url.replace(/^https?:\/\//, `https://${jekinsConfig.user.name}:${jekinsConfig.user.token}@`);
console.log('Build Info: ', buildUrl);
viewBuildInfo(buildUrl);
});
process.on('SIGINT', function() {
console.log(chalk.yellow('\nThe building already started, if you want stop current building, RUN: '));
const command = `jekins stop ${cmd.parent.rawArgs[3]} ${target} ${buildId}`;
console.log(chalk.green(command));
process.exit();
});
});
});
program.command('stop <target> <buildId>')
.description('stop speciafic building<buildId> of <target>')
.option('-p, --production', 'Production Enviroment')
.option('-b, --beta', 'Beta Enviroment')
.option('-d, --development', 'Development Enviroment')
.action(function(target, buildId, cmd) {
checkConfigFile();
console.log('stoping %s/%d', target, buildId);
getJekins(cmd).stop_build(target, buildId, function(err, data) {
if (err) {
return console.error('Error: ', err);
}
console.log('Stoped: ', data.body);
});
});
program.command('init')
.description('Init config file.')
.action(function(cmd) {
const object = Object.assign({}, jekinsConfigSample, jekinsConfig);
const file = getConfigFile();
fs.writeFileSync(file, yaml.safeDump(object));
console.log('Initialized config file at %s', chalk.green(file));
});
program.parse(process.argv);