@kamaalio/codemod-kit 0.0.26 → 0.0.27

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.
@@ -1,11 +1,12 @@
1
1
  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
- import type { Optional } from '../utils/type-utils.js';
4
+ import type { types } from '@kamaalio/kamaal';
5
5
  export type Codemod = {
6
6
  name: string;
7
7
  languages: Set<NapiLang> | Array<NapiLang>;
8
- transformer: (content: string, filename?: Optional<string>) => Promise<string>;
8
+ transformer: (content: string, filename?: types.Optional<string>) => Promise<string>;
9
+ postTransform?: (rootPath: string) => Promise<void>;
9
10
  };
10
11
  export type ModificationsReport = {
11
12
  changesApplied: number;
@@ -14,10 +15,10 @@ export type Modifications = {
14
15
  ast: SgRoot<TypesMap>;
15
16
  report: ModificationsReport;
16
17
  lang: NapiLang;
17
- filename: Optional<string>;
18
+ filename: types.Optional<string>;
18
19
  history: Array<SgRoot<TypesMap>>;
19
20
  };
20
21
  export type FindAndReplaceConfig = {
21
22
  rule: Rule<TypesMap>;
22
- transformer: ((node: SgNode<TypesMap, Kinds<TypesMap>>, rule: Rule<TypesMap>) => Optional<Edit | string> | Array<Edit | string>) | string;
23
+ transformer: ((node: SgNode<TypesMap, Kinds<TypesMap>>, rule: Rule<TypesMap>) => types.Optional<Edit | string> | Array<Edit | string>) | string;
23
24
  };
@@ -13,6 +13,7 @@ type RunCodemodOptions<C extends Codemod> = {
13
13
  hooks?: RunCodemodHooks<C>;
14
14
  log?: boolean;
15
15
  dry?: boolean;
16
+ rootPaths?: Array<string>;
16
17
  };
17
18
  export declare function runCodemods<C extends Codemod>(codemods: Array<C>, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Record<string, Array<Result<{
18
19
  hasChanges: boolean;
package/dist/index.cjs CHANGED
@@ -89,7 +89,7 @@ async function runCodemods(codemods, transformationPath, options) {
89
89
  return results;
90
90
  }
91
91
  async function runCodemod(codemod, transformationPath, options) {
92
- const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
92
+ const { hooks, log: enableLogging, dry: runInDryMode, rootPaths } = defaultedOptions(options);
93
93
  await hooks.preCodemodRun(codemod);
94
94
  const globItems = await external_fast_glob_default().glob([
95
95
  '**/*'
@@ -107,7 +107,7 @@ async function runCodemod(codemod, transformationPath, options) {
107
107
  });
108
108
  if (0 === targets.length) return [];
109
109
  if (enableLogging) console.log(`\u{1F9C9} '${codemod.name}' targeting ${targets.length} ${1 === targets.length ? 'file' : 'files'} to transform, chill and grab some mat\xe9`);
110
- return Promise.all(targets.map(async (filepath)=>{
110
+ const results = await Promise.all(targets.map(async (filepath)=>{
111
111
  const fullPath = external_node_path_default().join(transformationPath, filepath);
112
112
  try {
113
113
  const content = await promises_default().readFile(fullPath, {
@@ -131,6 +131,8 @@ async function runCodemod(codemod, transformationPath, options) {
131
131
  return (0, external_neverthrow_namespaceObject.err)(error);
132
132
  }
133
133
  }));
134
+ await Promise.all(rootPaths.map((rootPath)=>(codemod.postTransform ?? (async ()=>{}))(rootPath)));
135
+ return results;
134
136
  }
135
137
  function traverseUp(node, until) {
136
138
  let current = node.parent();
@@ -262,7 +264,8 @@ function defaultedOptions(options) {
262
264
  return {
263
265
  hooks: defaultedHooks(options?.hooks),
264
266
  log: options?.log ?? true,
265
- dry: options?.dry ?? false
267
+ dry: options?.dry ?? false,
268
+ rootPaths: options?.rootPaths ?? []
266
269
  };
267
270
  }
268
271
  function defaultedHooks(hooks) {
package/dist/index.js CHANGED
@@ -42,7 +42,7 @@ async function runCodemods(codemods, transformationPath, options) {
42
42
  return results;
43
43
  }
44
44
  async function runCodemod(codemod, transformationPath, options) {
45
- const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
45
+ const { hooks, log: enableLogging, dry: runInDryMode, rootPaths } = defaultedOptions(options);
46
46
  await hooks.preCodemodRun(codemod);
47
47
  const globItems = await fast_glob.glob([
48
48
  '**/*'
@@ -60,7 +60,7 @@ async function runCodemod(codemod, transformationPath, options) {
60
60
  });
61
61
  if (0 === targets.length) return [];
62
62
  if (enableLogging) console.log(`\u{1F9C9} '${codemod.name}' targeting ${targets.length} ${1 === targets.length ? 'file' : 'files'} to transform, chill and grab some mat\xe9`);
63
- return Promise.all(targets.map(async (filepath)=>{
63
+ const results = await Promise.all(targets.map(async (filepath)=>{
64
64
  const fullPath = node_path.join(transformationPath, filepath);
65
65
  try {
66
66
  const content = await promises.readFile(fullPath, {
@@ -84,6 +84,8 @@ async function runCodemod(codemod, transformationPath, options) {
84
84
  return err(error);
85
85
  }
86
86
  }));
87
+ await Promise.all(rootPaths.map((rootPath)=>(codemod.postTransform ?? (async ()=>{}))(rootPath)));
88
+ return results;
87
89
  }
88
90
  function traverseUp(node, until) {
89
91
  let current = node.parent();
@@ -215,7 +217,8 @@ function defaultedOptions(options) {
215
217
  return {
216
218
  hooks: defaultedHooks(options?.hooks),
217
219
  log: options?.log ?? true,
218
- dry: options?.dry ?? false
220
+ dry: options?.dry ?? false,
221
+ rootPaths: options?.rootPaths ?? []
219
222
  };
220
223
  }
221
224
  function defaultedHooks(hooks) {
@@ -1,4 +1,3 @@
1
- export type Optional<T> = T | undefined | null;
2
1
  export type ReplaceObjectProperty<A extends Record<string, unknown>, K extends keyof A, B> = Omit<A, K> & {
3
2
  [P in K]: B;
4
3
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kamaalio/codemod-kit",
3
- "version": "0.0.26",
3
+ "version": "0.0.27",
4
4
  "type": "module",
5
5
  "author": "Kamaal Farah",
6
6
  "license": "MIT",