-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
121 lines (99 loc) · 3.01 KB
/
release.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
#!/usr/bin/env node
import { readFileSync, writeFileSync } from 'fs'
import axios from 'axios'
import { exec } from 'child_process'
import { changelog } from './changelog.js'
const execShellCommand = (cmd) => {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
resolve(stdout ? stdout : stderr);
});
});
};
const [ version_number, prerelease ] = process.argv.slice(2)
let isPrerelease = false
if (prerelease) isPrerelease = true
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
if (!version_number) {
console.log('PAS DE VERSION DE RENSEIGNE')
process.exit(1)
}
const repo_owner = 'huiitre'
const repo_name = 'dtl_runpda'
const access_token = readFileSync('./github_token.txt', 'utf-8')
const createNpmTag = async (tag) => {
try {
// Créer le tag et incrémenter la version dans package.json
await execShellCommand(`npm version ${tag}`);
// Push des modifications dans le package.json
await execShellCommand('git push');
// Push du tag créé
await execShellCommand(`git push origin v${tag}`);
// Publication sur npm
await execShellCommand('npm publish');
return true
} catch (error) {
console.error('Une erreur est survenue lors de l\'exécution des commandes :', error);
throw error
}
}
const getRelease = ({ tag, changelog }) => {
return new Promise((resolve, reject) => {
axios.get(`https://api.github.com/repos/${repo_owner}/${repo_name}/releases/tags/${version_number}`, {
headers: {
Authorization: `token ${access_token}`
}
})
.then(data => {
resolve(data)
})
.catch(err => {
if (err.response.status == '404')
reject(`Le tag ${tag} n'existe pas`)
reject(err)
})
})
}
const createRelease = ({ tag, changelog }) => {
return new Promise((resolve, reject) => {
const releaseData = {
tag_name: tag,
name: `${tag}`,
body: `${changelog}`,
draft: false,
prerelease: isPrerelease
};
axios.post(`https://api.github.com/repos/${repo_owner}/${repo_name}/releases`, releaseData, {
headers: {
Authorization: `token ${access_token}`,
"Content-Type": "application/json"
}
})
.then(response => {
resolve(response.data);
})
.catch(error => {
reject(error);
});
});
}
(async() => {
try {
//* récupération du changelog
const changelogToVersion = changelog[`v${version_number}`] || false
if (!changelogToVersion)
throw(`Le changelog pour la version v${version_number} est vide`)
const result = await createNpmTag(version_number)
// writeFileSync('res.json', JSON.stringify(result))
await delay(5000);
const result2 = await createRelease({ tag: `v${version_number}`, changelog: changelogToVersion})
// writeFileSync('res2.json', JSON.stringify(result2))
} catch(err) {
console.log(err.toString())
// writeFileSync('err.json', err.toString())
}
})()