@kamaalio/codemod-kit 0.0.4 → 0.0.6
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/utils.d.ts +3 -3
- package/dist/index.cjs +8 -8
- package/dist/index.js +8 -8
- package/package.json +1 -1
package/dist/codemods/utils.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { type Result } from 'neverthrow';
|
|
2
2
|
import type { Codemod, Modifications } from './types.js';
|
|
3
3
|
type RunCodemodHooks = {
|
|
4
|
-
targetFiltering?: (filepath: string) => boolean;
|
|
4
|
+
targetFiltering?: (filepath: string, codemod: Codemod) => boolean;
|
|
5
5
|
preCodemodRun?: (codemod: Codemod) => Promise<void>;
|
|
6
|
-
postTransform?: (transformedContent: string) => Promise<string>;
|
|
6
|
+
postTransform?: (transformedContent: string, codemod: Codemod) => Promise<string>;
|
|
7
7
|
};
|
|
8
8
|
type RunCodemodOptions = {
|
|
9
9
|
hooks?: RunCodemodHooks;
|
|
@@ -11,5 +11,5 @@ type RunCodemodOptions = {
|
|
|
11
11
|
dry?: boolean;
|
|
12
12
|
};
|
|
13
13
|
export declare function runCodemods(codemods: Array<Codemod>, transformationPath: string, options?: RunCodemodOptions): Promise<Record<string, Array<Result<Modifications, Error>>>>;
|
|
14
|
-
export declare function runCodemod(codemod: Codemod, transformationPath: string,
|
|
14
|
+
export declare function runCodemod(codemod: Codemod, transformationPath: string, options?: RunCodemodOptions): Promise<Array<Result<Modifications, Error>>>;
|
|
15
15
|
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -63,24 +63,24 @@ function collectionIsEmpty(collection) {
|
|
|
63
63
|
return 0 === getCollectionCount(collection);
|
|
64
64
|
}
|
|
65
65
|
async function runCodemods(codemods, transformationPath, options) {
|
|
66
|
+
const results = {};
|
|
67
|
+
for (const codemod of codemods)results[codemod.name] = await runCodemod(codemod, transformationPath, options);
|
|
68
|
+
return results;
|
|
69
|
+
}
|
|
70
|
+
async function runCodemod(codemod, transformationPath, options) {
|
|
71
|
+
const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
|
|
66
72
|
const globItems = await external_fast_glob_default().glob([
|
|
67
73
|
'**/*'
|
|
68
74
|
], {
|
|
69
75
|
cwd: transformationPath
|
|
70
76
|
});
|
|
71
|
-
const results = {};
|
|
72
|
-
for (const codemod of codemods)results[codemod.name] = await runCodemod(codemod, transformationPath, globItems, options);
|
|
73
|
-
return results;
|
|
74
|
-
}
|
|
75
|
-
async function runCodemod(codemod, transformationPath, globItems, options) {
|
|
76
|
-
const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
|
|
77
77
|
const extensions = new Set(Array.from(codemod.languages).reduce((acc, language)=>{
|
|
78
78
|
const mappedExtensions = LANG_TO_EXTENSIONS_MAPPING[language.toLowerCase()];
|
|
79
79
|
if (null == mappedExtensions) return acc;
|
|
80
80
|
return acc.concat(Array.from(mappedExtensions));
|
|
81
81
|
}, []));
|
|
82
82
|
const targets = globItems.filter((filepath)=>{
|
|
83
|
-
if (!hooks.targetFiltering(filepath)) return false;
|
|
83
|
+
if (!hooks.targetFiltering(filepath, codemod)) return false;
|
|
84
84
|
const projectName = filepath.split('/')[0];
|
|
85
85
|
if (null == projectName) throw new Error('Invariant found, project name should be present');
|
|
86
86
|
return collectionIsEmpty(extensions) || extensions.has(external_node_path_default().extname(filepath));
|
|
@@ -95,7 +95,7 @@ async function runCodemod(codemod, transformationPath, globItems, options) {
|
|
|
95
95
|
});
|
|
96
96
|
const modifications = await codemod.transformer(content, fullPath);
|
|
97
97
|
if (modifications.report.changesApplied > 0) {
|
|
98
|
-
const transformedContent = await hooks.postTransform(modifications.ast.root().text());
|
|
98
|
+
const transformedContent = await hooks.postTransform(modifications.ast.root().text(), codemod);
|
|
99
99
|
if (!runInDryMode) await promises_default().writeFile(fullPath, transformedContent);
|
|
100
100
|
if (enableLogging) console.log(`\u{1F680} finished '${codemod.name}'`, {
|
|
101
101
|
filename: filepath,
|
package/dist/index.js
CHANGED
|
@@ -22,24 +22,24 @@ function collectionIsEmpty(collection) {
|
|
|
22
22
|
return 0 === getCollectionCount(collection);
|
|
23
23
|
}
|
|
24
24
|
async function runCodemods(codemods, transformationPath, options) {
|
|
25
|
+
const results = {};
|
|
26
|
+
for (const codemod of codemods)results[codemod.name] = await runCodemod(codemod, transformationPath, options);
|
|
27
|
+
return results;
|
|
28
|
+
}
|
|
29
|
+
async function runCodemod(codemod, transformationPath, options) {
|
|
30
|
+
const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
|
|
25
31
|
const globItems = await fast_glob.glob([
|
|
26
32
|
'**/*'
|
|
27
33
|
], {
|
|
28
34
|
cwd: transformationPath
|
|
29
35
|
});
|
|
30
|
-
const results = {};
|
|
31
|
-
for (const codemod of codemods)results[codemod.name] = await runCodemod(codemod, transformationPath, globItems, options);
|
|
32
|
-
return results;
|
|
33
|
-
}
|
|
34
|
-
async function runCodemod(codemod, transformationPath, globItems, options) {
|
|
35
|
-
const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
|
|
36
36
|
const extensions = new Set(Array.from(codemod.languages).reduce((acc, language)=>{
|
|
37
37
|
const mappedExtensions = LANG_TO_EXTENSIONS_MAPPING[language.toLowerCase()];
|
|
38
38
|
if (null == mappedExtensions) return acc;
|
|
39
39
|
return acc.concat(Array.from(mappedExtensions));
|
|
40
40
|
}, []));
|
|
41
41
|
const targets = globItems.filter((filepath)=>{
|
|
42
|
-
if (!hooks.targetFiltering(filepath)) return false;
|
|
42
|
+
if (!hooks.targetFiltering(filepath, codemod)) return false;
|
|
43
43
|
const projectName = filepath.split('/')[0];
|
|
44
44
|
if (null == projectName) throw new Error('Invariant found, project name should be present');
|
|
45
45
|
return collectionIsEmpty(extensions) || extensions.has(node_path.extname(filepath));
|
|
@@ -54,7 +54,7 @@ async function runCodemod(codemod, transformationPath, globItems, options) {
|
|
|
54
54
|
});
|
|
55
55
|
const modifications = await codemod.transformer(content, fullPath);
|
|
56
56
|
if (modifications.report.changesApplied > 0) {
|
|
57
|
-
const transformedContent = await hooks.postTransform(modifications.ast.root().text());
|
|
57
|
+
const transformedContent = await hooks.postTransform(modifications.ast.root().text(), codemod);
|
|
58
58
|
if (!runInDryMode) await promises.writeFile(fullPath, transformedContent);
|
|
59
59
|
if (enableLogging) console.log(`\u{1F680} finished '${codemod.name}'`, {
|
|
60
60
|
filename: filepath,
|