Skip to content

Commit

Permalink
Add CLI file functions for PWA (#3976)
Browse files Browse the repository at this point in the history
* Add CLI file functions for PWA

* Ugly fix

* Better fix
  • Loading branch information
haslinghuis authored May 22, 2024
1 parent 57a8f38 commit 2bd7f2b
Showing 1 changed file with 101 additions and 129 deletions.
230 changes: 101 additions & 129 deletions src/js/tabs/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import CliAutoComplete from "../CliAutoComplete";
import UI_PHONES from "../phones_ui";
import { gui_log } from "../gui_log";
import jBox from "jbox";
import { checkChromeRuntimeError } from "../utils/common";
import $ from 'jquery';
import { serialShim } from "../serial_shim";

Expand Down Expand Up @@ -110,31 +109,93 @@ cli.initialize = function (callback) {
self.GUI.windowWrapper.empty();
}

function executeCommands(outString) {
async function executeCommands(outString) {
self.history.add(outString.trim());

const outputArray = outString.split("\n");
return outputArray.reduce((p, line, index) =>
p.then((delay) =>
new Promise((resolve) => {
GUI.timeout_add('CLI_send_slowly', function () {
let processingDelay = self.lineDelayMs;
line = line.trim();
if (line.toLowerCase().startsWith('profile')) {
processingDelay = self.profileSwitchDelayMs;
}
const isLastCommand = outputArray.length === index + 1;
if (isLastCommand && self.cliBuffer) {
line = getCliCommand(line, self.cliBuffer);
}
self.sendLine(line, function () {
resolve(processingDelay);
});
}, delay);
}),
),
Promise.resolve(0),
);

outputArray.forEach((command, index) => {
let line = command.trim();
let processingDelay = self.lineDelayMs;
if (line.toLowerCase().startsWith('profile')) {
processingDelay = self.profileSwitchDelayMs;
}
const isLastCommand = outputArray.length === index + 1;
if (isLastCommand && self.cliBuffer) {
line = getCliCommand(line, self.cliBuffer);
}

GUI.timeout_add('CLI_send_slowly', function () {
self.sendLine(line, function () {
console.log('line sent', line);
});
}, processingDelay);
});
}

async function loadFile() {
const accept = {
types: [{
description: 'Text file',
accept: {'text/plain': ['.txt']},
}],
};
const previewArea = $("#snippetpreviewcontent textarea#preview");

function executeSnippet(fileName) {
const commands = previewArea.val();

tracking.sendEvent(tracking.EVENT_CATEGORIES.FLIGHT_CONTROLLER, 'CliExecuteFromFile', { filename: fileName });

executeCommands(commands);
self.GUI.snippetPreviewWindow.close();
}

function previewCommands(result, fileName) {
if (!self.GUI.snippetPreviewWindow) {
self.GUI.snippetPreviewWindow = new jBox("Modal", {
id: "snippetPreviewWindow",
width: 'auto',
height: 'auto',
closeButton: 'title',
animation: false,
isolateScroll: false,
title: i18n.getMessage("cliConfirmSnippetDialogTitle", { fileName: fileName }),
content: $('#snippetpreviewcontent'),
onCreated: () =>
$("#snippetpreviewcontent a.confirm").click(() => executeSnippet(fileName))
,
});
}
previewArea.val(result);
self.GUI.snippetPreviewWindow.open();
}

const [fileHandle] = await window.showOpenFilePicker(accept);
const file = await fileHandle.getFile();
const contents = await file.text();
previewCommands(contents, file.name);
}

async function saveFile(filename, content) {
const options = {
id: GUI.active_tab,
startIn: 'documents',
suggestedName: filename,
types: [
{
description: 'Text file',
accept: {
'text/plain': ['.txt'],
},
},
],
};

const fileHandle = await window.showSaveFilePicker(options);
const writable = await fileHandle.createWritable();
await writable.write(content);
await writable.close();
}

$('#content').load("./tabs/cli.html", function () {
Expand Down Expand Up @@ -164,46 +225,10 @@ cli.initialize = function (callback) {
.focus();
});

$('.tab-cli .save').click(function() {
const prefix = 'cli';
const suffix = 'txt';

const filename = generateFilename(prefix, suffix);

const accepts = [{
description: `${suffix.toUpperCase()} files`, extensions: [suffix],
}];

chrome.fileSystem.chooseEntry({type: 'saveFile', suggestedName: filename, accepts: accepts}, function(entry) {
if (checkChromeRuntimeError()) {
return;
}

if (!entry) {
console.log('No file selected');
return;
}

entry.createWriter(function (writer) {
writer.onerror = function (){
console.error('Failed to write file');
};

writer.onwriteend = function () {
if (self.outputHistory.length > 0 && writer.length === 0) {
writer.write(new Blob([self.outputHistory], {type: 'text/plain'}));
} else {
tracking.sendEvent(tracking.EVENT_CATEGORIES.FLIGHT_CONTROLLER, 'CliSave', { length: self.outputHistory.length });

console.log('write complete');
}
};
$('.tab-cli .save').on('click', function() {
const filename = generateFilename('cli', 'txt');

writer.truncate(0);
}, function (){
console.error('Failed to get file writer');
});
});
saveFile(filename, self.outputHistory);
});

$('.tab-cli .clear').click(function() {
Expand All @@ -218,60 +243,8 @@ cli.initialize = function (callback) {
self.GUI.copyButton.hide();
}

$('.tab-cli .load').click(function() {
const accepts = [
{
description: 'Config files', extensions: ["txt", "config"],
},
{
description: 'All files',
},
];

chrome.fileSystem.chooseEntry({type: 'openFile', accepts: accepts}, function(entry) {
if (checkChromeRuntimeError()) {
return;
}

const previewArea = $("#snippetpreviewcontent textarea#preview");

function executeSnippet(fileName) {
const commands = previewArea.val();

tracking.sendEvent(tracking.EVENT_CATEGORIES.FLIGHT_CONTROLLER, 'CliExecuteFromFile', { filename: fileName });

executeCommands(commands);
self.GUI.snippetPreviewWindow.close();
}

function previewCommands(result, fileName) {
if (!self.GUI.snippetPreviewWindow) {
self.GUI.snippetPreviewWindow = new jBox("Modal", {
id: "snippetPreviewWindow",
width: 'auto',
height: 'auto',
closeButton: 'title',
animation: false,
isolateScroll: false,
title: i18n.getMessage("cliConfirmSnippetDialogTitle", { fileName: fileName }),
content: $('#snippetpreviewcontent'),
onCreated: () =>
$("#snippetpreviewcontent a.confirm").click(() => executeSnippet(fileName))
,
});
}
previewArea.val(result);
self.GUI.snippetPreviewWindow.open();
}

entry.file((file) => {
const reader = new FileReader();
reader.onload =
() => previewCommands(reader.result, file.name);
reader.onerror = () => console.error(reader.error);
reader.readAsText(file);
});
});
$('.tab-cli .load').on('click', function() {
loadFile();
});

$('.tab-cli .support')
Expand All @@ -282,20 +255,19 @@ cli.initialize = function (callback) {
clearHistory();
const api = new BuildApi();

api.getSupportCommands(commands => {
api.getSupportCommands(async commands => {
commands = [`###\n# Problem description\n# ${data}\n###`, ...commands];
executeCommands(commands.join('\n')).then(() => {
const delay = setInterval(() => {
const time = new Date().getTime();
if (self.lastArrival < time - 250) {
clearInterval(delay);
const text = self.outputHistory;
api.submitSupportData(text, key => {
writeToOutput(i18n.getMessage('buildServerSupportRequestSubmission', [key]));
});
}
}, 250);
});
await executeCommands(commands.join('\n'));
const delay = setInterval(() => {
const time = new Date().getTime();
if (self.lastArrival < time - 250) {
clearInterval(delay);
const text = self.outputHistory;
api.submitSupportData(text, key => {
writeToOutput(i18n.getMessage('buildServerSupportRequestSubmission', [key]));
});
}
}, 250);
});
}

Expand Down

0 comments on commit 2bd7f2b

Please sign in to comment.