/ .github / scripts / transform-jupyter-python.mjs
transform-jupyter-python.mjs
 1  import { readFileSync, writeFileSync, lstatSync } from 'node:fs'
 2  
 3  const filePath = process.argv[2]
 4  
 5  if (!lstatSync(filePath).isFile()) {
 6    throw `${filePath} is not a file`
 7  }
 8  
 9  const pythonFile = readFileSync(filePath, { encoding: 'utf-8' })
10  
11  const pythonFileTransformed = pythonFile
12    .replace(/^(.+) = (.+.run\(.+)\n^\1/gm, '$1 = $2\n$1.save_html("$1.html")')
13  
14  writeFileSync(filePath, pythonFileTransformed)