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

Simplify via a first sync + uploading new changes #1

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
composer.lock
node_modules
demo/
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ serverless plugin install -n @bref.sh/live
Set up Bref Live's S3 bucket by running:

```
serverless bref-live-install
serverless bref:live:install
```

Run Bref Live at the top of your main file (`index.php` or similar):
Expand All @@ -42,9 +42,13 @@ require __DIR__.'/../vendor/autoload.php';
Run Bref Live:

```
serverless bref-live
serverless bref:live
```

Only file changes tracked by git will be synchronized to Lambda.
After a first sync, all file changes will be uploaded live to Lambda.

Anything "git ignored" will not be uploaded to Lambda (for example `vendor/` changes, caches, etc.).
After using Bref Live, reset the environment to a clean state via:

```
serverless deploy
```
2 changes: 2 additions & 0 deletions live.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

function live(string $entryPoint)
{
if (!getenv('BREF_LIVE_ENABLE')) return;

$baseRoot = getenv('LAMBDA_TASK_ROOT');
$newRoot = '/tmp/.bref-live';

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"homepage": "https://github.com/brefphp/live",
"repository": "https://github.com/brefphp/live",
"license": "MIT",
"main": "plugin.js",
"dependencies": {
"anymatch": "^3.1.2",
"archiver": "^5.3.0",
"chalk": "^4.1.1",
"chokidar": "^3.5.2",
"ora": "^5.4.1"
"chokidar": "^3.5.2"
},
"main": "plugin.js"
"peerDependencies": {
"serverless": "^3"
}
}
118 changes: 78 additions & 40 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
const child_process = require('child_process');
const archiver = require('archiver');
const os = require('os');
const chokidar = require('chokidar');
const anymatch = require('anymatch');
const ora = require('ora');
const chalk = require('chalk');

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

class ServerlessPlugin {
constructor(serverless) {
class BrefLive {
constructor(serverless, options, utils) {
this.serverless = serverless;
this.utils = utils;
this.commands = {
'bref-live': {
'bref:live': {
usage: 'Start Bref Live',
lifecycleEvents: ['start'],
},
'bref-live-install': {
'bref:live:install': {
usage: 'Install Bref Live',
lifecycleEvents: ['install'],
},
};
this.hooks = {
initialize: () => this.init(),
'bref-live:start': () => this.start(),
'bref-live-install:install': () => this.install(),
'bref:live:start': () => this.start(),
'bref:live:install:install': () => this.install(),
};
}

Expand All @@ -43,48 +34,81 @@ class ServerlessPlugin {
// TODO make those configurable in `bref.live` in serverless.yml
this.serverless.service.provider.environment.BREF_LIVE_BUCKET = this.bucketName;
this.serverless.service.provider.environment.BREF_LIVE_BUCKET_REGION = 'eu-west-3';
if (process.env.BREF_LIVE_ENABLE) {
this.serverless.service.provider.environment.BREF_LIVE_ENABLE = process.env.BREF_LIVE_ENABLE;
}

// TODO support include/exclude
this.packagePatterns = this.serverless.service.package.patterns ?? [];
}

async install() {
// TODO create the bucket, maybe with a separate CloudFormation stack?
console.log(`WIP - Create a bucket '${this.bucketName}' and make it accessible by Lambda.`);
console.log('Create it in an AWS region close to your location for faster uploads.');
console.log('In the future the CLI should create the bucket for you, sorry for the trouble :)');
}

async start() {
console.log(chalk.gray(`Bref Live will upload changes tracked by git: ${chalk.underline('git diff HEAD --name-only')}`));
this.spinner = ora('Watching changes').start();
this.changedFiles = [];

this.spinner = this.utils.progress.create();

// TODO implement a pattern matching that == the one used by Framework
const pathsToWatch = this.packagePatterns.filter((pattern) => !pattern.startsWith('!'));
if (pathsToWatch.length === 0) {
pathsToWatch.push('*');
}
const pathsToIgnore = this.packagePatterns.filter((pattern) => pattern.startsWith('!'))
.map((pattern) => pattern.replace('!', ''));

this.sync('Initial sync');
chokidar.watch('.', {
await this.initialSync();

this.spinner.update('Watching changes');
chokidar.watch(pathsToWatch, {
ignoreInitial: true,
ignored: ignoredPaths,
ignored: pathsToIgnore,
}).on('all', async (event, path) => {
if (this.isGitIgnored(path)) return;
await this.sync(path);
});

// TODO catch interrupt to cancel BREF_LIVE_ENABLE
return new Promise(resolve => {});
}

async initialSync() {
this.spinner.update('Deploying all functions');

this.serverless.service.provider.environment.BREF_LIVE_ENABLE = '1';
const functionNames = this.serverless.service.getAllFunctions();
await Promise.all(functionNames.map((functionName) => {
return this.spawnAsync('serverless', [
'deploy', 'function', '--function', functionName
], {
BREF_LIVE_ENABLE: '1',
});
}));
}

async sync(path) {
this.spinner.text = 'Uploading';
this.changedFiles.push(path);

this.spinner.update('Uploading');

const startTime = process.hrtime();
const startTimeHuman = new Date().toLocaleTimeString();
const functionNames = this.serverless.service.getAllFunctionsNames();
await Promise.all(functionNames.map((functionName) => this.uploadDiff(functionName)));
this.spinner.succeed(`${chalk.gray(startTimeHuman)} - ${this.elapsedTime(startTime)}s - ${path}`);

// New spinner
this.spinner = ora('Watching project').start();
const elapsedTime = `${this.elapsedTime(startTime)}s`;
this.utils.log.success(`${chalk.gray(startTimeHuman)} ${path} ${chalk.gray(elapsedTime)}`);

this.spinner.update('Watching changes');
}

async uploadDiff(functionName) {
const changedFilesOutput = this.spawnSync('git', ['diff', 'HEAD', '--name-only']);
let changedFiles = changedFilesOutput.split(os.EOL);
changedFiles = changedFiles.filter((file) => file !== '' && !anymatch(ignoredPaths, file));

const archive = archiver('zip', {});
for (const file of changedFiles) {
for (const file of this.changedFiles) {
archive.file(file, {name: file});
}
await archive.finalize();
Expand All @@ -98,17 +122,31 @@ class ServerlessPlugin {

elapsedTime(startTime){
const hrtime = process.hrtime(startTime);
return (hrtime[0] + (hrtime[1] / 1e9)).toFixed(3);
return (hrtime[0] + (hrtime[1] / 1e9)).toFixed(1);
}

isGitIgnored(path) {
return child_process.spawnSync('git', ['check-ignore', path]).status === 0;
}

spawnSync(cmd, args) {
const p = child_process.spawnSync(cmd, args);
return p.stdout.toString().trim();
async spawnAsync(command, args, env) {
const child = child_process.spawn(command, args, {
env: {
...process.env,
...env,
},
});
let output = "";
for await (const chunk of child.stdout) {
output += chunk;
}
for await (const chunk of child.stderr) {
output += chunk;
}
const exitCode = await new Promise( (resolve, reject) => {
child.on('close', resolve);
});
if( exitCode) {
throw new Error(`${output}`);
}
return output;
}
}

module.exports = ServerlessPlugin;
module.exports = BrefLive;