Skip to content

Commit

Permalink
feat: update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-stepanenko committed Sep 8, 2024
1 parent 6e4ea37 commit 2e4bedd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 95 deletions.
32 changes: 13 additions & 19 deletions packages/qwik/src/cli/migrate-v2/replace-package.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { basename } from 'path';
import { isBinaryPath } from './tools/binary-extensions';
import { visitNotIgnoredFiles } from './tools/visit-not-ignored-files';
import { readFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';
import { log } from '@clack/prompts';

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

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

replaceMentions(oldPackageName, newPackageName);
}

function replacePackageInDependencies(
oldPackageName: string,
newPackageName: string
newPackageName: string,
newPackageVersion: string
) {
visitNotIgnoredFiles('.', (path) => {
if (basename(path) !== 'package.json') {
Expand All @@ -35,24 +38,18 @@ function replacePackageInDependencies(
packageJson.optionalDependencies ?? {},
]) {
if (oldPackageName in deps) {
deps[newPackageName] = deps[oldPackageName];
deps[newPackageName] = newPackageVersion;
delete deps[oldPackageName];
}
}
writeFileSync(path, JSON.stringify(packageJson, null, 2));
updateFileContent(path, JSON.stringify(packageJson, null, 2));
} catch (e) {
console.warn(
`Could not replace ${oldPackageName} with ${newPackageName} in ${path}.`
);
console.warn(`Could not replace ${oldPackageName} with ${newPackageName} in ${path}.`);
}
});
}


function replaceMentions(
oldPackageName: string,
newPackageName: string
) {
function replaceMentions(oldPackageName: string, newPackageName: string) {
visitNotIgnoredFiles('.', (path) => {
if (isBinaryPath(path)) {
return;
Expand All @@ -76,10 +73,7 @@ function replaceMentions(
return;
}

writeFileSync(
path,
contents.replace(new RegExp(oldPackageName, 'g'), newPackageName)
);
updateFileContent(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.
Expand Down
21 changes: 12 additions & 9 deletions packages/qwik/src/cli/migrate-v2/run-migration.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { confirm, intro, isCancel } from '@clack/prompts';
import { confirm, intro, isCancel, log } from '@clack/prompts';
import type { AppCommand } from '../utils/app-command';
import { bgMagenta } from 'kleur/colors';
import { bgMagenta, green } from 'kleur/colors';
import { bye } from '../utils/utils';
import { replacePackage } from './replace-package';
import { updateDependencies } from './update-dependencies';
import { versions } from './versions';

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` +
// 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` +
` - related dependencies will be updated \n`
);
const proceed = await confirm({
Expand All @@ -22,11 +24,12 @@ export async function runV2Migration(app: AppCommand) {
}

try {
replacePackage('@builder.io/qwik', '@qwik.dev/qwik');
replacePackage('@builder.io/qwik-city', '@qwik.dev/city');
replacePackage('@builder.io/qwik-city', '@qwik.dev/city', versions['@qwik.dev/city']);
replacePackage('@builder.io/qwik', '@qwik.dev/qwik', versions['@qwik.dev/qwik']);
await updateDependencies();
log.success(`${green(`Your application has been successfully migrated to v2!`)}`);
} catch (error) {
console.log(error);
throw error
console.error(error);
throw error;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ import { existsSync, lstatSync, readFileSync, readdirSync } from 'fs';
import ignore from 'ignore';
import { join, relative } from 'path';

/**
* Utility to act on all files in a tree that are not ignored by git.
*/
export function visitNotIgnoredFiles(
dirPath: string,
visitor: (path: string) => void
): void {
/** Utility to act on all files in a tree that are not ignored by git. */
export function visitNotIgnoredFiles(dirPath: string, visitor: (path: string) => void): void {
let ig: ReturnType<typeof ignore> | undefined;
if (existsSync('.gitignore')) {
ig = ignore();
ig.add('.git');
ig.add(readFileSync('.gitignore', 'utf-8'));
}
// TODO: test how it works if invoked not from the workspace root
dirPath = relative(process.cwd(), dirPath);
if (dirPath !== '' && ig?.ignores(dirPath)) {
return;
}
for (const child of readdirSync(dirPath)) {
for (const child of readdirSync(join(process.cwd(), dirPath))) {
const fullPath = join(dirPath, child);
if (ig?.ignores(fullPath)) {
continue;
Expand Down
26 changes: 26 additions & 0 deletions packages/qwik/src/cli/migrate-v2/update-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { readPackageJson, writePackageJson } from './../utils/utils';
// import { getPackageManager } from './../utils/utils';
// import { installDeps } from '../utils/install-deps';
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 ??= {});

// 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];
}
}

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');
// }
}
5 changes: 5 additions & 0 deletions packages/qwik/src/cli/migrate-v2/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const versions = {
'@qwik.dev/qwik': '2.0.0',
'@qwik.dev/city': '2.0.0',
'eslint-plugin-qwik': '2.0.0',
};
4 changes: 2 additions & 2 deletions packages/qwik/src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ const COMMANDS = [
{
value: 'migrate-v2',
label: 'migrate-v2',
// TODO: package names
// TODO(migrate-v2): package names
hint: 'Rescopes the application from @builder.io/* namespace to @qwik.dev/*',
run: (app: AppCommand) => runV2Migration(app),
showInHelp: true,
showInHelp: false,
},
{
value: 'help',
Expand Down

0 comments on commit 2e4bedd

Please sign in to comment.