@kamaalio/codemod-kit 0.0.37 → 0.0.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/github/utils.d.ts +1 -1
- package/dist/index.cjs +30 -22
- package/dist/index.js +30 -22
- package/package.json +1 -1
package/dist/github/utils.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { Codemod, CodemodRunnerCodemod } from '../codemods/index.js';
|
|
|
4
4
|
export declare function makePullRequestsForCodemodResults<Tag = string, C extends Codemod = Codemod>(codemods: Array<CodemodRunnerCodemod<Tag, C>>, codemodResults: Record<string, Array<Result<{
|
|
5
5
|
hasChanges: boolean;
|
|
6
6
|
content: string;
|
|
7
|
-
}, Error>>>,
|
|
7
|
+
}, Error>>>, repositoriesMappedByCodemodName: Record<string, Array<Repository<Tag>>>): Promise<void>;
|
|
8
8
|
export declare function makePullRequestsForCodemodResult<Tag = string, C extends Codemod = Codemod>(codemod: CodemodRunnerCodemod<Tag, C>, codemodResult: Array<Result<{
|
|
9
9
|
hasChanges: boolean;
|
|
10
10
|
content: string;
|
package/dist/index.cjs
CHANGED
|
@@ -456,10 +456,12 @@ function dedupeRepositoriesToClone(repositories, cwd) {
|
|
|
456
456
|
}, initialDedupeResult).result;
|
|
457
457
|
return dedupedRepos;
|
|
458
458
|
}
|
|
459
|
-
async function makePullRequestsForCodemodResults(codemods, codemodResults,
|
|
459
|
+
async function makePullRequestsForCodemodResults(codemods, codemodResults, repositoriesMappedByCodemodName) {
|
|
460
460
|
for (const [codemodName, codemodResult] of Object.entries(codemodResults)){
|
|
461
461
|
const codemod = codemods.find((c)=>c.name === codemodName);
|
|
462
|
-
|
|
462
|
+
kamaal_namespaceObject.asserts.invariant(null != codemod, 'Codemod should have been present');
|
|
463
|
+
const repositories = repositoriesMappedByCodemodName[codemodName];
|
|
464
|
+
kamaal_namespaceObject.asserts.invariant(null != repositories, 'Repositories should have been present');
|
|
463
465
|
await makePullRequestsForCodemodResult(codemod, codemodResult, repositories);
|
|
464
466
|
}
|
|
465
467
|
}
|
|
@@ -482,13 +484,14 @@ async function makePullRequestsForCodemodResult(codemod, codemodResult, reposito
|
|
|
482
484
|
async function makePullRequest(params) {
|
|
483
485
|
return tryCatchAsync(()=>(0, external_execa_namespaceObject.$)({
|
|
484
486
|
cwd: params.workingDirectory
|
|
485
|
-
})`gh pr create --title ${params.title} --fill`);
|
|
487
|
+
})`gh pr create --draft --title ${params.title} --fill`);
|
|
486
488
|
}
|
|
487
489
|
async function runCodemodsOnProjects(repositoriesToClone, codemods, options) {
|
|
488
490
|
const clonedRepositories = await cloneRepositories(repositoriesToClone, options.workingDirectory);
|
|
489
491
|
console.log(`\u{1F5A8}\u{FE0F} cloned ${clonedRepositories.length} ${1 === clonedRepositories.length ? 'repository' : 'repositories'}`);
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
+
const codemodRepositoriesMappedByCodemodName = await prepareRepositoriesForCodemods(clonedRepositories, codemods, options.workingDirectory);
|
|
493
|
+
const codemodResults = await runCodemodRunner(codemods, codemodRepositoriesMappedByCodemodName, options.workingDirectory);
|
|
494
|
+
if (options.pushChanges) await makePullRequestsForCodemodResults(codemods, codemodResults, codemodRepositoriesMappedByCodemodName);
|
|
492
495
|
}
|
|
493
496
|
function utils_codemodTargetFiltering(repositories, failedRepositoryAddressesMappedByCodemodNames) {
|
|
494
497
|
return (filepath, codemod)=>{
|
|
@@ -514,9 +517,9 @@ async function codemodPostTransform(transformedContent) {
|
|
|
514
517
|
return transformedContent;
|
|
515
518
|
}
|
|
516
519
|
async function runCodemodRunner(codemods, repositories, workingDirectory) {
|
|
517
|
-
const codemodRepositoriesMappedByCodemodName = await prepareRepositoriesForCodemods(repositories, codemods, workingDirectory);
|
|
518
520
|
const results = Object.fromEntries(await Promise.all(codemods.map(async (codemod)=>{
|
|
519
|
-
const
|
|
521
|
+
const start = performance.now();
|
|
522
|
+
const codemodRepositories = repositories[codemod.name];
|
|
520
523
|
kamaal_namespaceObject.asserts.invariant(null != codemodRepositories, 'Codemod repositories should be present');
|
|
521
524
|
const codemodWorkingDirectory = external_node_path_default().resolve(workingDirectory, codemod.name.replace(/\//g, '-'));
|
|
522
525
|
const failedRepositoryAddressesMappedByCodemodNames = {};
|
|
@@ -524,12 +527,14 @@ async function runCodemodRunner(codemods, repositories, workingDirectory) {
|
|
|
524
527
|
rootPaths: codemodRepositories.map((repository)=>repository.path),
|
|
525
528
|
hooks: {
|
|
526
529
|
preCodemodRun: async (codemod)=>{
|
|
527
|
-
failedRepositoryAddressesMappedByCodemodNames[codemod.name] = await codemodPreCodemodRun(
|
|
530
|
+
failedRepositoryAddressesMappedByCodemodNames[codemod.name] = await codemodPreCodemodRun(codemodRepositories, codemod);
|
|
528
531
|
},
|
|
529
532
|
targetFiltering: utils_codemodTargetFiltering(groupByFlat(codemodRepositories, 'name'), failedRepositoryAddressesMappedByCodemodNames),
|
|
530
533
|
postTransform: codemodPostTransform
|
|
531
534
|
}
|
|
532
535
|
});
|
|
536
|
+
const end = performance.now();
|
|
537
|
+
console.log(`\u{2728} '${codemod.name}' codemod took ${((end - start) / 1000).toFixed(2)} seconds`);
|
|
533
538
|
return [
|
|
534
539
|
codemod.name,
|
|
535
540
|
result
|
|
@@ -551,21 +556,9 @@ async function prepareRepositoriesForCodemods(repositories, codemods, workingDir
|
|
|
551
556
|
if (prepareResult.isErr()) throw prepareResult.error;
|
|
552
557
|
return prepareResult.value;
|
|
553
558
|
}));
|
|
554
|
-
console.log(`\u{1F4CB} prepared the following repos for codemods
|
|
559
|
+
console.log(`\u{1F4CB} prepared the following repos for codemods:
|
|
555
560
|
\xb7 ${updatedRepositories.map((repo)=>repo.address).join("\n\xb7 ")}`);
|
|
556
|
-
return Object.fromEntries(await Promise.all(codemods.map(
|
|
557
|
-
const codemodWorkingDirectory = external_node_path_default().resolve(workingDirectory, codemod.name.replace(/\//g, '-'));
|
|
558
|
-
await (0, external_execa_namespaceObject.$)`mkdir -p ${codemodWorkingDirectory}`;
|
|
559
|
-
const codeRepositories = await Promise.all(updatedRepositories.map((repo)=>{
|
|
560
|
-
const newPath = external_node_path_default().join(codemodWorkingDirectory, repo.name);
|
|
561
|
-
console.log(`\xa9\u{FE0F} copying ${repo.name} -> ${newPath}`);
|
|
562
|
-
return repo.copy(newPath);
|
|
563
|
-
}));
|
|
564
|
-
return [
|
|
565
|
-
codemod.name,
|
|
566
|
-
codeRepositories
|
|
567
|
-
];
|
|
568
|
-
})));
|
|
561
|
+
return Object.fromEntries(await Promise.all(codemods.map(copyRepo(workingDirectory, updatedRepositories))));
|
|
569
562
|
}
|
|
570
563
|
async function runCodemods(codemods, transformationPath, options) {
|
|
571
564
|
const results = {};
|
|
@@ -741,6 +734,21 @@ function findAndReplace(content, rule, transformer) {
|
|
|
741
734
|
const edits = findAndReplaceEdits(content, rule, transformer);
|
|
742
735
|
return root.commitEdits(edits);
|
|
743
736
|
}
|
|
737
|
+
function copyRepo(workingDirectory, repositories) {
|
|
738
|
+
return async (codemod)=>{
|
|
739
|
+
const codemodWorkingDirectory = external_node_path_default().resolve(workingDirectory, codemod.name.replace(/\//g, '-'));
|
|
740
|
+
await (0, external_execa_namespaceObject.$)`mkdir -p ${codemodWorkingDirectory}`;
|
|
741
|
+
const codeRepositories = await Promise.all(repositories.map((repo)=>{
|
|
742
|
+
const newPath = external_node_path_default().join(codemodWorkingDirectory, repo.name);
|
|
743
|
+
console.log(`\xa9\u{FE0F} copying ${repo.name} -> ${newPath}`);
|
|
744
|
+
return repo.copy(newPath);
|
|
745
|
+
}));
|
|
746
|
+
return [
|
|
747
|
+
codemod.name,
|
|
748
|
+
codeRepositories
|
|
749
|
+
];
|
|
750
|
+
};
|
|
751
|
+
}
|
|
744
752
|
async function commitEditModifications(edits, modifications) {
|
|
745
753
|
if (0 === edits.length) return modifications;
|
|
746
754
|
const root = modifications.ast.root();
|
package/dist/index.js
CHANGED
|
@@ -407,10 +407,12 @@ function dedupeRepositoriesToClone(repositories, cwd) {
|
|
|
407
407
|
}, initialDedupeResult).result;
|
|
408
408
|
return dedupedRepos;
|
|
409
409
|
}
|
|
410
|
-
async function makePullRequestsForCodemodResults(codemods, codemodResults,
|
|
410
|
+
async function makePullRequestsForCodemodResults(codemods, codemodResults, repositoriesMappedByCodemodName) {
|
|
411
411
|
for (const [codemodName, codemodResult] of Object.entries(codemodResults)){
|
|
412
412
|
const codemod = codemods.find((c)=>c.name === codemodName);
|
|
413
|
-
|
|
413
|
+
asserts.invariant(null != codemod, 'Codemod should have been present');
|
|
414
|
+
const repositories = repositoriesMappedByCodemodName[codemodName];
|
|
415
|
+
asserts.invariant(null != repositories, 'Repositories should have been present');
|
|
414
416
|
await makePullRequestsForCodemodResult(codemod, codemodResult, repositories);
|
|
415
417
|
}
|
|
416
418
|
}
|
|
@@ -433,13 +435,14 @@ async function makePullRequestsForCodemodResult(codemod, codemodResult, reposito
|
|
|
433
435
|
async function makePullRequest(params) {
|
|
434
436
|
return tryCatchAsync(()=>$({
|
|
435
437
|
cwd: params.workingDirectory
|
|
436
|
-
})`gh pr create --title ${params.title} --fill`);
|
|
438
|
+
})`gh pr create --draft --title ${params.title} --fill`);
|
|
437
439
|
}
|
|
438
440
|
async function runCodemodsOnProjects(repositoriesToClone, codemods, options) {
|
|
439
441
|
const clonedRepositories = await cloneRepositories(repositoriesToClone, options.workingDirectory);
|
|
440
442
|
console.log(`\u{1F5A8}\u{FE0F} cloned ${clonedRepositories.length} ${1 === clonedRepositories.length ? 'repository' : 'repositories'}`);
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
+
const codemodRepositoriesMappedByCodemodName = await prepareRepositoriesForCodemods(clonedRepositories, codemods, options.workingDirectory);
|
|
444
|
+
const codemodResults = await runCodemodRunner(codemods, codemodRepositoriesMappedByCodemodName, options.workingDirectory);
|
|
445
|
+
if (options.pushChanges) await makePullRequestsForCodemodResults(codemods, codemodResults, codemodRepositoriesMappedByCodemodName);
|
|
443
446
|
}
|
|
444
447
|
function utils_codemodTargetFiltering(repositories, failedRepositoryAddressesMappedByCodemodNames) {
|
|
445
448
|
return (filepath, codemod)=>{
|
|
@@ -465,9 +468,9 @@ async function codemodPostTransform(transformedContent) {
|
|
|
465
468
|
return transformedContent;
|
|
466
469
|
}
|
|
467
470
|
async function runCodemodRunner(codemods, repositories, workingDirectory) {
|
|
468
|
-
const codemodRepositoriesMappedByCodemodName = await prepareRepositoriesForCodemods(repositories, codemods, workingDirectory);
|
|
469
471
|
const results = Object.fromEntries(await Promise.all(codemods.map(async (codemod)=>{
|
|
470
|
-
const
|
|
472
|
+
const start = performance.now();
|
|
473
|
+
const codemodRepositories = repositories[codemod.name];
|
|
471
474
|
asserts.invariant(null != codemodRepositories, 'Codemod repositories should be present');
|
|
472
475
|
const codemodWorkingDirectory = node_path.resolve(workingDirectory, codemod.name.replace(/\//g, '-'));
|
|
473
476
|
const failedRepositoryAddressesMappedByCodemodNames = {};
|
|
@@ -475,12 +478,14 @@ async function runCodemodRunner(codemods, repositories, workingDirectory) {
|
|
|
475
478
|
rootPaths: codemodRepositories.map((repository)=>repository.path),
|
|
476
479
|
hooks: {
|
|
477
480
|
preCodemodRun: async (codemod)=>{
|
|
478
|
-
failedRepositoryAddressesMappedByCodemodNames[codemod.name] = await codemodPreCodemodRun(
|
|
481
|
+
failedRepositoryAddressesMappedByCodemodNames[codemod.name] = await codemodPreCodemodRun(codemodRepositories, codemod);
|
|
479
482
|
},
|
|
480
483
|
targetFiltering: utils_codemodTargetFiltering(groupByFlat(codemodRepositories, 'name'), failedRepositoryAddressesMappedByCodemodNames),
|
|
481
484
|
postTransform: codemodPostTransform
|
|
482
485
|
}
|
|
483
486
|
});
|
|
487
|
+
const end = performance.now();
|
|
488
|
+
console.log(`\u{2728} '${codemod.name}' codemod took ${((end - start) / 1000).toFixed(2)} seconds`);
|
|
484
489
|
return [
|
|
485
490
|
codemod.name,
|
|
486
491
|
result
|
|
@@ -502,21 +507,9 @@ async function prepareRepositoriesForCodemods(repositories, codemods, workingDir
|
|
|
502
507
|
if (prepareResult.isErr()) throw prepareResult.error;
|
|
503
508
|
return prepareResult.value;
|
|
504
509
|
}));
|
|
505
|
-
console.log(`\u{1F4CB} prepared the following repos for codemods
|
|
510
|
+
console.log(`\u{1F4CB} prepared the following repos for codemods:
|
|
506
511
|
\xb7 ${updatedRepositories.map((repo)=>repo.address).join("\n\xb7 ")}`);
|
|
507
|
-
return Object.fromEntries(await Promise.all(codemods.map(
|
|
508
|
-
const codemodWorkingDirectory = node_path.resolve(workingDirectory, codemod.name.replace(/\//g, '-'));
|
|
509
|
-
await $`mkdir -p ${codemodWorkingDirectory}`;
|
|
510
|
-
const codeRepositories = await Promise.all(updatedRepositories.map((repo)=>{
|
|
511
|
-
const newPath = node_path.join(codemodWorkingDirectory, repo.name);
|
|
512
|
-
console.log(`\xa9\u{FE0F} copying ${repo.name} -> ${newPath}`);
|
|
513
|
-
return repo.copy(newPath);
|
|
514
|
-
}));
|
|
515
|
-
return [
|
|
516
|
-
codemod.name,
|
|
517
|
-
codeRepositories
|
|
518
|
-
];
|
|
519
|
-
})));
|
|
512
|
+
return Object.fromEntries(await Promise.all(codemods.map(copyRepo(workingDirectory, updatedRepositories))));
|
|
520
513
|
}
|
|
521
514
|
async function runCodemods(codemods, transformationPath, options) {
|
|
522
515
|
const results = {};
|
|
@@ -692,6 +685,21 @@ function findAndReplace(content, rule, transformer) {
|
|
|
692
685
|
const edits = findAndReplaceEdits(content, rule, transformer);
|
|
693
686
|
return root.commitEdits(edits);
|
|
694
687
|
}
|
|
688
|
+
function copyRepo(workingDirectory, repositories) {
|
|
689
|
+
return async (codemod)=>{
|
|
690
|
+
const codemodWorkingDirectory = node_path.resolve(workingDirectory, codemod.name.replace(/\//g, '-'));
|
|
691
|
+
await $`mkdir -p ${codemodWorkingDirectory}`;
|
|
692
|
+
const codeRepositories = await Promise.all(repositories.map((repo)=>{
|
|
693
|
+
const newPath = node_path.join(codemodWorkingDirectory, repo.name);
|
|
694
|
+
console.log(`\xa9\u{FE0F} copying ${repo.name} -> ${newPath}`);
|
|
695
|
+
return repo.copy(newPath);
|
|
696
|
+
}));
|
|
697
|
+
return [
|
|
698
|
+
codemod.name,
|
|
699
|
+
codeRepositories
|
|
700
|
+
];
|
|
701
|
+
};
|
|
702
|
+
}
|
|
695
703
|
async function commitEditModifications(edits, modifications) {
|
|
696
704
|
if (0 === edits.length) return modifications;
|
|
697
705
|
const root = modifications.ast.root();
|