<commit_before>// Copyright 2022 Google LLC
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { join } from 'path';
import { exit } from 'process';
import { getFirebaseTools, getNormalizedHostingConfig } from './firebase';

import { build } from './frameworks';
import { DEFAULT_REGION } from './utils';

export type PrepareOptions = {
    includeCloudFunctions: boolean;
}

type BuildResult = Awaited<ReturnType<typeof build>> & { functionName: string, site: string, hostingDist: string, functionsDist: string, };

export const prepare = async (targetNames: string[], context: any, options: any) => {
    let startBuildQueue: (arg: []) => void;
    let buildQueue = new Promise<BuildResult[]>((resolve) => startBuildQueue = resolve);
    await getFirebaseTools();
    const configs = getNormalizedHostingConfig()(options, { resolveTargets: true });
    if (configs.length === 0) return;
    configs.forEach(({ source, site, public: publicDir}: any) => {
        if (!source) return;
        const dist = join(process.cwd(), '.firebase', site);
        const hostingDist = join('.firebase', site, 'hosting');
        const functionsDist = join('.firebase', site, 'functions');
        if (publicDir) throw `hosting.public and hosting.source cannot both be set in firebase.json`;
        const getProjectPath = (...args: string[]) => join(process.cwd(), source, ...args);
        const functionName = `ssr${site.replace(/-/g, '')}`;
        buildQueue = buildQueue.then(results => build({
            dist,
            project: context.project,
            site,
            function: {
                name: functionName,
                region: DEFAULT_REGION,
                gen: 2,
            },
        }, getProjectPath).then(result => (results.push({ ...result, functionName, site, hostingDist, functionsDist }), results)));
    });
    startBuildQueue!([]);
    const results = await buildQueue;
    if (targetNames.includes('functions') && results.some(it => it.usingCloudFunctions)) {
        console.error('Unable to deploy functions alongside web frameworks at the moment.\nDeploy the seperately with "firebase deploy --only hosting" and "firebase deploy --only functions".');
        exit(1);
    }
    if (results.filter(it => it.usingCloudFunctions).length > 1) {
        console.error('Unable deploy multiple web frameworks that use Cloud Functions at the same time.\nDeploy the seperately with "firebase deploy --only hosting:YOUR_TARGET".');
        exit(1);
    }
    // TODO how should we handle pushing multiple sites?
    //      we should error if they are already deploying Cloud Functions for now
    results.forEach(({ usingCloudFunctions, hostingDist, functionsDist, functionName }) => {
        options.config.set('hosting.public', hostingDist);
        const rewrites = options.config.get('hosting.rewrites') || [];
        if (usingCloudFunctions) {
            if (!targetNames.includes('functions')) targetNames.push('functions');
            // TODO implement a better way of not deleting functions
            options.only = [options.only, 'hosting', `functions:${functionName}`].filter(it => it).join(',');
            options.config.set('functions', { source: functionsDist});
            // TODO get the other firebase.json modifications
            options.config.set('hosting.rewrites', [ ...rewrites, {
                source: '**',
                run: {
                    serviceId: functionName,
                    region: DEFAULT_REGION,
                },
            }]);
        } else {<commit_msg>Implement codebases<commit_after>// Copyright 2022 Google LLC
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { join } from 'path';
import { exit } from 'process';
import { getFirebaseTools, getNormalizedHostingConfig } from './firebase';

import { build } from './frameworks';
import { DEFAULT_REGION } from './utils';

export type PrepareOptions = {
    includeCloudFunctions: boolean;
}

type BuildResult = Awaited<ReturnType<typeof build>> & { functionName: string, site: string, hostingDist: string, functionsDist: string, };

export const prepare = async (targetNames: string[], context: any, options: any) => {
    let startBuildQueue: (arg: []) => void;
    let buildQueue = new Promise<BuildResult[]>((resolve) => startBuildQueue = resolve);
    await getFirebaseTools();
    const configs = getNormalizedHostingConfig()(options, { resolveTargets: true });
    if (configs.length === 0) return;
    configs.forEach(({ source, site, public: publicDir}: any) => {
        if (!source) return;
        const dist = join(process.cwd(), '.firebase', site);
        const hostingDist = join('.firebase', site, 'hosting');
        const functionsDist = join('.firebase', site, 'functions');
        if (publicDir) throw `hosting.public and hosting.source cannot both be set in firebase.json`;
        const getProjectPath = (...args: string[]) => join(process.cwd(), source, ...args);
        const functionName = `ssr${site.replace(/-/g, '')}`;
        buildQueue = buildQueue.then(results => build({
            dist,
            project: context.project,
            site,
            function: {
                name: functionName,
                region: DEFAULT_REGION,
                gen: 2,
            },
        }, getProjectPath).then(result => (results.push({ ...result, functionName, site, hostingDist, functionsDist }), results)));
    });
    startBuildQueue!([]);
    const results = await buildQueue;
    if (targetNames.includes('functions') && results.some(it => it.usingCloudFunctions)) {
        console.error('Unable to deploy functions alongside web frameworks at the moment.\nDeploy the seperately with "firebase deploy --only hosting" and "firebase deploy --only functions".');
        exit(1);
    }
    if (results.filter(it => it.usingCloudFunctions).length > 1) {
        console.error('Unable deploy multiple web frameworks that use Cloud Functions at the same time.\nDeploy the seperately with "firebase deploy --only hosting:YOUR_TARGET".');
        exit(1);
    }
    // TODO how should we handle pushing multiple sites?
    //      we should error if they are already deploying Cloud Functions for now
    results.forEach(({ usingCloudFunctions, hostingDist, functionsDist, functionName }) => {
        options.config.set('hosting.public', hostingDist);
        const rewrites = options.config.get('hosting.rewrites') || [];
        if (usingCloudFunctions) {
            if (!targetNames.includes('functions')) targetNames.push('functions');
            options.config.set('functions', {
                source: functionsDist,
                codebase: 'firebase-frameworks',
            });
            // TODO get the other firebase.json modifications
            options.config.set('hosting.rewrites', [ ...rewrites, {
                source: '**',
                run: {
                    serviceId: functionName,
                    region: DEFAULT_REGION,
                },
            }]);
        } else {
{'fraction_non_alphanumeric': 0.12518647439085032, 'fraction_numerical': 0.0032322227747389356, 'mean_word_length': 2.810990052108006, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4459145', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2059, 'n_words': 679}