@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
228 lines (227 loc) • 10.3 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const IProjectItemData_1 = require("./IProjectItemData");
const NpmPackageDefinition_1 = __importDefault(require("../devproject/NpmPackageDefinition"));
const JustConfig_1 = __importDefault(require("../devproject/JustConfig"));
const EslintConfig_1 = __importDefault(require("../devproject/EslintConfig"));
const PrettierRcConfig_1 = __importDefault(require("../devproject/PrettierRcConfig"));
const EnvSettings_1 = __importDefault(require("../devproject/EnvSettings"));
const VsCodeSettingsDefinition_1 = __importDefault(require("../devproject/VsCodeSettingsDefinition"));
const VsCodeLaunchDefinition_1 = __importDefault(require("../devproject/VsCodeLaunchDefinition"));
const VsCodeTasksDefinition_1 = __importDefault(require("../devproject/VsCodeTasksDefinition"));
const VsCodeExtensionsDefinition_1 = __importDefault(require("../devproject/VsCodeExtensionsDefinition"));
const BehaviorManifestDefinition_1 = __importDefault(require("../minecraft/BehaviorManifestDefinition"));
const IFile_1 = require("../storage/IFile");
class ProjectSetup {
static async setup(project, log) {
const results = [];
await project.inferProjectItemsFromFiles();
const projectFolder = project.projectFolder;
if (!projectFolder) {
log.error("No project folder found.");
return results;
}
// Read behavior pack manifest title
const bpTitle = await ProjectSetup.getBehaviorPackTitle(project);
// Ensure package.json
results.push(await ProjectSetup.ensurePackageJson(project, projectFolder, bpTitle));
// Ensure just.config.ts
results.push(await ProjectSetup.ensureJustConfig(projectFolder));
// Ensure eslint.config.mjs
results.push(await ProjectSetup.ensureEslintConfig(projectFolder));
// Ensure .prettierrc.json
results.push(await ProjectSetup.ensurePrettierRc(projectFolder));
// Ensure .env
results.push(await ProjectSetup.ensureEnv(project, projectFolder));
// Ensure .vscode files
results.push(await ProjectSetup.ensureVsCodeExtensions(projectFolder));
results.push(await ProjectSetup.ensureVsCodeLaunch(projectFolder));
results.push(await ProjectSetup.ensureVsCodeSettings(projectFolder));
results.push(await ProjectSetup.ensureVsCodeTasks(projectFolder));
return results;
}
static async getBehaviorPackTitle(project) {
const items = project.getItemsCopy();
for (const item of items) {
if (item.itemType === IProjectItemData_1.ProjectItemType.behaviorPackManifestJson && item.primaryFile) {
const manifest = await BehaviorManifestDefinition_1.default.ensureOnFile(item.primaryFile);
if (manifest && manifest.name) {
return manifest.name;
}
}
}
return undefined;
}
static async ensurePackageJson(project, projectFolder, bpTitle) {
const filePath = "package.json";
const file = await projectFolder.ensureFileFromRelativePath("/" + filePath);
const existed = await file.exists();
const npmPackage = await NpmPackageDefinition_1.default.ensureOnFile(file);
if (!npmPackage) {
return { filePath, status: "unchanged" };
}
const changed = await npmPackage.ensureSetupContent(bpTitle);
if (changed || !existed) {
await npmPackage.persist();
await file.saveContent(false);
let packageJsonContent;
if (file.content && typeof file.content === "string") {
packageJsonContent = file.content;
}
else if (npmPackage.definition) {
packageJsonContent = JSON.stringify(npmPackage.definition, null, 2);
}
return {
filePath,
status: existed ? "updated" : "created",
packageJsonContent,
};
}
return { filePath, status: "unchanged" };
}
static async ensureJustConfig(projectFolder) {
const filePath = "just.config.ts";
const file = await projectFolder.ensureFileFromRelativePath("/" + filePath);
const existed = await file.exists();
const justConfig = await JustConfig_1.default.ensureOnFile(file);
if (!justConfig) {
return { filePath, status: "unchanged" };
}
await justConfig.ensureMin();
await justConfig.save();
if (!existed) {
return { filePath, status: "created" };
}
return { filePath, status: "unchanged" };
}
static async ensureEslintConfig(projectFolder) {
const filePath = "eslint.config.mjs";
const file = await projectFolder.ensureFileFromRelativePath("/" + filePath);
const existed = await file.exists();
const eslintConfig = await EslintConfig_1.default.ensureOnFile(file);
if (!eslintConfig) {
return { filePath, status: "unchanged" };
}
await eslintConfig.ensureMin();
await eslintConfig.save();
if (!existed) {
return { filePath, status: "created" };
}
return { filePath, status: "unchanged" };
}
static async ensurePrettierRc(projectFolder) {
const filePath = ".prettierrc.json";
const file = await projectFolder.ensureFileFromRelativePath("/" + filePath);
const existed = await file.exists();
const prettierRc = await PrettierRcConfig_1.default.ensureOnFile(file);
if (!prettierRc) {
return { filePath, status: "unchanged" };
}
await prettierRc.ensureMinContent();
const persisted = await prettierRc.persist();
if (persisted) {
await file.saveContent(false);
return { filePath, status: existed ? "updated" : "created" };
}
if (!existed) {
return { filePath, status: "created" };
}
return { filePath, status: "unchanged" };
}
static async ensureEnv(project, projectFolder) {
const filePath = ".env";
const file = await projectFolder.ensureFileFromRelativePath("/" + filePath);
const existed = await file.exists();
await file.loadContent();
const existingContent = file.content && typeof file.content === "string" ? file.content : undefined;
const newContent = await EnvSettings_1.default.getContent(project, existingContent);
const changed = file.setContentIfSemanticallyDifferent(newContent, IFile_1.FileUpdateType.versionlessEdit);
if (changed || !existed) {
await file.saveContent(false);
return { filePath, status: existed ? "updated" : "created" };
}
return { filePath, status: "unchanged" };
}
static async ensureVsCodeExtensions(projectFolder) {
const filePath = ".vscode/extensions.json";
const file = await projectFolder.ensureFileFromRelativePath("/" + filePath);
const existed = await file.exists();
const extensions = await VsCodeExtensionsDefinition_1.default.ensureOnFile(file);
if (!extensions) {
return { filePath, status: "unchanged" };
}
await extensions.ensureMinContent();
const persisted = await extensions.persist();
if (persisted) {
await file.saveContent(false);
return { filePath, status: existed ? "updated" : "created" };
}
if (!existed) {
return { filePath, status: "created" };
}
return { filePath, status: "unchanged" };
}
static async ensureVsCodeLaunch(projectFolder) {
const filePath = ".vscode/launch.json";
const file = await projectFolder.ensureFileFromRelativePath("/" + filePath);
const existed = await file.exists();
const launch = await VsCodeLaunchDefinition_1.default.ensureOnFile(file);
if (!launch) {
return { filePath, status: "unchanged" };
}
await launch.ensureMinContent();
const persisted = await launch.persist();
if (persisted) {
await file.saveContent(false);
return { filePath, status: existed ? "updated" : "created" };
}
if (!existed) {
return { filePath, status: "created" };
}
return { filePath, status: "unchanged" };
}
static async ensureVsCodeSettings(projectFolder) {
const filePath = ".vscode/settings.json";
const file = await projectFolder.ensureFileFromRelativePath("/" + filePath);
const existed = await file.exists();
const settings = await VsCodeSettingsDefinition_1.default.ensureOnFile(file);
if (!settings) {
return { filePath, status: "unchanged" };
}
await settings.ensureMinContent();
const persisted = await settings.persist();
if (persisted) {
await file.saveContent(false);
return { filePath, status: existed ? "updated" : "created" };
}
if (!existed) {
return { filePath, status: "created" };
}
return { filePath, status: "unchanged" };
}
static async ensureVsCodeTasks(projectFolder) {
const filePath = ".vscode/tasks.json";
const file = await projectFolder.ensureFileFromRelativePath("/" + filePath);
const existed = await file.exists();
const tasks = await VsCodeTasksDefinition_1.default.ensureOnFile(file);
if (!tasks) {
return { filePath, status: "unchanged" };
}
await tasks.ensureMinContent();
const persisted = await tasks.persist();
if (persisted) {
await file.saveContent(false);
return { filePath, status: existed ? "updated" : "created" };
}
if (!existed) {
return { filePath, status: "created" };
}
return { filePath, status: "unchanged" };
}
}
exports.default = ProjectSetup;