-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·167 lines (119 loc) · 4.16 KB
/
index.js
File metadata and controls
executable file
·167 lines (119 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
const { join, isAbsolute } = require("path");
const yargs = require("yargs");
const loader = require("nmr-load-save");
const fileUtils = require("filelist-utils");
const playwright = require('playwright');
const usageMessage = "Usage: nmr-cli -u <url> or -p <path> -s<Optional parameter to capture spectra snapshots>"
/**
* How to Use the Command Line Tool:
* Example 1: Process spectra files from a URL
* Usage: nmr-cli -u https://example.com/file.zip
* -------------------------------------------------------------------------
* Example 2: process a spectra files from a directory
* Usage: nmr-cli -p /path/to/directory
* -------------------------------------------------------------------------
* you could also combine the above examples with an optional parameter to capturing a snapshot using the -s option
*
*/
const options = yargs
.usage(usageMessage)
.option("u", { alias: "url", describe: "File URL", type: "string", nargs: 1 })
.option("p", { alias: "path", describe: "Directory path", type: "string", nargs: 1 })
.option("s", { alias: "capture-snapshot", describe: "Capture snapshot", type: "boolean" }).showHelpOnFail();
function generateNMRiumURL() {
const baseURL = process.env['BASE_NMRIUM_URL'];
const url = new URL(baseURL)
url.searchParams.append('workspace', "embedded")
return url.toString()
}
async function captureSpectraViewAsBase64(nmriumState) {
const { data: { spectra }, version } = nmriumState;
const browser = await playwright.chromium.launch()
const context = await browser.newContext(playwright.devices['Desktop Chrome HiDPI'])
const page = await context.newPage()
const url = generateNMRiumURL()
await page.goto(url)
await page.locator('text=Loading').waitFor({ state: 'hidden' });
let snapshots = []
for (const spectrum of spectra || []) {
const spectrumObject = {
version,
data: {
spectra: [{ ...spectrum }],
}
}
// convert typed array to array
const stringObject = JSON.stringify(spectrumObject, (key, value) => {
return ArrayBuffer.isView(value) ? Array.from(value) : value
})
// load the spectrum into NMRium using the custom event
await page.evaluate(
`
window.postMessage({ type: "nmr-wrapper:load", data:{data: ${stringObject},type:"nmrium"}}, '*');
`
)
//wait for NMRium process and load spectra
await page.locator('text=Loading').waitFor({ state: 'hidden' });
// take a snapshot for the spectrum
try {
const snapshot = await page.locator('#nmrSVG .container').screenshot()
snapshots.push({
image: snapshot.toString('base64'),
id: spectrum.id,
})
} catch (e) {
console.log(e)
}
}
await context.close()
await browser.close()
return snapshots;
}
async function loadSpectrumFromURL(url, enableSnapshot = false) {
const { pathname: relativePath, origin: baseURL } = new URL(url);
const source = {
entries: [
{
relativePath,
}
],
baseURL
};
const fileCollection = await fileUtils.fileCollectionFromWebSource(source, {});
const {
nmriumState: { data, version },
} = await loader.read(fileCollection);
let images = []
if (enableSnapshot) {
images = await captureSpectraViewAsBase64({ data, version });
}
return { data, version, images };
}
async function loadSpectrumFromFilePath(path, enableSnapshot = false) {
const dirPath = isAbsolute(path) ? path : join(process.cwd(), path)
const fileCollection = await fileUtils.fileCollectionFromPath(dirPath, {});
const {
nmriumState: { data, version }
} = await loader.read(fileCollection);
let images = []
if (enableSnapshot) {
images = await captureSpectraViewAsBase64({ data, version });
}
return { data, version, images };
}
const parameters = options.argv;
if (parameters.u && parameters.p) {
options.showHelp();
} else {
if (parameters.u) {
loadSpectrumFromURL(parameters.u, parameters.s).then((result) => {
console.log(JSON.stringify(result))
})
}
if (parameters.p) {
loadSpectrumFromFilePath(parameters.p, parameters.s).then((result) => {
console.log(JSON.stringify(result))
})
}
}