"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.nodeArchToDartArch = exports.nodePlatformToDartPlatform = void 0;
const extractZip = require("extract-zip");
const fs_1 = require("fs");
const p = require("path");
const tar_1 = require("tar");
const yargs_1 = require("yargs");
const pkg = require("../package.json");
const utils = require("./utils");
const argv = (0, yargs_1.default)(process.argv.slice(2))
    .option('package', {
    type: 'string',
    description: 'Directory name under `npm` directory that contains optional dependencies.',
    demandOption: true,
    choices: Object.keys(pkg.optionalDependencies).map(name => name.split('sass-embedded-')[1]),
})
    .parseSync();
// Converts a Node-style platform name as returned by `process.platform` into a
// name used by Dart Sass. Throws if the operating system is not supported by
// Dart Sass Embedded.
function nodePlatformToDartPlatform(platform) {
    switch (platform) {
        case 'android':
            return 'android';
        case 'linux':
            return 'linux';
        case 'linux-musl':
            return 'linux-musl';
        case 'darwin':
            return 'macos';
        case 'win32':
            return 'windows';
        default:
            throw Error(`Platform ${platform} is not supported.`);
    }
}
exports.nodePlatformToDartPlatform = nodePlatformToDartPlatform;
// Converts a Node-style architecture name as returned by `process.arch` into a
// name used by Dart Sass. Throws if the architecture is not supported by Dart
// Sass Embedded.
function nodeArchToDartArch(arch) {
    switch (arch) {
        case 'ia32':
            return 'ia32';
        case 'x86':
            return 'ia32';
        case 'x64':
            return 'x64';
        case 'arm':
            return 'arm';
        case 'arm64':
            return 'arm64';
        default:
            throw Error(`Architecture ${arch} is not supported.`);
    }
}
exports.nodeArchToDartArch = nodeArchToDartArch;
// Get the platform's file extension for archives.
function getArchiveExtension(platform) {
    return platform === 'windows' ? '.zip' : '.tar.gz';
}
// Downloads the release for `repo` located at `assetUrl`, then unzips it into
// `outPath`.
async function downloadRelease(options) {
    console.log(`Downloading ${options.repo} release asset.`);
    const response = await fetch(options.assetUrl, {
        redirect: 'follow',
    });
    if (!response.ok) {
        throw Error(`Failed to download ${options.repo} release asset: ${response.statusText}`);
    }
    const releaseAsset = Buffer.from(await response.arrayBuffer());
    console.log(`Unzipping ${options.repo} release asset to ${options.outPath}.`);
    await utils.cleanDir(p.join(options.outPath, options.repo));
    const archiveExtension = options.assetUrl.endsWith('.zip')
        ? '.zip'
        : '.tar.gz';
    const zippedAssetPath = options.outPath + '/' + options.repo + archiveExtension;
    await fs_1.promises.writeFile(zippedAssetPath, releaseAsset);
    if (archiveExtension === '.zip') {
        await extractZip(zippedAssetPath, {
            dir: p.join(process.cwd(), options.outPath),
        });
    }
    else {
        (0, tar_1.extract)({
            file: zippedAssetPath,
            cwd: options.outPath,
            sync: true,
        });
    }
    await fs_1.promises.unlink(zippedAssetPath);
}
// Patch the launcher script if needed.
//
// For linux both `-linux-` and `-linux-musl-` packages will be installed
// because npm doesn't know how to select packages based on LibC. To avoid
// conflicts, only the `-linux-` packages have "bin" scripts defined in
// package.json, which we patch to detect which LibC is available and launch the
// correct binary.
async function patchLauncherScript(path, dartPlatform, dartArch) {
    if (dartPlatform !== 'linux')
        return;
    const scriptPath = p.join(path, 'dart-sass', 'sass');
    console.log(`Patching ${scriptPath} script.`);
    const shebang = Buffer.from('#!/bin/sh\n');
    const buffer = await fs_1.promises.readFile(scriptPath);
    if (!buffer.subarray(0, shebang.length).equals(shebang)) {
        throw new Error(`${scriptPath} is not a shell script!`);
    }
    const lines = buffer.toString('utf-8').split('\n');
    const index = lines.findIndex(line => line.startsWith('path='));
    if (index < 0) {
        throw new Error(`The format of ${scriptPath} has changed!`);
    }
    lines.splice(index + 1, 0, '# Detect linux-musl', 'if grep -qm 1 /ld-musl- /proc/self/exe; then', `  path="$path/../../sass-embedded-linux-musl-${dartArch}/dart-sass"`, 'fi');
    await fs_1.promises.writeFile(scriptPath, lines.join('\n'));
}
void (async () => {
    try {
        const version = pkg['compiler-version'];
        if (version.endsWith('-dev')) {
            throw Error("Can't release optional packages for a -dev compiler version.");
        }
        const index = argv.package.lastIndexOf('-');
        const nodePlatform = argv.package.substring(0, index);
        const nodeArch = argv.package.substring(index + 1);
        const dartPlatform = nodePlatformToDartPlatform(nodePlatform);
        const dartArch = nodeArchToDartArch(nodeArch);
        const outPath = p.join('npm', argv.package);
        await downloadRelease({
            repo: 'dart-sass',
            assetUrl: 'https://github.com/sass/dart-sass/releases/download/' +
                `${version}/dart-sass-${version}-` +
                `${dartPlatform}-${dartArch}${getArchiveExtension(dartPlatform)}`,
            outPath,
        });
        await patchLauncherScript(outPath, dartPlatform, dartArch);
    }
    catch (error) {
        console.error(error);
        process.exitCode = 1;
    }
})();
//# sourceMappingURL=prepare-optional-release.js.map