Skip to content

Commit 3e1cc62

Browse files
feat: migrate to nmrium-core and nmrium-core-plugins
1 parent 6e352d5 commit 3e1cc62

File tree

9 files changed

+506
-682
lines changed

9 files changed

+506
-682
lines changed

app/scripts/nmr-cli/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# build the image ` docker build --tag nmr-cli . `
22
# run the container ` docker run -it nmr-cli bash `
33

4-
FROM mcr.microsoft.com/playwright:v1.53.0-jammy
4+
FROM mcr.microsoft.com/playwright:v1.54.1-jammy
55

66

77
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

app/scripts/nmr-cli/package-lock.json

Lines changed: 122 additions & 299 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/scripts/nmr-cli/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
"nmr-cli": "./build/index.js"
1616
},
1717
"dependencies": {
18+
"@zakodium/nmrium-core": "^0.1.3",
19+
"@zakodium/nmrium-core-plugins": "^0.1.4",
1820
"axios": "^1.10.0",
1921
"filelist-utils": "^1.11.3",
20-
"nmr-load-save": "^3.6.0",
2122
"nmr-processing": "^19.0.0",
22-
"playwright": "^1.53.0",
23+
"playwright": "^1.54.1",
2324
"yargs": "^18.0.0"
2425
},
2526
"devDependencies": {
26-
"@types/node": "^24.0.3",
27+
"@types/node": "^24.0.14",
2728
"@types/yargs": "^17.0.33",
2829
"ts-node": "^10.9.2",
2930
"typescript": "^5.8.3"

app/scripts/nmr-cli/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import yargs, { type Argv, type CommandModule, type Options } from 'yargs'
33
import { loadSpectrumFromURL, loadSpectrumFromFilePath } from './prase-spectra'
44
import { generateSpectrumFromPublicationString } from './publication-string'
55
import { parsePredictionCommand } from './prediction/parsePredictionCommand'
6-
import { hideBin } from 'yargs/helpers';
6+
import { hideBin } from 'yargs/helpers'
77

88
const usageMessage = `
99
Usage: nmr-cli <command> [options]
@@ -126,4 +126,4 @@ yargs(hideBin(process.argv))
126126
.command(parsePredictionCommand)
127127
.showHelpOnFail(true)
128128
.help()
129-
.parse()
129+
.parse()
Lines changed: 96 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,130 @@
1-
import { join, isAbsolute } from "path";
2-
import { type NmriumState, read } from "nmr-load-save";
3-
import { fileCollectionFromWebSource, fileCollectionFromPath } from "filelist-utils";
4-
import playwright from 'playwright';
5-
1+
import { join, isAbsolute } from 'path'
2+
import { type NmriumState } from '@zakodium/nmrium-core'
3+
import init from '@zakodium/nmrium-core-plugins'
4+
import {
5+
fileCollectionFromWebSource,
6+
fileCollectionFromPath,
7+
} from 'filelist-utils'
8+
import playwright from 'playwright'
69

710
interface Snapshot {
8-
image: string,
9-
id: string;
11+
image: string
12+
id: string
1013
}
1114

15+
const core = init()
1216

1317
function generateNMRiumURL() {
14-
const baseURL = process.env['BASE_NMRIUM_URL'] || '';
15-
const url = new URL(baseURL)
16-
url.searchParams.append('workspace', "embedded")
17-
return url.toString()
18+
const baseURL = process.env['BASE_NMRIUM_URL'] || ''
19+
const url = new URL(baseURL)
20+
url.searchParams.append('workspace', 'embedded')
21+
return url.toString()
1822
}
1923

20-
2124
async function captureSpectraViewAsBase64(nmriumState: Partial<NmriumState>) {
22-
const { data: { spectra } = { spectra: [] }, version } = nmriumState;
23-
const browser = await playwright.chromium.launch()
24-
const context = await browser.newContext(playwright.devices['Desktop Chrome HiDPI'])
25-
const page = await context.newPage()
26-
27-
const url = generateNMRiumURL()
28-
29-
await page.goto(url)
25+
const { data: { spectra } = { spectra: [] }, version } = nmriumState
26+
const browser = await playwright.chromium.launch()
27+
const context = await browser.newContext(
28+
playwright.devices['Desktop Chrome HiDPI']
29+
)
30+
const page = await context.newPage()
3031

31-
await page.locator('text=Loading').waitFor({ state: 'hidden' });
32+
const url = generateNMRiumURL()
3233

33-
let snapshots: Snapshot[] = []
34+
await page.goto(url)
3435

35-
for (const spectrum of spectra || []) {
36-
const spectrumObject = {
37-
version,
38-
data: {
39-
spectra: [{ ...spectrum }],
40-
}
36+
await page.locator('text=Loading').waitFor({ state: 'hidden' })
4137

42-
}
38+
let snapshots: Snapshot[] = []
4339

44-
// convert typed array to array
45-
const stringObject = JSON.stringify(spectrumObject, (key, value: unknown) => {
46-
return ArrayBuffer.isView(value) ? Array.from(value as unknown as Iterable<unknown>) : value
47-
})
40+
for (const spectrum of spectra || []) {
41+
const spectrumObject = {
42+
version,
43+
data: {
44+
spectra: [{ ...spectrum }],
45+
},
46+
}
4847

49-
// load the spectrum into NMRium using the custom event
50-
await page.evaluate(
51-
`
48+
// convert typed array to array
49+
const stringObject = JSON.stringify(
50+
spectrumObject,
51+
(key, value: unknown) => {
52+
return ArrayBuffer.isView(value)
53+
? Array.from(value as unknown as Iterable<unknown>)
54+
: value
55+
}
56+
)
57+
58+
// load the spectrum into NMRium using the custom event
59+
await page.evaluate(
60+
`
5261
window.postMessage({ type: "nmr-wrapper:load", data:{data: ${stringObject},type:"nmrium"}}, '*');
5362
`
54-
)
55-
56-
//wait for NMRium process and load spectra
57-
await page.locator('text=Loading').waitFor({ state: 'hidden' });
58-
59-
// take a snapshot for the spectrum
60-
try {
61-
const snapshot = await page.locator('#nmrSVG .container').screenshot()
62-
63-
snapshots.push({
64-
image: snapshot.toString('base64'),
65-
id: spectrum.id,
66-
})
67-
} catch (e) {
68-
console.log(e)
69-
}
70-
}
63+
)
7164

