UNPKG

5.49 kBPlain TextView Raw
1// This is to get require to work correctly
2const appPath = require('electron').remote.app.getAppPath();
3const newAppPath = require('path').resolve(appPath, '../../../../');
4module.paths = [newAppPath];
5// This is to get require to work correctly
6
7import jsverify from 'jsverify-es-module';
8const child_process = require('child_process');
9const uuid = require('uuid/v1');
10const path = require('path');
11const fs = require('fs-extra');
12
13let pastValues: number[] = [];
14export const arbPort = jsverify.bless({
15 generator: () => {
16 return getNewValue();
17 }
18});
19
20function getNewValue(): number {
21 const potentialValue = jsverify.sampler(jsverify.integer(6000, 10000))();
22
23 if (pastValues.includes(potentialValue)) {
24 return getNewValue();
25 }
26 else {
27 pastValues = [...pastValues, potentialValue];
28 return potentialValue;
29 }
30}
31
32export function wait(time: number) {
33 return new Promise((resolve, reject) => {
34 setTimeout(() => {
35 resolve();
36 }, time);
37 });
38}
39
40export function getPromisePieces() {
41 let theResolve: (value?: {} | PromiseLike<{}> | undefined) => void = (value) => {throw new Error('This should not happen')};
42 const thePromise = new Promise((resolve, reject) => {
43 theResolve = resolve;
44 });
45 return {
46 thePromise,
47 theResolve
48 };
49}
50
51export function loadZwitterion(port: number) {
52 return new Promise((resolve, reject) => {
53 const zwitterionProcess = child_process.fork('./main.js', ['--port', `${port}`]);
54
55 zwitterionProcess.on('error', (error: any) => {
56 console.log(error);
57 });
58
59 zwitterionProcess.on('message', (e: any) => {
60 if (e === 'ZWITTERION_LISTENING') {
61 resolve(zwitterionProcess);
62 }
63 });
64 });
65}
66
67const arbPathInfo = jsverify.bless({
68 generator: () => {
69 const numLevels = jsverify.sampler(jsverify.integer(0, 10))();
70 const fileNameWithoutExtension = uuid();
71 const pathWithoutFileNamePieces = new Array(numLevels).fill(0).map((x) => {
72 return uuid();
73 });
74 const pathWithoutFileName = pathWithoutFileNamePieces.join('/') ? pathWithoutFileNamePieces.join('/') + '/' : '';
75 const topLevelDirectory = pathWithoutFileNamePieces[0];
76 const pathWithoutExtension = `${pathWithoutFileName}${fileNameWithoutExtension}`;
77 return {
78 pathWithoutExtension,
79 pathWithoutFileName,
80 fileNameWithoutExtension,
81 topLevelDirectory
82 };
83 }
84});
85
86export const arbScriptElementsInfo = (hasModuleDependencies: boolean) => {
87 return jsverify.bless({
88 generator: () => {
89 const numScriptElements = jsverify.sampler(jsverify.integer(0, 3))(); //TODO try to make more scripts without running out of stack space
90 return new Array(numScriptElements).fill(0).map((x) => {
91 const currentArbPathInfo = jsverify.sampler(arbPathInfo)();
92 const extension = jsverify.sampler(jsverify.oneof([jsverify.constant('.js'), jsverify.constant('.ts')/*, jsverify.constant('')*/]))();
93 const srcPath = `${currentArbPathInfo.pathWithoutExtension}${extension}`;
94 const modulePath = `${currentArbPathInfo.pathWithoutExtension}`;
95 const esModule = jsverify.sampler(jsverify.bool)();
96 // const nodeModule = jsverify.sampler(jsverify.bool)();
97 // const tsFileFromBareSpecifier = extension === '' && jsverify.sampler(jsverify.bool)();
98 // const filePath = `${currentArbPathInfo.pathWithoutExtension}${tsFileFromBareSpecifier ? '.ts' : extension}`;
99 const moduleDependencies = esModule ? hasModuleDependencies ? jsverify.sampler(arbScriptElementsInfo(false))() : [] : [];
100
101 return {
102 ...currentArbPathInfo,
103 fileName: `${currentArbPathInfo.fileNameWithoutExtension}${extension}`,
104 srcPath,
105 modulePath,
106 moduleDependencies,
107 extension,
108 element: `<script${esModule ? ' type="module" ' : ' '}src="${srcPath}"></script>`,
109 contents: `
110 ${moduleDependencies.map((moduleDependency: any, index: number) => {
111 const relativePath = path.relative(currentArbPathInfo.pathWithoutFileName, moduleDependency.modulePath);
112 const normalizedRelativePath = relativePath[0] === '.' ? relativePath : `./${relativePath}`;
113
114 return `
115 import Dependency${index} from '${moduleDependency.extension === '.ts' ? normalizedRelativePath : `${normalizedRelativePath}.js` }';
116 console.log(Dependency${index}); //This makes it so the import doesn't get compiled away
117 `;
118 }).join('\n')}
119
120 ${extension === '.ts' ? 'var typeScriptValue: number = 5;' : 'var javaScriptValue = 5;'}
121
122 if (!window.ZWITTERION_TEST) {
123 window.ZWITTERION_TEST = {};
124 }
125
126 window.ZWITTERION_TEST['${srcPath}'] = '${srcPath}';
127
128 ${!hasModuleDependencies ? 'export default {}' : ''}
129 `
130 };
131 });
132 }
133 });
134};