love/source/scripts/version.ts

120 lines
2.8 KiB
TypeScript

import {promises as fsp} from 'fs';
import {join} from 'path';
import prompts from 'prompts';
import semver from 'semver';
export interface Versions {
[index: string]: string;
'css-custom-properties': string;
firefox: string;
kitty: string;
'sublime-text': string;
tauon: string;
vscode: string;
}
export async function main(): Promise<void> {
const versions: Versions = await getVersions();
const command = await prompts({
message: 'What do you want to do?',
name: 'command',
type: 'select',
choices: [
{
title: 'Show',
value: 'show'
},
{
title: 'Update',
value: 'update'
}
],
initial: 0
});
if (command.command === 'show') {
console.log(versions);
return;
}
if (command.command === 'update') {
const whichTheme = await prompts({
message: 'Which theme do you want to update?',
name: 'theme',
type: 'select',
choices: Object.keys(versions).map((value) => ({
title: `${value} (${String(versions[value])})`,
value
})),
initial: 0
});
const currentVersion: string = versions[whichTheme.theme];
const whichVersion = await prompts({
message: 'To what version do you want to update?',
name: 'version',
type: 'select',
choices: [
{
title: `Major (${currentVersion} -> ${semver.inc(
currentVersion,
'major'
)!})`,
value: 'major'
},
{
title: `Minor (${currentVersion} -> ${semver.inc(
currentVersion,
'minor'
)!})`,
value: 'minor'
},
{
title: `Patch (${currentVersion} -> ${semver.inc(
currentVersion,
'patch'
)!})`,
value: 'patch'
}
],
initial: 0
});
const selectedTheme: string = whichTheme.theme;
const newVersion: string = semver.inc(
currentVersion,
whichVersion.version
)!;
versions[selectedTheme] = newVersion;
await fsp.writeFile(
join(__dirname, '../../love-versions.json'),
JSON.stringify(versions, null, 2) + '\n'
);
if (['vscode'].includes(selectedTheme)) {
const packageLocation: string = join(
__dirname,
`../${selectedTheme}/package.json`
);
const themePackage: any = JSON.parse(
await fsp.readFile(packageLocation, 'utf8')
);
themePackage.version = newVersion;
await fsp.writeFile(
packageLocation,
JSON.stringify(themePackage, null, 2) + '\n'
);
}
}
}
export async function getVersions(): Promise<Versions> {
return JSON.parse(
await fsp.readFile(join(__dirname, '../../love-versions.json'), 'utf8')
);
}
if (require.main === module) {
void main();
}