@kamaalio/codemod-kit 0.0.7 → 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 -8
- package/dist/index.js +9 -8
- 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
|
@@ -81,8 +81,6 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
81
81
|
}, []));
|
|
82
82
|
const targets = globItems.filter((filepath)=>{
|
|
83
83
|
if (!hooks.targetFiltering(filepath, codemod)) return false;
|
|
84
|
-
const projectName = filepath.split('/')[0];
|
|
85
|
-
if (null == projectName) throw new Error('Invariant found, project name should be present');
|
|
86
84
|
return collectionIsEmpty(extensions) || extensions.has(external_node_path_default().extname(filepath));
|
|
87
85
|
});
|
|
88
86
|
if (0 === targets.length) return [];
|
|
@@ -93,16 +91,19 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
93
91
|
const content = await promises_default().readFile(fullPath, {
|
|
94
92
|
encoding: 'utf-8'
|
|
95
93
|
});
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
94
|
+
const modifiedContent = await codemod.transformer(content, fullPath);
|
|
95
|
+
const hasChanges = modifiedContent !== content;
|
|
96
|
+
if (hasChanges) {
|
|
97
|
+
const transformedContent = await hooks.postTransform(modifiedContent, codemod);
|
|
99
98
|
if (!runInDryMode) await promises_default().writeFile(fullPath, transformedContent);
|
|
100
99
|
if (enableLogging) console.log(`\u{1F680} finished '${codemod.name}'`, {
|
|
101
|
-
filename: filepath
|
|
102
|
-
report: modifications.report
|
|
100
|
+
filename: filepath
|
|
103
101
|
});
|
|
104
102
|
}
|
|
105
|
-
return (0, external_neverthrow_namespaceObject.ok)(
|
|
103
|
+
return (0, external_neverthrow_namespaceObject.ok)({
|
|
104
|
+
hasChanges,
|
|
105
|
+
content: modifiedContent
|
|
106
|
+
});
|
|
106
107
|
} catch (error) {
|
|
107
108
|
if (enableLogging) console.error(`\u{274C} '${codemod.name}' failed to parse file`, filepath, error);
|
|
108
109
|
return (0, external_neverthrow_namespaceObject.err)(error);
|
package/dist/index.js
CHANGED
|
@@ -40,8 +40,6 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
40
40
|
}, []));
|
|
41
41
|
const targets = globItems.filter((filepath)=>{
|
|
42
42
|
if (!hooks.targetFiltering(filepath, codemod)) return false;
|
|
43
|
-
const projectName = filepath.split('/')[0];
|
|
44
|
-
if (null == projectName) throw new Error('Invariant found, project name should be present');
|
|
45
43
|
return collectionIsEmpty(extensions) || extensions.has(node_path.extname(filepath));
|
|
46
44
|
});
|
|
47
45
|
if (0 === targets.length) return [];
|
|
@@ -52,16 +50,19 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
52
50
|
const content = await promises.readFile(fullPath, {
|
|
53
51
|
encoding: 'utf-8'
|
|
54
52
|
});
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
const modifiedContent = await codemod.transformer(content, fullPath);
|
|
54
|
+
const hasChanges = modifiedContent !== content;
|
|
55
|
+
if (hasChanges) {
|
|
56
|
+
const transformedContent = await hooks.postTransform(modifiedContent, codemod);
|
|
58
57
|
if (!runInDryMode) await promises.writeFile(fullPath, transformedContent);
|
|
59
58
|
if (enableLogging) console.log(`\u{1F680} finished '${codemod.name}'`, {
|
|
60
|
-
filename: filepath
|
|
61
|
-
report: modifications.report
|
|
59
|
+
filename: filepath
|
|
62
60
|
});
|
|
63
61
|
}
|
|
64
|
-
return ok(
|
|
62
|
+
return ok({
|
|
63
|
+
hasChanges,
|
|
64
|
+
content: modifiedContent
|
|
65
|
+
});
|
|
65
66
|
} catch (error) {
|
|
66
67
|
if (enableLogging) console.error(`\u{274C} '${codemod.name}' failed to parse file`, filepath, error);
|
|
67
68
|
return err(error);
|