-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: updated package names, added additional migrations
- Loading branch information
1 parent
954f94c
commit 8b80fe9
Showing
6 changed files
with
150 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Project, ts } from 'ts-morph'; | ||
import { visitNotIgnoredFiles } from './tools/visit-not-ignored-files'; | ||
import { log } from '@clack/prompts'; | ||
|
||
export function replaceImportInFiles( | ||
changes: [oldImport: string, newImport: string][], | ||
library: string | ||
) { | ||
const project = new Project(); | ||
|
||
visitNotIgnoredFiles('.', (path) => { | ||
if (!path.endsWith('.ts') && !path.endsWith('.tsx')) { | ||
return; | ||
} | ||
project.addSourceFileAtPath(path); | ||
}); | ||
|
||
project.getSourceFiles().forEach((sourceFile) => { | ||
let hasChanges = false; | ||
|
||
sourceFile.getImportDeclarations().forEach((importDeclaration) => { | ||
// startsWith is used in order to handle nested imports | ||
if (importDeclaration.getModuleSpecifierValue().startsWith(library)) { | ||
for (const [oldImport, newImport] of changes) { | ||
importDeclaration.getNamedImports().forEach((namedImport) => { | ||
if (namedImport.getName() === oldImport) { | ||
namedImport.setName(newImport); | ||
hasChanges = true; | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
sourceFile.getDescendantsOfKind(ts.SyntaxKind.Identifier).forEach((identifier) => { | ||
for (const [oldImport, newImport] of changes) { | ||
if (identifier.getText() === oldImport) { | ||
identifier.replaceWithText(newImport); | ||
hasChanges = true; | ||
} | ||
} | ||
}); | ||
|
||
if (hasChanges) { | ||
sourceFile.saveSync(); | ||
log.info(`Updated imports in ${sourceFile.getFilePath()}`); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,48 @@ | ||
import { readPackageJson, writePackageJson } from './../utils/utils'; | ||
// import { getPackageManager } from './../utils/utils'; | ||
// import { installDeps } from '../utils/install-deps'; | ||
import { installDeps } from '../utils/install-deps'; | ||
import { getPackageManager, readPackageJson, writePackageJson } from './../utils/utils'; | ||
import { versions } from './versions'; | ||
|
||
export async function updateDependencies() { | ||
// TODO(migrate-v2): rely on workspaceRoot instead? | ||
const packageJson = await readPackageJson(process.cwd()); | ||
const devDependencies = (packageJson.devDependencies ??= {}); | ||
const dependencies = (packageJson.dependencies ??= {}); | ||
|
||
// TODO: this logic should be enhanced to check in both dependencies and devDependencies | ||
for (const key of Object.keys(devDependencies)) { | ||
if (Object.prototype.hasOwnProperty.call(versions, key)) { | ||
// for now only updating existing dependencies if they exist in root package.json | ||
devDependencies[key] = versions[key as unknown as keyof typeof versions]; | ||
} | ||
} | ||
for (const key of Object.keys(dependencies)) { | ||
if (Object.prototype.hasOwnProperty.call(versions, key)) { | ||
dependencies[key] = versions[key as unknown as keyof typeof versions]; | ||
} | ||
} | ||
|
||
await writePackageJson(process.cwd(), packageJson); | ||
// TODO(migrate-v2): not installing dependencies because we don't have correct versions set | ||
// const { install } = installDeps(getPackageManager(), process.cwd()); | ||
// const passed = await install; | ||
// if (!passed) { | ||
// throw new Error('Failed to install dependencies'); | ||
// } | ||
runInstall(); | ||
} | ||
|
||
export async function installTsMorph() { | ||
const packageJson = await readPackageJson(process.cwd()); | ||
if (packageJson.dependencies?.['ts-morph'] || packageJson.devDependencies?.['ts-morph']) { | ||
return false; | ||
} | ||
(packageJson.devDependencies ??= {})['ts-morph'] = 'latest'; | ||
await runInstall(); | ||
return true; | ||
} | ||
|
||
async function runInstall() { | ||
const { install } = installDeps(getPackageManager(), process.cwd()); | ||
const passed = await install; | ||
if (!passed) { | ||
throw new Error('Failed to install dependencies'); | ||
} | ||
} | ||
|
||
export async function removeTsMorphFromPackageJson() { | ||
const packageJson = await readPackageJson(process.cwd()); | ||
delete packageJson.dependencies?.['ts-morph']; | ||
delete packageJson.devDependencies?.['ts-morph']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export const versions = { | ||
'@qwik.dev/qwik': '2.0.0', | ||
'@qwik.dev/city': '2.0.0', | ||
'@qwik.dev/core': '2.0.0', | ||
'@qwik.dev/router': '2.0.0', | ||
'@qwik.dev/react': '2.0.0', | ||
'eslint-plugin-qwik': '2.0.0', | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.