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 c84e7b1
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 15 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
49 changes: 49 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,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()}`);
}
});
}
35 changes: 30 additions & 5 deletions packages/qwik/src/cli/migrate-v2/run-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import type { AppCommand } from '../utils/app-command';
import { bgMagenta, green } from 'kleur/colors';
import { bye } from '../utils/utils';
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,29 @@ 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'],
['qwikCity', 'qwikRouter'],
['QwikCityVitePluginOptions', 'QwikRouterVitePluginOptions'],
['QwikCityPlugin', 'QwikRouterPlugin'],
['createQwikCity', 'createQwikRouter'],
['QwikCityNodeRequestOptions', 'QwikRouterNodeRequestOptions'],
],
'@builder.io/qwik-city'
);

replacePackage('@builder.io/qwik-city', '@qwik.dev/router', versions['@qwik.dev/router']);
replacePackage('@builder.io/qwik-react', '@qwik.dev/react', versions['@qwik.dev/react']);
// "@builder.io/qwik" should be the last one because it's name is a substring of the package names above
replacePackage('@builder.io/qwik', '@qwik.dev/core', versions['@qwik.dev/core']);

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 c84e7b1

Please sign in to comment.