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 fa897de
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/bin/vip-deploy-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import chalk from 'chalk';
import debugLib from 'debug';
import { prompt } from 'enquirer';
import fs from 'fs';
import gql from 'graphql-tag';
import { mkdtemp } from 'node:fs/promises';
import os from 'os';
import path from 'path';

/**
* 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,32 @@ 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()
// eslint-disable-next-line no-useless-escape
.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 fa897de

Please sign in to comment.