-
Notifications
You must be signed in to change notification settings - Fork 5
/
version-bump.js
160 lines (139 loc) · 4.76 KB
/
version-bump.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
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
153
154
155
156
157
158
159
160
const fs = require('fs');
const path = require('path');
const process = require('process');
const { exec } = require('child_process');
const minimist = require('minimist');
async function getPublishedVersion(token, publisher, extensionId) {
return new Promise((resolve, reject) => {
const execCmd = `tfx extension show -t ${token} --publisher ${publisher} --extension-id ${extensionId} --json`;
const execOptions = {
maxBuffer: 1024 * 1024 * 512
};
exec(execCmd, execOptions, (err, stdout, stderr) => {
if (err) {
reject({ err, stderr });
return;
}
const extensionData = JSON.parse(stdout);
if (!extensionData) {
// Default to 1.0.0 if no extension is found in the market
resolve({ major: 1, minor: 0, patch: 0 });
return;
}
try {
const [major, minor, patch] = extensionData.versions[0].version.split('.');
resolve({
major: Number(major),
minor: Number(minor),
patch: Number(patch)
});
} catch (err) {
reject(err);
}
});
});
}
async function getJsonContent(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, jsonString) => {
if (err) {
reject(`Error reading file from disk: ${err}`);
return;
}
try {
resolve(JSON.parse(jsonString));
} catch (err) {
reject(`Error parsing JSON string: ${err}`);
}
});
});
}
async function setJsonContent(filePath, content) {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, JSON.stringify(content, null, 2), err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
async function updateJsonContent(filePath, update) {
const content = await getJsonContent(filePath);
update(content);
await setJsonContent(filePath, content);
console.log(`Updated file: ${filePath}`);
}
const releaseTypes = new Set(['dev', 'hotfix', 'prod']);
const run = async args => {
if (!args.token) throw new Error('Argument --token is missing (Azure DevOps PAT token for VS Marketplace)');
if (!args['release-type'] || !releaseTypes.has(args['release-type']))
throw new Error('Argument --release-type is missing (valid values: dev, hotfix, prod)');
console.log(`Updating extension information for ${args['release-type']}`);
const settings = {
id: 'HelmfileInstaller',
name: 'Helmfile Installer',
publisher: 'GSoft',
version: { major: 1, minor: 0, patch: 0 },
galleryFlags: ['Public'],
public: true
};
const prodVersion = await getPublishedVersion(args.token, settings.publisher, settings.id);
console.log('Current production extension version:', JSON.stringify(prodVersion, null, 2));
switch (args['release-type']) {
case 'dev':
settings.id += '-dev';
settings.name += ' (dev)';
settings.publisher = 'gsoft-dev';
settings.galleryFlags = ['Preview'];
settings.public = false;
const devVersion = await getPublishedVersion(args.token, settings.publisher, settings.id);
console.log('Current development extension version:', JSON.stringify(devVersion, null, 2));
settings.version.major = Math.max(prodVersion.major, devVersion.major);
settings.version.minor = Math.max(prodVersion.minor, devVersion.minor);
settings.version.patch = prodVersion.major !== devVersion.major || prodVersion.minor !== devVersion.minor ? 1 : devVersion.patch + 1;
break;
case 'hotfix':
settings.version = prodVersion;
settings.version.patch++;
break;
case 'prod':
settings.version = prodVersion;
settings.version.minor++;
settings.version.patch = 0;
break;
}
console.log('New extension information will be:', JSON.stringify(settings, null, 2));
const vssExtensionJsonPath = path.resolve(__dirname, 'vss-extension.json');
await updateJsonContent(vssExtensionJsonPath, content => {
content.id = settings.id;
content.version = `${settings.version.major}.${settings.version.minor}.${settings.version.patch}`;
content.name = settings.name;
content.publisher = settings.publisher;
content.galleryFlags = settings.galleryFlags;
content.public = settings.public;
});
const taskJsonPath = path.resolve(__dirname, 'task/task.json');
await updateJsonContent(taskJsonPath, content => {
content.version = {
Major: settings.version.major,
Minor: settings.version.minor,
Patch: settings.version.patch
};
});
};
const args = minimist(process.argv.slice(2), {
string: ['token', 'release-type'],
default: { 'release-type': 'dev' }
});
run(args).then(
() => {
console.log('Version bump finished');
process.exit(0);
},
err => {
console.error(err);
process.exit(1);
}
);