import { logger, flush as flushLogFiles } from 'pot-logger';
import Connection from '../Connection';
import { prepareRun, prepareTarget } from '../utils/PrepareCli';
import { flush as schema } from '../schemas/cli';

export const flush = async function flush(options = {}) {
	prepareRun(schema, options);
	const { connection, targetName } = await prepareTarget(options);
	const stateList = await connection.each('getState');
	const logs = stateList
		.filter(({ logsDir }) => !!logsDir)
		.map(({ logsDir, monitor }) => ({
			logsDir,
			removeDir: monitor.status !== 'running',
		}));
	await Promise.all(logs.map(flushLogFiles));
	logger.info(`"${targetName}" flushed`);
};

export const flushAll = async function flushAll(options = {}) {
	const names = await Connection.getNames();
	return Promise.all(names.map(async (name) => flush({ ...options, name })));
};

@@ -208,16 +208,584 @@
 as/cli';
+%0Aimport globby from 'globby';%0Aimport workspace from '../utils/workspace';%0Aimport %7B difference %7D from 'lodash';%0Aimport %7B join %7D from 'path';%0A%0Aconst flushOfflineDirs = async function flushOfflineDirs(onlineNames) %7B%0A%09try %7B%0A%09%09const cwd = await workspace.getLogsDir();%0A%09%09const all = await globby('*', %7B%0A%09%09%09onlyDirectories: true,%0A%09%09%09cwd,%0A%09%09%7D);%0A%09%09const offlines = difference(all, onlineNames);%0A%09%09await Promise.all(%0A%09%09%09offlines.map(async (name) =%3E %7B%0A%09%09%09%09const logsDir = join(cwd, name);%0A%09%09%09%09return flushLogFiles(%7B logsDir, removeDir: true %7D);%0A%09%09%09%7D),%0A%09%09);%0A%09%7D%0A%09catch (err) %7B%7D%0A%7D;
 %0A%0Aexport
@@ -1345,16 +1345,42 @@
 ames();%0A
+%09flushOfflineDirs(names);%0A
 %09return 

enhance flushall command