index.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env node
  2. /**
  3. * This script can be used to initiate the transition for a plugin from ESM source to
  4. * TS source. It will rename the files, update the imports, and add a `tsconfig.json`.
  5. */
  6. import { opendir, readFile, open, writeFile, rm } from 'node:fs/promises'
  7. import { argv } from 'node:process'
  8. import { extname } from 'node:path'
  9. import { existsSync } from 'node:fs'
  10. const packageRoot = new URL(`../../packages/${argv[2]}/`, import.meta.url)
  11. let dir
  12. try {
  13. dir = await opendir(new URL('./src/', packageRoot), { recursive: true })
  14. } catch (cause) {
  15. throw new Error(`Unable to find package "${argv[2]}"`, { cause })
  16. }
  17. const packageJSON = JSON.parse(
  18. await readFile(new URL('./package.json', packageRoot), 'utf-8'),
  19. )
  20. if (packageJSON.type !== 'module') {
  21. throw new Error('Cannot convert non-ESM package to TS')
  22. }
  23. const references = Object.keys(packageJSON.dependencies || {})
  24. .concat(Object.keys(packageJSON.peerDependencies || {}))
  25. .concat(Object.keys(packageJSON.devDependencies || {}))
  26. .filter((pkg) => pkg.startsWith('@uppy/'))
  27. .map((pkg) => ({
  28. path: `../${pkg.slice('@uppy/'.length)}/tsconfig.build.json`,
  29. }))
  30. const depsNotYetConvertedToTS = references.filter(
  31. (ref) => !existsSync(new URL(ref.path, packageRoot)),
  32. )
  33. if (depsNotYetConvertedToTS.length) {
  34. // We need to first convert the dependencies, otherwise we won't be working with the correct types.
  35. throw new Error('Some dependencies have not yet been converted to TS', {
  36. cause: depsNotYetConvertedToTS.map((ref) =>
  37. ref.path.replace(/^\.\./, '@uppy'),
  38. ),
  39. })
  40. }
  41. let tsConfig
  42. try {
  43. tsConfig = await open(new URL('./tsconfig.json', packageRoot), 'wx')
  44. } catch (cause) {
  45. throw new Error('It seems this package has already been transitioned to TS', {
  46. cause,
  47. })
  48. }
  49. for await (const dirent of dir) {
  50. if (!dirent.isDirectory()) {
  51. const { path: filepath } = dirent
  52. const ext = extname(filepath)
  53. await writeFile(
  54. filepath.replace(ext, ext.replace('js', 'ts')),
  55. (await readFile(filepath, 'utf-8')).replace(
  56. /((?:^|\n)import[^\n]*["']\.\.?\/[^'"]+\.)js(x?["'])/g,
  57. '$1ts$2',
  58. ),
  59. )
  60. await rm(filepath)
  61. }
  62. }
  63. await tsConfig.writeFile(
  64. `${JSON.stringify(
  65. {
  66. extends: '../../../tsconfig.shared',
  67. compilerOptions: {
  68. emitDeclarationOnly: false,
  69. noEmit: true,
  70. },
  71. include: ['./package.json', './src/**/*.'],
  72. references,
  73. },
  74. undefined,
  75. 2,
  76. )}\n`,
  77. )
  78. await tsConfig.close()
  79. await writeFile(
  80. new URL('./tsconfig.build.json', import.meta.url),
  81. `${JSON.stringify(
  82. {
  83. extends: '../../../tsconfig.shared',
  84. compilerOptions: {
  85. outDir: './lib',
  86. rootDir: './src',
  87. resolveJsonModule: false,
  88. noImplicitAny: false,
  89. skipLibCheck: true,
  90. },
  91. include: ['./src/**/*.'],
  92. exclude: ['./src/**/*.test.ts'],
  93. references,
  94. },
  95. undefined,
  96. 2,
  97. )}\n`,
  98. )
  99. console.log('Done')