-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7840f17
commit 37c92c2
Showing
8 changed files
with
508 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.` | ||
); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
Oops, something went wrong.