1 |
|
2 | const appPath = require('electron').remote.app.getAppPath();
|
3 | const newAppPath = require('path').resolve(appPath, '../../../../');
|
4 | module.paths = [newAppPath];
|
5 |
|
6 |
|
7 | import jsverify from 'jsverify-es-module';
|
8 | const child_process = require('child_process');
|
9 | const uuid = require('uuid/v1');
|
10 | const path = require('path');
|
11 | const fs = require('fs-extra');
|
12 |
|
13 | let pastValues: number[] = [];
|
14 | export const arbPort = jsverify.bless({
|
15 | generator: () => {
|
16 | return getNewValue();
|
17 | }
|
18 | });
|
19 |
|
20 | function 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 |
|
32 | export function wait(time: number) {
|
33 | return new Promise((resolve, reject) => {
|
34 | setTimeout(() => {
|
35 | resolve();
|
36 | }, time);
|
37 | });
|
38 | }
|
39 |
|
40 | export 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 |
|
51 | export 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 |
|
67 | const 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 |
|
86 | export const arbScriptElementsInfo = (hasModuleDependencies: boolean) => {
|
87 | return jsverify.bless({
|
88 | generator: () => {
|
89 | const numScriptElements = jsverify.sampler(jsverify.integer(0, 3))();
|
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')]))();
|
93 | const srcPath = `${currentArbPathInfo.pathWithoutExtension}${extension}`;
|
94 | const modulePath = `${currentArbPathInfo.pathWithoutExtension}`;
|
95 | const esModule = jsverify.sampler(jsverify.bool)();
|
96 |
|
97 |
|
98 |
|
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});
|
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 | };
|