@kamaalio/codemod-kit 0.0.8 → 0.0.9
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 +9 -6
- package/dist/index.js +9 -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
|
@@ -91,16 +91,19 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
91
91
|
const content = await promises_default().readFile(fullPath, {
|
|
92
92
|
encoding: 'utf-8'
|
|
93
93
|
});
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
const modifiedContent = await codemod.transformer(content, fullPath);
|
|
95
|
+
const hasChanges = modifiedContent !== content;
|
|
96
|
+
if (hasChanges) {
|
|
97
|
+
const transformedContent = await hooks.postTransform(modifiedContent, codemod);
|
|
97
98
|
if (!runInDryMode) await promises_default().writeFile(fullPath, transformedContent);
|
|
98
99
|
if (enableLogging) console.log(`\u{1F680} finished '${codemod.name}'`, {
|
|
99
|
-
filename: filepath
|
|
100
|
-
report: modifications.report
|
|
100
|
+
filename: filepath
|
|
101
101
|
});
|
|
102
102
|
}
|
|
103
|
-
return (0, external_neverthrow_namespaceObject.ok)(
|
|
103
|
+
return (0, external_neverthrow_namespaceObject.ok)({
|
|
104
|
+
hasChanges,
|
|
105
|
+
content: modifiedContent
|
|
106
|
+
});
|
|
104
107
|
} catch (error) {
|
|
105
108
|
if (enableLogging) console.error(`\u{274C} '${codemod.name}' failed to parse file`, filepath, error);
|
|
106
109
|
return (0, external_neverthrow_namespaceObject.err)(error);
|
package/dist/index.js
CHANGED
|
@@ -50,16 +50,19 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
50
50
|
const content = await promises.readFile(fullPath, {
|
|
51
51
|
encoding: 'utf-8'
|
|
52
52
|
});
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
const modifiedContent = await codemod.transformer(content, fullPath);
|
|
54
|
+
const hasChanges = modifiedContent !== content;
|
|
55
|
+
if (hasChanges) {
|
|
56
|
+
const transformedContent = await hooks.postTransform(modifiedContent, codemod);
|
|
56
57
|
if (!runInDryMode) await promises.writeFile(fullPath, transformedContent);
|
|
57
58
|
if (enableLogging) console.log(`\u{1F680} finished '${codemod.name}'`, {
|
|
58
|
-
filename: filepath
|
|
59
|
-
report: modifications.report
|
|
59
|
+
filename: filepath
|
|
60
60
|
});
|
|
61
61
|
}
|
|
62
|
-
return ok(
|
|
62
|
+
return ok({
|
|
63
|
+
hasChanges,
|
|
64
|
+
content: modifiedContent
|
|
65
|
+
});
|
|
63
66
|
} catch (error) {
|
|
64
67
|
if (enableLogging) console.error(`\u{274C} '${codemod.name}' failed to parse file`, filepath, error);
|
|
65
68
|
return err(error);
|