|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
18 | | -import browser from "webextension-polyfill"; |
19 | | -import parser from "ua-parser-js"; |
20 | | -import { js_beautify as beautify } from "js-beautify"; |
21 | | -import UpgradeProject from "./migrate"; |
22 | | -import { verifyFile, FileTypes, migrateTestCase, migrateProject, migrateUrls } from "./legacy/migrate"; |
23 | | -import TestCase from "../models/TestCase"; |
24 | | -import UiState from "../stores/view/UiState"; |
25 | | -import PlaybackState from "../stores/view/PlaybackState"; |
26 | | -import ModalState from "../stores/view/ModalState"; |
27 | | -import Selianize, { ParseError } from "selianize"; |
28 | | -import Manager from "../../plugin/manager"; |
29 | | -import chromeGetFile from "./filesystem/chrome"; |
30 | | -import firefoxGetFile from "./filesystem/firefox"; |
31 | | - |
32 | | -export const supportedFileFormats = ".side, text/html"; |
33 | | -const parsedUA = parser(window.navigator.userAgent); |
| 18 | +import browser from 'webextension-polyfill' |
| 19 | +import { js_beautify as beautify } from 'js-beautify' |
| 20 | +import UpgradeProject from './migrate' |
| 21 | +import { |
| 22 | + verifyFile, |
| 23 | + FileTypes, |
| 24 | + migrateTestCase, |
| 25 | + migrateProject, |
| 26 | + migrateUrls, |
| 27 | +} from './legacy/migrate' |
| 28 | +import TestCase from '../models/TestCase' |
| 29 | +import UiState from '../stores/view/UiState' |
| 30 | +import PlaybackState from '../stores/view/PlaybackState' |
| 31 | +import ModalState from '../stores/view/ModalState' |
| 32 | +import Selianize, { ParseError } from 'selianize' |
| 33 | +import Manager from '../../plugin/manager' |
| 34 | +import chromeGetFile from './filesystem/chrome' |
| 35 | +import firefoxGetFile from './filesystem/firefox' |
| 36 | +import { userAgent as parsedUA } from '../../common/utils' |
| 37 | +export const supportedFileFormats = '.side, text/html' |
34 | 38 |
|
35 | 39 | export function getFile(path) { |
36 | | - const browserName = parsedUA.browser.name; |
| 40 | + const browserName = parsedUA.browser.name |
37 | 41 | return (() => { |
38 | | - if (browserName === "Chrome") { |
39 | | - return chromeGetFile(path); |
40 | | - } else if (browserName === "Firefox") { |
41 | | - return firefoxGetFile(path); |
| 42 | + if (browserName === 'Chrome') { |
| 43 | + return chromeGetFile(path) |
| 44 | + } else if (browserName === 'Firefox') { |
| 45 | + return firefoxGetFile(path) |
42 | 46 | } else { |
43 | | - return Promise.reject(new Error("Operation is not supported in this browser")); |
| 47 | + return Promise.reject( |
| 48 | + new Error('Operation is not supported in this browser') |
| 49 | + ) |
44 | 50 | } |
45 | 51 | })().then(blob => { |
46 | | - return new Promise((res) => { |
47 | | - const reader = new FileReader(); |
48 | | - reader.addEventListener("load", () => { |
49 | | - res(reader.result); |
50 | | - }); |
51 | | - reader.readAsDataURL(blob); |
52 | | - }); |
53 | | - }); |
| 52 | + return new Promise(res => { |
| 53 | + const reader = new FileReader() |
| 54 | + reader.addEventListener('load', () => { |
| 55 | + res(reader.result) |
| 56 | + }) |
| 57 | + reader.readAsDataURL(blob) |
| 58 | + }) |
| 59 | + }) |
54 | 60 | } |
55 | 61 |
|
56 | 62 | export function loadAsText(blob) { |
57 | | - return new Promise((res) => { |
58 | | - const fileReader = new FileReader(); |
59 | | - fileReader.onload = (e) => { |
60 | | - res(e.target.result); |
61 | | - }; |
| 63 | + return new Promise(res => { |
| 64 | + const fileReader = new FileReader() |
| 65 | + fileReader.onload = e => { |
| 66 | + res(e.target.result) |
| 67 | + } |
62 | 68 |
|
63 | | - fileReader.readAsText(blob); |
64 | | - }); |
| 69 | + fileReader.readAsText(blob) |
| 70 | + }) |
65 | 71 | } |
66 | 72 |
|
67 | 73 | export function saveProject(_project) { |
68 | | - const project = _project.toJS(); |
69 | | - downloadProject(project); |
70 | | - UiState.saved(); |
| 74 | + const project = _project.toJS() |
| 75 | + downloadProject(project) |
| 76 | + UiState.saved() |
71 | 77 | } |
72 | 78 |
|
73 | 79 | function downloadProject(project) { |
74 | 80 | return exportProject(project).then(snapshot => { |
75 | 81 | if (snapshot) { |
76 | | - project.snapshot = snapshot; |
77 | | - Object.assign(project, Manager.emitDependencies()); |
| 82 | + project.snapshot = snapshot |
| 83 | + Object.assign(project, Manager.emitDependencies()) |
78 | 84 | } |
79 | 85 | return browser.downloads.download({ |
80 | | - filename: project.name + ".side", |
81 | | - url: createBlob("application/json", beautify(JSON.stringify(project), { indent_size: 2 })), |
| 86 | + filename: project.name + '.side', |
| 87 | + url: createBlob( |
| 88 | + 'application/json', |
| 89 | + beautify(JSON.stringify(project), { indent_size: 2 }) |
| 90 | + ), |
82 | 91 | saveAs: true, |
83 | | - conflictAction: "overwrite" |
84 | | - }); |
85 | | - }); |
| 92 | + conflictAction: 'overwrite', |
| 93 | + }) |
| 94 | + }) |
86 | 95 | } |
87 | 96 |
|
88 | 97 | function exportProject(project) { |
89 | 98 | return Manager.validatePluginExport(project).then(() => { |
90 | | - return Selianize(project, { silenceErrors: true, skipStdLibEmitting: true }).catch(err => { |
91 | | - const markdown = ParseError(err && err.message || err); |
92 | | - ModalState.showAlert({ |
93 | | - title: "Error saving project", |
94 | | - description: markdown, |
95 | | - confirmLabel: "Download log", |
96 | | - cancelLabel: "Close" |
97 | | - }, (choseDownload) => { |
98 | | - if (choseDownload) { |
99 | | - browser.downloads.download({ |
100 | | - filename: project.name + "-logs.md", |
101 | | - url: createBlob("text/markdown", markdown), |
102 | | - saveAs: true, |
103 | | - conflictAction: "overwrite" |
104 | | - }); |
| 99 | + return Selianize(project, { |
| 100 | + silenceErrors: true, |
| 101 | + skipStdLibEmitting: true, |
| 102 | + }).catch(err => { |
| 103 | + const markdown = ParseError((err && err.message) || err) |
| 104 | + ModalState.showAlert( |
| 105 | + { |
| 106 | + title: 'Error saving project', |
| 107 | + description: markdown, |
| 108 | + confirmLabel: 'Download log', |
| 109 | + cancelLabel: 'Close', |
| 110 | + }, |
| 111 | + choseDownload => { |
| 112 | + if (choseDownload) { |
| 113 | + browser.downloads.download({ |
| 114 | + filename: project.name + '-logs.md', |
| 115 | + url: createBlob('text/markdown', markdown), |
| 116 | + saveAs: true, |
| 117 | + conflictAction: 'overwrite', |
| 118 | + }) |
| 119 | + } |
105 | 120 | } |
106 | | - }); |
107 | | - return Promise.reject(); |
108 | | - }); |
109 | | - }); |
| 121 | + ) |
| 122 | + return Promise.reject() |
| 123 | + }) |
| 124 | + }) |
110 | 125 | } |
111 | 126 |
|
112 | | -let previousFile = null; |
| 127 | +let previousFile = null |
| 128 | +// eslint-disable-next-line |
113 | 129 | function createBlob(mimeType, data) { |
114 | 130 | const blob = new Blob([data], { |
115 | | - type: "text/plain" |
116 | | - }); |
| 131 | + type: 'text/plain', |
| 132 | + }) |
117 | 133 | // If we are replacing a previously generated file we need to |
118 | 134 | // manually revoke the object URL to avoid memory leaks. |
119 | 135 | if (previousFile !== null) { |
120 | | - window.URL.revokeObjectURL(previousFile); |
| 136 | + window.URL.revokeObjectURL(previousFile) |
121 | 137 | } |
122 | | - previousFile = window.URL.createObjectURL(blob); |
123 | | - return previousFile; |
| 138 | + previousFile = window.URL.createObjectURL(blob) |
| 139 | + return previousFile |
124 | 140 | } |
125 | 141 |
|
126 | 142 | export function loadProject(project, file) { |
127 | 143 | function displayError(error) { |
128 | 144 | ModalState.showAlert({ |
129 | | - title: "Error migrating project", |
| 145 | + title: 'Error migrating project', |
130 | 146 | description: error.message, |
131 | | - confirmLabel: "Close" |
132 | | - }); |
| 147 | + confirmLabel: 'Close', |
| 148 | + }) |
133 | 149 | } |
134 | | - loadAsText(file).then((contents) => { |
| 150 | + loadAsText(file).then(contents => { |
135 | 151 | if (/\.side$/.test(file.name)) { |
136 | | - loadJSProject(project, UpgradeProject(JSON.parse(contents))); |
| 152 | + loadJSProject(project, UpgradeProject(JSON.parse(contents))) |
137 | 153 | } else { |
138 | 154 | try { |
139 | | - const type = verifyFile(contents); |
| 155 | + const type = verifyFile(contents) |
140 | 156 | if (type === FileTypes.Suite) { |
141 | | - ModalState.importSuite(contents, (files) => { |
142 | | - loadJSProject(project, migrateProject(files)); |
143 | | - }); |
| 157 | + ModalState.importSuite(contents, files => { |
| 158 | + try { |
| 159 | + loadJSProject(project, migrateProject(files)) |
| 160 | + } catch (error) { |
| 161 | + displayError(error) |
| 162 | + } |
| 163 | + }) |
144 | 164 | } else if (type === FileTypes.TestCase) { |
145 | | - const { test, baseUrl } = migrateTestCase(contents); |
146 | | - if (!project.urls.includes(baseUrl)) { |
147 | | - ModalState.showAlert({ |
148 | | - title: "Migrate test case", |
149 | | - description: `The test case you're trying to migrate has a different base URL (${baseUrl}) than the project's one. \nIn order to migrate the test case URLs will be made absolute.`, |
150 | | - confirmLabel: "Migrate", |
151 | | - cancelLabel: "Discard" |
152 | | - }, (choseMigration) => { |
153 | | - if (choseMigration) { |
154 | | - UiState.selectTest(project.addTestCase(TestCase.fromJS(migrateUrls(test, baseUrl)))); |
| 165 | + let { test, baseUrl } = migrateTestCase(contents) |
| 166 | + if (project.urls.length && !project.urls.includes(baseUrl)) { |
| 167 | + ModalState.showAlert( |
| 168 | + { |
| 169 | + title: 'Migrate test case', |
| 170 | + description: `The test case you're trying to migrate has a different base URL (${baseUrl}) than the project's one. \nIn order to migrate the test case URLs will be made absolute.`, |
| 171 | + confirmLabel: 'Migrate', |
| 172 | + cancelLabel: 'Discard', |
| 173 | + }, |
| 174 | + choseMigration => { |
| 175 | + if (choseMigration) { |
| 176 | + UiState.selectTest( |
| 177 | + project.addTestCase( |
| 178 | + TestCase.fromJS(migrateUrls(test, baseUrl)) |
| 179 | + ) |
| 180 | + ) |
| 181 | + } |
155 | 182 | } |
156 | | - }); |
| 183 | + ) |
157 | 184 | } else { |
158 | | - UiState.selectTest(project.addTestCase(TestCase.fromJS(test, baseUrl))); |
| 185 | + UiState.selectTest( |
| 186 | + project.addTestCase(TestCase.fromJS(test, baseUrl)) |
| 187 | + ) |
159 | 188 | } |
160 | 189 | } |
161 | 190 | } catch (error) { |
162 | | - displayError(error); |
| 191 | + displayError(error) |
163 | 192 | } |
164 | 193 | } |
165 | | - }); |
| 194 | + }) |
166 | 195 | } |
167 | 196 |
|
168 | 197 | export function loadJSProject(project, data) { |
169 | | - UiState.changeView("Tests"); |
170 | | - PlaybackState.clearPlayingCache(); |
171 | | - UiState.clearViewCache(); |
172 | | - project.fromJS(data); |
173 | | - UiState.projectChanged(); |
| 198 | + UiState.changeView('Tests') |
| 199 | + PlaybackState.clearPlayingCache() |
| 200 | + UiState.clearViewCache() |
| 201 | + project.fromJS(data) |
| 202 | + UiState.projectChanged() |
174 | 203 | Manager.emitMessage({ |
175 | | - action: "event", |
176 | | - event: "projectLoaded", |
| 204 | + action: 'event', |
| 205 | + event: 'projectLoaded', |
177 | 206 | options: { |
178 | | - projectName: project.name |
179 | | - } |
180 | | - }); |
| 207 | + projectName: project.name, |
| 208 | + }, |
| 209 | + }) |
181 | 210 | } |
0 commit comments