Skip to content

Commit

Permalink
[DOP-4033]: Update next-gen-deploy to work
Browse files Browse the repository at this point in the history
  • Loading branch information
branberry committed Oct 6, 2023
1 parent 9b28c5d commit 5c44115
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 41 deletions.
26 changes: 18 additions & 8 deletions src/commands/src/helpers/execution-helper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import path from 'path';
import fs from 'fs';
import { checkIfPatched, getCommitBranch, getCommitHash, getPatchId, getRepoDir } from '.';
import { executeCliCommand, getCommitBranch, getCommitHash, getPatchId, getRepoDir } from '.';
import { promisify } from 'util';

const existsAsync = promisify(fs.exists);
const writeFileAsync = promisify(fs.writeFile);

async function cloneRepo(repoName: string) {
await executeCliCommand({
command: 'git',
args: ['clone', `https://github.com/mongodb/${repoName}`],
options: { cwd: `${process.cwd()}/repos` },
});
}
async function createEnvProdFile(repoDir: string, projectName: string, baseUrl: string, prefix = '') {
const prodFileName = `${process.cwd()}/snooty/.env.production`;

Expand All @@ -26,9 +33,12 @@ async function createEnvProdFile(repoDir: string, projectName: string, baseUrl:
}
}

export async function getCliBuildDependencies(repoDir: string, projectName: string, baseUrl: string) {
export async function getCliBuildDependencies(repoName: string, projectName: string, baseUrl: string) {
await cloneRepo(repoName);

const repoDir = getRepoDir(repoName);

const commandPromises = [
checkIfPatched(repoDir),
getCommitHash(repoDir),
getCommitBranch(repoDir),
getPatchId(repoDir),
Expand All @@ -38,11 +48,11 @@ export async function getCliBuildDependencies(repoDir: string, projectName: stri

const deps = await Promise.all(commandPromises);
return {
hasPatch: deps[0] as string,
commitHash: deps[1] as string,
commitBranch: deps[2] as string,
patchId: deps[3] as string | undefined,
hasRedirects: deps[4] as boolean,
commitHash: deps[0] as string,
commitBranch: deps[1] as string,
patchId: deps[2] as string | undefined,
hasRedirects: deps[3] as boolean,
bundlePath: `${repoDir}/bundle.zip`,
repoDir,
};
}
22 changes: 20 additions & 2 deletions src/commands/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,27 @@ export async function executeAndPipeCommands(
const cmdTo = spawn(cmdToParams.command, cmdToParams.args || [], cmdToParams.options || {});

cmdFrom.stdout?.on('data', (data: Buffer) => {
if (!cmdTo.stdin?.writable) {
cmdFrom.stdin?.end();
cmdFrom.kill();
return;
}

cmdTo.stdin?.write(data);
});

cmdFrom.stdout?.on('error', (err) => {
console.log('error on cmdFrom out', err);
});

cmdTo.stdin?.on('finish', () => {
console.log('finished stdin');
});

cmdTo.stdin?.on('error', () => {
console.log('stdin done');
});

cmdFrom.on('error', (err) => {
reject(new ExecuteCommandError('The first command failed', err));
hasRejected = true;
Expand Down Expand Up @@ -75,11 +93,11 @@ export async function executeAndPipeCommands(
console.error('Options provided: ', cmdToParams.options);

if (outputText) {
console.error(outputText.join());
console.error('output', outputText.join());
}

if (errorText) {
console.error(errorText.join());
console.error('error', errorText.join());
}

reject(new ExecuteCommandError('The command failed', exitCode));
Expand Down
23 changes: 20 additions & 3 deletions src/commands/src/shared/next-gen-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ interface NextGenDeployParams {
mutPrefix: string;
gitBranch: string;
hasConfigRedirects: boolean;
url: string;
}

export async function nextGenDeploy({ bucket, mutPrefix, gitBranch, hasConfigRedirects }: NextGenDeployParams) {
export async function nextGenDeploy({ bucket, mutPrefix, gitBranch, hasConfigRedirects, url }: NextGenDeployParams) {
// const hasConfigRedirects = await existsAsync(path.join(process.cwd(), 'config/redirects'));

if (hasConfigRedirects && (gitBranch === 'main' || gitBranch === 'master')) {
Expand All @@ -16,8 +17,24 @@ export async function nextGenDeploy({ bucket, mutPrefix, gitBranch, hasConfigRed
}

// yes | mut-publish public ${BUCKET} --prefix="${MUT_PREFIX}" --deploy --deployed-url-prefix=${URL} --json --all-subdirectories ${ARGS};
await executeAndPipeCommands(
const { outputText } = await executeAndPipeCommands(
{ command: 'yes' },
{ command: 'mut-publish', args: ['public', bucket, `--prefix=${mutPrefix}`, '--json', '--all-subdirectories'] }
{
command: 'mut-publish',
args: [
'public',
bucket,
`--prefix=${mutPrefix}`,
'--deploy',
`--deployed-url-prefix=${url}`,
'--json',
'--all-subdirectories',
],
options: {
cwd: `${process.cwd()}/snooty`,
},
}
);

return `${outputText}\n Hosted at ${url}/${mutPrefix}`;
}
29 changes: 15 additions & 14 deletions src/commands/src/shared/next-gen-stage.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import {
checkIfPatched,
executeAndWriteToFile,
executeCliCommand,
getCommitBranch,
getCommitHash,
getPatchId,
} from '../helpers';
import { executeCliCommand, getCommitHash, getPatchId } from '../helpers';

interface StageParams {
repoDir: string;
mutPrefix: string;
projectName: string;
bucketName: string;
bucket: string;
url: string;
patchId?: string;
commitBranch: string;
}

export async function nextGenStage({ repoDir, mutPrefix, projectName, bucketName, url }: StageParams) {
const [hasPatch, commitBranch] = await Promise.all([checkIfPatched(repoDir), getCommitBranch(repoDir)]);

export async function nextGenStage({
repoDir,
mutPrefix,
projectName,
bucket,
url,
patchId,
commitBranch,
}: StageParams) {
let hostedAtUrl = `${url}/${mutPrefix}/docsworker/${commitBranch}/`;
let prefix = mutPrefix;

const commandArgs = ['public', bucketName, '--stage'];
const commandArgs = ['public', bucket, '--stage'];

if (hasPatch && projectName === mutPrefix) {
if (patchId && projectName === mutPrefix) {
const [commitHash, patchId] = await Promise.all([getCommitHash(repoDir), getPatchId(repoDir)]);
prefix = `${commitHash}/${patchId}/${mutPrefix}`;
hostedAtUrl = `${url}/${commitHash}/${patchId}/${mutPrefix}/docsworker/${commitBranch}/`;
Expand Down
37 changes: 23 additions & 14 deletions src/entrypoints/localApp.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { executeCliCommand, getRepoDir } from '../commands/src/helpers';
import { nextGenParse } from '../commands/src/shared/next-gen-parse';
import { nextGenHtml } from '../commands/src/shared/next-gen-html';
import { getCliBuildDependencies } from '../commands/src/helpers/execution-helper';
import { nextGenStage } from '../commands/src/shared/next-gen-stage';
import { oasPageBuild } from '../commands/src/shared/oas-page-build';
import { persistenceModule } from '../commands/src/shared/persistence-module';
import { nextGenDeploy } from '../commands/src/shared/next-gen-deploy';

async function localApp() {
// TODO: Fetch this from repos_branches
const repoName = 'docs-java';
const projectName = 'java';
const baseUrl = 'https://www.mongodb.com';
const repoDir = getRepoDir(repoName);
const bucket = 'docs-java-dotcomstg';
const mutPrefix = 'docs/drivers/java/sync';

// mocking out the clone aspect of a job
await executeCliCommand({
command: 'git',
args: ['clone', `https://github.com/mongodb/${repoName}`],
options: { cwd: `${process.cwd()}/repos` },
});

const { commitHash, patchId, bundlePath } = await getCliBuildDependencies(repoDir, projectName, baseUrl);
const { commitHash, patchId, bundlePath, commitBranch, hasRedirects, repoDir } = await getCliBuildDependencies(
repoName,
projectName,
baseUrl
);

console.log('Hello');

Expand All @@ -43,7 +42,6 @@ async function localApp() {
console.log('next-gen-html complete');

console.log('Begin oas-page-build...');
const mutPrefix = 'docs';
const siteUrl = mutPrefix ? `${baseUrl}/${mutPrefix}` : `${baseUrl}`;
const oasPageBuildRes = await oasPageBuild({ repoDir, bundlePath, siteUrl });
console.log('oas-page-build compelte');
Expand All @@ -52,16 +50,27 @@ async function localApp() {
console.log('Begin next-gen-stage...');

const resultMessage = await nextGenStage({
patchId,
commitBranch,
repoDir,
projectName,
bucketName: 'docs-mongodb-org-stg',
bucket,
url: baseUrl,
mutPrefix,
});
console.log(resultMessage);
console.log('Begin next-gen-stage complete');
console.log('next-gen-stage complete');

console.log('Begin next-gen-deploy');
console.log('Begin next-gen-deploy...');
const deployRes = await nextGenDeploy({
bucket,
hasConfigRedirects: hasRedirects,
gitBranch: commitBranch,
mutPrefix,
url: baseUrl,
});
console.log(deployRes);
console.log('next-gen-deploy complete');
}

localApp();

0 comments on commit 5c44115

Please sign in to comment.