ObjToSchematic/tools/build-palette.ts

68 lines
2.2 KiB
TypeScript
Raw Normal View History

import fs from 'fs';
import path from 'path';
import prompt from 'prompt';
2022-09-12 08:07:42 +08:00
import { Palette } from '../src/palette';
import { AppPaths, PathUtil } from '../src/util/path_util';
2022-09-12 08:07:42 +08:00
import { log, LogStyle } from './logging';
const PALETTE_NAME_REGEX = /^[a-zA-Z\-]+$/;
void async function main() {
AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..'));
log(LogStyle.Info, 'Creating a new palette...');
const paletteBlocksDir = path.join(AppPaths.Get.tools, './new-palette-blocks.txt');
if (!fs.existsSync(paletteBlocksDir)) {
log(LogStyle.Failure, 'Could not find /tools/new-palette-blocks.txt');
return;
}
log(LogStyle.Success, 'Found list of blocks to use in /tools/new-palette-blocks.txt');
let blocksToUse: string[] = fs.readFileSync(paletteBlocksDir, 'utf8').replace(/\r/g, '').split('\n');
blocksToUse = blocksToUse.filter((block) => {
return block.length !== 0;
});
if (blocksToUse.length === 0) {
log(LogStyle.Failure, 'No blocks listed for palette');
log(LogStyle.Info, 'List the blocks you want from /tools/all-supported-blocks.txt ');
return;
}
log(LogStyle.Info, `Found ${blocksToUse.length} blocks to use`);
const schema: prompt.Schema = {
properties: {
paletteName: {
pattern: PALETTE_NAME_REGEX,
description: 'What do you want to call this block palette? (e.g. my-block-palette)',
message: 'Must be only letters or dash',
required: true,
},
},
};
const promptUser = await prompt.get(schema);
log(LogStyle.Info, 'Creating palette...');
const palette = Palette.create();
if (palette === undefined) {
log(LogStyle.Failure, 'Invalid palette name');
return;
}
log(LogStyle.Info, 'Adding blocks to palette...');
for (const blockNames of blocksToUse) {
palette.add(blockNames);
}
log(LogStyle.Info, 'Saving palette...');
const success = palette.save(promptUser.paletteName as string);
if (success) {
log(LogStyle.Success, 'Palette saved.');
} else {
log(LogStyle.Failure, 'Could not save palette.');
}
2022-03-01 03:47:44 +08:00
}();