-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprerelease_to_release.js
118 lines (98 loc) · 2.96 KB
/
prerelease_to_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
#!/usr/bin/env node
import { readFileSync, writeFileSync } from 'fs'
import axios from 'axios'
import { exec } from 'child_process'
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 ] = process.argv.slice(2)
if (!version_number) {
console.log('PAS DE VERSION DE RENSEIGNE')
process.exit(1)
}
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const repo_owner = 'huiitre'
const repo_name = 'dtl_runpda'
const access_token = readFileSync('./github_token.txt', 'utf-8')
const getPreRelease = (tag) => {
return new Promise((resolve, reject) => {
axios.get(`https://api.github.com/repos/${repo_owner}/${repo_name}/releases/tags/${tag}`, {
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 updatePreReleaseToRelease = (releaseId) => {
return new Promise((resolve, reject) => {
const release_data = {
tag_name: `${version_number}`,
name: `${version_number}`,
prerelease: false,
make_latest: true
};
axios.patch(`https://api.github.com/repos/${repo_owner}/${repo_name}/releases/${releaseId}`, release_data, {
headers: {
Authorization: `token ${access_token}`,
"Content-Type": "application/json"
}
})
.then(data => {
resolve(data)
})
.catch(err => reject(err))
})
}
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
}
}
(async() => {
try {
const { data } = await getPreRelease(`v${version_number}`)
writeFileSync('res.json', JSON.stringify(data))
const releaseId = data.id
const isPrerelease = data.prerelease
if (!isPrerelease) {
throw (`Le tag ${version_number} n'est pas une pré-release`)
}
//* on push le tag sur npm
// const result = await createNpmTag(version_number)
// writeFileSync('res.json', JSON.stringify(result))
await delay(5000)
//* on update le tag sur git
const { data: data2 } = await updatePreReleaseToRelease(releaseId)
// writeFileSync('res2.json', JSON.stringify(data2))
} catch(err) {
console.log(err.toString())
writeFileSync('err.json', err.toString())
}
})()