Skip to content

Commit c84e7b1

Browse files
feat: updated package names, added additional migrations
1 parent 954f94c commit c84e7b1

File tree

6 files changed

+144
-15
lines changed

6 files changed

+144
-15
lines changed

packages/qwik/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"kleur": "4.1.5",
1919
"prettier": "3.3.3",
2020
"vitest": "2.1.4",
21-
"ignore": "5.3.1"
21+
"ignore": "5.3.1",
22+
"ts-morph": "23.0.0"
2223
},
2324
"engines": {
2425
"node": "^18.17.0 || ^20.3.0 || >=21.0.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Project, ts } from 'ts-morph';
2+
import { visitNotIgnoredFiles } from './tools/visit-not-ignored-files';
3+
import { log } from '@clack/prompts';
4+
5+
export function replaceImportInFiles(
6+
changes: [oldImport: string, newImport: string][],
7+
library: string
8+
) {
9+
const project = new Project();
10+
11+
visitNotIgnoredFiles('.', (path) => {
12+
if (!path.endsWith('.ts') && !path.endsWith('.tsx')) {
13+
return;
14+
}
15+
project.addSourceFileAtPath(path);
16+
});
17+
18+
project.getSourceFiles().forEach((sourceFile) => {
19+
let hasChanges = false;
20+
21+
sourceFile.getImportDeclarations().forEach((importDeclaration) => {
22+
// startsWith is used in order to handle nested imports
23+
if (importDeclaration.getModuleSpecifierValue().startsWith(library)) {
24+
for (const [oldImport, newImport] of changes) {
25+
importDeclaration.getNamedImports().forEach((namedImport) => {
26+
if (namedImport.getName() === oldImport) {
27+
namedImport.setName(newImport);
28+
hasChanges = true;
29+
}
30+
});
31+
}
32+
}
33+
});
34+
35+
sourceFile.getDescendantsOfKind(ts.SyntaxKind.Identifier).forEach((identifier) => {
36+
for (const [oldImport, newImport] of changes) {
37+
if (identifier.getText() === oldImport) {
38+
identifier.replaceWithText(newImport);
39+
hasChanges = true;
40+
}
41+
}
42+
});
43+
44+
if (hasChanges) {
45+
sourceFile.saveSync();
46+
log.info(`Updated imports in ${sourceFile.getFilePath()}`);
47+
}
48+
});
49+
}

packages/qwik/src/cli/migrate-v2/run-migration.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import type { AppCommand } from '../utils/app-command';
33
import { bgMagenta, green } from 'kleur/colors';
44
import { bye } from '../utils/utils';
55
import { replacePackage } from './replace-package';
6-
import { updateDependencies } from './update-dependencies';
6+
import {
7+
installTsMorph,
8+
removeTsMorphFromPackageJson,
9+
updateDependencies,
10+
} from './update-dependencies';
711
import { versions } from './versions';
12+
import { replaceImportInFiles } from './rename-import';
813

914
export async function runV2Migration(app: AppCommand) {
1015
intro(
1116
`✨ ${bgMagenta(' This command will migrate your Qwik application from v1 to v2 \n')}` +
1217
`This includes the following: \n` +
13-
// TODO(migrate-v2): package names
14-
` - "@builder.io/qwik", "@builder.io/qwik-city" packages will be rescoped to "@qwik.dev/core" and "@qwik.dev/city" \n` +
18+
` - "@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` +
1519
` - related dependencies will be updated \n`
1620
);
1721
const proceed = await confirm({
@@ -24,8 +28,29 @@ export async function runV2Migration(app: AppCommand) {
2428
}
2529

2630
try {
27-
replacePackage('@builder.io/qwik-city', '@qwik.dev/city', versions['@qwik.dev/city']);
28-
replacePackage('@builder.io/qwik', '@qwik.dev/qwik', versions['@qwik.dev/qwik']);
31+
const installedTsMorph = await installTsMorph();
32+
33+
replaceImportInFiles(
34+
[
35+
['QwikCityProvider', 'QwikRouterProvider'],
36+
['qwikCity', 'qwikRouter'],
37+
['QwikCityVitePluginOptions', 'QwikRouterVitePluginOptions'],
38+
['QwikCityPlugin', 'QwikRouterPlugin'],
39+
['createQwikCity', 'createQwikRouter'],
40+
['QwikCityNodeRequestOptions', 'QwikRouterNodeRequestOptions'],
41+
],
42+
'@builder.io/qwik-city'
43+
);
44+
45+
replacePackage('@builder.io/qwik-city', '@qwik.dev/router', versions['@qwik.dev/router']);
46+
replacePackage('@builder.io/qwik-react', '@qwik.dev/react', versions['@qwik.dev/react']);
47+
// "@builder.io/qwik" should be the last one because it's name is a substring of the package names above
48+
replacePackage('@builder.io/qwik', '@qwik.dev/core', versions['@qwik.dev/core']);
49+
50+
if (installedTsMorph) {
51+
await removeTsMorphFromPackageJson();
52+
}
53+
2954
await updateDependencies();
3055
log.success(`${green(`Your application has been successfully migrated to v2!`)}`);
3156
} catch (error) {

packages/qwik/src/cli/migrate-v2/update-dependencies.ts

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { readPackageJson, writePackageJson } from './../utils/utils';
1+
import { installDeps } from '../utils/install-deps';
2+
import { getPackageManager, readPackageJson, writePackageJson } from './../utils/utils';
23
// import { getPackageManager } from './../utils/utils';
3-
// import { installDeps } from '../utils/install-deps';
44
import { versions } from './versions';
55

66
export async function updateDependencies() {
@@ -18,9 +18,29 @@ export async function updateDependencies() {
1818

1919
await writePackageJson(process.cwd(), packageJson);
2020
// TODO(migrate-v2): not installing dependencies because we don't have correct versions set
21-
// const { install } = installDeps(getPackageManager(), process.cwd());
22-
// const passed = await install;
23-
// if (!passed) {
24-
// throw new Error('Failed to install dependencies');
25-
// }
21+
// runInstall();
22+
}
23+
24+
export async function installTsMorph() {
25+
const packageJson = await readPackageJson(process.cwd());
26+
if (packageJson.dependencies?.['ts-morph'] || packageJson.devDependencies?.['ts-morph']) {
27+
return false;
28+
}
29+
(packageJson.devDependencies ??= {})['ts-morph'] = 'latest';
30+
await runInstall();
31+
return true;
32+
}
33+
34+
async function runInstall() {
35+
const { install } = installDeps(getPackageManager(), process.cwd());
36+
const passed = await install;
37+
if (!passed) {
38+
throw new Error('Failed to install dependencies');
39+
}
40+
}
41+
42+
export async function removeTsMorphFromPackageJson() {
43+
const packageJson = await readPackageJson(process.cwd());
44+
delete packageJson.dependencies?.['ts-morph'];
45+
delete packageJson.devDependencies?.['ts-morph'];
2646
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const versions = {
2-
'@qwik.dev/qwik': '2.0.0',
3-
'@qwik.dev/city': '2.0.0',
2+
'@qwik.dev/core': '2.0.0',
3+
'@qwik.dev/router': '2.0.0',
4+
'@qwik.dev/react': '2.0.0',
45
'eslint-plugin-qwik': '2.0.0',
56
};

pnpm-lock.yaml

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)