"use strict";
// Copyright 2022 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEmbeddedProtocol = void 0;
const path_equal_1 = require("path-equal");
const shell = require("shelljs");
const pkg = require("../package.json");
const utils = require("./utils");
/**
 * Downloads and builds the Embedded Sass protocol definition.
 *
 * Can check out and build the source from a Git `ref` or build from the source
 * at `path`. By default, checks out the tagged version specified in
 * package.json's `protocol-version` field. If this version ends in `-dev`,
 * checks out the latest revision from GitHub instead.
 */
async function getEmbeddedProtocol(outPath, options) {
    if (!options || 'ref' in options) {
        let ref = options === null || options === void 0 ? void 0 : options.ref;
        if (ref === undefined) {
            const version = pkg['protocol-version'];
            ref = version.endsWith('-dev') ? 'main' : version;
        }
        utils.fetchRepo({ repo: 'embedded-protocol', outPath: 'build', ref });
    }
    else if (!(0, path_equal_1.pathEqual)(options.path, 'build/embedded-protocol')) {
        await utils.cleanDir('build/embedded-protocol');
        await utils.link(options.path, 'build/embedded-protocol');
    }
    buildEmbeddedProtocol();
}
exports.getEmbeddedProtocol = getEmbeddedProtocol;
// Builds the embedded proto into a TS file.
function buildEmbeddedProtocol() {
    const version = shell.exec('npx buf --version', { silent: true }).stdout.trim();
    console.log(`Building TS with buf ${version}.`);
    shell.exec('npx buf generate');
}
//# sourceMappingURL=get-embedded-protocol.js.map