如何在 Yeoman 生成器中创建一个新的 .net 核心项目

How to crete a new .net core project in the Yeoman generator

我正在编写一个 Yeoman 生成器。怎么调用

dotnet new

在我的发电机里? 有没有更好的方法在 yeoman 生成器中创建一个新的 .net 核心项目?

一种可能是直接调用 dotnet new

const { execSync, exec } = require('child_process');

function formated_exec_sync(command) {
    console.log("exec->" + command);
    execSync(command, {stdio: 'inherit'});
}

function new_solution(name) {
    formated_exec_sync(`dotnet new sln --name ${name}`);
}

function new_console_project(name) {
    formated_exec_sync(`dotnet new console -lang "C#" --name ${name}`);
}

function add_project_to_solution(project_name, solution_name) {
    formated_exec_sync(`dotnet sln ${solution_name}.sln add ${project_name}`);
}

function add_package_to_project(package, project, version = null) {
    let command = `dotnet add ${project} package`;
    if (version != null)
        command += ` --version ${version}`;
    command += ` ${package}`
    formated_exec_sync(command);
}

module.exports = {
    new_solution,
    new_console_project,
    add_project_to_solution,
    add_package_to_project,
}