Skip to content

Commit

Permalink
feat: add migrate-v2 command
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-stepanenko committed Jun 29, 2024
1 parent 7840f17 commit 37c92c2
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/qwik/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"@builder.io/qwik": "workspace:^",
"@builder.io/qwik-dom": "workspace:^",
"image-size": "^1.0.2",
"kleur": "4.1.5"
"kleur": "4.1.5",
"ignore": "^5.3.1"
},
"engines": {
"node": ">=16.8.0 <18.0.0 || >=18.11"
Expand Down
91 changes: 91 additions & 0 deletions packages/qwik/src/cli/migrate-v2/replace-package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { basename } from 'path';
import { isBinaryPath } from './tools/binary-extensions';
import { visitNotIgnoredFiles } from './tools/visit-not-ignored-files';
import { readFileSync } from 'fs';
import { log } from '@clack/prompts';

function writeFileSync(path: string, content: string) {
log.info(`"${path}" has been updated`);
}

export function replacePackage(
oldPackageName: string,
newPackageName: string
): void {
replacePackageInDependencies(oldPackageName, newPackageName);

replaceMentions(oldPackageName, newPackageName);
}

function replacePackageInDependencies(
oldPackageName: string,
newPackageName: string
) {
visitNotIgnoredFiles('.', (path) => {
if (basename(path) !== 'package.json') {
return;
}

try {
const packageJson = JSON.parse(readFileSync(path, 'utf-8'));
for (const deps of [
packageJson.dependencies ?? {},
packageJson.devDependencies ?? {},
packageJson.peerDependencies ?? {},
packageJson.optionalDependencies ?? {},
]) {
if (oldPackageName in deps) {
deps[newPackageName] = deps[oldPackageName];
delete deps[oldPackageName];
}
}
writeFileSync(path, JSON.stringify(packageJson, null, 2));
} catch (e) {
console.warn(
`Could not replace ${oldPackageName} with ${newPackageName} in ${path}.`
);
}
});
}


function replaceMentions(
oldPackageName: string,
newPackageName: string
) {
visitNotIgnoredFiles('.', (path) => {
if (isBinaryPath(path)) {
return;
}

const ignoredFiles = [
'yarn.lock',
'package-lock.json',
'pnpm-lock.yaml',
'bun.lockb',
'CHANGELOG.md',
];
if (ignoredFiles.includes(basename(path))) {
return;
}

try {
const contents = readFileSync(path, 'utf-8');

if (!contents.includes(oldPackageName)) {
return;
}

writeFileSync(
path,
contents.replace(new RegExp(oldPackageName, 'g'), newPackageName)
);
} catch {
// Its **probably** ok, contents can be null if the file is too large or
// there was an access exception.
log.warn(
`An error was thrown when trying to update ${path}. If you believe the migration should have updated it, be sure to review the file and open an issue.`
);
}
});
}
32 changes: 32 additions & 0 deletions packages/qwik/src/cli/migrate-v2/run-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { confirm, intro, isCancel } from '@clack/prompts';
import type { AppCommand } from '../utils/app-command';
import { bgMagenta } from 'kleur/colors';
import { bye } from '../utils/utils';
import { replacePackage } from './replace-package';

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: package names
` - "@builder.io/qwik", "@builder.io/qwik-city" packages will be rescoped to "@qwik.dev/core" and "@qwik.dev/qwik-city" \n` +
` - related dependencies will be updated \n`
);
const proceed = await confirm({
message: 'Do you want to proceed?',
initialValue: true,
});

if (isCancel(proceed) || !proceed) {
bye();
}

try {
replacePackage('@builder.io/qwik', '@qwik.dev/qwik');
replacePackage('@builder.io/qwik-city', '@qwik.dev/city');
} catch (error) {
console.log(error);
throw error
}

}
Loading

0 comments on commit 37c92c2

Please sign in to comment.