2022-02-20 05:50:17 +08:00
|
|
|
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';
|
2022-09-12 02:21:33 +08:00
|
|
|
import { AppPaths, PathUtil } from '../src/util/path_util';
|
2022-09-12 08:07:42 +08:00
|
|
|
import { log, LogStyle } from './logging';
|
2022-02-20 05:50:17 +08:00
|
|
|
|
2022-08-20 01:58:17 +08:00
|
|
|
const PALETTE_NAME_REGEX = /^[a-zA-Z\-]+$/;
|
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
void async function main() {
|
2022-09-12 02:21:33 +08:00
|
|
|
AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..'));
|
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
log(LogStyle.Info, 'Creating a new palette...');
|
2022-09-12 02:21:33 +08:00
|
|
|
|
|
|
|
const paletteBlocksDir = path.join(AppPaths.Get.tools, './new-palette-blocks.txt');
|
2022-02-20 05:50:17 +08:00
|
|
|
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');
|
2022-09-12 02:21:33 +08:00
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
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`);
|
2022-09-12 02:21:33 +08:00
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
const schema: prompt.Schema = {
|
|
|
|
properties: {
|
|
|
|
paletteName: {
|
2022-08-20 01:58:17 +08:00
|
|
|
pattern: PALETTE_NAME_REGEX,
|
2022-02-20 05:50:17 +08:00
|
|
|
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);
|
|
|
|
|
2022-08-20 01:58:17 +08:00
|
|
|
log(LogStyle.Info, 'Creating palette...');
|
|
|
|
const palette = Palette.create();
|
|
|
|
if (palette === undefined) {
|
|
|
|
log(LogStyle.Failure, 'Invalid palette name');
|
|
|
|
return;
|
|
|
|
}
|
2022-02-20 05:50:17 +08:00
|
|
|
|
2022-08-20 01:58:17 +08:00
|
|
|
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
|
|
|
}();
|