import { logger, setLoggers } from 'pot-logger';
import workspace from './utils/workspace';
import Connection from './Connection';
import ensureSelected from './utils/ensureSelected';
import inquirer from 'inquirer';

export const stop = async function stop(options = {}) {
	let { name } = options;
	const { force, logLevel } = options;

	workspace.set(options);
	setLoggers('logLevel', logLevel);

	name = await ensureSelected({
		value: name,
		message: 'Please select the target app.',
		errorMessage: 'No process is running',
		getChoices: Connection.getNames,
	});

	name += ''; // prevent `name` is `Number`

	const connection = await Connection.getByName(name);

	if (!connection) {
		logger.error(`"${name}" NOT found`);
		return false;
	}

	if (!force) {
		const confirmed = await inquirer.prompt({
			type: 'confirm',
			name: 'yes',
			message: `Are you sure to stop "${name}"?`,
			default: false,
		});

		if (!confirmed.yes) {
			return false;
		}
	}

	return connection.requestStopServer({ shouldLog: true });
};

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

export default stop;

@@ -934,16 +934,50 @@
 .yes) %7B%0A
+%09%09%09await connection.disconnect();%0A
 %09%09%09retur
@@ -985,16 +985,16 @@
  false;%0A
-
 %09%09%7D%0A%09%7D%0A%0A
@@ -1163,22 +1163,44 @@
 mes();%0A%09
-return
+if (options.force) %7B%0A%09%09await
  Promise
@@ -1260,16 +1260,101 @@
 e %7D)));%0A
+%09%7D%0A%09else %7B%0A%09%09for (const name of names) %7B%0A%09%09%09await stop(%7B ...options, name %7D);%0A%09%09%7D%0A%09%7D%0A
 %7D;%0A%0Aexpo

fix stop bug