-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.js
More file actions
141 lines (123 loc) · 4.68 KB
/
version.js
File metadata and controls
141 lines (123 loc) · 4.68 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
const fs = require('fs');
const path = require('path');
const versionFile = './version.json';
const indexFile = './index.html';
function createVersionFileIfNotExists() {
if (!fs.existsSync(versionFile)) {
const initialVersion = {
version: '1.0.0',
date: new Date().toISOString().split('T')[0],
changelog: 'Version initiale',
history: [{
version: '1.0.0',
date: new Date().toISOString().split('T')[0],
changelog: 'Version initiale',
type: 'initial'
}]
};
fs.writeFileSync(versionFile, JSON.stringify(initialVersion, null, 2));
console.log('Fichier version.json créé avec la version 1.0.0');
}
}
function updateVersion(type = 'patch', changelog) {
// Vérifier que le changelog est fourni
if (!changelog) {
console.error('Erreur: Un changelog est obligatoire pour la mise à jour de version.');
console.log('Usage: node version.js [major|minor|patch] add "changelog des changements"');
process.exit(1);
}
// Créer le fichier version.json s'il n'existe pas
createVersionFileIfNotExists();
// Lire la version actuelle
const versionData = JSON.parse(fs.readFileSync(versionFile, 'utf8'));
const [major, minor, patch] = versionData.version.split('.').map(Number);
let newVersion;
const normalizedType = type.toLowerCase();
switch(normalizedType) {
case 'major':
newVersion = `${major + 1}.0.0`;
break;
case 'minor':
newVersion = `${major}.${minor + 1}.0`;
break;
case 'patch':
default:
newVersion = `${major}.${minor}.${patch + 1}`;
break;
}
// Mettre à jour version.json avec le changelog
const currentDate = new Date().toISOString().split('T')[0];
versionData.version = newVersion;
versionData.date = currentDate; // Changé de buildDate à date
versionData.changelog = changelog;
// Ajouter l'historique des versions
if (!versionData.history) {
versionData.history = [];
}
versionData.history.unshift({
version: newVersion,
date: currentDate,
changelog: changelog,
type: type
});
fs.writeFileSync(versionFile, JSON.stringify(versionData, null, 2));
// Mettre à jour index.html
let htmlContent = fs.readFileSync(indexFile, 'utf8');
htmlContent = htmlContent.replace(
/Version: <span id="app-version">[\d.]+<\/span>/,
`Version: <span id="app-version">${newVersion}</span>`
);
fs.writeFileSync(indexFile, htmlContent);
console.log(`Version mise à jour vers ${newVersion}`);
console.log(`Changelog: ${changelog}`);
}
function showCurrentVersion() {
createVersionFileIfNotExists();
const versionData = JSON.parse(fs.readFileSync(versionFile, 'utf8'));
console.log(`Version actuelle: ${versionData.version}`);
}
function showCurrentChangelog() {
createVersionFileIfNotExists();
const versionData = JSON.parse(fs.readFileSync(versionFile, 'utf8'));
console.log(`Changelog actuel: ${versionData.changelog}`);
}
// Gérer les arguments de ligne de commande
const args = process.argv.slice(2);
// Si "now" est le premier argument, utiliser patch par défaut
if (args[0] === 'now') {
const changelog = args.slice(1).join(' ');
if (!changelog) {
showCurrentVersion();
showCurrentChangelog();
process.exit(0);
}
updateVersion('patch', changelog);
} else if (args[0] === 'version') {
showCurrentVersion();
} else if (args[0] === 'changelog') {
showCurrentChangelog();
} else if (args[0] === 'add') {
// Format: node version.js add "changelog" (patch par défaut)
const changelog = args.slice(1).join(' ');
if (!changelog) {
showCurrentVersion();
process.exit(0);
}
updateVersion('patch', changelog);
} else {
// Format: node version.js [major|minor|patch] add "changelog"
const type = args[0] || 'patch';
const addKeyword = args[1];
const changelog = args.slice(2).join(' ');
if (addKeyword !== 'add') {
console.error('Erreur: Le mot-clé "add" est requis.');
console.log('Usage: node version.js [major|minor|patch] add "changelog des changements"');
console.log('Ou: node version.js add "changelog des changements" (pour patch par défaut)');
process.exit(1);
}
if (!changelog) {
showCurrentVersion();
process.exit(0);
}
updateVersion(type, changelog);
}