index.js (2198B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 import fs from "fs"; 6 import path from "path"; 7 8 import { makeMockSourceAndContent } from "../../../../utils/test-mockup"; 9 import { setSource } from "../../sources"; 10 import * as asyncValue from "../../../../utils/async-value"; 11 12 export function getFixture(name, type = "js") { 13 return fs.readFileSync( 14 path.join(__dirname, `../fixtures/${name}.${type}`), 15 "utf8" 16 ); 17 } 18 19 function getSourceContent(name, type = "js") { 20 const text = getFixture(name, type); 21 let contentType = "text/javascript"; 22 if (type === "html") { 23 contentType = "text/html"; 24 } else if (type === "vue") { 25 contentType = "text/vue"; 26 } else if (type === "ts") { 27 contentType = "text/typescript"; 28 } else if (type === "tsx") { 29 contentType = "text/typescript-jsx"; 30 } 31 32 return { 33 type: "text", 34 value: text, 35 contentType, 36 }; 37 } 38 39 export function getSource(name, type) { 40 const { value: text, contentType } = getSourceContent(name, type); 41 42 return makeMockSourceAndContent(undefined, name, contentType, text); 43 } 44 45 export function populateSource(name, type) { 46 const { content, ...source } = getSource(name, type); 47 setSource({ 48 id: source.id, 49 text: content.value, 50 contentType: content.contentType, 51 isWasm: false, 52 }); 53 return { 54 ...source, 55 content: asyncValue.fulfilled(content), 56 }; 57 } 58 59 export function getOriginalSource(name, type) { 60 return getOriginalSourceWithContent(name, type); 61 } 62 63 export function getOriginalSourceWithContent(name, type) { 64 const { value: text, contentType } = getSourceContent(name, type); 65 66 return makeMockSourceAndContent( 67 undefined, 68 `${name}/originalSource-1`, 69 contentType, 70 text 71 ); 72 } 73 74 export function populateOriginalSource(name, type) { 75 const { content, ...source } = getOriginalSourceWithContent(name, type); 76 setSource({ 77 id: source.id, 78 text: content.value, 79 contentType: content.contentType, 80 isWasm: false, 81 }); 82 return { 83 ...source, 84 content: asyncValue.fulfilled(content), 85 }; 86 }