2020-04-06 08:53:34 +00:00
|
|
|
import * as chokidar from 'chokidar';
|
|
|
|
import * as Docker from 'dockerode';
|
|
|
|
import * as _ from 'lodash';
|
|
|
|
import * as Path from 'path';
|
|
|
|
|
|
|
|
import { Dockerfile, Livepush } from 'livepush';
|
|
|
|
|
|
|
|
// TODO: Pass build args to the livepush process
|
|
|
|
export async function startLivepush(opts: {
|
|
|
|
dockerfile: Dockerfile;
|
|
|
|
containerId: string;
|
|
|
|
docker: Docker;
|
|
|
|
noinit: boolean;
|
2022-07-06 19:51:49 +00:00
|
|
|
stageImages?: string[];
|
2020-04-06 08:53:34 +00:00
|
|
|
}) {
|
|
|
|
const livepush = await Livepush.init({
|
2022-07-06 19:51:49 +00:00
|
|
|
stageImages: [],
|
2020-04-06 08:53:34 +00:00
|
|
|
...opts,
|
|
|
|
context: Path.join(__dirname, '..'),
|
|
|
|
});
|
|
|
|
|
|
|
|
livepush.addListener('commandExecute', ({ command }) => {
|
|
|
|
console.log(`Executing command: ${command} `);
|
|
|
|
});
|
|
|
|
livepush.addListener('commandReturn', ({ returnCode }) => {
|
|
|
|
if (returnCode !== 0) {
|
|
|
|
console.log(` Command executed with code ${returnCode}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
livepush.addListener('commandOutput', ({ output }) => {
|
|
|
|
console.log(output.data.toString());
|
|
|
|
});
|
|
|
|
livepush.addListener('containerRestart', () => {
|
|
|
|
console.log('Restarting container');
|
|
|
|
});
|
|
|
|
|
|
|
|
const livepushExecutor = getExecutor(livepush);
|
2022-07-06 19:51:49 +00:00
|
|
|
const watcher = chokidar
|
2020-04-06 08:53:34 +00:00
|
|
|
.watch('.', {
|
2020-04-07 09:50:59 +00:00
|
|
|
ignored: /((^|[\/\\])\..|(node_modules|sync|test)\/.*)/,
|
2020-04-06 08:53:34 +00:00
|
|
|
ignoreInitial: opts.noinit,
|
|
|
|
})
|
2020-04-14 09:24:25 +00:00
|
|
|
.on('add', (path) => livepushExecutor(path))
|
|
|
|
.on('change', (path) => livepushExecutor(path))
|
|
|
|
.on('unlink', (path) => livepushExecutor(undefined, path));
|
2022-07-06 19:51:49 +00:00
|
|
|
|
|
|
|
return async () => {
|
|
|
|
await watcher.close();
|
|
|
|
await livepush.cleanupIntermediateContainers();
|
|
|
|
};
|
2020-04-06 08:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getExecutor = (livepush: Livepush) => {
|
2020-04-14 09:24:25 +00:00
|
|
|
let changedFiles: string[] = [];
|
|
|
|
let deletedFiles: string[] = [];
|
2020-04-06 08:53:34 +00:00
|
|
|
const actualExecutor = _.debounce(async () => {
|
|
|
|
await livepush.performLivepush(changedFiles, deletedFiles);
|
2020-04-14 09:24:25 +00:00
|
|
|
changedFiles = [];
|
|
|
|
deletedFiles = [];
|
2020-04-06 08:53:34 +00:00
|
|
|
});
|
|
|
|
return (changed?: string, deleted?: string) => {
|
|
|
|
if (changed) {
|
|
|
|
changedFiles.push(changed);
|
|
|
|
}
|
|
|
|
if (deleted) {
|
|
|
|
deletedFiles.push(deleted);
|
|
|
|
}
|
|
|
|
actualExecutor();
|
|
|
|
};
|
|
|
|
};
|