v3mt
Version:
A CLI toolkit for managing and deploying Victoria 3 mods, including sending mod files to the game, launching the game, and more.
84 lines (83 loc) • 3.21 kB
JavaScript
import fs from 'fs';
import path from 'path';
import * as jsonc from 'jsonc-parser';
import { vsc_presentation, vsc_tasks } from './vsc-source.js';
import ensureDirSync from '../../utils/filesystem/tools/ensureDirSync.js';
import Logger from '../../utils/logger/logger.js';
const command = 'setup-vsc-tasks';
const use = (program) => {
return program.command(command).description('Setup tasks within Visual Studio Code').action(task);
};
const setupVSCTasks = { use, command, task };
export default setupVSCTasks;
async function task() {
const vscodeDir = ensureDirSync(path.resolve(process.cwd(), '.vscode'));
const tasksFile = path.join(vscodeDir, 'tasks.json');
if (!fs.existsSync(tasksFile)) {
createTasksFile(tasksFile);
}
else {
updateTasksFile(tasksFile);
}
}
function createTasksFile(tasksFile) {
const newContent = {
version: '2.0.0',
presentation: vsc_presentation,
tasks: vsc_tasks,
};
fs.writeFileSync(tasksFile, JSON.stringify(newContent, null, 2), 'utf8');
Logger.pass(`Created tasks.json at ${tasksFile}`);
}
function updateTasksFile(tasksFile) {
let content = fs.readFileSync(tasksFile, 'utf8');
const parsed = jsonc.parse(content);
if (!parsed || typeof parsed !== 'object') {
Logger.kill('Invalid tasks.json format, aborting update.');
}
const formattingOptions = { insertSpaces: true, tabSize: 2 };
try {
if (!parsed.version) {
const edits = jsonc.modify(content, ['version'], '2.0.0', {
formattingOptions,
});
content = jsonc.applyEdits(content, edits);
}
if (!parsed.presentation) {
const edits = jsonc.modify(content, ['presentation'], vsc_presentation, {
formattingOptions,
});
content = jsonc.applyEdits(content, edits);
}
else {
for (const key in vsc_presentation) {
if (parsed.presentation[key] === undefined) {
const edits = jsonc.modify(content, ['presentation', key], vsc_presentation[key], {
formattingOptions,
});
content = jsonc.applyEdits(content, edits);
}
}
}
if (!Array.isArray(parsed.tasks)) {
const edits = jsonc.modify(content, ['tasks'], [], { formattingOptions });
content = jsonc.applyEdits(content, edits);
}
const currentTasks = parsed.tasks ?? [];
const tasksToAdd = vsc_tasks.filter((newTask) => !currentTasks.some((t) => t.label === newTask.label));
if (tasksToAdd.length > 0) {
for (const newTask of tasksToAdd) {
const edits = jsonc.modify(content, ['tasks', -1], newTask, {
formattingOptions,
isArrayInsertion: true,
});
content = jsonc.applyEdits(content, edits);
}
}
fs.writeFileSync(tasksFile, content, 'utf8');
Logger.pass('Updated tasks.json successfully');
}
catch (err) {
Logger.kill('Failed to modify tasks.json');
}
}