Skip to content

Commit

Permalink
feat: updated package names, added additional migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-stepanenko committed Nov 3, 2024
1 parent 954f94c commit 09cef2f
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/qwik/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"kleur": "4.1.5",
"prettier": "3.3.3",
"vitest": "2.1.4",
"ignore": "5.3.1"
"ignore": "5.3.1",
"ts-morph": "23.0.0"
},
"engines": {
"node": "^18.17.0 || ^20.3.0 || >=21.0.0"
Expand Down
41 changes: 41 additions & 0 deletions packages/qwik/src/cli/migrate-v2/rename-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Project, ts } from 'ts-morph';
import { visitNotIgnoredFiles } from './tools/visit-not-ignored-files';
import { log } from '@clack/prompts';

export function replaceImportInFiles(oldImport: string, newImport: string, library: string) {
const project = new Project();

visitNotIgnoredFiles('.', (path) => {
if (!path.endsWith('.ts')) {
return;
}
project.addSourceFileAtPath(path);
});

project.getSourceFiles().forEach((sourceFile) => {
let hasChanges = false;

sourceFile.getImportDeclarations().forEach((importDeclaration) => {
if (importDeclaration.getModuleSpecifierValue() === library) {
importDeclaration.getNamedImports().forEach((namedImport) => {
if (namedImport.getName() === oldImport) {
namedImport.renameAlias(newImport);
hasChanges = true;
}
});
}
});

sourceFile.getDescendantsOfKind(ts.SyntaxKind.Identifier).forEach((identifier) => {
if (identifier.getText() === oldImport) {
identifier.replaceWithText(newImport);
hasChanges = true;
}
});

if (hasChanges) {
sourceFile.saveSync();
log.info(`Updated imports of "${oldImport}" in ${sourceFile.getFilePath()}`);
}
});
}
27 changes: 21 additions & 6 deletions packages/qwik/src/cli/migrate-v2/run-migration.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { confirm, intro, isCancel, log } from '@clack/prompts';
import type { AppCommand } from '../utils/app-command';
import { bgMagenta, green } from 'kleur/colors';
import { bye } from '../utils/utils';
import { bye, readPackageJson } from '../utils/utils';

Check failure on line 4 in packages/qwik/src/cli/migrate-v2/run-migration.ts

View workflow job for this annotation

GitHub Actions / Build Qwik

'readPackageJson' is declared but its value is never read.
import { replacePackage } from './replace-package';
import { updateDependencies } from './update-dependencies';
import {
installTsMorph,
removeTsMorphFromPackageJson,
updateDependencies,
} from './update-dependencies';
import { versions } from './versions';
import { replaceImportInFiles } from './rename-import';

export async function runV2Migration(app: AppCommand) {
intro(
`✨ ${bgMagenta(' This command will migrate your Qwik application from v1 to v2 \n')}` +
`This includes the following: \n` +
// TODO(migrate-v2): package names
` - "@builder.io/qwik", "@builder.io/qwik-city" packages will be rescoped to "@qwik.dev/core" and "@qwik.dev/city" \n` +
` - "@builder.io/qwik", "@builder.io/qwik-city" and "@builder.io/qwik-react" packages will be rescoped to "@qwik.dev/core", "@qwik.dev/router" and "@qwik.dev/react" respectively \n` +
` - related dependencies will be updated \n`
);
const proceed = await confirm({
Expand All @@ -24,8 +28,19 @@ export async function runV2Migration(app: AppCommand) {
}

try {
replacePackage('@builder.io/qwik-city', '@qwik.dev/city', versions['@qwik.dev/city']);
replacePackage('@builder.io/qwik', '@qwik.dev/qwik', versions['@qwik.dev/qwik']);
const installedTsMorph = await installTsMorph();

replaceImportInFiles('QwikCityProvider', 'QwikRouterProvider', '@builder.io/qwik-city');
replaceImportInFiles('qwikCity', 'qwikRouter', '@builder.io/qwik-city');

replacePackage('@builder.io/qwik', '@qwik.dev/core', versions['@qwik.dev/core']);
replacePackage('@builder.io/qwik-city', '@qwik.dev/router', versions['@qwik.dev/router']);
replacePackage('@builder.io/qwik-react', '@qwik.dev/react', versions['@qwik.dev/react']);

if (installedTsMorph) {
await removeTsMorphFromPackageJson();
}

await updateDependencies();
log.success(`${green(`Your application has been successfully migrated to v2!`)}`);
} catch (error) {
Expand Down
34 changes: 27 additions & 7 deletions packages/qwik/src/cli/migrate-v2/update-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readPackageJson, writePackageJson } from './../utils/utils';
import { installDeps } from '../utils/install-deps';
import { getPackageManager, readPackageJson, writePackageJson } from './../utils/utils';
// import { getPackageManager } from './../utils/utils';
// import { installDeps } from '../utils/install-deps';
import { versions } from './versions';

export async function updateDependencies() {
Expand All @@ -18,9 +18,29 @@ export async function updateDependencies() {

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'];
}
5 changes: 3 additions & 2 deletions packages/qwik/src/cli/migrate-v2/versions.ts
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',
};
33 changes: 33 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 09cef2f

Please sign in to comment.