<commit_before>import * as childProcess from "child_process";
import { randomBytes } from "crypto";
import * as fs from "fs-extra-promise";
import * as path from "path";
import { format } from "util";
import { addPasswordToGitURL, azCommandOutputs, runAzCommand,
        runExecFailOnStderr } from "./common-utilities";
import Resource from "./resource";
import ResourceGroup from "./resourcegroup";

export default class WebappNodeAzure extends Resource {
    private static findDefaultResourceGroup(resources: Resource[])
                    : ResourceGroup {
        let globalResourceGroup = resources.find((resource) => {
            return resource instanceof ResourceGroup &&
                resource.isGlobalDefault;
        }) as ResourceGroup;

        if (globalResourceGroup !== undefined) {
            return globalResourceGroup;
        }

        globalResourceGroup = resources.find((resource) => {
            return resource instanceof ResourceGroup;
        }) as ResourceGroup;

        if (globalResourceGroup !== undefined) {
            return globalResourceGroup;
        }

        throw new Error("There is no defined resource group object!");
    }

    private appBaseName: string;

    private resourceGroup: ResourceGroup;

    private webAppServicePlanNameProperty: string;

    private webAppDNSNameProperty: string;

    public get webAppServicePlanName() {
        return this.webAppServicePlanNameProperty;
    }

    public setWebAppServicePlanName(name: string): WebappNodeAzure {
        this.webAppServicePlanNameProperty = name;
        return this;
    }

    public get webAppDNSName() {
        return this.webAppDNSNameProperty;
    }

    public setWebAppServiceDNSName(name: string): WebappNodeAzure {
        this.webAppDNSNameProperty = name;
        return this;
    }

    public async deployResource(directoryPath: string,
                                resources: Resource[]): Promise<void> {
        const userName = randomBytes(10).toString("hex");
        const password = randomBytes(20).toString("hex");
        await runAzCommand(
            format("az webapp deployment user set --user-name \"%s\"\
            --password \"%s\"", userName, password));

        if (this.appBaseName === undefined) {
            this.appBaseName = path.basename(directoryPath);
        }

        if (this.resourceGroup === undefined) {
            this.resourceGroup =
                WebappNodeAzure.findDefaultResourceGroup(resources);
        }

        if (this.webAppServicePlanName === undefined) {
            this.setWebAppServicePlanName(
                this.resourceGroup.resourceGroupName +
                    path.basename(directoryPath) + "webAppPlan");
        }

        await runAzCommand(
            format("az appservice plan create --name \"%s\" \
                    --resource-group \"%s\" --sku FREE",
                    this.webAppServicePlanName,
                    this.resourceGroup.resourceGroupName));

        if (this.webAppDNSNameProperty === undefined) {
            this.setWebAppServiceDNSName(
                this.resourceGroup.resourceGroupName +
                    path.basename(directoryPath) + "webapp");
        }

        await runAzCommand(
            format("az webapp create --name \"%s\" \
                    --resource-group \"%s\" --plan \"%s\"",
                this.webAppDNSName,
                this.resourceGroup.resourceGroupName,
                this.webAppServicePlanName));

        await this.deployToWebApp(directoryPath, password);
    }

    public async setup(directoryPath: string) {
        fs.copyAsync("../assets/webapp-node-azure", directoryPath);
    }

