333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
'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', // default is travis and appveyor
  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 = [ '6, 8, 10' ];
  }
}
 
const defaultOS = {
  travis: '',
  'azure-pipelines': '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,
  };
}
config.command = Object.assign({
  travis: 'ci',
  appveyor: 'test',
  'azure-pipelines': '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 === '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);
}