72-
await context.close()
73-
await browser.close()
65+
//wait for NMRium process and load spectra
66+
await page.locator('text=Loading').waitFor({ state: 'hidden' })
7467

75-
return snapshots;
76-
}
68+
// take a snapshot for the spectrum
69+
try {
70+
const snapshot = await page.locator('#nmrSVG .container').screenshot()
7771

78-
async function loadSpectrumFromURL(url: string, enableSnapshot = false) {
79-
const { pathname: relativePath, origin: baseURL } = new URL(url);
80-
const source = {
81-
entries: [
82-
{
83-
relativePath,
84-
}
85-
],
86-
baseURL
87-
};
88-
const fileCollection = await fileCollectionFromWebSource(source, {});
89-
90-
const {
91-
nmriumState: { data, version },
92-
} = await read(fileCollection);
93-
94-
let images: Snapshot[] = []
95-
96-
if (enableSnapshot) {
97-
images = await captureSpectraViewAsBase64({ data, version });
72+
snapshots.push({
73+
image: snapshot.toString('base64'),
74+
id: spectrum.id,
75+
})
76+
} catch (e) {
77+
console.log(e)
9878
}
79+
}
9980

81+
await context.close()
82+
await browser.close()
10083

101-
return { data, version, images };
84+
return snapshots
10285
}
10386

87+
async function loadSpectrumFromURL(url: string, enableSnapshot = false) {
88+
const { pathname: relativePath, origin: baseURL } = new URL(url)
89+
const source = {
90+
entries: [
91+
{
92+
relativePath,
93+
},
94+
],
95+
baseURL,
96+
}
97+
const fileCollection = await fileCollectionFromWebSource(source, {})
98+
99+
const {
100+
nmriumState: { data, version },
101+
} = await core.read(fileCollection)
102+
103+
let images: Snapshot[] = []
104+
105+
if (enableSnapshot) {
106+
images = await captureSpectraViewAsBase64({ data, version })
107+
}
108+
109+
return { data, version, images }
110+
}
104111

105112
async function loadSpectrumFromFilePath(path: string, enableSnapshot = false) {
106-
const dirPath = isAbsolute(path) ? path : join(process.cwd(), path)
113+
const dirPath = isAbsolute(path) ? path : join(process.cwd(), path)
107114

108-
const fileCollection = await fileCollectionFromPath(dirPath, {});
115+
const fileCollection = await fileCollectionFromPath(dirPath, {})
109116

110-
const {
111-
nmriumState: { data, version }
112-
} = await read(fileCollection);
117+
const {
118+
nmriumState: { data, version },
119+
} = await core.read(fileCollection)
113120

114-
let images: Snapshot[] = []
121+
let images: Snapshot[] = []
115122

116-
if (enableSnapshot) {
117-
images = await captureSpectraViewAsBase64({ data, version });
118-
}
123+
if (enableSnapshot) {
124+
images = await captureSpectraViewAsBase64({ data, version })
125+
}
119126

120-
121-
return { data, version, images };
127+
return { data, version, images }
122128
}
123129

124-
125-
export {
126-
loadSpectrumFromFilePath,
127-
loadSpectrumFromURL
128-
};
129-
130+
export { loadSpectrumFromFilePath, loadSpectrumFromURL }

0 commit comments

Comments
 (0)