install.js
4.87 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
const nunjucks = require('nunjucks');
const path = require('path');
const fs = require('fs');
const engine = nunjucks.configure({
autoescape: false,
watch: false,
});
let root;
// support npminstall path
if (__dirname.indexOf('.npminstall') >= 0) {
root = path.join(__dirname, '../../../../..');
} else {
root = path.join(__dirname, '../..');
}
if (process.env.CI_ROOT_FOR_TEST) {
root = process.env.CI_ROOT_FOR_TEST;
}
let pkg;
try {
pkg = require(path.join(root, 'package.json'));
} catch (err) {
console.error('read package.json error: %s', err.message);
console.error('[egg-ci] stop create ci yml');
process.exit(0);
}
const config = Object.assign({
type: 'travis, appveyor, github', // default is travis, appveyor and github
version: '',
npminstall: true,
// auto detect nyc_output/*.json files, please use on travis windows platfrom
nyc: false,
license: false,
}, pkg.ci);
if (!('afterScript' in config)) {
const codecovCMD = config.nyc ? 'codecov --disable=gcov -f .nyc_output/*.json' : 'codecov';
if (config.npminstall) {
config.afterScript = `
after_script:
- npminstall codecov && ${codecovCMD}
`.trim();
} else {
config.afterScript = `
after_script:
- npm i codecov && ${codecovCMD}
`.trim();
}
}
config.types = arrayify(config.type);
config.versions = arrayify(config.version);
if (config.services) config.services = arrayify(config.services);
if (config.versions.length === 0) {
const installNode = pkg.engines && (pkg.engines['install-node'] ||
pkg.engines['install-alinode']);
if (!installNode) {
// default version is LTS
config.versions = [ '8', '10', '12' ];
}
}
const defaultOS = {
travis: '',
'azure-pipelines': 'linux, windows, macos',
github: 'linux, windows, macos',
};
if (pkg.ci && pkg.ci.os) {
config.os = Object.assign(defaultOS, pkg.ci.os);
} else {
config.os = defaultOS;
}
for (const platfrom in config.os) {
config.os[platfrom] = arrayify(config.os[platfrom]);
}
if (config.os && config.os.travis && config.os.travis.length === 0) {
config.os.travis = null;
}
const originCommand = config.command;
if (typeof originCommand === 'string') {
config.command = {
travis: originCommand,
appveyor: originCommand,
github: originCommand,
'azure-pipelines': originCommand,
};
}
config.command = Object.assign({
travis: 'ci',
appveyor: 'test',
'azure-pipelines': 'ci',
github: 'ci',
}, config.command);
let ymlName = '';
let ymlContent = '';
let ymlName2 = '';
let ymlContent2 = '';
for (const type of config.types) {
if (type === 'travis') {
ymlContent = engine.renderString(getTpl('travis'), config);
ymlName = '.travis.yml';
} else if (type === 'appveyor') {
ymlContent = engine.renderString(getTpl('appveyor'), config);
ymlName = 'appveyor.yml';
} else if (type === 'github') {
const os = config.os.github.map(name => {
if (name === 'linux') name = 'ubuntu';
return `${name}-latest`;
});
ymlContent = getTpl('github.yml')
.replace('{{github_node_version}}', config.versions.join(', '))
.replace('{{github_os}}', os.join(', '))
.replace('{{github_command_ci}}', config.command.github)
.replace('{{github_npm_install}}', config.npminstall ? 'npm i -g npminstall && npminstall' : 'npm i');
ymlName = '.github/workflows/nodejs.yml';
let dir = path.join(root, '.github');
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
dir = path.join(root, '.github', 'workflows');
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
} else if (type === 'azure-pipelines') {
ymlContent = engine.renderString(getTpl('azure-pipelines.yml'), config);
ymlName = 'azure-pipelines.yml';
ymlContent2 = engine.renderString(getTpl('azure-pipelines.template.yml'), config);
ymlName2 = 'azure-pipelines.template.yml';
} else {
throw new Error(`${type} type not support`);
}
const ymlPath = path.join(root, ymlName);
fs.writeFileSync(ymlPath, ymlContent);
console.log(`[egg-ci] create ${ymlPath} success`);
if (ymlName2) {
const ymlPath2 = path.join(root, ymlName2);
fs.writeFileSync(ymlPath2, ymlContent2);
console.log(`[egg-ci] create ${ymlPath2} success`);
}
}
if (config.license) {
let data = {
year: '2017',
fullname: 'Alibaba Group Holding Limited and other contributors.',
};
if (typeof config.license === 'object') {
data = Object.assign(data, config.license);
}
if (Number(data.year) < new Date().getFullYear()) {
data.year = `${data.year}-present`;
}
const licenseContent = engine.renderString(getTpl('license'), data);
const licensePath = path.join(root, 'LICENSE');
fs.writeFileSync(licensePath, licenseContent);
console.log(`[egg-ci] create ${licensePath} success`);
}
function getTpl(name) {
return fs.readFileSync(path.join(__dirname, 'templates', name), 'utf8');
}
function arrayify(str) {
if (Array.isArray(str)) return str;
return str.split(/\s*,\s*/).filter(s => !!s);
}