@kamaalio/codemod-kit 0.0.8 → 0.0.10
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/codemods/types.d.ts +1 -1
- package/dist/codemods/utils.d.ts +9 -3
- package/dist/index.cjs +10 -6
- package/dist/index.js +10 -6
- package/package.json +1 -1
package/dist/codemods/types.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export type Codemod = {
|
|
|
6
6
|
name: string;
|
|
7
7
|
languages: Set<NapiLang> | Array<NapiLang>;
|
|
8
8
|
commitMessage: string;
|
|
9
|
-
transformer: (content: SgRoot<TypesMap> | string, filename?: Optional<string>) => Promise<
|
|
9
|
+
transformer: (content: SgRoot<TypesMap> | string, filename?: Optional<string>) => Promise<string>;
|
|
10
10
|
};
|
|
11
11
|
export type ModificationsReport = {
|
|
12
12
|
changesApplied: number;
|
package/dist/codemods/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Result } from 'neverthrow';
|
|
2
|
-
import type { Codemod
|
|
2
|
+
import type { Codemod } from './types.js';
|
|
3
3
|
type RunCodemodHooks<C extends Codemod> = {
|
|
4
4
|
targetFiltering?: (filepath: string, codemod: C) => boolean;
|
|
5
5
|
preCodemodRun?: (codemod: C) => Promise<void>;
|
|
@@ -10,6 +10,12 @@ type RunCodemodOptions<C extends Codemod> = {
|
|
|
10
10
|
log?: boolean;
|
|
11
11
|
dry?: boolean;
|
|
12
12
|
};
|
|
13
|
-
export declare function runCodemods<C extends Codemod>(codemods: Array<C>, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Record<string, Array<Result<
|
|
14
|
-
|
|
13
|
+
export declare function runCodemods<C extends Codemod>(codemods: Array<C>, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Record<string, Array<Result<{
|
|
14
|
+
hasChanges: boolean;
|
|
15
|
+
content: string;
|
|
16
|
+
}, Error>>>>;
|
|
17
|
+
export declare function runCodemod<C extends Codemod>(codemod: C, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Array<Result<{
|
|
18
|
+
hasChanges: boolean;
|
|
19
|
+
content: string;
|
|
20
|
+
}, Error>>>;
|
|
15
21
|
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -69,6 +69,7 @@ async function runCodemods(codemods, transformationPath, options) {
|
|
|
69
69
|
}
|
|
70
70
|
async function runCodemod(codemod, transformationPath, options) {
|
|
71
71
|
const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
|
|
72
|
+
await hooks.preCodemodRun(codemod);
|
|
72
73
|
const globItems = await external_fast_glob_default().glob([
|
|
73
74
|
'**/*'
|
|
74
75
|
], {
|
|
@@ -91,16 +92,19 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
91
92
|
const content = await promises_default().readFile(fullPath, {
|
|
92
93
|
encoding: 'utf-8'
|
|
93
94
|
});
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
const modifiedContent = await codemod.transformer(content, fullPath);
|
|
96
|
+
const hasChanges = modifiedContent !== content;
|
|
97
|
+
if (hasChanges) {
|
|
98
|
+
const transformedContent = await hooks.postTransform(modifiedContent, codemod);
|
|
97
99
|
if (!runInDryMode) await promises_default().writeFile(fullPath, transformedContent);
|
|
98
100
|
if (enableLogging) console.log(`\u{1F680} finished '${codemod.name}'`, {
|
|
99
|
-
filename: filepath
|
|
100
|
-
report: modifications.report
|
|
101
|
+
filename: filepath
|
|
101
102
|
});
|
|
102
103
|
}
|
|
103
|
-
return (0, external_neverthrow_namespaceObject.ok)(
|
|
104
|
+
return (0, external_neverthrow_namespaceObject.ok)({
|
|
105
|
+
hasChanges,
|
|
106
|
+
content: modifiedContent
|
|
107
|
+
});
|
|
104
108
|
} catch (error) {
|
|
105
109
|
if (enableLogging) console.error(`\u{274C} '${codemod.name}' failed to parse file`, filepath, error);
|
|
106
110
|
return (0, external_neverthrow_namespaceObject.err)(error);
|
package/dist/index.js
CHANGED
|
@@ -28,6 +28,7 @@ async function runCodemods(codemods, transformationPath, options) {
|
|
|
28
28
|
}
|
|
29
29
|
async function runCodemod(codemod, transformationPath, options) {
|
|
30
30
|
const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
|
|
31
|
+
await hooks.preCodemodRun(codemod);
|
|
31
32
|
const globItems = await fast_glob.glob([
|
|
32
33
|
'**/*'
|
|
33
34
|
], {
|
|
@@ -50,16 +51,19 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
50
51
|
const content = await promises.readFile(fullPath, {
|
|
51
52
|
encoding: 'utf-8'
|
|
52
53
|
});
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
const modifiedContent = await codemod.transformer(content, fullPath);
|
|
55
|
+
const hasChanges = modifiedContent !== content;
|
|
56
|
+
if (hasChanges) {
|
|
57
|
+
const transformedContent = await hooks.postTransform(modifiedContent, codemod);
|
|
56
58
|
if (!runInDryMode) await promises.writeFile(fullPath, transformedContent);
|
|
57
59
|
if (enableLogging) console.log(`\u{1F680} finished '${codemod.name}'`, {
|
|
58
|
-
filename: filepath
|
|
59
|
-
report: modifications.report
|
|
60
|
+
filename: filepath
|
|
60
61
|
});
|
|
61
62
|
}
|
|
62
|
-
return ok(
|
|
63
|
+
return ok({
|
|
64
|
+
hasChanges,
|
|
65
|
+
content: modifiedContent
|
|
66
|
+
});
|
|
63
67
|
} catch (error) {
|
|
64
68
|
if (enableLogging) console.error(`\u{274C} '${codemod.name}' failed to parse file`, filepath, error);
|
|
65
69
|
return err(error);
|