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