    private async deployToWebApp(directoryPath: string, password: string) {
        const webAppGitURLResult =
            await runAzCommand(
                format("az webapp deployment source config-local-git \
                        --name \"%s\" --resource-group \"%s\" \
                        --query url --output tsv", this.webAppDNSName,
                        this.resourceGroup.resourceGroupName),
                    azCommandOutputs.string);

        const gitCloneDepotParentPath = path.join(directoryPath, ".sleeve");

        const gitCloneDepotPath = path.join(gitCloneDepotParentPath,
                                            this.webAppDNSName);

        await fs.ensureDirAsync(gitCloneDepotParentPath);

        const gitURLWithPassword = addPasswordToGitURL(webAppGitURLResult,
                                                        password);

        await childProcess.exec(format("git clone %s", gitURLWithPassword),
                                { cwd: gitCloneDepotParentPath });<commit_msg>Prep for release<commit_after>import * as childProcess from "child_process";
import { randomBytes } from "crypto";
import * as fs from "fs-extra-promise";
import * as path from "path";
import { format } from "util";
import { addPasswordToGitURL, azCommandOutputs, runAzCommand,
        runExecFailOnStderr } from "./common-utilities";
import Resource from "./resource";
import ResourceGroup from "./resourcegroup";

export default class WebappNodeAzure extends Resource {
    private static findDefaultResourceGroup(resources: Resource[])
                    : ResourceGroup {
        let globalResourceGroup = resources.find((resource) => {
            return resource instanceof ResourceGroup &&
                resource.isGlobalDefault;
        }) as ResourceGroup;

        if (globalResourceGroup !== undefined) {
            return globalResourceGroup;
        }

        globalResourceGroup = resources.find((resource) => {
            return resource instanceof ResourceGroup;
        }) as ResourceGroup;

        if (globalResourceGroup !== undefined) {
            return globalResourceGroup;
        }

        throw new Error("There is no defined resource group object!");
    }

    private appBaseName: string;

    private resourceGroup: ResourceGroup;

    private webAppServicePlanNameProperty: string;

    private webAppDNSNameProperty: string;

    public get webAppServicePlanName() {
        return this.webAppServicePlanNameProperty;
    }

    public setWebAppServicePlanName(name: string): WebappNodeAzure {
        this.webAppServicePlanNameProperty = name;
        return this;
    }

    public get webAppDNSName() {
        return this.webAppDNSNameProperty;
    }

    public setWebAppServiceDNSName(name: string): WebappNodeAzure {
        this.webAppDNSNameProperty = name;
        return this;
    }

    public async deployResource(directoryPath: string,
                                resources: Resource[]): Promise<void> {
        const userName = randomBytes(10).toString("hex");
        const password = <PASSWORD>Bytes(20).toString("hex");
        await runAzCommand(
            format("az webapp deployment user set --user-name \"%s\"\
            --password \"%s\"", userName, password));

        if (this.appBaseName === undefined) {
            this.appBaseName = path.basename(directoryPath);
        }

        if (this.resourceGroup === undefined) {
            this.resourceGroup =
                WebappNodeAzure.findDefaultResourceGroup(resources);
        }

        if (this.webAppServicePlanName === undefined) {
            this.setWebAppServicePlanName(
                this.resourceGroup.resourceGroupName +
                    path.basename(directoryPath) + "webAppPlan");
        }

        await runAzCommand(
            format("az appservice plan create --name \"%s\" \
                    --resource-group \"%s\" --sku FREE",
                    this.webAppServicePlanName,
                    this.resourceGroup.resourceGroupName));

        if (this.webAppDNSNameProperty === undefined) {
            this.setWebAppServiceDNSName(
                this.resourceGroup.resourceGroupName +
                    path.basename(directoryPath) + "webapp");
        }

        await runAzCommand(
            format("az webapp create --name \"%s\" \
                    --resource-group \"%s\" --plan \"%s\"",
                this.webAppDNSName,
                this.resourceGroup.resourceGroupName,
                this.webAppServicePlanName));

        await this.deployToWebApp(directoryPath, password);
    }

    public async setup(directoryPath: string) {
        fs.copyAsync(path.join(__dirname, "../assets/webapp-node-azure"),
                        directoryPath);
    }

    private async deployToWebApp(directoryPath: string, password: string) {
        const webAppGitURLResult =
            await runAzCommand(
                format("az webapp deployment source config-local-git \
                        --name \"%s\" --resource-group \"%s\" \
                        --query url --output tsv", this.webAppDNSName,
                        this.resourceGroup.resourceGroupName),
                    azCommandOutputs.string);

        const gitCloneDepotParentPath = path.join(directoryPath, ".sleeve");

        const gitCloneDepotPath = path.join(gitCloneDepotParentPath,
                                            this.webAppDNSName);

        await fs.ensureDirAsync(gitCloneDepotParentPath);

        const gitURLWithPassword = addPasswordToGitURL(webAppGitURLResult,
                                                        password);

        await childProcess.exec(format("git clone %s", gitURLWithPassword),
                                { cwd: gitCloneDepotParentPath });
{'fraction_non_alphanumeric': 0.09449318253884367, 'fraction_numerical': 0.0008455765775288025, 'mean_word_length': 2.0790758216726326, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4771742', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2285, 'n_words': 519}