UNPKG

2.4 kBJavaScriptView Raw
1import gulp from 'gulp';
2import fs from 'fs-extra';
3import axios from './index.js';
4
5gulp.task('default', async function(){
6 console.log('hello!');
7});
8
9const clear = gulp.task('clear', async function() {
10 await fs.emptyDir('./dist/')
11});
12
13const bower = gulp.task('bower', async function () {
14 const npm = JSON.parse(await fs.readFile('package.json'));
15 const bower = JSON.parse(await fs.readFile('bower.json'));
16
17 const fields = [
18 'name',
19 'description',
20 'version',
21 'homepage',
22 'license',
23 'keywords'
24 ];
25
26 for (let i = 0, l = fields.length; i < l; i++) {
27 const field = fields[i];
28 bower[field] = npm[field];
29 }
30
31 await fs.writeFile('bower.json', JSON.stringify(bower, null, 2));
32});
33
34async function getContributors(user, repo, maxCount = 1) {
35 const contributors = (await axios.get(
36 `https://api.github.com/repos/${encodeURIComponent(user)}/${encodeURIComponent(repo)}/contributors`,
37 { params: { per_page: maxCount } }
38 )).data;
39
40 return Promise.all(contributors.map(async (contributor)=> {
41 return {...contributor, ...(await axios.get(
42 `https://api.github.com/users/${encodeURIComponent(contributor.login)}`
43 )).data};
44 }))
45}
46
47const packageJSON = gulp.task('package', async function () {
48 const CONTRIBUTION_THRESHOLD = 3;
49
50 const npm = JSON.parse(await fs.readFile('package.json'));
51
52 try {
53 const contributors = await getContributors('axios', 'axios', 15);
54
55 npm.contributors = contributors
56 .filter(
57 ({type, contributions}) => type.toLowerCase() === 'user' && contributions >= CONTRIBUTION_THRESHOLD
58 )
59 .map(({login, name, url}) => `${name || login} (https://github.com/${login})`);
60
61 await fs.writeFile('package.json', JSON.stringify(npm, null, 2));
62 } catch (err) {
63 if (axios.isAxiosError(err) && err.response && err.response.status === 403) {
64 throw Error(`GitHub API Error: ${err.response.data && err.response.data.message}`);
65 }
66 throw err;
67 }
68});
69
70const env = gulp.task('env', async function () {
71 var npm = JSON.parse(await fs.readFile('package.json'));
72
73 await fs.writeFile('./lib/env/data.js', Object.entries({
74 VERSION: npm.version
75 }).map(([key, value]) => {
76 return `export const ${key} = ${JSON.stringify(value)};`
77 }).join('\n'));
78});
79
80const version = gulp.series('bower', 'env', 'package');
81
82export {
83 bower,
84 env,
85 clear,
86 version,
87 packageJSON
88}