@bemedev/codebase 0.1.2 → 0.2.0
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/README.md +8 -8
- package/lib/_virtual/_rolldown/runtime.cjs +23 -0
- package/lib/analyse.cjs +48 -62
- package/lib/analyse.cjs.map +1 -1
- package/lib/analyse.d.ts +21 -3
- package/lib/analyse.js +45 -60
- package/lib/analyse.js.map +1 -1
- package/lib/analyse.utils.cjs +154 -205
- package/lib/analyse.utils.cjs.map +1 -1
- package/lib/analyse.utils.d.ts +1 -2
- package/lib/analyse.utils.js +153 -203
- package/lib/analyse.utils.js.map +1 -1
- package/lib/cli/cli.cjs +28 -27
- package/lib/cli/cli.cjs.map +1 -1
- package/lib/cli/cli.d.ts +0 -1
- package/lib/cli/cli.js +25 -24
- package/lib/cli/cli.js.map +1 -1
- package/lib/cli/constants.cjs +6 -5
- package/lib/cli/constants.cjs.map +1 -1
- package/lib/cli/constants.d.ts +0 -1
- package/lib/cli/constants.js +5 -3
- package/lib/cli/constants.js.map +1 -1
- package/lib/cli/index.cjs +6 -6
- package/lib/cli/index.cjs.map +1 -1
- package/lib/cli/index.d.ts +0 -1
- package/lib/cli/index.js +7 -4
- package/lib/cli/index.js.map +1 -1
- package/lib/constants.cjs +26 -28
- package/lib/constants.cjs.map +1 -1
- package/lib/constants.d.ts +0 -1
- package/lib/constants.js +24 -26
- package/lib/constants.js.map +1 -1
- package/lib/exports.cjs +51 -72
- package/lib/exports.cjs.map +1 -1
- package/lib/exports.d.ts +1 -2
- package/lib/exports.js +49 -70
- package/lib/exports.js.map +1 -1
- package/lib/functions/add.cjs +71 -84
- package/lib/functions/add.cjs.map +1 -1
- package/lib/functions/add.d.ts +1 -2
- package/lib/functions/add.js +68 -82
- package/lib/functions/add.js.map +1 -1
- package/lib/functions/generate.cjs +53 -56
- package/lib/functions/generate.cjs.map +1 -1
- package/lib/functions/generate.d.ts +1 -2
- package/lib/functions/generate.js +51 -54
- package/lib/functions/generate.js.map +1 -1
- package/lib/functions/index.cjs +11 -16
- package/lib/functions/index.d.ts +0 -1
- package/lib/functions/index.js +5 -5
- package/lib/functions/init.cjs +68 -97
- package/lib/functions/init.cjs.map +1 -1
- package/lib/functions/init.d.ts +1 -2
- package/lib/functions/init.js +66 -95
- package/lib/functions/init.js.map +1 -1
- package/lib/functions/remove.cjs +85 -103
- package/lib/functions/remove.cjs.map +1 -1
- package/lib/functions/remove.d.ts +1 -2
- package/lib/functions/remove.js +82 -101
- package/lib/functions/remove.js.map +1 -1
- package/lib/helpers.cjs +32 -38
- package/lib/helpers.cjs.map +1 -1
- package/lib/helpers.d.ts +3 -4
- package/lib/helpers.js +30 -36
- package/lib/helpers.js.map +1 -1
- package/lib/imports.cjs +88 -118
- package/lib/imports.cjs.map +1 -1
- package/lib/imports.d.ts +2 -3
- package/lib/imports.js +86 -116
- package/lib/imports.js.map +1 -1
- package/lib/index.cjs +22 -24
- package/lib/index.d.ts +4 -4
- package/lib/index.js +8 -6
- package/lib/schemas.cjs +53 -63
- package/lib/schemas.cjs.map +1 -1
- package/lib/schemas.d.ts +5 -2
- package/lib/schemas.js +43 -35
- package/lib/schemas.js.map +1 -1
- package/lib/types.d.ts +0 -1
- package/lib/utils.cjs +9 -10
- package/lib/utils.cjs.map +1 -1
- package/lib/utils.d.ts +0 -1
- package/lib/utils.js +8 -8
- package/lib/utils.js.map +1 -1
- package/package.json +56 -44
- package/CHANGE_LOG.md +0 -143
- package/lib/analyse.d.ts.map +0 -1
- package/lib/analyse.utils.d.ts.map +0 -1
- package/lib/cli/cli.d.ts.map +0 -1
- package/lib/cli/constants.d.ts.map +0 -1
- package/lib/cli/index.d.ts.map +0 -1
- package/lib/constants.d.ts.map +0 -1
- package/lib/exports.d.ts.map +0 -1
- package/lib/functions/add.d.ts.map +0 -1
- package/lib/functions/generate.d.ts.map +0 -1
- package/lib/functions/index.cjs.map +0 -1
- package/lib/functions/index.d.ts.map +0 -1
- package/lib/functions/index.js.map +0 -1
- package/lib/functions/init.d.ts.map +0 -1
- package/lib/functions/remove.d.ts.map +0 -1
- package/lib/helpers.d.ts.map +0 -1
- package/lib/imports.d.ts.map +0 -1
- package/lib/index.cjs.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/schemas.d.ts.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/utils.d.ts.map +0 -1
package/lib/helpers.cjs
CHANGED
|
@@ -1,52 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const transformModule = ({ cwd = process.cwd(), relativePath, moduleSpecifier
|
|
8
|
-
|
|
9
|
-
return out;
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
require("./_virtual/_rolldown/runtime.cjs");
|
|
3
|
+
const require_constants = require("./constants.cjs");
|
|
4
|
+
let path = require("path");
|
|
5
|
+
let fs = require("fs");
|
|
6
|
+
//#region src/helpers.ts
|
|
7
|
+
const transformModule = ({ cwd = process.cwd(), relativePath, moduleSpecifier }) => {
|
|
8
|
+
return (0, path.relative)(cwd, (0, path.resolve)((0, path.dirname)(relativePath), moduleSpecifier)).replaceAll("/", ".");
|
|
10
9
|
};
|
|
11
10
|
const writeFileAnalysis = (fileAnalysis, folderPath) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
return console.error(` ❌ Erreur pour ${relativePath}:`, error);
|
|
30
|
-
}
|
|
11
|
+
const relativePath = fileAnalysis.relativePath;
|
|
12
|
+
const destPath = (0, path.join)(folderPath, relativePath);
|
|
13
|
+
const destDir = (0, path.dirname)(destPath);
|
|
14
|
+
try {
|
|
15
|
+
(0, fs.mkdirSync)(destDir, { recursive: true });
|
|
16
|
+
let fileContent = fileAnalysis.text;
|
|
17
|
+
require_constants.REPLACERS.init.forEach(([search, replace]) => {
|
|
18
|
+
fileContent = fileContent.replaceAll(search, replace);
|
|
19
|
+
});
|
|
20
|
+
(0, fs.writeFileSync)(destPath, fileContent, "utf8");
|
|
21
|
+
console.log(` ✅ ${relativePath}`);
|
|
22
|
+
return relativePath.slice(0, -3).replaceAll("/", ".");
|
|
23
|
+
} catch (error) {
|
|
24
|
+
return console.error(` ❌ Erreur pour ${relativePath}:`, error);
|
|
25
|
+
}
|
|
31
26
|
};
|
|
32
27
|
const consoleStars = () => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
console.log();
|
|
29
|
+
console.log("*".repeat(30));
|
|
30
|
+
console.log();
|
|
36
31
|
};
|
|
37
32
|
const toArray = (value) => {
|
|
38
|
-
|
|
33
|
+
return Array.isArray(value) ? value : !value ? [] : [value];
|
|
39
34
|
};
|
|
40
35
|
const getFolderPath = (root) => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const folderPath = srcExists ? path.join(cwd, 'src', root) : path.join(cwd, root);
|
|
44
|
-
return folderPath;
|
|
36
|
+
const cwd = process.cwd();
|
|
37
|
+
return (0, fs.existsSync)((0, path.join)(cwd, "src")) ? (0, path.join)(cwd, "src", root) : (0, path.join)(cwd, root);
|
|
45
38
|
};
|
|
46
|
-
|
|
39
|
+
//#endregion
|
|
47
40
|
exports.consoleStars = consoleStars;
|
|
48
41
|
exports.getFolderPath = getFolderPath;
|
|
49
42
|
exports.toArray = toArray;
|
|
50
43
|
exports.transformModule = transformModule;
|
|
51
44
|
exports.writeFileAnalysis = writeFileAnalysis;
|
|
52
|
-
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=helpers.cjs.map
|
package/lib/helpers.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.cjs","sources":["../src/helpers.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from
|
|
1
|
+
{"version":3,"file":"helpers.cjs","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from \"node:fs\";\nimport { dirname, join, relative, resolve } from \"node:path\";\nimport { REPLACERS } from \"./constants\";\nimport { FileAnalysis } from \"./schemas\";\n\nexport type TransformModuleArgs = {\n cwd?: string;\n relativePath: string;\n moduleSpecifier: string;\n};\n\nexport const transformModule = ({\n cwd = process.cwd(),\n relativePath,\n moduleSpecifier,\n}: TransformModuleArgs) => {\n const out = relative(\n cwd,\n resolve(dirname(relativePath), moduleSpecifier),\n ).replaceAll(\"/\", \".\");\n\n return out;\n};\n\nexport const writeFileAnalysis = (\n fileAnalysis: FileAnalysis,\n folderPath: string,\n) => {\n const relativePath = fileAnalysis.relativePath;\n\n // Créer le chemin de destination dans .bemedev en maintenant la structure\n const destPath = join(folderPath, relativePath);\n const destDir = dirname(destPath);\n\n try {\n // Créer le dossier de destination si nécessaire\n mkdirSync(destDir, { recursive: true });\n\n let fileContent = fileAnalysis.text;\n REPLACERS.init.forEach(([search, replace]) => {\n fileContent = fileContent.replaceAll(search, replace);\n });\n\n // Écrire le contenu du fichier types\n writeFileSync(destPath, fileContent, \"utf8\");\n\n console.log(` ✅ ${relativePath}`);\n return relativePath.slice(0, -3).replaceAll(\"/\", \".\");\n } catch (error) {\n return console.error(` ❌ Erreur pour ${relativePath}:`, error);\n }\n};\n\nexport const consoleStars = () => {\n console.log();\n console.log(\"*\".repeat(30));\n console.log();\n};\n\nexport const toArray = <T>(value?: T | T[]): T[] => {\n return Array.isArray(value) ? value : !value ? [] : [value];\n};\n\nexport const getFolderPath = (root: string) => {\n const cwd = process.cwd();\n const srcExists = existsSync(join(cwd, \"src\"));\n const folderPath = srcExists ? join(cwd, \"src\", root) : join(cwd, root);\n\n return folderPath;\n};\n"],"mappings":";;;;;;A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alysis } from
|
|
1
|
+
import { FileAnalysis } from "./schemas";
|
|
2
2
|
export type TransformModuleArgs = {
|
|
3
3
|
cwd?: string;
|
|
4
4
|
relativePath: string;
|
|
5
5
|
moduleSpecifier: string;
|
|
6
6
|
};
|
|
7
|
-
export declare const transformModule: ({ cwd, relativePath, moduleSpecifier, }: TransformModuleArgs) =>
|
|
8
|
-
export declare const writeFileAnalysis: (fileAnalysis: FileAnalysis, folderPath: string) =>
|
|
7
|
+
export declare const transformModule: ({ cwd, relativePath, moduleSpecifier, }: TransformModuleArgs) => any;
|
|
8
|
+
export declare const writeFileAnalysis: (fileAnalysis: FileAnalysis, folderPath: string) => any;
|
|
9
9
|
export declare const consoleStars: () => void;
|
|
10
10
|
export declare const toArray: <T>(value?: T | T[]) => T[];
|
|
11
11
|
export declare const getFolderPath: (root: string) => string;
|
|
12
|
-
//# sourceMappingURL=helpers.d.ts.map
|
package/lib/helpers.js
CHANGED
|
@@ -1,46 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const transformModule = ({ cwd = process.cwd(), relativePath, moduleSpecifier
|
|
6
|
-
|
|
7
|
-
return out;
|
|
1
|
+
import { REPLACERS } from "./constants.js";
|
|
2
|
+
import { dirname, join, relative, resolve } from "path";
|
|
3
|
+
import { existsSync, mkdirSync, writeFileSync } from "fs";
|
|
4
|
+
//#region src/helpers.ts
|
|
5
|
+
const transformModule = ({ cwd = process.cwd(), relativePath, moduleSpecifier }) => {
|
|
6
|
+
return relative(cwd, resolve(dirname(relativePath), moduleSpecifier)).replaceAll("/", ".");
|
|
8
7
|
};
|
|
9
8
|
const writeFileAnalysis = (fileAnalysis, folderPath) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
return console.error(` ❌ Erreur pour ${relativePath}:`, error);
|
|
28
|
-
}
|
|
9
|
+
const relativePath = fileAnalysis.relativePath;
|
|
10
|
+
const destPath = join(folderPath, relativePath);
|
|
11
|
+
const destDir = dirname(destPath);
|
|
12
|
+
try {
|
|
13
|
+
mkdirSync(destDir, { recursive: true });
|
|
14
|
+
let fileContent = fileAnalysis.text;
|
|
15
|
+
REPLACERS.init.forEach(([search, replace]) => {
|
|
16
|
+
fileContent = fileContent.replaceAll(search, replace);
|
|
17
|
+
});
|
|
18
|
+
writeFileSync(destPath, fileContent, "utf8");
|
|
19
|
+
console.log(` ✅ ${relativePath}`);
|
|
20
|
+
return relativePath.slice(0, -3).replaceAll("/", ".");
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return console.error(` ❌ Erreur pour ${relativePath}:`, error);
|
|
23
|
+
}
|
|
29
24
|
};
|
|
30
25
|
const consoleStars = () => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
console.log();
|
|
27
|
+
console.log("*".repeat(30));
|
|
28
|
+
console.log();
|
|
34
29
|
};
|
|
35
30
|
const toArray = (value) => {
|
|
36
|
-
|
|
31
|
+
return Array.isArray(value) ? value : !value ? [] : [value];
|
|
37
32
|
};
|
|
38
33
|
const getFolderPath = (root) => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const folderPath = srcExists ? join(cwd, 'src', root) : join(cwd, root);
|
|
42
|
-
return folderPath;
|
|
34
|
+
const cwd = process.cwd();
|
|
35
|
+
return existsSync(join(cwd, "src")) ? join(cwd, "src", root) : join(cwd, root);
|
|
43
36
|
};
|
|
44
|
-
|
|
37
|
+
//#endregion
|
|
45
38
|
export { consoleStars, getFolderPath, toArray, transformModule, writeFileAnalysis };
|
|
46
|
-
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=helpers.js.map
|
package/lib/helpers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sources":["../src/helpers.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from
|
|
1
|
+
{"version":3,"file":"helpers.js","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from \"node:fs\";\nimport { dirname, join, relative, resolve } from \"node:path\";\nimport { REPLACERS } from \"./constants\";\nimport { FileAnalysis } from \"./schemas\";\n\nexport type TransformModuleArgs = {\n cwd?: string;\n relativePath: string;\n moduleSpecifier: string;\n};\n\nexport const transformModule = ({\n cwd = process.cwd(),\n relativePath,\n moduleSpecifier,\n}: TransformModuleArgs) => {\n const out = relative(\n cwd,\n resolve(dirname(relativePath), moduleSpecifier),\n ).replaceAll(\"/\", \".\");\n\n return out;\n};\n\nexport const writeFileAnalysis = (\n fileAnalysis: FileAnalysis,\n folderPath: string,\n) => {\n const relativePath = fileAnalysis.relativePath;\n\n // Créer le chemin de destination dans .bemedev en maintenant la structure\n const destPath = join(folderPath, relativePath);\n const destDir = dirname(destPath);\n\n try {\n // Créer le dossier de destination si nécessaire\n mkdirSync(destDir, { recursive: true });\n\n let fileContent = fileAnalysis.text;\n REPLACERS.init.forEach(([search, replace]) => {\n fileContent = fileContent.replaceAll(search, replace);\n });\n\n // Écrire le contenu du fichier types\n writeFileSync(destPath, fileContent, \"utf8\");\n\n console.log(` ✅ ${relativePath}`);\n return relativePath.slice(0, -3).replaceAll(\"/\", \".\");\n } catch (error) {\n return console.error(` ❌ Erreur pour ${relativePath}:`, error);\n }\n};\n\nexport const consoleStars = () => {\n console.log();\n console.log(\"*\".repeat(30));\n console.log();\n};\n\nexport const toArray = <T>(value?: T | T[]): T[] => {\n return Array.isArray(value) ? value : !value ? [] : [value];\n};\n\nexport const getFolderPath = (root: string) => {\n const cwd = process.cwd();\n const srcExists = existsSync(join(cwd, \"src\"));\n const folderPath = srcExists ? join(cwd, \"src\", root) : join(cwd, root);\n\n return folderPath;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3
|
+
let path = require("path");
|
|
4
|
+
let ts_morph = require("ts-morph");
|
|
5
|
+
//#region src/imports.ts
|
|
6
6
|
/**
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
* Résout le moduleSpecifier en utilisant les paths du tsconfig si il commence par "#"
|
|
8
|
+
*/
|
|
9
9
|
const resolveModuleSpecifier = (sourceFile, moduleSpecifier) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
relativedPath = relativedPath.replace('*', match[1]);
|
|
28
|
-
}
|
|
29
|
-
// Calculer le chemin relatif depuis le fichier source actuel
|
|
30
|
-
const sourceFileDir = path.relative(process.cwd(), sourceFile.getDirectoryPath());
|
|
31
|
-
const relativePath = path.relative(sourceFileDir, relativedPath);
|
|
32
|
-
// S'assurer que le chemin relatif commence par ./ ou ../
|
|
33
|
-
return relativePath.startsWith('.')
|
|
34
|
-
? relativePath
|
|
35
|
-
: `./${relativePath}`;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return moduleSpecifier;
|
|
10
|
+
const paths = sourceFile.getProject().getCompilerOptions().paths;
|
|
11
|
+
if (!paths) return moduleSpecifier;
|
|
12
|
+
const baseUrl = sourceFile.getProject().getCompilerOptions().baseUrl;
|
|
13
|
+
const paths2 = Object.entries(paths);
|
|
14
|
+
for (const [pattern, mappings] of paths2) {
|
|
15
|
+
const regexPattern = pattern.replace(/\*/g, "(.*)");
|
|
16
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
17
|
+
const match = moduleSpecifier.match(regex);
|
|
18
|
+
if (match) {
|
|
19
|
+
const first = mappings[0];
|
|
20
|
+
let relativedPath = baseUrl ? (0, path.join)(baseUrl, first) : first;
|
|
21
|
+
if (match[1]) relativedPath = relativedPath.replace("*", match[1]);
|
|
22
|
+
const relativePath = (0, path.relative)((0, path.relative)(process.cwd(), sourceFile.getDirectoryPath()), relativedPath);
|
|
23
|
+
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return moduleSpecifier;
|
|
39
27
|
};
|
|
40
28
|
/**
|
|
41
|
-
|
|
42
|
-
|
|
29
|
+
* Analyse les imports d'un fichier
|
|
30
|
+
*/
|
|
43
31
|
const analyzeImports = (sourceFile) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const arg = callExpr.getArguments()[0];
|
|
90
|
-
if (arg && arg.getKind() === tsMorph.SyntaxKind.StringLiteral) {
|
|
91
|
-
const rawModuleSpecifier = arg.getText().replace(/['"]/g, '');
|
|
92
|
-
const moduleSpecifier = resolveModuleSpecifier(sourceFile, rawModuleSpecifier);
|
|
93
|
-
imports.push({
|
|
94
|
-
moduleSpecifier,
|
|
95
|
-
kind: 'side-effect',
|
|
96
|
-
isDynamic: true,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
return imports;
|
|
32
|
+
const imports = [];
|
|
33
|
+
sourceFile.getImportDeclarations().forEach((importDecl) => {
|
|
34
|
+
const isTypeOnly = importDecl.isTypeOnly();
|
|
35
|
+
const moduleSpecifier = resolveModuleSpecifier(sourceFile, importDecl.getModuleSpecifierValue());
|
|
36
|
+
const defaultImport = importDecl.getDefaultImport();
|
|
37
|
+
if (defaultImport) imports.push({
|
|
38
|
+
moduleSpecifier,
|
|
39
|
+
kind: "default",
|
|
40
|
+
default: defaultImport.getText(),
|
|
41
|
+
isTypeOnly
|
|
42
|
+
});
|
|
43
|
+
const namespaceImport = importDecl.getNamespaceImport();
|
|
44
|
+
if (namespaceImport) imports.push({
|
|
45
|
+
moduleSpecifier,
|
|
46
|
+
kind: "namespace",
|
|
47
|
+
default: namespaceImport.getText(),
|
|
48
|
+
isTypeOnly
|
|
49
|
+
});
|
|
50
|
+
const namedImports = importDecl.getNamedImports();
|
|
51
|
+
if (namedImports.length > 0) imports.push({
|
|
52
|
+
moduleSpecifier,
|
|
53
|
+
kind: "named",
|
|
54
|
+
namedImports: namedImports.map((ni) => ni.getName()),
|
|
55
|
+
isTypeOnly
|
|
56
|
+
});
|
|
57
|
+
if (!defaultImport && !namespaceImport && namedImports.length === 0) imports.push({
|
|
58
|
+
moduleSpecifier,
|
|
59
|
+
kind: "side-effect",
|
|
60
|
+
isTypeOnly
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.CallExpression).forEach((callExpr) => {
|
|
64
|
+
if (callExpr.getExpression().getKind() === ts_morph.SyntaxKind.ImportKeyword) {
|
|
65
|
+
const arg = callExpr.getArguments()[0];
|
|
66
|
+
if (arg && arg.getKind() === ts_morph.SyntaxKind.StringLiteral) {
|
|
67
|
+
const moduleSpecifier = resolveModuleSpecifier(sourceFile, arg.getText().replace(/['"]/g, ""));
|
|
68
|
+
imports.push({
|
|
69
|
+
moduleSpecifier,
|
|
70
|
+
kind: "side-effect",
|
|
71
|
+
isDynamic: true
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return imports;
|
|
102
77
|
};
|
|
103
78
|
const buildImportStrings = (imports) => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
case 'default':
|
|
119
|
-
return `import ${imp.default} from '${imp.moduleSpecifier}';`;
|
|
120
|
-
default:
|
|
121
|
-
return '';
|
|
122
|
-
}
|
|
123
|
-
});
|
|
79
|
+
return imports.map((imp) => {
|
|
80
|
+
switch (imp.kind) {
|
|
81
|
+
case "named": {
|
|
82
|
+
const namedImports = imp.namedImports?.join(", ") || "";
|
|
83
|
+
return `import ${imp.isTypeOnly ? "type " : ""}{ ${namedImports} } from '${imp.moduleSpecifier}';`;
|
|
84
|
+
}
|
|
85
|
+
case "namespace": return `import ${imp.isTypeOnly ? "type " : ""}* as ${imp.default} from '${imp.moduleSpecifier}';`;
|
|
86
|
+
case "side-effect":
|
|
87
|
+
if (imp.isDynamic) return `// Dynamic import: import('${imp.moduleSpecifier}')`;
|
|
88
|
+
return `import '${imp.moduleSpecifier}';`;
|
|
89
|
+
case "default": return `import ${imp.isTypeOnly ? "type " : ""}${imp.default} from '${imp.moduleSpecifier}';`;
|
|
90
|
+
default: return "";
|
|
91
|
+
}
|
|
92
|
+
});
|
|
124
93
|
};
|
|
125
|
-
|
|
94
|
+
//#endregion
|
|
126
95
|
exports.analyzeImports = analyzeImports;
|
|
127
96
|
exports.buildImportStrings = buildImportStrings;
|
|
128
|
-
|
|
97
|
+
|
|
98
|
+
//# sourceMappingURL=imports.cjs.map
|
package/lib/imports.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"imports.cjs","sources":["../src/imports.ts"],"sourcesContent":["import { join, relative } from
|
|
1
|
+
{"version":3,"file":"imports.cjs","names":["SyntaxKind"],"sources":["../src/imports.ts"],"sourcesContent":["import { join, relative } from \"node:path\";\nimport { SourceFile, SyntaxKind } from \"ts-morph\";\nimport type { ImportInfo } from \"./schemas\";\n\n/**\n * Résout le moduleSpecifier en utilisant les paths du tsconfig si il commence par \"#\"\n */\nconst resolveModuleSpecifier = (\n sourceFile: SourceFile,\n moduleSpecifier: string,\n): string => {\n const paths = sourceFile.getProject().getCompilerOptions().paths;\n\n if (!paths) return moduleSpecifier;\n\n const baseUrl = sourceFile.getProject().getCompilerOptions().baseUrl;\n const paths2 = Object.entries(paths);\n\n // Chercher la correspondance dans les paths\n for (const [pattern, mappings] of paths2) {\n // Remplacer * par une regex pour matcher\n const regexPattern = pattern.replace(/\\*/g, \"(.*)\");\n const regex = new RegExp(`^${regexPattern}$`);\n const match = moduleSpecifier.match(regex);\n\n if (match) {\n // Prendre le premier mapping disponible\n const first = mappings[0];\n\n // Résoudre le chemin absolu\n let relativedPath = baseUrl ? join(baseUrl, first) : first;\n\n if (match[1]) {\n relativedPath = relativedPath.replace(\"*\", match[1]);\n }\n\n // Calculer le chemin relatif depuis le fichier source actuel\n const sourceFileDir = relative(\n process.cwd(),\n sourceFile.getDirectoryPath(),\n );\n const relativePath = relative(sourceFileDir, relativedPath);\n\n // S'assurer que le chemin relatif commence par ./ ou ../\n return relativePath.startsWith(\".\") ? relativePath : `./${relativePath}`;\n }\n }\n\n return moduleSpecifier;\n};\n\n/**\n * Analyse les imports d'un fichier\n */\nexport const analyzeImports = (sourceFile: SourceFile): ImportInfo[] => {\n const imports: ImportInfo[] = [];\n\n // Import declarations (import ... from '...')\n sourceFile.getImportDeclarations().forEach((importDecl) => {\n // Determine if this is a type-only import\n const isTypeOnly = importDecl.isTypeOnly();\n\n const rawModuleSpecifier = importDecl.getModuleSpecifierValue();\n const moduleSpecifier = resolveModuleSpecifier(\n sourceFile,\n rawModuleSpecifier,\n );\n\n // Import default\n const defaultImport = importDecl.getDefaultImport();\n if (defaultImport) {\n imports.push({\n moduleSpecifier,\n kind: \"default\",\n default: defaultImport.getText(),\n isTypeOnly,\n });\n }\n\n // Import namespace (* as name)\n const namespaceImport = importDecl.getNamespaceImport();\n if (namespaceImport) {\n imports.push({\n moduleSpecifier,\n kind: \"namespace\",\n default: namespaceImport.getText(),\n isTypeOnly,\n });\n }\n\n // Named imports ({ name1, name2 })\n const namedImports = importDecl.getNamedImports();\n if (namedImports.length > 0) {\n imports.push({\n moduleSpecifier,\n kind: \"named\",\n namedImports: namedImports.map((ni) => ni.getName()),\n isTypeOnly,\n });\n }\n\n // Side-effect import (import '...')\n if (!defaultImport && !namespaceImport && namedImports.length === 0) {\n imports.push({\n moduleSpecifier,\n kind: \"side-effect\",\n isTypeOnly,\n });\n }\n });\n\n // Dynamic imports (import('...'))\n sourceFile\n .getDescendantsOfKind(SyntaxKind.CallExpression)\n .forEach((callExpr) => {\n if (callExpr.getExpression().getKind() === SyntaxKind.ImportKeyword) {\n const arg = callExpr.getArguments()[0];\n if (arg && arg.getKind() === SyntaxKind.StringLiteral) {\n const rawModuleSpecifier = arg.getText().replace(/['\"]/g, \"\");\n const moduleSpecifier = resolveModuleSpecifier(\n sourceFile,\n rawModuleSpecifier,\n );\n imports.push({\n moduleSpecifier,\n kind: \"side-effect\",\n isDynamic: true,\n });\n }\n }\n });\n\n return imports;\n};\n\nexport const buildImportStrings = (imports: ImportInfo[]) => {\n return imports.map((imp) => {\n switch (imp.kind) {\n case \"named\": {\n const namedImports = imp.namedImports?.join(\", \") || \"\";\n return `import ${imp.isTypeOnly ? \"type \" : \"\"}{ ${namedImports} } from '${imp.moduleSpecifier}';`;\n }\n case \"namespace\":\n return `import ${imp.isTypeOnly ? \"type \" : \"\"}* as ${imp.default} from '${imp.moduleSpecifier}';`;\n case \"side-effect\": {\n if (imp.isDynamic) {\n return `// Dynamic import: import('${imp.moduleSpecifier}')`;\n }\n return `import '${imp.moduleSpecifier}';`;\n }\n\n case \"default\":\n return `import ${imp.isTypeOnly ? \"type \" : \"\"}${imp.default} from '${imp.moduleSpecifier}';`;\n default:\n return \"\";\n }\n });\n};\n"],"mappings":";;;;;;;;AAOA,MAAM,0BACJ,YACA,oBACW;CACX,MAAM,QAAQ,WAAW,YAAY,CAAC,oBAAoB,CAAC;AAE3D,KAAI,CAAC,MAAO,QAAO;CAEnB,MAAM,UAAU,WAAW,YAAY,CAAC,oBAAoB,CAAC;CAC7D,MAAM,SAAS,OAAO,QAAQ,MAAM;AAGpC,MAAK,MAAM,CAAC,SAAS,aAAa,QAAQ;EAExC,MAAM,eAAe,QAAQ,QAAQ,OAAO,OAAO;EACnD,MAAM,QAAQ,IAAI,OAAO,IAAI,aAAa,GAAG;EAC7C,MAAM,QAAQ,gBAAgB,MAAM,MAAM;AAE1C,MAAI,OAAO;GAET,MAAM,QAAQ,SAAS;GAGvB,IAAI,gBAAgB,WAAA,GAAA,KAAA,MAAe,SAAS,MAAM,GAAG;AAErD,OAAI,MAAM,GACR,iBAAgB,cAAc,QAAQ,KAAK,MAAM,GAAG;GAQtD,MAAM,gBAAA,GAAA,KAAA,WAAA,GAAA,KAAA,UAHJ,QAAQ,KAAK,EACb,WAAW,kBAAkB,CAC9B,EAC4C,cAAc;AAG3D,UAAO,aAAa,WAAW,IAAI,GAAG,eAAe,KAAK;;;AAI9D,QAAO;;;;;AAMT,MAAa,kBAAkB,eAAyC;CACtE,MAAM,UAAwB,EAAE;AAGhC,YAAW,uBAAuB,CAAC,SAAS,eAAe;EAEzD,MAAM,aAAa,WAAW,YAAY;EAG1C,MAAM,kBAAkB,uBACtB,YAFyB,WAAW,yBAAyB,CAI9D;EAGD,MAAM,gBAAgB,WAAW,kBAAkB;AACnD,MAAI,cACF,SAAQ,KAAK;GACX;GACA,MAAM;GACN,SAAS,cAAc,SAAS;GAChC;GACD,CAAC;EAIJ,MAAM,kBAAkB,WAAW,oBAAoB;AACvD,MAAI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F,QACE,QAAO;;GAEX"}
|
package/lib/imports.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { SourceFile } from
|
|
2
|
-
import type { ImportInfo } from
|
|
1
|
+
import { SourceFile } from "ts-morph";
|
|
2
|
+
import type { ImportInfo } from "./schemas";
|
|
3
3
|
/**
|
|
4
4
|
* Analyse les imports d'un fichier
|
|
5
5
|
*/
|
|
6
6
|
export declare const analyzeImports: (sourceFile: SourceFile) => ImportInfo[];
|
|
7
7
|
export declare const buildImportStrings: (imports: ImportInfo[]) => string[];
|
|
8
|
-
//# sourceMappingURL=imports.d.ts.map
|