Feature: `cadl install` command (#54)

This commit is contained in:
Timothee Guerin 2021-11-11 14:25:53 -08:00 коммит произвёл GitHub
Родитель f44ba2d20a
Коммит 88a51f1b6d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 51 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/compiler",
"comment": "**Added** `cadl install` command which shell out to `npm install`",
"type": "minor"
}
],
"packageName": "@cadl-lang/compiler"
}

Просмотреть файл

@ -12,6 +12,7 @@ import { compile, Program } from "../core/program.js";
import { initCadlProject } from "../init/index.js"; import { initCadlProject } from "../init/index.js";
import { compilerAssert, dumpError, logDiagnostics } from "./diagnostics.js"; import { compilerAssert, dumpError, logDiagnostics } from "./diagnostics.js";
import { findUnformattedCadlFiles, formatCadlFiles } from "./formatter.js"; import { findUnformattedCadlFiles, formatCadlFiles } from "./formatter.js";
import { installCadlDependencies } from "./install.js";
import { Diagnostic } from "./types.js"; import { Diagnostic } from "./types.js";
import { cadlVersion, NodeHost } from "./util.js"; import { cadlVersion, NodeHost } from "./util.js";
@ -161,6 +162,12 @@ async function main() {
}), }),
(args) => initCadlProject(NodeHost, process.cwd(), args.templatesUrl) (args) => initCadlProject(NodeHost, process.cwd(), args.templatesUrl)
) )
.command(
"install",
"Install cadl dependencies",
() => {},
() => installCadlDependencies(process.cwd())
)
.command( .command(
"info", "info",
"Show information about current Cadl compiler.", "Show information about current Cadl compiler.",

Просмотреть файл

@ -0,0 +1,34 @@
import { spawn } from "child_process";
interface SpawnError {
errno: number;
code: "ENOENT";
sysCall: string;
path: string;
spawnArgs: string[];
}
export async function installCadlDependencies(directory: string): Promise<void> {
const cmd = process.platform === "win32" ? "npm.cmd" : "npm";
const child = spawn(cmd, ["install"], {
stdio: "inherit",
cwd: directory,
env: process.env,
});
return new Promise(() => {
child.on("error", (error: SpawnError) => {
if (error.code === "ENOENT") {
console.error(
"Cannot find `npm` executable. Make sure to have npm installed in your path."
);
} else {
console.error("Error", error);
}
process.exit(error.errno);
});
child.on("exit", (exitCode) => {
process.exit(exitCode ?? -1);
});
});
}