bedrock-development
Version:
APIs for creating and editing files related to Minecraft Bedrock development.
148 lines (147 loc) • 5.04 kB
JavaScript
import { Chalk } from 'chalk';
import * as path from 'path';
import { Directories, getFiles, setFiles } from './file_manager.js';
/**
* @remarks A globally accessible instance of the {@link Chalk} class that provides colored text in the terminal.
*/
export const chalk = new Chalk();
/**
* @remarks The format version shared by all Minecraft Types.
*/
export const currentFormatVersion = '1.20.50';
/**
* @remarks A class for working with name data like identifiers.
*/
export class NameData {
/**
* @remarks The name of the development team.
*/
static get TeamName() {
return NameData.teamName;
}
/**
* @remarks The name of the Project
*/
static get ProjectName() {
return NameData.projectName;
}
/**
* @remarks Creates a namedata object from a source string.
* @param name The source string to create namedata from.
* @example
* ```typescript
* let name = new NameData("subfolder/minecraft:test");
* ```
*/
constructor(name) {
var _a, _b;
this.original = name;
this.fullname = path.basename(name);
this.namespace = (_a = this.fullname.split(/\.|:/)[1]) !== null && _a !== void 0 ? _a : `${NameData.teamName}_${NameData.ProjectName}`;
this.shortname = (_b = this.fullname.split(/\.|:/).pop()) !== null && _b !== void 0 ? _b : '';
if (!this.fullname.includes(this.namespace)) {
this.fullname = this.namespace + ':' + this.shortname;
}
this.directory = path.dirname(name) + '/';
if (this.directory === './') {
this.directory = '';
}
const words = this.splitWords(this.shortname);
for (let i = 0; i < words.length; i++) {
words[i] = words[i][0].toUpperCase() + words[i].substring(1);
}
this.display = words.join(' ');
}
splitWords(name) {
name = name.replace(/_/g, ' ');
return name.split(' ');
}
static setAddonNamespace(namespace) {
var _a, _b;
NameData.teamName = (_a = namespace.split(/_/).shift()) !== null && _a !== void 0 ? _a : '';
NameData.projectName = (_b = namespace.split(/_/).pop()) !== null && _b !== void 0 ? _b : '';
}
}
NameData.teamName = '';
NameData.projectName = '';
/**
* @remarks Determines if a value is an object or a primitive.
* @param item The item to check.
* @returns True if the item is an object.
*/
export function isObject(item) {
return item && typeof item === "object" && !Array.isArray(item);
}
/**
* @remarks Performs a deep merge between two objects.
* @param target The target object to merge with.
* @param source The source object to merge with the target.
* @returns An object with the properties of the source and target merged deeply.
* @example
* ```typescript
* mergeDeep({subProperty: {targetKey: 1}}, {subProperty: {sourceKey: 2}});
* // Returns {subProperty: {targetKey: 1, sourceKey: 2}};
* ```
*/
export function mergeDeep(target, source) {
let output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach((key) => {
if (isObject(source[key])) {
if (!(key in target))
Object.assign(output, { [key]: source[key] });
else
output[key] = mergeDeep(target[key], source[key]);
}
else if (Array.isArray(source[key])) {
if (!(key in target))
Object.assign(output, { [key]: source[key] });
else
Object.assign(output, { [key]: target[key].concat(source[key]) });
}
else {
Object.assign(output, { [key]: source[key] });
}
});
}
return output;
}
/**
* @remarks Gets the config data from the working directory.
*/
export function getConfig() {
const config = getFiles('bedrock.config.json')[0];
if (config) {
return JSON.parse(config.fileContents);
}
}
export function implementConfig() {
const config = getConfig();
if (config) {
setAddonName(config.addon_namespace);
console.log(`${chalk.green(`Got addon namespace (${config.addon_namespace}) from config file`)}`);
}
}
/**
* @remarks Sets the bedrock.config.json file contents.
* @param config The config data to write.
*/
export function setConfig(config) {
const files = getFiles('bedrock.config.json');
if (!files.length) {
files.push({ filePath: 'bedrock.config.json', fileContents: '' });
}
files.forEach(file => {
file.fileContents = JSON.stringify(config, null, '\t');
file.handleExisting = 'overwrite';
});
setFiles(files);
}
/**
* @remarks Sets the global addon data from the addon namespace.
* @param addon The addon name as <team_name>_<project_name>.
*/
export function setAddonName(addon) {
Directories.ADDON_PATH = addon.replace(/_/, '/');
NameData.setAddonNamespace(addon);
}