love/source/scripts/sublime-text.ts

76 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-04-14 11:26:29 +00:00
import {promises as fsp} from 'fs';
import {join} from 'path';
import Zip from 'jszip';
import nunjucks from 'nunjucks';
import {generateLove, LoveVariant} from './love';
import {getVersions, Versions} from './version';
2020-04-14 11:26:29 +00:00
export async function main(): Promise<void> {
2020-04-14 11:26:29 +00:00
const themeDirectory: string = join(__dirname, '../sublime-text/');
2020-04-14 11:49:46 +00:00
// Configure Nunjucks to use the templates for `source/sublime-text/`.
2020-04-14 11:26:29 +00:00
nunjucks.configure(themeDirectory, {
lstripBlocks: true,
trimBlocks: true,
throwOnUndefined: true
});
const love: LoveVariant[] = generateLove();
const versions: Versions = await getVersions();
2020-04-14 11:26:29 +00:00
const sublimePackage: Zip = new Zip();
for (const variant of love) {
const template: string = await fsp.readFile(
join(themeDirectory, 'love-template.sublime-color-scheme'),
'utf8'
);
const outputPath: string = join(
themeDirectory,
`love-${variant.name}.sublime-color-scheme`
);
const output: string = nunjucks.renderString(template, {
love: variant,
version: versions['sublime-text']
2020-04-14 11:26:29 +00:00
});
let formattedOutput = '';
for (const line of output.split('\n')) {
// Don't include the line in the output if it's a comment:
// * Starts with `//` or
// * Starts with `/*` or
// * Ends with `*/`
// This allows us to use Nunjucks templating inside the JSON.
if (
/^\s+\/\/.+$/.exec(line) ||
/^\s+\/\*.+$/.exec(line) ||
/\*\/$/.exec(line)
) {
continue;
}
formattedOutput += line;
formattedOutput += '\n';
}
let outputObject: any;
try {
outputObject = JSON.parse(formattedOutput);
} catch (error) {
console.log('Could not parse formatted output as regular JSON:');
throw error;
}
await fsp.writeFile(outputPath, JSON.stringify(outputObject));
sublimePackage.file(
`${String(outputObject.name)}.sublime-color-scheme`,
JSON.stringify(outputObject)
);
}
const zip: Buffer = await sublimePackage.generateAsync({type: 'nodebuffer'});
await fsp.writeFile(join(themeDirectory, 'Love.sublime-package'), zip);
}
if (require.main === module) {
void main();
2020-04-14 11:26:29 +00:00
}