mocha-to-vitest.mjs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import minimist from 'minimist'
  2. import { exec } from 'node:child_process'
  3. import { promisify } from 'node:util'
  4. import Runner from 'jscodeshift/src/Runner.js'
  5. import { fileURLToPath } from 'node:url'
  6. import path from 'node:path'
  7. // use minimist to get a list of files from the argv
  8. const {
  9. dryRun,
  10. verbose,
  11. usage,
  12. _: files,
  13. } = minimist(process.argv.slice(2), {
  14. boolean: ['dryRun', 'usage', 'verbose'],
  15. })
  16. function printUsage() {
  17. console.log(
  18. 'node scripts/vitest/mocha-to-vitest.mjs [files] [--dryRun] [--format] [--lint] [--usage] [--verbose]'
  19. )
  20. console.log(
  21. 'WARNING: this will only work in local development as important dependencies will be missing in production'
  22. )
  23. console.log('Options:')
  24. console.log(' files: a list of files to convert')
  25. console.log('--dryRun: do not actually run the commands, just print them')
  26. console.log('--format: run prettier to fix formatting')
  27. console.log(' --lint: run eslint to fix linting')
  28. console.log(' --usage: show this help message')
  29. console.log('--verbose: enable verbose output')
  30. process.exit(0)
  31. }
  32. if (usage) {
  33. printUsage()
  34. }
  35. if (!Array.isArray(files) || files.length === 0) {
  36. console.error('You must provide a list of files to convert')
  37. printUsage()
  38. process.exit(1)
  39. }
  40. const promisifiedExec = promisify(exec)
  41. const transforms = [
  42. './codemods/replaceDoneWithPromise.js',
  43. './codemods/convertThisToCtx.js',
  44. './codemods/replaceSandboxedModuleWithDoMock.js',
  45. './codemods/replaceDirectChaiUsage.js',
  46. ]
  47. const config = {
  48. output: import.meta.dirname,
  49. silent: verbose,
  50. print: false,
  51. verbose: verbose ? 1 : 0,
  52. hoist: true,
  53. dry: dryRun,
  54. runInBand: true,
  55. }
  56. if (dryRun) {
  57. console.log('Dry run mode enabled. No changes will be made.')
  58. }
  59. for (const transformPath of transforms) {
  60. if (verbose) {
  61. console.log(`Running transform: ${transformPath}`)
  62. }
  63. const transform = fileURLToPath(await import.meta.resolve(transformPath))
  64. await Runner.run(transform, files, config)
  65. }
  66. const webRoot = fileURLToPath(new URL('../../', import.meta.url))
  67. if (!dryRun) {
  68. for (const file of files) {
  69. // move files with git mv
  70. const newFileName = file
  71. .replace('Tests.mjs', '.test.mjs')
  72. .replace('Tests.js', '.test.js')
  73. .replace('Test.js', '.test.js')
  74. .replace('Test.mjs', '.test.mjs')
  75. await promisifiedExec(`git mv ${file} ${newFileName}`)
  76. const relativePath = path.relative(webRoot, file)
  77. console.log(`transformed ${relativePath} and renamed it for vitest`)
  78. }
  79. }