Skip to content

Commit

Permalink
Add logic to rename on upload to prevent file being overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahum committed Dec 1, 2023
1 parent b147fd3 commit d441d16
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/bin/vip-deploy-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import chalk from 'chalk';
import debugLib from 'debug';
import { prompt } from 'enquirer';
import gql from 'graphql-tag';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { mkdtemp } from 'node:fs/promises';

/**
* Internal dependencies
Expand Down Expand Up @@ -167,6 +171,10 @@ void command( {
const [ fileName ] = arg;
const fileMeta = await getFileMeta( fileName );

if ( ! fs.existsSync( fileMeta.fileName ) ) {
throw new Error( `Unable to access file ${ fileMeta.fileName }` );
}

debug( 'Options: ', opts );
debug( 'Args: ', arg );

Expand All @@ -181,8 +189,6 @@ void command( {
const formattedEnvironment = formatEnvironment( opts.env.type );
const launched = opts.env.launched;

const fileNameToUpload = fileName;

// PROMPT TO PROCEED WITH THE DEPLOY
await promptToContinue( {
launched,
Expand Down Expand Up @@ -236,7 +242,29 @@ Processing the file for deployment to your environment...
progressTracker.setUploadPercentage( percentage );
};

fileMeta.fileName = fileNameToUpload;
// Rename the file so it doesn't get overwritten
let tmpDir;
let newFileBasename;
let newFileName;
try {
tmpDir = await mkdtemp( path.join( os.tmpdir(), 'vip-manual-deploys' ) );

const datePrefix = new Date()
.toISOString()
.replace( /[\-T:\.Z]/g, '' )
.slice( 0, 14 );
newFileBasename = `${ datePrefix }-${ fileMeta.basename }`;
debug( `Renaming the file to ${ chalk.cyan( newFileBasename ) } from ${ fileMeta.basename } prior to transfer...` );
newFileName = `${ tmpDir }/${ newFileBasename }`;

fs.copyFileSync( fileMeta.fileName, newFileName );
fileMeta.fileName = newFileName;
fileMeta.basename = newFileBasename;
} catch ( err ) {
throw new Error(
`Unable to copy file to temporary working directory: ${ err.message }`
);
}

try {
const {
Expand Down

0 comments on commit d441d16

Please sign in to comment.