@kamaalio/codemod-kit 0.0.27 → 0.0.28

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.
@@ -2,11 +2,20 @@ import type { Edit, Rule, SgNode, SgRoot } from '@ast-grep/napi';
2
2
  import type { NapiLang } from '@ast-grep/napi/types/lang.js';
3
3
  import type { Kinds, TypesMap } from '@ast-grep/napi/types/staticTypes.js';
4
4
  import type { types } from '@kamaalio/kamaal';
5
+ export type RunCodemodOkResult = {
6
+ hasChanges: boolean;
7
+ content: string;
8
+ fullPath: string;
9
+ root: string;
10
+ };
5
11
  export type Codemod = {
6
12
  name: string;
7
13
  languages: Set<NapiLang> | Array<NapiLang>;
8
14
  transformer: (content: string, filename?: types.Optional<string>) => Promise<string>;
9
- postTransform?: (rootPath: string) => Promise<void>;
15
+ postTransform?: (rootPath: {
16
+ root: string;
17
+ results: Array<RunCodemodOkResult>;
18
+ }) => Promise<void>;
10
19
  };
11
20
  export type ModificationsReport = {
12
21
  changesApplied: number;
@@ -3,7 +3,7 @@ import { type Edit, type SgRoot, type SgNode } from '@ast-grep/napi';
3
3
  import type { Kinds, TypesMap } from '@ast-grep/napi/types/staticTypes.js';
4
4
  import type { NapiLang } from '@ast-grep/napi/types/lang.js';
5
5
  import { type types } from '@kamaalio/kamaal';
6
- import type { Codemod, FindAndReplaceConfig, Modifications } from './types.js';
6
+ import type { Codemod, FindAndReplaceConfig, Modifications, RunCodemodOkResult } from './types.js';
7
7
  type RunCodemodHooks<C extends Codemod> = {
8
8
  targetFiltering?: (filepath: string, codemod: C) => boolean;
9
9
  preCodemodRun?: (codemod: C) => Promise<void>;
@@ -15,14 +15,9 @@ type RunCodemodOptions<C extends Codemod> = {
15
15
  dry?: boolean;
16
16
  rootPaths?: Array<string>;
17
17
  };
18
- export declare function runCodemods<C extends Codemod>(codemods: Array<C>, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Record<string, Array<Result<{
19
- hasChanges: boolean;
20
- content: string;
21
- }, Error>>>>;
22
- export declare function runCodemod<C extends Codemod>(codemod: C, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Array<Result<{
23
- hasChanges: boolean;
24
- content: string;
25
- }, Error>>>;
18
+ type RunCodemodResult = Result<RunCodemodOkResult, Error>;
19
+ export declare function runCodemods<C extends Codemod>(codemods: Array<C>, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Record<string, Array<RunCodemodResult>>>;
20
+ export declare function runCodemod<C extends Codemod>(codemod: C, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Array<RunCodemodResult>>;
26
21
  export declare function traverseUp(node: SgNode<TypesMap, Kinds<TypesMap>>, until: (node: SgNode<TypesMap, Kinds<TypesMap>>) => boolean): types.Optional<SgNode<TypesMap, Kinds<TypesMap>>>;
27
22
  export declare function findAndReplaceConfigModifications(modifications: Modifications, config: Array<FindAndReplaceConfig>): Promise<Modifications>;
28
23
  export declare function findAndReplaceConfig(content: SgRoot<TypesMap>, lang: NapiLang, config: Array<FindAndReplaceConfig>): Promise<string>;
package/dist/index.cjs CHANGED
@@ -83,6 +83,19 @@ function getCollectionCount(collection) {
83
83
  function collectionIsEmpty(collection) {
84
84
  return 0 === getCollectionCount(collection);
85
85
  }
86
+ function groupBy(array, key) {
87
+ const arrayCopy = [
88
+ ...array
89
+ ];
90
+ return arrayCopy.reduce((acc, current)=>{
91
+ const keyValue = String(current[key]);
92
+ if (null == acc[keyValue]) acc[keyValue] = [
93
+ current
94
+ ];
95
+ else acc[keyValue].push(current);
96
+ return acc;
97
+ }, {});
98
+ }
86
99
  async function runCodemods(codemods, transformationPath, options) {
87
100
  const results = {};
88
101
  for (const codemod of codemods)results[codemod.name] = await runCodemod(codemod, transformationPath, options);
@@ -124,14 +137,25 @@ async function runCodemod(codemod, transformationPath, options) {
124
137
  }
125
138
  return (0, external_neverthrow_namespaceObject.ok)({
126
139
  hasChanges,
127
- content: modifiedContent
140
+ content: modifiedContent,
141
+ fullPath,
142
+ root: filepath.split('/')[0]
128
143
  });
129
144
  } catch (error) {
130
145
  if (enableLogging) console.error(`\u{274C} '${codemod.name}' failed to parse file`, filepath, error);
131
146
  return (0, external_neverthrow_namespaceObject.err)(error);
132
147
  }
133
148
  }));
134
- await Promise.all(rootPaths.map((rootPath)=>(codemod.postTransform ?? (async ()=>{}))(rootPath)));
149
+ const successes = kamaal_namespaceObject.arrays.compactMap(results, (result)=>{
150
+ if (result.isErr()) return null;
151
+ return result.value;
152
+ });
153
+ const successesGroupedByRoot = groupBy(successes, 'root');
154
+ const rootPathsWithResults = rootPaths.map((root)=>({
155
+ root,
156
+ results: successesGroupedByRoot[root] ?? []
157
+ }));
158
+ await Promise.all(rootPathsWithResults.map((r)=>(codemod.postTransform ?? (async ()=>{}))(r)));
135
159
  return results;
136
160
  }
137
161
  function traverseUp(node, until) {
package/dist/index.js CHANGED
@@ -36,6 +36,19 @@ function getCollectionCount(collection) {
36
36
  function collectionIsEmpty(collection) {
37
37
  return 0 === getCollectionCount(collection);
38
38
  }
39
+ function groupBy(array, key) {
40
+ const arrayCopy = [
41
+ ...array
42
+ ];
43
+ return arrayCopy.reduce((acc, current)=>{
44
+ const keyValue = String(current[key]);
45
+ if (null == acc[keyValue]) acc[keyValue] = [
46
+ current
47
+ ];
48
+ else acc[keyValue].push(current);
49
+ return acc;
50
+ }, {});
51
+ }
39
52
  async function runCodemods(codemods, transformationPath, options) {
40
53
  const results = {};
41
54
  for (const codemod of codemods)results[codemod.name] = await runCodemod(codemod, transformationPath, options);
@@ -77,14 +90,25 @@ async function runCodemod(codemod, transformationPath, options) {
77
90
  }
78
91
  return ok({
79
92
  hasChanges,
80
- content: modifiedContent
93
+ content: modifiedContent,
94
+ fullPath,
95
+ root: filepath.split('/')[0]
81
96
  });
82
97
  } catch (error) {
83
98
  if (enableLogging) console.error(`\u{274C} '${codemod.name}' failed to parse file`, filepath, error);
84
99
  return err(error);
85
100
  }
86
101
  }));
87
- await Promise.all(rootPaths.map((rootPath)=>(codemod.postTransform ?? (async ()=>{}))(rootPath)));
102
+ const successes = arrays.compactMap(results, (result)=>{
103
+ if (result.isErr()) return null;
104
+ return result.value;
105
+ });
106
+ const successesGroupedByRoot = groupBy(successes, 'root');
107
+ const rootPathsWithResults = rootPaths.map((root)=>({
108
+ root,
109
+ results: successesGroupedByRoot[root] ?? []
110
+ }));
111
+ await Promise.all(rootPathsWithResults.map((r)=>(codemod.postTransform ?? (async ()=>{}))(r)));
88
112
  return results;
89
113
  }
90
114
  function traverseUp(node, until) {
@@ -0,0 +1 @@
1
+ export declare function groupBy<T, K extends keyof T>(array: Array<T>, key: K): Record<string, Array<T>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kamaalio/codemod-kit",
3
- "version": "0.0.27",
3
+ "version": "0.0.28",
4
4
  "type": "module",
5
5
  "author": "Kamaal Farah",
6
6
  "license": "MIT",