Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
Restore original versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jul 18, 2021
1 parent 89a36cc commit 9a314f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
31 changes: 29 additions & 2 deletions live.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function live(string $entryPoint)
require $newRoot . '/vendor/autoload.php';

$startSync = microtime(true);
applyChanges($newRoot, $stderr);
applyChanges($baseRoot, $newRoot, $stderr);
$syncDuration = (int) ((microtime(true) - $startSync) * 1000);
fwrite($stderr, "Bref Live Edit: Synchronized live code in $syncDuration ms\n");

Expand All @@ -50,6 +50,11 @@ function live(string $entryPoint)

function recursiveCopy($source, $target)
{
// Missing file
if (! file_exists($source)) {
unlink($target);
return;
}
// Check for symlinks
if (is_link($source)) {
symlink(readlink($source), $target);
Expand Down Expand Up @@ -78,7 +83,7 @@ function recursiveCopy($source, $target)
$dir->close();
}

function applyChanges(string $newRoot, $stderr)
function applyChanges(string $baseRoot, string $newRoot, $stderr)
{
$region = getenv('AWS_REGION');
$functionName = getenv('AWS_LAMBDA_FUNCTION_NAME');
Expand Down Expand Up @@ -114,7 +119,29 @@ function applyChanges(string $newRoot, $stderr)

$zip = new ZipArchive();
$zip->open('/tmp/.bref-live.zip');

// Restore previously modified paths
if (file_exists('/tmp/.bref-live-modified.json')) {
$previouslyModifiedPaths = file('/tmp/.bref-live-modified.json');
foreach ($previouslyModifiedPaths as $path) {
$path = trim($path);
// If the previously changed files are no longer in the zip, we need to restore the original version
if ($zip->locateName($path) === false) {
recursiveCopy($baseRoot . '/' . $path, $newRoot . '/' . $path);
}
}
}

// Extract new files from zip
$zip->extractTo($newRoot);

// Store modified paths
$modifiedPaths = [];
for ($i = 0; $i < $zip->numFiles; $i++){
$modifiedPaths[] = $zip->getNameIndex($i);
}
file_put_contents('/tmp/.bref-live-modified.json', implode(PHP_EOL, $modifiedPaths));

$zip->close();
}

Expand Down
1 change: 1 addition & 0 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const anymatch = require('anymatch');

const ignoredPaths = [
'.git/*',
'.serverless',
'.serverless/*',
'serverless.yml',
];
Expand Down

0 comments on commit 9a314f6

Please sign in to comment.