Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate snapshot files too [LIBS-681] #878

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
35 changes: 29 additions & 6 deletions cli/src/commands/migrate/js-to-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ const isJsxInFile = async (filepath) => {
}
}

const jsRegex = /\.js(\.snap)?/ // handles .js or .js.snap files
const renameFile = async (filepath) => {
const newPath = filepath.concat('x') // Add 'x' to the end to make it 'jsx'
const newPath = filepath.replace(jsRegex, '.jsx$1')
reporter.debug(`Renaming ${filepath} to ${newPath}`)
await fs.rename(filepath, newPath)
}
Expand Down Expand Up @@ -168,7 +169,7 @@ const handler = async ({
globMatches.map(async (matchPath) => {
const jsxIsInFile = await isJsxInFile(matchPath)
if (jsxIsInFile) {
await renameFile(matchPath, renamedFiles)
await renameFile(matchPath)
renamedFiles.add(matchPath)
}
})
Expand All @@ -181,7 +182,7 @@ const handler = async ({
// a renamed item in the set. If so, rewrite the new extension
const globMatches2 = await fg.glob(globString + '(|x)', globOptions)
reporter.info(`Scanning ${globMatches2.length} files to update imports...`)
let fileUpdatedCount = 0
let filesUpdatedCount = 0
await Promise.all(
globMatches2.map(async (matchPath) => {
const importsAreUpdated = await updateImports({
Expand All @@ -190,13 +191,35 @@ const handler = async ({
skipUpdatingImportsWithoutExtension,
})
if (importsAreUpdated) {
fileUpdatedCount++
filesUpdatedCount++
}
})
)
reporter.print(`Updated imports in ${fileUpdatedCount} file(s)`)
reporter.print(`Updated imports in ${filesUpdatedCount} file(s)`)

// 3. Update d2.config.js
// 3. Update snapshot files
const snapshotGlobString = globString + '.snap'
const snapshotGlobMatches = await fg.glob(snapshotGlobString, globOptions)
reporter.info(
`Checking ${snapshotGlobMatches.length} snapshots to update filenames...`
)
let snapshotsUpdatedCount = 0
await Promise.all(
snapshotGlobMatches.map(async (matchPath) => {
// Default snapshot location is './__snapshots__/<baseTestFilename>.snap'
const baseTestFilename = matchPath
.replace(/\.snap$/, '')
.replace('__snapshots__/', '')
const baseTestFileIsUpdated = renamedFiles.has(baseTestFilename)
if (baseTestFileIsUpdated) {
await renameFile(matchPath)
snapshotsUpdatedCount++
}
})
)
reporter.print(`Updated ${snapshotsUpdatedCount} snapshot filename(s)`)

// 4. Update d2.config.js
const d2ConfigPath = path.join(process.cwd(), 'd2.config.js')
reporter.info('Checking d2.config.js for entry points to update...')
reporter.debug(`d2 config path: ${d2ConfigPath}`)
Expand Down
Loading