Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/scripts/nmr-cli/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# build the image ` docker build --tag nmr-cli . `
# run the container ` docker run -it nmr-cli bash `

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


SHELL ["/bin/bash", "-o", "pipefail", "-c"]
Expand Down
421 changes: 122 additions & 299 deletions app/scripts/nmr-cli/package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions app/scripts/nmr-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
"nmr-cli": "./build/index.js"
},
"dependencies": {
"@zakodium/nmrium-core": "^0.1.3",
"@zakodium/nmrium-core-plugins": "^0.1.4",
"axios": "^1.10.0",
"filelist-utils": "^1.11.3",
"nmr-load-save": "^3.6.0",
"nmr-processing": "^19.0.0",
"playwright": "^1.53.0",
"playwright": "^1.54.1",
"yargs": "^18.0.0"
},
"devDependencies": {
"@types/node": "^24.0.3",
"@types/node": "^24.0.14",
"@types/yargs": "^17.0.33",
"ts-node": "^10.9.2",
"typescript": "^5.8.3"
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/nmr-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import yargs, { type Argv, type CommandModule, type Options } from 'yargs'
import { loadSpectrumFromURL, loadSpectrumFromFilePath } from './prase-spectra'
import { generateSpectrumFromPublicationString } from './publication-string'
import { parsePredictionCommand } from './prediction/parsePredictionCommand'
import { hideBin } from 'yargs/helpers';
import { hideBin } from 'yargs/helpers'

const usageMessage = `
Usage: nmr-cli <command> [options]
Expand Down Expand Up @@ -126,4 +126,4 @@ yargs(hideBin(process.argv))
.command(parsePredictionCommand)
.showHelpOnFail(true)
.help()
.parse()
.parse()
191 changes: 96 additions & 95 deletions app/scripts/nmr-cli/src/prase-spectra.ts
Original file line number Diff line number Diff line change
@@ -1,129 +1,130 @@
import { join, isAbsolute } from "path";
import { type NmriumState, read } from "nmr-load-save";
import { fileCollectionFromWebSource, fileCollectionFromPath } from "filelist-utils";
import playwright from 'playwright';

import { join, isAbsolute } from 'path'
import { type NmriumState } from '@zakodium/nmrium-core'
import init from '@zakodium/nmrium-core-plugins'
import {
fileCollectionFromWebSource,
fileCollectionFromPath,
} from 'filelist-utils'
import playwright from 'playwright'

interface Snapshot {
image: string,
id: string;
image: string
id: string
}

const core = init()

function generateNMRiumURL() {
const baseURL = process.env['BASE_NMRIUM_URL'] || '';
const url = new URL(baseURL)
url.searchParams.append('workspace', "embedded")
return url.toString()
const baseURL = process.env['BASE_NMRIUM_URL'] || ''
const url = new URL(baseURL)
url.searchParams.append('workspace', 'embedded')
return url.toString()
}


async function captureSpectraViewAsBase64(nmriumState: Partial<NmriumState>) {
const { data: { spectra } = { 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)
const { data: { spectra } = { spectra: [] }, version } = nmriumState
const browser = await playwright.chromium.launch()
const context = await browser.newContext(
playwright.devices['Desktop Chrome HiDPI']
)
const page = await context.newPage()

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

let snapshots: Snapshot[] = []
await page.goto(url)

for (const spectrum of spectra || []) {
const spectrumObject = {
version,
data: {
spectra: [{ ...spectrum }],
}
await page.locator('text=Loading').waitFor({ state: 'hidden' })

}
let snapshots: Snapshot[] = []

// convert typed array to array
const stringObject = JSON.stringify(spectrumObject, (key, value: unknown) => {
return ArrayBuffer.isView(value) ? Array.from(value as unknown as Iterable<unknown>) : value
})
for (const spectrum of spectra || []) {
const spectrumObject = {
version,
data: {
spectra: [{ ...spectrum }],
},
}

// load the spectrum into NMRium using the custom event
await page.evaluate(
`
// convert typed array to array
const stringObject = JSON.stringify(
spectrumObject,
(key, value: unknown) => {
return ArrayBuffer.isView(value)
? Array.from(value as unknown as Iterable<unknown>)
: 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()
//wait for NMRium process and load spectra
await page.locator('text=Loading').waitFor({ state: 'hidden' })

return snapshots;
}
// take a snapshot for the spectrum
try {
const snapshot = await page.locator('#nmrSVG .container').screenshot()

async function loadSpectrumFromURL(url: string, enableSnapshot = false) {
const { pathname: relativePath, origin: baseURL } = new URL(url);
const source = {
entries: [
{
relativePath,
}
],
baseURL
};
const fileCollection = await fileCollectionFromWebSource(source, {});

const {
nmriumState: { data, version },
} = await read(fileCollection);

let images: Snapshot[] = []

if (enableSnapshot) {
images = await captureSpectraViewAsBase64({ data, version });
snapshots.push({
image: snapshot.toString('base64'),
id: spectrum.id,
})
} catch (e) {
console.log(e)
}
}

await context.close()
await browser.close()

return { data, version, images };
return snapshots
}

async function loadSpectrumFromURL(url: string, enableSnapshot = false) {
const { pathname: relativePath, origin: baseURL } = new URL(url)
const source = {
entries: [
{
relativePath,
},
],
baseURL,
}
const fileCollection = await fileCollectionFromWebSource(source, {})

const {
nmriumState: { data, version },
} = await core.read(fileCollection)

let images: Snapshot[] = []

if (enableSnapshot) {
images = await captureSpectraViewAsBase64({ data, version })
}

return { data, version, images }
}

async function loadSpectrumFromFilePath(path: string, enableSnapshot = false) {
const dirPath = isAbsolute(path) ? path : join(process.cwd(), path)
const dirPath = isAbsolute(path) ? path : join(process.cwd(), path)

const fileCollection = await fileCollectionFromPath(dirPath, {});
const fileCollection = await fileCollectionFromPath(dirPath, {})

const {
nmriumState: { data, version }
} = await read(fileCollection);
const {
nmriumState: { data, version },
} = await core.read(fileCollection)

let images: Snapshot[] = []
let images: Snapshot[] = []

if (enableSnapshot) {
images = await captureSpectraViewAsBase64({ data, version });
}
if (enableSnapshot) {
images = await captureSpectraViewAsBase64({ data, version })
}


return { data, version, images };
return { data, version, images }
}


export {
loadSpectrumFromFilePath,
loadSpectrumFromURL
};

export { loadSpectrumFromFilePath, loadSpectrumFromURL }
Loading
Loading