@bemedev/codebase 1.2.2 โ 1.4.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 +20 -2
- package/lib/functions/index.cjs +2 -1
- package/lib/functions/index.d.ts +1 -0
- package/lib/functions/index.d.ts.map +1 -1
- package/lib/functions/index.js +3 -2
- package/lib/functions/init.cjs +2 -16
- package/lib/functions/init.cjs.map +1 -1
- package/lib/functions/init.d.ts +0 -1
- package/lib/functions/init.d.ts.map +1 -1
- package/lib/functions/init.js +3 -16
- package/lib/functions/init.js.map +1 -1
- package/lib/functions/lift.cjs +125 -0
- package/lib/functions/lift.cjs.map +1 -0
- package/lib/functions/lift.d.ts +2 -0
- package/lib/functions/lift.d.ts.map +1 -0
- package/lib/functions/lift.js +124 -0
- package/lib/functions/lift.js.map +1 -0
- package/lib/functions/remove.cjs.map +1 -1
- package/lib/functions/remove.d.ts.map +1 -1
- package/lib/functions/remove.js.map +1 -1
- package/lib/functions/softInit.cjs +30 -0
- package/lib/functions/softInit.cjs.map +1 -0
- package/lib/functions/softInit.d.ts +13 -0
- package/lib/functions/softInit.d.ts.map +1 -0
- package/lib/functions/softInit.js +29 -0
- package/lib/functions/softInit.js.map +1 -0
- package/lib/helpers.cjs +16 -0
- package/lib/helpers.cjs.map +1 -1
- package/lib/helpers.d.ts +2 -1
- package/lib/helpers.d.ts.map +1 -1
- package/lib/helpers.js +16 -1
- package/lib/helpers.js.map +1 -1
- package/lib/imports.cjs +4 -1
- package/lib/imports.cjs.map +1 -1
- package/lib/imports.d.ts.map +1 -1
- package/lib/imports.js +4 -1
- package/lib/imports.js.map +1 -1
- package/lib/index.cjs +2 -1
- package/lib/index.js +3 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @bemedev/codebase
|
|
2
2
|
|
|
3
3
|

|
|
4
|
-

|
|
5
5
|

|
|
6
6
|
|
|
7
7
|
A powerful CLI to generate and analyze your TypeScript/JavaScript
|
|
@@ -17,6 +17,8 @@ comprehensive analyses of your source code.
|
|
|
17
17
|
- **๐ฆ Partial import**: Selective import of library parts
|
|
18
18
|
- **๐ฏ Flexible exclusion**: Ability to exclude specific files
|
|
19
19
|
- **๐ Statistics**: Detailed reports about your codebase
|
|
20
|
+
- **๐งน Code pruning**: Automatic removal of unused declarations, empty
|
|
21
|
+
files, and empty folders in target directories
|
|
20
22
|
|
|
21
23
|
## ๐ Prerequisites
|
|
22
24
|
|
|
@@ -116,6 +118,20 @@ remove(
|
|
|
116
118
|
);
|
|
117
119
|
```
|
|
118
120
|
|
|
121
|
+
#### Pruning unused code programmatically
|
|
122
|
+
|
|
123
|
+
You can use the `lift` function to prune unused declarations
|
|
124
|
+
(references, types, variables, classes, functions, or enums) and clean
|
|
125
|
+
up imports within a target folder, automatically deleting files and
|
|
126
|
+
folders that become empty.
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { lift } from '@bemedev/codebase';
|
|
130
|
+
|
|
131
|
+
// Prune unused code and perform tree shaking in the specified folder
|
|
132
|
+
lift('my-project-src');
|
|
133
|
+
```
|
|
134
|
+
|
|
119
135
|
## ๐ Output format
|
|
120
136
|
|
|
121
137
|
The generated JSON file contains:
|
|
@@ -146,7 +162,9 @@ src/
|
|
|
146
162
|
โ โโโ add.ts # Add dependencies
|
|
147
163
|
โ โโโ generate.ts # Generate analysis
|
|
148
164
|
โ โโโ init.ts # Initialization
|
|
149
|
-
โ
|
|
165
|
+
โ โโโ lift.ts # Code pruning/tree shaking
|
|
166
|
+
โ โโโ remove.ts # Removal
|
|
167
|
+
โ โโโ softInit.ts # Soft initialization helper
|
|
150
168
|
โโโ analyse.ts # Analysis engine
|
|
151
169
|
โโโ types.ts # TypeScript definitions
|
|
152
170
|
โโโ constants.ts # Global constants
|
package/lib/functions/index.cjs
CHANGED
|
@@ -2,10 +2,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_functions_add = require("./add.cjs");
|
|
3
3
|
const require_functions_generate = require("./generate.cjs");
|
|
4
4
|
const require_functions_init = require("./init.cjs");
|
|
5
|
+
const require_functions_lift = require("./lift.cjs");
|
|
5
6
|
const require_functions_remove = require("./remove.cjs");
|
|
6
7
|
exports.add = require_functions_add.add;
|
|
7
|
-
exports.createTypesStructure = require_functions_init.createTypesStructure;
|
|
8
8
|
exports.generate = require_functions_generate.generate;
|
|
9
9
|
exports.init = require_functions_init.init;
|
|
10
|
+
exports.lift = require_functions_lift.lift;
|
|
10
11
|
exports.remove = require_functions_remove.remove;
|
|
11
12
|
exports.transformJSON = require_functions_generate.transformJSON;
|
package/lib/functions/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC"}
|
package/lib/functions/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { add } from "./add.js";
|
|
2
2
|
import { generate, transformJSON } from "./generate.js";
|
|
3
|
-
import {
|
|
3
|
+
import { init } from "./init.js";
|
|
4
|
+
import { lift } from "./lift.js";
|
|
4
5
|
import { remove } from "./remove.js";
|
|
5
|
-
export { add,
|
|
6
|
+
export { add, generate, init, lift, remove, transformJSON };
|
package/lib/functions/init.cjs
CHANGED
|
@@ -4,19 +4,6 @@ const require_helpers = require("../helpers.cjs");
|
|
|
4
4
|
let path = require("path");
|
|
5
5
|
let fs = require("fs");
|
|
6
6
|
//#region src/functions/init.ts
|
|
7
|
-
const createTypesStructure = (folderPath, CODEBASE_ANALYSIS) => {
|
|
8
|
-
const entries = Object.entries(CODEBASE_ANALYSIS).filter(([key]) => {
|
|
9
|
-
return key.endsWith("types") || key.endsWith("constants");
|
|
10
|
-
});
|
|
11
|
-
const PATHS = [];
|
|
12
|
-
console.log(`๐ง Creating types structure (${entries.length} files)...`);
|
|
13
|
-
for (const [, fileAnalysis] of entries) {
|
|
14
|
-
const file = require_helpers.writeFileAnalysis(fileAnalysis, folderPath);
|
|
15
|
-
if (file) PATHS.push(file);
|
|
16
|
-
}
|
|
17
|
-
console.log(`โ
Types structure successfully created!`);
|
|
18
|
-
return PATHS;
|
|
19
|
-
};
|
|
20
7
|
const init = (CODEBASE_ANALYSIS, { root, json, path: path$1 = require_constants.DEFAULT_PATH_KEY, bin = require_constants.DEFAULT_CLI_NAME }) => {
|
|
21
8
|
const cwd = process.cwd();
|
|
22
9
|
const configFile = (0, path.join)(cwd, json);
|
|
@@ -31,7 +18,7 @@ const init = (CODEBASE_ANALYSIS, { root, json, path: path$1 = require_constants.
|
|
|
31
18
|
}
|
|
32
19
|
let files = [];
|
|
33
20
|
try {
|
|
34
|
-
files = createTypesStructure(folderPath, CODEBASE_ANALYSIS);
|
|
21
|
+
files = require_helpers.createTypesStructure(folderPath, CODEBASE_ANALYSIS);
|
|
35
22
|
} catch {
|
|
36
23
|
console.error(`โ Error creating the types structure:`);
|
|
37
24
|
return false;
|
|
@@ -63,11 +50,10 @@ const init = (CODEBASE_ANALYSIS, { root, json, path: path$1 = require_constants.
|
|
|
63
50
|
console.error(`โ Error creating the file ${json}:`, error);
|
|
64
51
|
return false;
|
|
65
52
|
}
|
|
66
|
-
console.log(`๐
|
|
53
|
+
console.log(`๐ ${bin} initialization completed successfully!`);
|
|
67
54
|
return true;
|
|
68
55
|
};
|
|
69
56
|
//#endregion
|
|
70
|
-
exports.createTypesStructure = createTypesStructure;
|
|
71
57
|
exports.init = init;
|
|
72
58
|
|
|
73
59
|
//# sourceMappingURL=init.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.cjs","names":["
|
|
1
|
+
{"version":3,"file":"init.cjs","names":["DEFAULT_PATH_KEY","DEFAULT_CLI_NAME","getFolderPath","createTypesStructure","path","PROPERTIES"],"sources":["../../src/functions/init.ts"],"sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';\nimport { join, relative } from 'path';\nimport {\n DEFAULT_CLI_NAME,\n DEFAULT_PATH_KEY,\n PROPERTIES,\n} from '../constants';\nimport { createTypesStructure, getFolderPath } from '../helpers';\nimport { CodebaseAnalysis } from '../schemas';\n\nexport interface InitOptions {\n /**\n * Custom location for the .bemedev folder\n * Default: 'src/.bemedev' if src exists, otherwise '.bemedev' at the root\n */\n root: string;\n json: string;\n path?: string;\n bin?: string;\n}\n\nexport const init = (\n CODEBASE_ANALYSIS: CodebaseAnalysis,\n {\n root,\n json,\n path = DEFAULT_PATH_KEY,\n bin = DEFAULT_CLI_NAME,\n }: InitOptions,\n) => {\n const cwd = process.cwd();\n const configFile = join(cwd, json);\n const configExists = existsSync(configFile);\n if (configExists) return true;\n const folderPath = getFolderPath(root);\n\n // 1. Create the folder\n try {\n mkdirSync(folderPath, { recursive: true });\n console.log(`โ
Folder ${bin} created in: ${root}`);\n } catch (error) {\n console.error(`โ Error creating the folder ${bin}:`, error);\n return false;\n }\n\n // eslint-disable-next-line no-useless-assignment\n let files: string[] = [];\n // 1.5. Create the types files structure\n try {\n files = createTypesStructure(folderPath, CODEBASE_ANALYSIS);\n } catch {\n console.error(`โ Error creating the types structure:`);\n return false;\n }\n\n // 2. Update tsconfig.json\n const tsconfigPath = join(cwd, 'tsconfig.json');\n\n if (existsSync(tsconfigPath)) {\n try {\n const tsconfigContent = readFileSync(tsconfigPath, 'utf8');\n const tsconfig = JSON.parse(tsconfigContent);\n\n // Initialize compilerOptions and paths if they do not exist\n if (!tsconfig.compilerOptions) {\n tsconfig.compilerOptions = {};\n }\n\n if (!tsconfig.compilerOptions.paths) {\n tsconfig.compilerOptions.paths = {};\n }\n\n // Add the path #bemedev/*\n const relativePath = relative(process.cwd(), folderPath);\n\n tsconfig.compilerOptions.paths[path] = [`./${relativePath}/*`];\n\n writeFileSync(\n tsconfigPath,\n JSON.stringify(tsconfig, null, 2),\n 'utf8',\n );\n console.log(`โ
Path ${path} added to tsconfig.json`);\n } catch (error) {\n console.error(`โ Error updating tsconfig.json:`, error);\n return false;\n }\n } else {\n console.warn(`โ ๏ธ File tsconfig.json not found, path not added`);\n }\n\n // 3. Create the .bemedev.json file at the root\n\n const jsonConfig = {\n version: '1.0.0',\n [PROPERTIES.PATH]: root,\n [PROPERTIES.FILES]: files,\n };\n\n try {\n writeFileSync(\n configFile,\n JSON.stringify(jsonConfig, null, 2),\n 'utf8',\n );\n console.log(`โ
File ${json} created at the root of the project`);\n } catch (error) {\n console.error(`โ Error creating the file ${json}:`, error);\n return false;\n }\n\n console.log(`๐ ${bin} initialization completed successfully!`);\n return true;\n};\n"],"mappings":";;;;;;AAqBA,MAAa,QACX,mBACA,EACE,MACA,MACA,MAAA,SAAOA,kBAAAA,kBACP,MAAMC,kBAAAA,uBAEL;CACH,MAAM,MAAM,QAAQ,IAAI;CACxB,MAAM,cAAA,GAAA,KAAA,KAAA,CAAkB,KAAK,IAAI;CAEjC,KAAA,GAAA,GAAA,WAAA,CADgC,UACjB,GAAG,OAAO;CACzB,MAAM,aAAaC,gBAAAA,cAAc,IAAI;CAGrC,IAAI;EACF,CAAA,GAAA,GAAA,UAAA,CAAU,YAAY,EAAE,WAAW,KAAK,CAAC;EACzC,QAAQ,IAAI,YAAY,IAAI,eAAe,MAAM;CACnD,SAAS,OAAO;EACd,QAAQ,MAAM,+BAA+B,IAAI,IAAI,KAAK;EAC1D,OAAO;CACT;CAGA,IAAI,QAAkB,CAAC;CAEvB,IAAI;EACF,QAAQC,gBAAAA,qBAAqB,YAAY,iBAAiB;CAC5D,QAAQ;EACN,QAAQ,MAAM,uCAAuC;EACrD,OAAO;CACT;CAGA,MAAM,gBAAA,GAAA,KAAA,KAAA,CAAoB,KAAK,eAAe;CAE9C,KAAA,GAAA,GAAA,WAAA,CAAe,YAAY,GACzB,IAAI;EACF,MAAM,mBAAA,GAAA,GAAA,aAAA,CAA+B,cAAc,MAAM;EACzD,MAAM,WAAW,KAAK,MAAM,eAAe;EAG3C,IAAI,CAAC,SAAS,iBACZ,SAAS,kBAAkB,CAAC;EAG9B,IAAI,CAAC,SAAS,gBAAgB,OAC5B,SAAS,gBAAgB,QAAQ,CAAC;EAIpC,MAAM,gBAAA,GAAA,KAAA,SAAA,CAAwB,QAAQ,IAAI,GAAG,UAAU;EAEvD,SAAS,gBAAgB,MAAMC,UAAQ,CAAC,KAAK,aAAa,GAAG;EAE7D,CAAA,GAAA,GAAA,cAAA,CACE,cACA,KAAK,UAAU,UAAU,MAAM,CAAC,GAChC,MACF;EACA,QAAQ,IAAI,UAAUA,OAAK,wBAAwB;CACrD,SAAS,OAAO;EACd,QAAQ,MAAM,mCAAmC,KAAK;EACtD,OAAO;CACT;MAEA,QAAQ,KAAK,iDAAiD;CAKhE,MAAM,aAAa;EACjB,SAAS;GACRC,kBAAAA,WAAW,OAAO;GAClBA,kBAAAA,WAAW,QAAQ;CACtB;CAEA,IAAI;EACF,CAAA,GAAA,GAAA,cAAA,CACE,YACA,KAAK,UAAU,YAAY,MAAM,CAAC,GAClC,MACF;EACA,QAAQ,IAAI,UAAU,KAAK,oCAAoC;CACjE,SAAS,OAAO;EACd,QAAQ,MAAM,6BAA6B,KAAK,IAAI,KAAK;EACzD,OAAO;CACT;CAEA,QAAQ,IAAI,MAAM,IAAI,wCAAwC;CAC9D,OAAO;AACT"}
|
package/lib/functions/init.d.ts
CHANGED
|
@@ -9,6 +9,5 @@ export interface InitOptions {
|
|
|
9
9
|
path?: string;
|
|
10
10
|
bin?: string;
|
|
11
11
|
}
|
|
12
|
-
export declare const createTypesStructure: (folderPath: string, CODEBASE_ANALYSIS: CodebaseAnalysis) => string[];
|
|
13
12
|
export declare const init: (CODEBASE_ANALYSIS: CodebaseAnalysis, { root, json, path, bin, }: InitOptions) => boolean;
|
|
14
13
|
//# sourceMappingURL=init.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/functions/init.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/functions/init.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,eAAO,MAAM,IAAI,GACf,mBAAmB,gBAAgB,EACnC,4BAKG,WAAW,YAqFf,CAAC"}
|
package/lib/functions/init.js
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
1
|
import { DEFAULT_CLI_NAME, DEFAULT_PATH_KEY, PROPERTIES } from "../constants.js";
|
|
2
|
-
import {
|
|
2
|
+
import { createTypesStructure, getFolderPath } from "../helpers.js";
|
|
3
3
|
import { join, relative } from "path";
|
|
4
4
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
5
5
|
//#region src/functions/init.ts
|
|
6
|
-
const createTypesStructure = (folderPath, CODEBASE_ANALYSIS) => {
|
|
7
|
-
const entries = Object.entries(CODEBASE_ANALYSIS).filter(([key]) => {
|
|
8
|
-
return key.endsWith("types") || key.endsWith("constants");
|
|
9
|
-
});
|
|
10
|
-
const PATHS = [];
|
|
11
|
-
console.log(`๐ง Creating types structure (${entries.length} files)...`);
|
|
12
|
-
for (const [, fileAnalysis] of entries) {
|
|
13
|
-
const file = writeFileAnalysis(fileAnalysis, folderPath);
|
|
14
|
-
if (file) PATHS.push(file);
|
|
15
|
-
}
|
|
16
|
-
console.log(`โ
Types structure successfully created!`);
|
|
17
|
-
return PATHS;
|
|
18
|
-
};
|
|
19
6
|
const init = (CODEBASE_ANALYSIS, { root, json, path = DEFAULT_PATH_KEY, bin = DEFAULT_CLI_NAME }) => {
|
|
20
7
|
const cwd = process.cwd();
|
|
21
8
|
const configFile = join(cwd, json);
|
|
@@ -62,10 +49,10 @@ const init = (CODEBASE_ANALYSIS, { root, json, path = DEFAULT_PATH_KEY, bin = DE
|
|
|
62
49
|
console.error(`โ Error creating the file ${json}:`, error);
|
|
63
50
|
return false;
|
|
64
51
|
}
|
|
65
|
-
console.log(`๐
|
|
52
|
+
console.log(`๐ ${bin} initialization completed successfully!`);
|
|
66
53
|
return true;
|
|
67
54
|
};
|
|
68
55
|
//#endregion
|
|
69
|
-
export {
|
|
56
|
+
export { init };
|
|
70
57
|
|
|
71
58
|
//# sourceMappingURL=init.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.js","names":[],"sources":["../../src/functions/init.ts"],"sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';\nimport { join, relative } from 'path';\nimport {\n DEFAULT_CLI_NAME,\n DEFAULT_PATH_KEY,\n PROPERTIES,\n} from '../constants';\nimport {
|
|
1
|
+
{"version":3,"file":"init.js","names":[],"sources":["../../src/functions/init.ts"],"sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';\nimport { join, relative } from 'path';\nimport {\n DEFAULT_CLI_NAME,\n DEFAULT_PATH_KEY,\n PROPERTIES,\n} from '../constants';\nimport { createTypesStructure, getFolderPath } from '../helpers';\nimport { CodebaseAnalysis } from '../schemas';\n\nexport interface InitOptions {\n /**\n * Custom location for the .bemedev folder\n * Default: 'src/.bemedev' if src exists, otherwise '.bemedev' at the root\n */\n root: string;\n json: string;\n path?: string;\n bin?: string;\n}\n\nexport const init = (\n CODEBASE_ANALYSIS: CodebaseAnalysis,\n {\n root,\n json,\n path = DEFAULT_PATH_KEY,\n bin = DEFAULT_CLI_NAME,\n }: InitOptions,\n) => {\n const cwd = process.cwd();\n const configFile = join(cwd, json);\n const configExists = existsSync(configFile);\n if (configExists) return true;\n const folderPath = getFolderPath(root);\n\n // 1. Create the folder\n try {\n mkdirSync(folderPath, { recursive: true });\n console.log(`โ
Folder ${bin} created in: ${root}`);\n } catch (error) {\n console.error(`โ Error creating the folder ${bin}:`, error);\n return false;\n }\n\n // eslint-disable-next-line no-useless-assignment\n let files: string[] = [];\n // 1.5. Create the types files structure\n try {\n files = createTypesStructure(folderPath, CODEBASE_ANALYSIS);\n } catch {\n console.error(`โ Error creating the types structure:`);\n return false;\n }\n\n // 2. Update tsconfig.json\n const tsconfigPath = join(cwd, 'tsconfig.json');\n\n if (existsSync(tsconfigPath)) {\n try {\n const tsconfigContent = readFileSync(tsconfigPath, 'utf8');\n const tsconfig = JSON.parse(tsconfigContent);\n\n // Initialize compilerOptions and paths if they do not exist\n if (!tsconfig.compilerOptions) {\n tsconfig.compilerOptions = {};\n }\n\n if (!tsconfig.compilerOptions.paths) {\n tsconfig.compilerOptions.paths = {};\n }\n\n // Add the path #bemedev/*\n const relativePath = relative(process.cwd(), folderPath);\n\n tsconfig.compilerOptions.paths[path] = [`./${relativePath}/*`];\n\n writeFileSync(\n tsconfigPath,\n JSON.stringify(tsconfig, null, 2),\n 'utf8',\n );\n console.log(`โ
Path ${path} added to tsconfig.json`);\n } catch (error) {\n console.error(`โ Error updating tsconfig.json:`, error);\n return false;\n }\n } else {\n console.warn(`โ ๏ธ File tsconfig.json not found, path not added`);\n }\n\n // 3. Create the .bemedev.json file at the root\n\n const jsonConfig = {\n version: '1.0.0',\n [PROPERTIES.PATH]: root,\n [PROPERTIES.FILES]: files,\n };\n\n try {\n writeFileSync(\n configFile,\n JSON.stringify(jsonConfig, null, 2),\n 'utf8',\n );\n console.log(`โ
File ${json} created at the root of the project`);\n } catch (error) {\n console.error(`โ Error creating the file ${json}:`, error);\n return false;\n }\n\n console.log(`๐ ${bin} initialization completed successfully!`);\n return true;\n};\n"],"mappings":";;;;;AAqBA,MAAa,QACX,mBACA,EACE,MACA,MACA,OAAO,kBACP,MAAM,uBAEL;CACH,MAAM,MAAM,QAAQ,IAAI;CACxB,MAAM,aAAa,KAAK,KAAK,IAAI;CAEjC,IADqB,WAAW,UACjB,GAAG,OAAO;CACzB,MAAM,aAAa,cAAc,IAAI;CAGrC,IAAI;EACF,UAAU,YAAY,EAAE,WAAW,KAAK,CAAC;EACzC,QAAQ,IAAI,YAAY,IAAI,eAAe,MAAM;CACnD,SAAS,OAAO;EACd,QAAQ,MAAM,+BAA+B,IAAI,IAAI,KAAK;EAC1D,OAAO;CACT;CAGA,IAAI,QAAkB,CAAC;CAEvB,IAAI;EACF,QAAQ,qBAAqB,YAAY,iBAAiB;CAC5D,QAAQ;EACN,QAAQ,MAAM,uCAAuC;EACrD,OAAO;CACT;CAGA,MAAM,eAAe,KAAK,KAAK,eAAe;CAE9C,IAAI,WAAW,YAAY,GACzB,IAAI;EACF,MAAM,kBAAkB,aAAa,cAAc,MAAM;EACzD,MAAM,WAAW,KAAK,MAAM,eAAe;EAG3C,IAAI,CAAC,SAAS,iBACZ,SAAS,kBAAkB,CAAC;EAG9B,IAAI,CAAC,SAAS,gBAAgB,OAC5B,SAAS,gBAAgB,QAAQ,CAAC;EAIpC,MAAM,eAAe,SAAS,QAAQ,IAAI,GAAG,UAAU;EAEvD,SAAS,gBAAgB,MAAM,QAAQ,CAAC,KAAK,aAAa,GAAG;EAE7D,cACE,cACA,KAAK,UAAU,UAAU,MAAM,CAAC,GAChC,MACF;EACA,QAAQ,IAAI,UAAU,KAAK,wBAAwB;CACrD,SAAS,OAAO;EACd,QAAQ,MAAM,mCAAmC,KAAK;EACtD,OAAO;CACT;MAEA,QAAQ,KAAK,iDAAiD;CAKhE,MAAM,aAAa;EACjB,SAAS;GACR,WAAW,OAAO;GAClB,WAAW,QAAQ;CACtB;CAEA,IAAI;EACF,cACE,YACA,KAAK,UAAU,YAAY,MAAM,CAAC,GAClC,MACF;EACA,QAAQ,IAAI,UAAU,KAAK,oCAAoC;CACjE,SAAS,OAAO;EACd,QAAQ,MAAM,6BAA6B,KAAK,IAAI,KAAK;EACzD,OAAO;CACT;CAEA,QAAQ,IAAI,MAAM,IAAI,wCAAwC;CAC9D,OAAO;AACT"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_helpers = require("../helpers.cjs");
|
|
3
|
+
let path = require("path");
|
|
4
|
+
let ts_morph = require("ts-morph");
|
|
5
|
+
let fs = require("fs");
|
|
6
|
+
//#region src/functions/lift.ts
|
|
7
|
+
const lift = (root) => {
|
|
8
|
+
const folderPath = require_helpers.getFolderPath(root);
|
|
9
|
+
if (!(0, fs.existsSync)(folderPath)) {
|
|
10
|
+
console.warn(`Folder not found: ${folderPath}`);
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const tsconfigPath = (0, path.join)(process.cwd(), "tsconfig.json");
|
|
14
|
+
const project = (0, fs.existsSync)(tsconfigPath) ? new ts_morph.Project({ tsConfigFilePath: tsconfigPath }) : new ts_morph.Project();
|
|
15
|
+
project.addSourceFilesAtPaths((0, path.join)(folderPath, "**/*.ts"));
|
|
16
|
+
project.addSourceFilesAtPaths((0, path.join)(folderPath, "**/*.tsx"));
|
|
17
|
+
const isInsideFolder = (filePath) => {
|
|
18
|
+
return filePath.startsWith(folderPath);
|
|
19
|
+
};
|
|
20
|
+
let changed = true;
|
|
21
|
+
while (changed) {
|
|
22
|
+
changed = false;
|
|
23
|
+
const declarations = project.getSourceFiles().filter((sf) => isInsideFolder(sf.getFilePath())).flatMap((sf) => [
|
|
24
|
+
...sf.getTypeAliases(),
|
|
25
|
+
...sf.getInterfaces(),
|
|
26
|
+
...sf.getVariableDeclarations(),
|
|
27
|
+
...sf.getFunctions(),
|
|
28
|
+
...sf.getClasses(),
|
|
29
|
+
...sf.getEnums()
|
|
30
|
+
]);
|
|
31
|
+
for (const decl of declarations) {
|
|
32
|
+
if (decl.wasForgotten()) continue;
|
|
33
|
+
const nameNode = decl.getNameNode();
|
|
34
|
+
if (!nameNode || !ts_morph.Node.isIdentifier(nameNode)) continue;
|
|
35
|
+
const referencedSymbols = nameNode.findReferences();
|
|
36
|
+
let refCount = 0;
|
|
37
|
+
for (const refSymbol of referencedSymbols) for (const ref of refSymbol.getReferences()) if (ref.getNode() !== nameNode) refCount++;
|
|
38
|
+
if (refCount === 0) {
|
|
39
|
+
decl.remove();
|
|
40
|
+
changed = true;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (changed) continue;
|
|
45
|
+
const importDeclarations = project.getSourceFiles().filter((sf) => isInsideFolder(sf.getFilePath())).flatMap((sf) => sf.getImportDeclarations());
|
|
46
|
+
for (const imp of importDeclarations) {
|
|
47
|
+
if (imp.wasForgotten()) continue;
|
|
48
|
+
const sf = imp.getSourceFile();
|
|
49
|
+
const namedImports = imp.getNamedImports();
|
|
50
|
+
for (const spec of namedImports) {
|
|
51
|
+
if (spec.wasForgotten()) continue;
|
|
52
|
+
const nameNode = spec.getNameNode();
|
|
53
|
+
if (!ts_morph.Node.isIdentifier(nameNode)) continue;
|
|
54
|
+
const referencedSymbols = nameNode.findReferences();
|
|
55
|
+
let refCount = 0;
|
|
56
|
+
for (const refSymbol of referencedSymbols) for (const ref of refSymbol.getReferences()) if (ref.getSourceFile() === sf && ref.getNode() !== nameNode) refCount++;
|
|
57
|
+
if (refCount === 0) {
|
|
58
|
+
spec.remove();
|
|
59
|
+
changed = true;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (changed) break;
|
|
64
|
+
const defaultImport = imp.getDefaultImport();
|
|
65
|
+
if (defaultImport && !defaultImport.wasForgotten() && ts_morph.Node.isIdentifier(defaultImport)) {
|
|
66
|
+
const referencedSymbols = defaultImport.findReferences();
|
|
67
|
+
let refCount = 0;
|
|
68
|
+
for (const refSymbol of referencedSymbols) for (const ref of refSymbol.getReferences()) if (ref.getSourceFile() === sf && ref.getNode() !== defaultImport) refCount++;
|
|
69
|
+
if (refCount === 0) {
|
|
70
|
+
const hasNamed = imp.getNamedImports().length > 0;
|
|
71
|
+
const hasNamespace = !!imp.getNamespaceImport();
|
|
72
|
+
if (!hasNamed && !hasNamespace) imp.remove();
|
|
73
|
+
else imp.removeDefaultImport();
|
|
74
|
+
changed = true;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (changed) break;
|
|
79
|
+
const namespaceImport = imp.getNamespaceImport();
|
|
80
|
+
if (namespaceImport && !namespaceImport.wasForgotten() && ts_morph.Node.isIdentifier(namespaceImport)) {
|
|
81
|
+
const referencedSymbols = namespaceImport.findReferences();
|
|
82
|
+
let refCount = 0;
|
|
83
|
+
for (const refSymbol of referencedSymbols) for (const ref of refSymbol.getReferences()) if (ref.getSourceFile() === sf && ref.getNode() !== namespaceImport) refCount++;
|
|
84
|
+
if (refCount === 0) {
|
|
85
|
+
imp.remove();
|
|
86
|
+
changed = true;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (changed) break;
|
|
91
|
+
const hasNamed = imp.getNamedImports().length > 0;
|
|
92
|
+
const hasDefault = !!imp.getDefaultImport();
|
|
93
|
+
const hasNamespace = !!imp.getNamespaceImport();
|
|
94
|
+
if (!hasNamed && !hasDefault && !hasNamespace) {
|
|
95
|
+
imp.remove();
|
|
96
|
+
changed = true;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
project.saveSync();
|
|
102
|
+
const sourceFiles = project.getSourceFiles();
|
|
103
|
+
for (const sf of sourceFiles) {
|
|
104
|
+
const filePath = sf.getFilePath();
|
|
105
|
+
if (isInsideFolder(filePath) && sf.getFullText().trim() === "") {
|
|
106
|
+
project.removeSourceFile(sf);
|
|
107
|
+
if ((0, fs.existsSync)(filePath)) (0, fs.unlinkSync)(filePath);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const cleanEmptyDirs = (dir) => {
|
|
111
|
+
if (!(0, fs.existsSync)(dir) || !(0, fs.statSync)(dir).isDirectory()) return;
|
|
112
|
+
const files = (0, fs.readdirSync)(dir);
|
|
113
|
+
for (const file of files) {
|
|
114
|
+
const fullPath = (0, path.join)(dir, file);
|
|
115
|
+
if ((0, fs.statSync)(fullPath).isDirectory()) cleanEmptyDirs(fullPath);
|
|
116
|
+
}
|
|
117
|
+
if ((0, fs.readdirSync)(dir).length === 0) (0, fs.rmdirSync)(dir);
|
|
118
|
+
};
|
|
119
|
+
cleanEmptyDirs(folderPath);
|
|
120
|
+
return true;
|
|
121
|
+
};
|
|
122
|
+
//#endregion
|
|
123
|
+
exports.lift = lift;
|
|
124
|
+
|
|
125
|
+
//# sourceMappingURL=lift.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lift.cjs","names":["getFolderPath","Project","Node"],"sources":["../../src/functions/lift.ts"],"sourcesContent":["import {\n existsSync,\n readdirSync,\n rmdirSync,\n statSync,\n unlinkSync,\n} from 'fs';\nimport { join } from 'path';\nimport { Project, Node } from 'ts-morph';\nimport { getFolderPath } from '../helpers';\n\nexport const lift = (root: string) => {\n const folderPath = getFolderPath(root);\n if (!existsSync(folderPath)) {\n console.warn(`Folder not found: ${folderPath}`);\n return false;\n }\n\n const tsconfigPath = join(process.cwd(), 'tsconfig.json');\n const project = existsSync(tsconfigPath)\n ? new Project({ tsConfigFilePath: tsconfigPath })\n : new Project();\n\n // Add all source files recursively\n project.addSourceFilesAtPaths(join(folderPath, '**/*.ts'));\n project.addSourceFilesAtPaths(join(folderPath, '**/*.tsx'));\n\n const isInsideFolder = (filePath: string) => {\n return filePath.startsWith(folderPath);\n };\n\n let changed = true;\n while (changed) {\n changed = false;\n\n // A: Declarations inside the target folder\n const declarations = project\n .getSourceFiles()\n .filter(sf => isInsideFolder(sf.getFilePath()))\n .flatMap(sf => [\n ...sf.getTypeAliases(),\n ...sf.getInterfaces(),\n ...sf.getVariableDeclarations(),\n ...sf.getFunctions(),\n ...sf.getClasses(),\n ...sf.getEnums(),\n ]);\n\n for (const decl of declarations) {\n if (decl.wasForgotten()) continue;\n\n const nameNode = decl.getNameNode();\n if (!nameNode || !Node.isIdentifier(nameNode)) continue;\n\n const referencedSymbols = nameNode.findReferences();\n let refCount = 0;\n for (const refSymbol of referencedSymbols) {\n for (const ref of refSymbol.getReferences()) {\n if (ref.getNode() !== nameNode) {\n refCount++;\n }\n }\n }\n\n if (refCount === 0) {\n decl.remove();\n changed = true;\n break;\n }\n }\n\n if (changed) continue;\n\n // B: Imports inside the target folder\n const importDeclarations = project\n .getSourceFiles()\n .filter(sf => isInsideFolder(sf.getFilePath()))\n .flatMap(sf => sf.getImportDeclarations());\n\n for (const imp of importDeclarations) {\n if (imp.wasForgotten()) continue;\n const sf = imp.getSourceFile();\n\n // 1. Named imports\n const namedImports = imp.getNamedImports();\n for (const spec of namedImports) {\n if (spec.wasForgotten()) continue;\n const nameNode = spec.getNameNode();\n if (!Node.isIdentifier(nameNode)) continue;\n\n const referencedSymbols = nameNode.findReferences();\n let refCount = 0;\n for (const refSymbol of referencedSymbols) {\n for (const ref of refSymbol.getReferences()) {\n if (\n ref.getSourceFile() === sf &&\n ref.getNode() !== nameNode\n ) {\n refCount++;\n }\n }\n }\n if (refCount === 0) {\n spec.remove();\n changed = true;\n break;\n }\n }\n if (changed) break;\n\n // 2. Default import\n const defaultImport = imp.getDefaultImport();\n if (\n defaultImport &&\n !defaultImport.wasForgotten() &&\n Node.isIdentifier(defaultImport)\n ) {\n const referencedSymbols = defaultImport.findReferences();\n let refCount = 0;\n for (const refSymbol of referencedSymbols) {\n for (const ref of refSymbol.getReferences()) {\n if (\n ref.getSourceFile() === sf &&\n ref.getNode() !== defaultImport\n ) {\n refCount++;\n }\n }\n }\n if (refCount === 0) {\n const hasNamed = imp.getNamedImports().length > 0;\n const hasNamespace = !!imp.getNamespaceImport();\n if (!hasNamed && !hasNamespace) {\n imp.remove();\n } else {\n imp.removeDefaultImport();\n }\n changed = true;\n break;\n }\n }\n if (changed) break;\n\n // 3. Namespace import\n const namespaceImport = imp.getNamespaceImport();\n if (\n namespaceImport &&\n !namespaceImport.wasForgotten() &&\n Node.isIdentifier(namespaceImport)\n ) {\n const referencedSymbols = namespaceImport.findReferences();\n let refCount = 0;\n for (const refSymbol of referencedSymbols) {\n for (const ref of refSymbol.getReferences()) {\n if (\n ref.getSourceFile() === sf &&\n ref.getNode() !== namespaceImport\n ) {\n refCount++;\n }\n }\n }\n if (refCount === 0) {\n imp.remove();\n changed = true;\n break;\n }\n }\n if (changed) break;\n\n // 4. Empty import declarations\n const hasNamed = imp.getNamedImports().length > 0;\n const hasDefault = !!imp.getDefaultImport();\n const hasNamespace = !!imp.getNamespaceImport();\n if (!hasNamed && !hasDefault && !hasNamespace) {\n imp.remove();\n changed = true;\n break;\n }\n }\n }\n\n // Save changes\n project.saveSync();\n\n // Delete files that are empty (after trimming whitespace) and inside the target folder\n const sourceFiles = project.getSourceFiles();\n for (const sf of sourceFiles) {\n const filePath = sf.getFilePath();\n if (isInsideFolder(filePath) && sf.getFullText().trim() === '') {\n project.removeSourceFile(sf);\n if (existsSync(filePath)) {\n unlinkSync(filePath);\n }\n }\n }\n\n // Helper to recursively clean empty directories\n const cleanEmptyDirs = (dir: string) => {\n if (!existsSync(dir) || !statSync(dir).isDirectory()) return;\n\n const files = readdirSync(dir);\n for (const file of files) {\n const fullPath = join(dir, file);\n if (statSync(fullPath).isDirectory()) {\n cleanEmptyDirs(fullPath);\n }\n }\n\n const remaining = readdirSync(dir);\n if (remaining.length === 0) {\n rmdirSync(dir);\n }\n };\n\n cleanEmptyDirs(folderPath);\n\n return true;\n};\n"],"mappings":";;;;;;AAWA,MAAa,QAAQ,SAAiB;CACpC,MAAM,aAAaA,gBAAAA,cAAc,IAAI;CACrC,IAAI,EAAA,GAAA,GAAA,WAAA,CAAY,UAAU,GAAG;EAC3B,QAAQ,KAAK,qBAAqB,YAAY;EAC9C,OAAO;CACT;CAEA,MAAM,gBAAA,GAAA,KAAA,KAAA,CAAoB,QAAQ,IAAI,GAAG,eAAe;CACxD,MAAM,WAAA,GAAA,GAAA,WAAA,CAAqB,YAAY,IACnC,IAAIC,SAAAA,QAAQ,EAAE,kBAAkB,aAAa,CAAC,IAC9C,IAAIA,SAAAA,QAAQ;CAGhB,QAAQ,uBAAA,GAAA,KAAA,KAAA,CAA2B,YAAY,SAAS,CAAC;CACzD,QAAQ,uBAAA,GAAA,KAAA,KAAA,CAA2B,YAAY,UAAU,CAAC;CAE1D,MAAM,kBAAkB,aAAqB;EAC3C,OAAO,SAAS,WAAW,UAAU;CACvC;CAEA,IAAI,UAAU;CACd,OAAO,SAAS;EACd,UAAU;EAGV,MAAM,eAAe,QAClB,eAAe,CAAC,CAChB,QAAO,OAAM,eAAe,GAAG,YAAY,CAAC,CAAC,CAAC,CAC9C,SAAQ,OAAM;GACb,GAAG,GAAG,eAAe;GACrB,GAAG,GAAG,cAAc;GACpB,GAAG,GAAG,wBAAwB;GAC9B,GAAG,GAAG,aAAa;GACnB,GAAG,GAAG,WAAW;GACjB,GAAG,GAAG,SAAS;EACjB,CAAC;EAEH,KAAK,MAAM,QAAQ,cAAc;GAC/B,IAAI,KAAK,aAAa,GAAG;GAEzB,MAAM,WAAW,KAAK,YAAY;GAClC,IAAI,CAAC,YAAY,C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AAA,GAAA,SAAA,CAAU,GAAG,CAAC,CAAC,YAAY,GAAG;EAEtD,MAAM,SAAA,GAAA,GAAA,YAAA,CAAoB,GAAG;EAC7B,KAAK,MAAM,QAAQ,OAAO;GACxB,MAAM,YAAA,GAAA,KAAA,KAAA,CAAgB,KAAK,IAAI;GAC/B,KAAA,GAAA,GAAA,SAAA,CAAa,QAAQ,CAAC,CAAC,YAAY,GACjC,eAAe,QAAQ;EAE3B;EAGA,KAAA,GAAA,GAAA,YAAA,CAD8B,GAClB,CAAC,CAAC,WAAW,GACvB,CAAA,GAAA,GAAA,UAAA,CAAU,GAAG;CAEjB;CAEA,eAAe,UAAU;CAEzB,OAAO;AACT"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lift.d.ts","sourceRoot":"","sources":["../../src/functions/lift.ts"],"names":[],"mappings":"AAWA,eAAO,MAAM,IAAI,GAAI,MAAM,MAAM,YA+MhC,CAAC"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { getFolderPath } from "../helpers.js";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { Node, Project } from "ts-morph";
|
|
4
|
+
import { existsSync, readdirSync, rmdirSync, statSync, unlinkSync } from "fs";
|
|
5
|
+
//#region src/functions/lift.ts
|
|
6
|
+
const lift = (root) => {
|
|
7
|
+
const folderPath = getFolderPath(root);
|
|
8
|
+
if (!existsSync(folderPath)) {
|
|
9
|
+
console.warn(`Folder not found: ${folderPath}`);
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
const tsconfigPath = join(process.cwd(), "tsconfig.json");
|
|
13
|
+
const project = existsSync(tsconfigPath) ? new Project({ tsConfigFilePath: tsconfigPath }) : new Project();
|
|
14
|
+
project.addSourceFilesAtPaths(join(folderPath, "**/*.ts"));
|
|
15
|
+
project.addSourceFilesAtPaths(join(folderPath, "**/*.tsx"));
|
|
16
|
+
const isInsideFolder = (filePath) => {
|
|
17
|
+
return filePath.startsWith(folderPath);
|
|
18
|
+
};
|
|
19
|
+
let changed = true;
|
|
20
|
+
while (changed) {
|
|
21
|
+
changed = false;
|
|
22
|
+
const declarations = project.getSourceFiles().filter((sf) => isInsideFolder(sf.getFilePath())).flatMap((sf) => [
|
|
23
|
+
...sf.getTypeAliases(),
|
|
24
|
+
...sf.getInterfaces(),
|
|
25
|
+
...sf.getVariableDeclarations(),
|
|
26
|
+
...sf.getFunctions(),
|
|
27
|
+
...sf.getClasses(),
|
|
28
|
+
...sf.getEnums()
|
|
29
|
+
]);
|
|
30
|
+
for (const decl of declarations) {
|
|
31
|
+
if (decl.wasForgotten()) continue;
|
|
32
|
+
const nameNode = decl.getNameNode();
|
|
33
|
+
if (!nameNode || !Node.isIdentifier(nameNode)) continue;
|
|
34
|
+
const referencedSymbols = nameNode.findReferences();
|
|
35
|
+
let refCount = 0;
|
|
36
|
+
for (const refSymbol of referencedSymbols) for (const ref of refSymbol.getReferences()) if (ref.getNode() !== nameNode) refCount++;
|
|
37
|
+
if (refCount === 0) {
|
|
38
|
+
decl.remove();
|
|
39
|
+
changed = true;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (changed) continue;
|
|
44
|
+
const importDeclarations = project.getSourceFiles().filter((sf) => isInsideFolder(sf.getFilePath())).flatMap((sf) => sf.getImportDeclarations());
|
|
45
|
+
for (const imp of importDeclarations) {
|
|
46
|
+
if (imp.wasForgotten()) continue;
|
|
47
|
+
const sf = imp.getSourceFile();
|
|
48
|
+
const namedImports = imp.getNamedImports();
|
|
49
|
+
for (const spec of namedImports) {
|
|
50
|
+
if (spec.wasForgotten()) continue;
|
|
51
|
+
const nameNode = spec.getNameNode();
|
|
52
|
+
if (!Node.isIdentifier(nameNode)) continue;
|
|
53
|
+
const referencedSymbols = nameNode.findReferences();
|
|
54
|
+
let refCount = 0;
|
|
55
|
+
for (const refSymbol of referencedSymbols) for (const ref of refSymbol.getReferences()) if (ref.getSourceFile() === sf && ref.getNode() !== nameNode) refCount++;
|
|
56
|
+
if (refCount === 0) {
|
|
57
|
+
spec.remove();
|
|
58
|
+
changed = true;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (changed) break;
|
|
63
|
+
const defaultImport = imp.getDefaultImport();
|
|
64
|
+
if (defaultImport && !defaultImport.wasForgotten() && Node.isIdentifier(defaultImport)) {
|
|
65
|
+
const referencedSymbols = defaultImport.findReferences();
|
|
66
|
+
let refCount = 0;
|
|
67
|
+
for (const refSymbol of referencedSymbols) for (const ref of refSymbol.getReferences()) if (ref.getSourceFile() === sf && ref.getNode() !== defaultImport) refCount++;
|
|
68
|
+
if (refCount === 0) {
|
|
69
|
+
const hasNamed = imp.getNamedImports().length > 0;
|
|
70
|
+
const hasNamespace = !!imp.getNamespaceImport();
|
|
71
|
+
if (!hasNamed && !hasNamespace) imp.remove();
|
|
72
|
+
else imp.removeDefaultImport();
|
|
73
|
+
changed = true;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (changed) break;
|
|
78
|
+
const namespaceImport = imp.getNamespaceImport();
|
|
79
|
+
if (namespaceImport && !namespaceImport.wasForgotten() && Node.isIdentifier(namespaceImport)) {
|
|
80
|
+
const referencedSymbols = namespaceImport.findReferences();
|
|
81
|
+
let refCount = 0;
|
|
82
|
+
for (const refSymbol of referencedSymbols) for (const ref of refSymbol.getReferences()) if (ref.getSourceFile() === sf && ref.getNode() !== namespaceImport) refCount++;
|
|
83
|
+
if (refCount === 0) {
|
|
84
|
+
imp.remove();
|
|
85
|
+
changed = true;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (changed) break;
|
|
90
|
+
const hasNamed = imp.getNamedImports().length > 0;
|
|
91
|
+
const hasDefault = !!imp.getDefaultImport();
|
|
92
|
+
const hasNamespace = !!imp.getNamespaceImport();
|
|
93
|
+
if (!hasNamed && !hasDefault && !hasNamespace) {
|
|
94
|
+
imp.remove();
|
|
95
|
+
changed = true;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
project.saveSync();
|
|
101
|
+
const sourceFiles = project.getSourceFiles();
|
|
102
|
+
for (const sf of sourceFiles) {
|
|
103
|
+
const filePath = sf.getFilePath();
|
|
104
|
+
if (isInsideFolder(filePath) && sf.getFullText().trim() === "") {
|
|
105
|
+
project.removeSourceFile(sf);
|
|
106
|
+
if (existsSync(filePath)) unlinkSync(filePath);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const cleanEmptyDirs = (dir) => {
|
|
110
|
+
if (!existsSync(dir) || !statSync(dir).isDirectory()) return;
|
|
111
|
+
const files = readdirSync(dir);
|
|
112
|
+
for (const file of files) {
|
|
113
|
+
const fullPath = join(dir, file);
|
|
114
|
+
if (statSync(fullPath).isDirectory()) cleanEmptyDirs(fullPath);
|
|
115
|
+
}
|
|
116
|
+
if (readdirSync(dir).length === 0) rmdirSync(dir);
|
|
117
|
+
};
|
|
118
|
+
cleanEmptyDirs(folderPath);
|
|
119
|
+
return true;
|
|
120
|
+
};
|
|
121
|
+
//#endregion
|
|
122
|
+
export { lift };
|
|
123
|
+
|
|
124
|
+
//# sourceMappingURL=lift.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lift.js","names":[],"sources":["../../src/functions/lift.ts"],"sourcesContent":["import {\n existsSync,\n readdirSync,\n rmdirSync,\n statSync,\n unlinkSync,\n} from 'fs';\nimport { join } from 'path';\nimport { Project, Node } from 'ts-morph';\nimport { getFolderPath } from '../helpers';\n\nexport const lift = (root: string) => {\n const folderPath = getFolderPath(root);\n if (!existsSync(folderPath)) {\n console.warn(`Folder not found: ${folderPath}`);\n return false;\n }\n\n const tsconfigPath = join(process.cwd(), 'tsconfig.json');\n const project = existsSync(tsconfigPath)\n ? new Project({ tsConfigFilePath: tsconfigPath })\n : new Project();\n\n // Add all source files recursively\n project.addSourceFilesAtPaths(join(folderPath, '**/*.ts'));\n project.addSourceFilesAtPaths(join(folderPath, '**/*.tsx'));\n\n const isInsideFolder = (filePath: string) => {\n return filePath.startsWith(folderPath);\n };\n\n let changed = true;\n while (changed) {\n changed = false;\n\n // A: Declarations inside the target folder\n const declarations = project\n .getSourceFiles()\n .filter(sf => isInsideFolder(sf.getFilePath()))\n .flatMap(sf => [\n ...sf.getTypeAliases(),\n ...sf.getInterfaces(),\n ...sf.getVariableDeclarations(),\n ...sf.getFunctions(),\n ...sf.getClasses(),\n ...sf.getEnums(),\n ]);\n\n for (const decl of declarations) {\n if (decl.wasForgotten()) continue;\n\n const nameNode = decl.getNameNode();\n if (!nameNode || !Node.isIdentifier(nameNode)) continue;\n\n const referencedSymbols = nameNode.findReferences();\n let refCount = 0;\n for (const refSymbol of referencedSymbols) {\n for (const ref of refSymbol.getReferences()) {\n if (ref.getNode() !== nameNode) {\n refCount++;\n }\n }\n }\n\n if (refCount === 0) {\n decl.remove();\n changed = true;\n break;\n }\n }\n\n if (changed) continue;\n\n // B: Imports inside the target folder\n const importDeclarations = project\n .getSourceFiles()\n .filter(sf => isInsideFolder(sf.getFilePath()))\n .flatMap(sf => sf.getImportDeclarations());\n\n for (const imp of importDeclarations) {\n if (imp.wasForgotten()) continue;\n const sf = imp.getSourceFile();\n\n // 1. Named imports\n const namedImports = imp.getNamedImports();\n for (const spec of namedImports) {\n if (spec.wasForgotten()) continue;\n const nameNode = spec.getNameNode();\n if (!Node.isIdentifier(nameNode)) continue;\n\n const referencedSymbols = nameNode.findReferences();\n let refCount = 0;\n for (const refSymbol of referencedSymbols) {\n for (const ref of refSymbol.getReferences()) {\n if (\n ref.getSourceFile() === sf &&\n ref.getNode() !== nameNode\n ) {\n refCount++;\n }\n }\n }\n if (refCount === 0) {\n spec.remove();\n changed = true;\n break;\n }\n }\n if (changed) break;\n\n // 2. Default import\n const defaultImport = imp.getDefaultImport();\n if (\n defaultImport &&\n !defaultImport.wasForgotten() &&\n Node.isIdentifier(defaultImport)\n ) {\n const referencedSymbols = defaultImport.findReferences();\n let refCount = 0;\n for (const refSymbol of referencedSymbols) {\n for (const ref of refSymbol.getReferences()) {\n if (\n ref.getSourceFile() === sf &&\n ref.getNode() !== defaultImport\n ) {\n refCount++;\n }\n }\n }\n if (refCount === 0) {\n const hasNamed = imp.getNamedImports().length > 0;\n const hasNamespace = !!imp.getNamespaceImport();\n if (!hasNamed && !hasNamespace) {\n imp.remove();\n } else {\n imp.removeDefaultImport();\n }\n changed = true;\n break;\n }\n }\n if (changed) break;\n\n // 3. Namespace import\n const namespaceImport = imp.getNamespaceImport();\n if (\n namespaceImport &&\n !namespaceImport.wasForgotten() &&\n Node.isIdentifier(namespaceImport)\n ) {\n const referencedSymbols = namespaceImport.findReferences();\n let refCount = 0;\n for (const refSymbol of referencedSymbols) {\n for (const ref of refSymbol.getReferences()) {\n if (\n ref.getSourceFile() === sf &&\n ref.getNode() !== namespaceImport\n ) {\n refCount++;\n }\n }\n }\n if (refCount === 0) {\n imp.remove();\n changed = true;\n break;\n }\n }\n if (changed) break;\n\n // 4. Empty import declarations\n const hasNamed = imp.getNamedImports().length > 0;\n const hasDefault = !!imp.getDefaultImport();\n const hasNamespace = !!imp.getNamespaceImport();\n if (!hasNamed && !hasDefault && !hasNamespace) {\n imp.remove();\n changed = true;\n break;\n }\n }\n }\n\n // Save changes\n project.saveSync();\n\n // Delete files that are empty (after trimming whitespace) and inside the target folder\n const sourceFiles = project.getSourceFiles();\n for (const sf of sourceFiles) {\n const filePath = sf.getFilePath();\n if (isInsideFolder(filePath) && sf.getFullText().trim() === '') {\n project.removeSourceFile(sf);\n if (existsSync(filePath)) {\n unlinkSync(filePath);\n }\n }\n }\n\n // Helper to recursively clean empty directories\n const cleanEmptyDirs = (dir: string) => {\n if (!existsSync(dir) || !statSync(dir).isDirectory()) return;\n\n const files = readdirSync(dir);\n for (const file of files) {\n const fullPath = join(dir, file);\n if (statSync(fullPath).isDirectory()) {\n cleanEmptyDirs(fullPath);\n }\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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove.cjs","names":["transformModule","getFolderPath","PATH_PROPERTY","FILES_PROPERTY","consoleStars","path"],"sources":["../../src/functions/remove.ts"],"sourcesContent":["import edit, { JsonEditor } from 'edit-json-file';\nimport { unlinkSync } from 'fs';\nimport { join } from 'path';\nimport { FILES_PROPERTY, PATH_PROPERTY } from '../constants';\nimport {
|
|
1
|
+
{"version":3,"file":"remove.cjs","names":["transformModule","getFolderPath","PATH_PROPERTY","FILES_PROPERTY","consoleStars","path"],"sources":["../../src/functions/remove.ts"],"sourcesContent":["import edit, { JsonEditor } from 'edit-json-file';\nimport { unlinkSync } from 'fs';\nimport { join } from 'path';\nimport { FILES_PROPERTY, PATH_PROPERTY } from '../constants';\nimport { consoleStars, getFolderPath, transformModule } from '#helpers';\nimport { CodebaseAnalysis, FileAnalysis } from '../schemas';\n\nconst transformModules = (\n entries: [string, FileAnalysis][],\n ...files: string[]\n) => {\n const cwd = process.cwd();\n const out = entries\n .map(\n ([key, { imports, relativePath }]) =>\n [key, relativePath, imports] as const,\n )\n .map(([key, relativePath, imports]) => {\n const specifiers = imports\n .map(({ moduleSpecifier }) => {\n return transformModule({\n cwd,\n relativePath,\n moduleSpecifier,\n });\n })\n .map(_path => [_path, `${_path}.index`]) // Add .index variants\n .flat()\n .filter(s => files.includes(s));\n\n return [key, Array.from(new Set(specifiers))] as const;\n });\n\n return out;\n};\n\nexport const remove = (\n CODEBASE_ANALYSIS: CodebaseAnalysis,\n jsonConfigPath: string,\n ...paths: string[]\n) => {\n const isEmpty = paths.length === 0;\n if (isEmpty) return console.warn('No files specified for removal.');\n try {\n const cwd = process.cwd();\n const json = join(cwd, jsonConfigPath);\n let file: JsonEditor | undefined = edit(json);\n\n if (!file) return;\n\n const root = getFolderPath(file.get(PATH_PROPERTY) as string);\n const files = file.get(FILES_PROPERTY) as string[];\n\n const entries2 = Object.entries(CODEBASE_ANALYSIS).filter(([key]) =>\n files.includes(key),\n );\n\n const entries = entries2.filter(([key]) =>\n paths.some(p => key.startsWith(p)),\n );\n\n // Check dependencies before deletion\n const safesToRemove: string[] = [];\n const cannotsRemove: [string, string[]][] = [];\n\n entries.forEach(([key]) => {\n const modules = transformModules(entries2, ...files);\n const importedFroms = modules\n .filter(([, specifiers]) => specifiers.includes(key))\n .map(([k]) => k);\n\n const check = importedFroms.length > 0;\n\n console.log('modules', '=>', importedFroms);\n console.log('key', '=>', key);\n\n if (check) return cannotsRemove.push([key, importedFroms]);\n return safesToRemove.push(key);\n });\n\n consoleStars();\n console.log(`๐ง Deletion of files (${entries.length} files)...`);\n\n // Display files that cannot be deleted\n if (cannotsRemove.length > 0) {\n const len = cannotsRemove.length;\n const one = 'file cannot be deleted (imported in other files)';\n const many = 'files cannot be deleted (imported in other files)';\n\n console.warn(`โ ๏ธ ${len} ${len === 1 ? one : many} :`);\n cannotsRemove.forEach(([key, modules]) => {\n console.warn(` - โ ๏ธ ${key} imported by:`);\n modules.forEach(m => console.warn(` -> ๐ ${m}`));\n });\n }\n\n if (safesToRemove.length === 0) {\n console.warn('โ No files can be deleted.');\n return consoleStars();\n }\n\n const formatteds = safesToRemove.map(key => {\n const _path = CODEBASE_ANALYSIS[key].relativePath;\n const absolute = join(root, _path);\n return [key, absolute] as const;\n });\n\n console.log(`๐๏ธ Deleting files (${safesToRemove.length} files)...`);\n\n let success = 0;\n const length = formatteds.length;\n\n formatteds.forEach(([key, path]) => {\n try {\n unlinkSync(path);\n console.log(` - ๐๏ธ ${key}`);\n file?.set(\n FILES_PROPERTY,\n files.filter(key1 => key1 !== key),\n );\n success++;\n } catch {\n console.error(` - โ Error, ${key} :`);\n }\n });\n\n file.save();\n console.log(`๐๏ธ Files deleted! (${success}/${length})`);\n file = undefined;\n } catch {\n console.error(`โ Error while deleting files`);\n consoleStars();\n return false;\n }\n consoleStars();\n return true;\n};\n"],"mappings":";;;;;;;;;AAOA,MAAM,oBACJ,SACA,GAAG,UACA;CACH,MAAM,MAAM,QAAQ,IAAI;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ACN,QAAQ,MAAM,8BAA8B;EAC5C,gBAAA,aAAa;EACb,OAAO;CACT;CACA,gBAAA,aAAa;CACb,OAAO;AACT"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove.d.ts","sourceRoot":"","sources":["../../src/functions/remove.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"remove.d.ts","sourceRoot":"","sources":["../../src/functions/remove.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,gBAAgB,EAAgB,MAAM,YAAY,CAAC;AA+B5D,eAAO,MAAM,MAAM,GACjB,mBAAmB,gBAAgB,EACnC,gBAAgB,MAAM,EACtB,GAAG,OAAO,MAAM,EAAE,mBAiGnB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove.js","names":[],"sources":["../../src/functions/remove.ts"],"sourcesContent":["import edit, { JsonEditor } from 'edit-json-file';\nimport { unlinkSync } from 'fs';\nimport { join } from 'path';\nimport { FILES_PROPERTY, PATH_PROPERTY } from '../constants';\nimport {
|
|
1
|
+
{"version":3,"file":"remove.js","names":[],"sources":["../../src/functions/remove.ts"],"sourcesContent":["import edit, { JsonEditor } from 'edit-json-file';\nimport { unlinkSync } from 'fs';\nimport { join } from 'path';\nimport { FILES_PROPERTY, PATH_PROPERTY } from '../constants';\nimport { consoleStars, getFolderPath, transformModule } from '#helpers';\nimport { CodebaseAnalysis, FileAnalysis } from '../schemas';\n\nconst transformModules = (\n entries: [string, FileAnalysis][],\n ...files: string[]\n) => {\n const cwd = process.cwd();\n const out = entries\n .map(\n ([key, { imports, relativePath }]) =>\n [key, relativePath, imports] as const,\n )\n .map(([key, relativePath, imports]) => {\n const specifiers = imports\n .map(({ moduleSpecifier }) => {\n return transformModule({\n cwd,\n relativePath,\n moduleSpecifier,\n });\n })\n .map(_path => [_path, `${_path}.index`]) // Add .index variants\n .flat()\n .filter(s => files.includes(s));\n\n return [key, Array.from(new Set(specifiers))] as const;\n });\n\n return out;\n};\n\nexport const remove = (\n CODEBASE_ANALYSIS: CodebaseAnalysis,\n jsonConfigPath: string,\n ...paths: string[]\n) => {\n const isEmpty = paths.length === 0;\n if (isEmpty) return console.warn('No files specified for removal.');\n try {\n const cwd = process.cwd();\n const json = join(cwd, jsonConfigPath);\n let file: JsonEditor | undefined = edit(json);\n\n if (!file) return;\n\n const root = getFolderPath(file.get(PATH_PROPERTY) as string);\n const files = file.get(FILES_PROPERTY) as string[];\n\n const entries2 = Object.entries(CODEBASE_ANALYSIS).filter(([key]) =>\n files.includes(key),\n );\n\n const entries = entries2.filter(([key]) =>\n paths.some(p => key.startsWith(p)),\n );\n\n // Check dependencies before deletion\n const safesToRemove: string[] = [];\n const cannotsRemove: [string, string[]][] = [];\n\n entries.forEach(([key]) => {\n const modules = transformModules(entries2, ...files);\n const importedFroms = modules\n .filter(([, specifiers]) => specifiers.includes(key))\n .map(([k]) => k);\n\n const check = importedFroms.length > 0;\n\n console.log('modules', '=>', importedFroms);\n console.log('key', '=>', key);\n\n if (check) return cannotsRemove.push([key, importedFroms]);\n return safesToRemove.push(key);\n });\n\n consoleStars();\n console.log(`๐ง Deletion of files (${entries.length} files)...`);\n\n // Display files that cannot be deleted\n if (cannotsRemove.length > 0) {\n const len = cannotsRemove.length;\n const one = 'file cannot be deleted (imported in other files)';\n const many = 'files cannot be deleted (imported in other files)';\n\n console.warn(`โ ๏ธ ${len} ${len === 1 ? one : many} :`);\n cannotsRemove.forEach(([key, modules]) => {\n console.warn(` - โ ๏ธ ${key} imported by:`);\n modules.forEach(m => console.warn(` -> ๐ ${m}`));\n });\n }\n\n if (safesToRemove.length === 0) {\n console.warn('โ No files can be deleted.');\n return consoleStars();\n }\n\n const formatteds = safesToRemove.map(key => {\n const _path = CODEBASE_ANALYSIS[key].relativePath;\n const absolute = join(root, _path);\n return [key, absolute] as const;\n });\n\n console.log(`๐๏ธ Deleting files (${safesToRemove.length} files)...`);\n\n let success = 0;\n const length = formatteds.length;\n\n formatteds.forEach(([key, path]) => {\n try {\n unlinkSync(path);\n console.log(` - ๐๏ธ ${key}`);\n file?.set(\n FILES_PROPERTY,\n files.filter(key1 => key1 !== key),\n );\n success++;\n } catch {\n console.error(` - โ Error, ${key} :`);\n }\n });\n\n file.save();\n console.log(`๐๏ธ Files deleted! (${success}/${length})`);\n file = undefined;\n } catch {\n console.error(`โ Error while deleting files`);\n consoleStars();\n return false;\n }\n consoleStars();\n return true;\n};\n"],"mappings":";;;;;;AAOA,MAAM,oBACJ,SACA,GAAG,UACA;CACH,MAAM,MAAM,QAAQ,IAAI;CAsBxB,OArBY,QACT,KACE,CAAC,KAAK,EAAE,SAAS,oBAChB;EAAC;EAAK;EAAc;CAAO,CAC/B,CAAC,CACA,KAAK,CAAC,KAAK,cAAc,aAAa;EACrC,MAAM,aAAa,QAChB,KAAK,EAAE,sBAAsB;GAC5B,OAAO,gBAAgB;IACrB;IACA;IACA;GACF,CAAC;EACH,CAAC,CAAC,CACD,KAAI,UAAS,CAAC,OAAO,GAAG,MAAM,OAAO,CAAC,CAAC,CACvC,KAAK,CAAC,CACN,QAAO,MAAK,MAAM,SAAS,CAAC,CAAC;EAEhC,OAAO,CAAC,KAAK,MAAM,KAAK,IAAI,IAAI,UAAU,CAAC,CAAC;CAC9C,CAEO;AACX;AAEA,MAAa,UACX,mBACA,gBACA,GAAG,UACA;CAEH,IADgB,MAAM,WAAW,GACpB,OAAO,QAAQ,KAAK,iCAAiC;CAClE,IAAI;EAGF,IAAI,OAA+B,KADtB,KADD,QAAQ,IACA,GAAG,cACoB,CAAC;EAE5C,IAAI,CAAC,MAAM;EAEX,MAAM,OAAO,cAAc,KAAK,IAAI,aAAa,CAAW;EAC5D,MAAM,QAAQ,KAAK,IAAI,cAAc;EAErC,MAAM,WAAW,OAAO,QAAQ,iBAAiB,CAAC,CAAC,QAAQ,CAAC,SAC1D,MAAM,SAAS,GAAG,CACpB;EAEA,MAAM,UAAU,SAAS,QAAQ,CAAC,SAChC,MAAM,MAAK,MAAK,IA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,QAAO,SAAQ,SAAS,GAAG,CACnC;IACA;GACF,QAAQ;IACN,QAAQ,MAAM,gBAAgB,IAAI,GAAG;GACvC;EACF,CAAC;EAED,KAAK,KAAK;EACV,QAAQ,IAAI,uBAAuB,QAAQ,GAAG,OAAO,EAAE;EACvD,OAAO,KAAA;CACT,QAAQ;EACN,QAAQ,MAAM,8BAA8B;EAC5C,aAAa;EACb,OAAO;CACT;CACA,aAAa;CACb,OAAO;AACT"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_constants = require("../constants.cjs");
|
|
3
|
+
const require_helpers = require("../helpers.cjs");
|
|
4
|
+
let path = require("path");
|
|
5
|
+
let fs = require("fs");
|
|
6
|
+
//#region src/functions/softInit.ts
|
|
7
|
+
const softInit = (CODEBASE_ANALYSIS, { root, json, path: path$1 = require_constants.DEFAULT_PATH_KEY, bin = require_constants.DEFAULT_CLI_NAME }) => {
|
|
8
|
+
const cwd = process.cwd();
|
|
9
|
+
if (!(0, fs.existsSync)((0, path.join)(cwd, json))) return false;
|
|
10
|
+
const folderPath = require_helpers.getFolderPath(root);
|
|
11
|
+
if (!(0, fs.existsSync)(folderPath)) return false;
|
|
12
|
+
const tsconfigPath = (0, path.join)(cwd, "tsconfig.json");
|
|
13
|
+
if (!(0, fs.existsSync)(tsconfigPath)) return false;
|
|
14
|
+
const tsconfigContent = (0, fs.readFileSync)(tsconfigPath, "utf8");
|
|
15
|
+
const tsconfig = JSON.parse(tsconfigContent);
|
|
16
|
+
const relativePath = (0, path.relative)(process.cwd(), folderPath);
|
|
17
|
+
if (!(tsconfig?.compilerOptions?.paths?.[path$1]?.[0] === `./${relativePath}/*`)) return false;
|
|
18
|
+
try {
|
|
19
|
+
require_helpers.createTypesStructure(folderPath, CODEBASE_ANALYSIS);
|
|
20
|
+
} catch {
|
|
21
|
+
console.error(`โ Error creating the types structure:`);
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
console.log(`๐ ${bin} soft initialization completed successfully!`);
|
|
25
|
+
return true;
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
exports.softInit = softInit;
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=softInit.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"softInit.cjs","names":["DEFAULT_PATH_KEY","DEFAULT_CLI_NAME","getFolderPath","path"],"sources":["../../src/functions/softInit.ts"],"sourcesContent":["import { existsSync, readFileSync } from 'fs';\nimport { join, relative } from 'path';\nimport { DEFAULT_CLI_NAME, DEFAULT_PATH_KEY } from '../constants';\nimport { createTypesStructure, getFolderPath } from '../helpers';\nimport { CodebaseAnalysis } from '../schemas';\n\nexport interface InitOptions {\n /**\n * Custom location for the .bemedev folder\n * Default: 'src/.bemedev' if src exists, otherwise '.bemedev' at the root\n */\n root: string;\n json: string;\n path?: string;\n bin?: string;\n}\n\nexport const softInit = (\n CODEBASE_ANALYSIS: CodebaseAnalysis,\n {\n root,\n json,\n path = DEFAULT_PATH_KEY,\n bin = DEFAULT_CLI_NAME,\n }: InitOptions,\n) => {\n const cwd = process.cwd();\n const configFile = join(cwd, json);\n const configExists = existsSync(configFile);\n if (!configExists) return false;\n\n const folderPath = getFolderPath(root);\n if (!existsSync(folderPath)) return false;\n\n const tsconfigPath = join(cwd, 'tsconfig.json');\n const tsConfigExists = existsSync(tsconfigPath);\n if (!tsConfigExists) return false;\n\n const tsconfigContent = readFileSync(tsconfigPath, 'utf8');\n const tsconfig = JSON.parse(tsconfigContent);\n const relativePath = relative(process.cwd(), folderPath);\n\n const checkPaths =\n tsconfig?.compilerOptions?.paths?.[path]?.[0] ===\n `./${relativePath}/*`;\n\n if (!checkPaths) return false;\n\n try {\n createTypesStructure(folderPath, CODEBASE_ANALYSIS);\n } catch {\n console.error(`โ Error creating the types structure:`);\n return false;\n }\n\n // 3. Create the .bemedev.json file at the root\n\n console.log(`๐ ${bin} soft initialization completed successfully!`);\n return true;\n};\n"],"mappings":";;;;;;AAiBA,MAAa,YACX,mBACA,EACE,MACA,MACA,MAAA,SAAOA,kBAAAA,kBACP,MAAMC,kBAAAA,uBAEL;CACH,MAAM,MAAM,QAAQ,IAAI;CAGxB,IAAI,EAAA,GAAA,GAAA,WAAA,EAAA,GAAA,KAAA,KAAA,CAFoB,KAAK,IACY,CACzB,GAAG,OAAO;CAE1B,MAAM,aAAaC,gBAAAA,cAAc,IAAI;CACrC,IAAI,EAAA,GAAA,GAAA,WAAA,CAAY,UAAU,GAAG,OAAO;CAEpC,MAAM,gBAAA,GAAA,KAAA,KAAA,CAAoB,KAAK,eAAe;CAE9C,IAAI,EAAA,GAAA,GAAA,WAAA,CAD8B,YAChB,GAAG,OAAO;CAE5B,MAAM,mBAAA,GAAA,GAAA,aAAA,CAA+B,cAAc,MAAM;CACzD,MAAM,WAAW,KAAK,MAAM,eAAe;CAC3C,MAAM,gBAAA,GAAA,KAAA,SAAA,CAAwB,QAAQ,IAAI,GAAG,UAAU;CAMvD,IAAI,EAHF,UAAU,iBAAiB,QAAQC,OAAK,GAAG,OAC3C,KAAK,aAAa,MAEH,OAAO;CAExB,IAAI;EACF,gBAAA,qBAAqB,YAAY,iBAAiB;CACpD,QAAQ;EACN,QAAQ,MAAM,uCAAuC;EACrD,OAAO;CACT;CAIA,QAAQ,IAAI,MAAM,IAAI,6CAA6C;CACnE,OAAO;AACT"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CodebaseAnalysis } from '../schemas';
|
|
2
|
+
export interface InitOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Custom location for the .bemedev folder
|
|
5
|
+
* Default: 'src/.bemedev' if src exists, otherwise '.bemedev' at the root
|
|
6
|
+
*/
|
|
7
|
+
root: string;
|
|
8
|
+
json: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
bin?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare const softInit: (CODEBASE_ANALYSIS: CodebaseAnalysis, { root, json, path, bin, }: InitOptions) => boolean;
|
|
13
|
+
//# sourceMappingURL=softInit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"softInit.d.ts","sourceRoot":"","sources":["../../src/functions/softInit.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,eAAO,MAAM,QAAQ,GACnB,mBAAmB,gBAAgB,EACnC,4BAKG,WAAW,YAmCf,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DEFAULT_CLI_NAME, DEFAULT_PATH_KEY } from "../constants.js";
|
|
2
|
+
import { createTypesStructure, getFolderPath } from "../helpers.js";
|
|
3
|
+
import { join, relative } from "path";
|
|
4
|
+
import { existsSync, readFileSync } from "fs";
|
|
5
|
+
//#region src/functions/softInit.ts
|
|
6
|
+
const softInit = (CODEBASE_ANALYSIS, { root, json, path = DEFAULT_PATH_KEY, bin = DEFAULT_CLI_NAME }) => {
|
|
7
|
+
const cwd = process.cwd();
|
|
8
|
+
if (!existsSync(join(cwd, json))) return false;
|
|
9
|
+
const folderPath = getFolderPath(root);
|
|
10
|
+
if (!existsSync(folderPath)) return false;
|
|
11
|
+
const tsconfigPath = join(cwd, "tsconfig.json");
|
|
12
|
+
if (!existsSync(tsconfigPath)) return false;
|
|
13
|
+
const tsconfigContent = readFileSync(tsconfigPath, "utf8");
|
|
14
|
+
const tsconfig = JSON.parse(tsconfigContent);
|
|
15
|
+
const relativePath = relative(process.cwd(), folderPath);
|
|
16
|
+
if (!(tsconfig?.compilerOptions?.paths?.[path]?.[0] === `./${relativePath}/*`)) return false;
|
|
17
|
+
try {
|
|
18
|
+
createTypesStructure(folderPath, CODEBASE_ANALYSIS);
|
|
19
|
+
} catch {
|
|
20
|
+
console.error(`โ Error creating the types structure:`);
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
console.log(`๐ ${bin} soft initialization completed successfully!`);
|
|
24
|
+
return true;
|
|
25
|
+
};
|
|
26
|
+
//#endregion
|
|
27
|
+
export { softInit };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=softInit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"softInit.js","names":[],"sources":["../../src/functions/softInit.ts"],"sourcesContent":["import { existsSync, readFileSync } from 'fs';\nimport { join, relative } from 'path';\nimport { DEFAULT_CLI_NAME, DEFAULT_PATH_KEY } from '../constants';\nimport { createTypesStructure, getFolderPath } from '../helpers';\nimport { CodebaseAnalysis } from '../schemas';\n\nexport interface InitOptions {\n /**\n * Custom location for the .bemedev folder\n * Default: 'src/.bemedev' if src exists, otherwise '.bemedev' at the root\n */\n root: string;\n json: string;\n path?: string;\n bin?: string;\n}\n\nexport const softInit = (\n CODEBASE_ANALYSIS: CodebaseAnalysis,\n {\n root,\n json,\n path = DEFAULT_PATH_KEY,\n bin = DEFAULT_CLI_NAME,\n }: InitOptions,\n) => {\n const cwd = process.cwd();\n const configFile = join(cwd, json);\n const configExists = existsSync(configFile);\n if (!configExists) return false;\n\n const folderPath = getFolderPath(root);\n if (!existsSync(folderPath)) return false;\n\n const tsconfigPath = join(cwd, 'tsconfig.json');\n const tsConfigExists = existsSync(tsconfigPath);\n if (!tsConfigExists) return false;\n\n const tsconfigContent = readFileSync(tsconfigPath, 'utf8');\n const tsconfig = JSON.parse(tsconfigContent);\n const relativePath = relative(process.cwd(), folderPath);\n\n const checkPaths =\n tsconfig?.compilerOptions?.paths?.[path]?.[0] ===\n `./${relativePath}/*`;\n\n if (!checkPaths) return false;\n\n try {\n createTypesStructure(folderPath, CODEBASE_ANALYSIS);\n } catch {\n console.error(`โ Error creating the types structure:`);\n return false;\n }\n\n // 3. Create the .bemedev.json file at the root\n\n console.log(`๐ ${bin} soft initialization completed successfully!`);\n return true;\n};\n"],"mappings":";;;;;AAiBA,MAAa,YACX,mBACA,EACE,MACA,MACA,OAAO,kBACP,MAAM,uBAEL;CACH,MAAM,MAAM,QAAQ,IAAI;CAGxB,IAAI,CADiB,WADF,KAAK,KAAK,IACY,CACzB,GAAG,OAAO;CAE1B,MAAM,aAAa,cAAc,IAAI;CACrC,IAAI,CAAC,WAAW,UAAU,GAAG,OAAO;CAEpC,MAAM,eAAe,KAAK,KAAK,eAAe;CAE9C,IAAI,CADmB,WAAW,YAChB,GAAG,OAAO;CAE5B,MAAM,kBAAkB,aAAa,cAAc,MAAM;CACzD,MAAM,WAAW,KAAK,MAAM,eAAe;CAC3C,MAAM,eAAe,SAAS,QAAQ,IAAI,GAAG,UAAU;CAMvD,IAAI,EAHF,UAAU,iBAAiB,QAAQ,KAAK,GAAG,OAC3C,KAAK,aAAa,MAEH,OAAO;CAExB,IAAI;EACF,qBAAqB,YAAY,iBAAiB;CACpD,QAAQ;EACN,QAAQ,MAAM,uCAAuC;EACrD,OAAO;CACT;CAIA,QAAQ,IAAI,MAAM,IAAI,6CAA6C;CACnE,OAAO;AACT"}
|
package/lib/helpers.cjs
CHANGED
|
@@ -41,8 +41,24 @@ const getFolderPath = (root) => {
|
|
|
41
41
|
const cwd = process.cwd();
|
|
42
42
|
return (0, fs.existsSync)((0, path.join)(cwd, "src")) ? (0, path.join)(cwd, "src", root) : (0, path.join)(cwd, root);
|
|
43
43
|
};
|
|
44
|
+
const createTypesStructure = (folderPath, CODEBASE_ANALYSIS) => {
|
|
45
|
+
const entries = Object.entries(CODEBASE_ANALYSIS).filter(([key]) => {
|
|
46
|
+
return key.endsWith("types") || key.endsWith("constants");
|
|
47
|
+
});
|
|
48
|
+
const PATHS = [];
|
|
49
|
+
console.log(`๐ง Creating types structure (${entries.length} files)...`);
|
|
50
|
+
for (const [, fileAnalysis] of entries) try {
|
|
51
|
+
const file = writeFileAnalysis(fileAnalysis, folderPath);
|
|
52
|
+
if (file) PATHS.push(file);
|
|
53
|
+
} catch {
|
|
54
|
+
console.error(`โ Error creating the file ${fileAnalysis.relativePath}:`);
|
|
55
|
+
}
|
|
56
|
+
console.log(`โ
Types structure successfully created!`);
|
|
57
|
+
return PATHS;
|
|
58
|
+
};
|
|
44
59
|
//#endregion
|
|
45
60
|
exports.consoleStars = consoleStars;
|
|
61
|
+
exports.createTypesStructure = createTypesStructure;
|
|
46
62
|
exports.getFolderPath = getFolderPath;
|
|
47
63
|
exports.pathToJsonKey = pathToJsonKey;
|
|
48
64
|
exports.toArray = toArray;
|
package/lib/helpers.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.cjs","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { dirname, join, relative, resolve, parse } from '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 );\n\n return out;\n};\n\nexport const pathToJsonKey = (relativePath: string, name: string) => {\n const parts = relativePath.split('/');\n parts.pop();\n parts.push(name);\n return parts.join('/');\n};\n\nexport const writeFileAnalysis = (\n fileAnalysis: FileAnalysis,\n folderPath: string,\n) => {\n const relativePath = fileAnalysis.relativePath;\n // Create the destination path in root maintaining the structure\n const destPath = join(folderPath, relativePath);\n const parsed = parse(destPath);\n\n try {\n // Create the destination folder if necessary\n mkdirSync(parsed.dir, { recursive: true });\n let fileContent = fileAnalysis.text;\n\n REPLACERS.init.forEach(([search, replace]) => {\n fileContent = fileContent.replaceAll(search, replace);\n });\n\n // Write the types file content\n writeFileSync(destPath, fileContent, 'utf8');\n console.log(` โ
${relativePath}`);\n return pathToJsonKey(relativePath, parsed.name);\n } catch (error) {\n return console.error(` โ Error for ${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\n ? join(cwd, 'src', root)\n : join(cwd, root);\n\n return folderPath;\n};\n"],"mappings":";;;;;AAWA,MAAa,mBAAmB,EAC9B,MAAM,QAAQ,IAAI,GAClB,cACA,sBACyB;CAMzB,QAAA,GAAA,KAAA,SAAA,CAJE,MAAA,GAAA,KAAA,QAAA,EAAA,GAAA,KAAA,QAAA,CACgB,YAAY,GAAG,eAAe,CAGvC;AACX;AAEA,MAAa,iBAAiB,cAAsB,SAAiB;CACnE,MAAM,QAAQ,aAAa,MAAM,GAAG;CACpC,MAAM,IAAI;CACV,MAAM,KAAK,IAAI;CACf,OAAO,MAAM,KAAK,GAAG;AACvB;AAEA,MAAa,qBACX,cACA,eACG;CACH,MAAM,eAAe,aAAa;CAElC,MAAM,YAAA,GAAA,KAAA,KAAA,CAAgB,YAAY,YAAY;CAC9C,MAAM,UAAA,GAAA,KAAA,MAAA,CAAe,QAAQ;CAE7B,IAAI;EAEF,CAAA,GAAA,GAAA,UAAA,CAAU,OAAO,KAAK,EAAE,WAAW,KAAK,CAAC;EACzC,IAAI,cAAc,aAAa;EAE/B,kBAAA,UAAU,KAAK,SAAS,CAAC,QAAQ,aAAa;GAC5C,cAAc,YAAY,WAAW,QAAQ,OAAO;EACtD,CAAC;EAGD,CAAA,GAAA,GAAA,cAAA,CAAc,UAAU,aAAa,MAAM;EAC3C,QAAQ,IAAI,OAAO,cAAc;EACjC,OAAO,cAAc,cAAc,OAAO,IAAI;CAChD,SAAS,OAAO;EACd,OAAO,QAAQ,MAAM,iBAAiB,aAAa,IAAI,KAAK;CAC9D;AACF;AAEA,MAAa,qBAAqB;CAChC,QAAQ,IAAI;CACZ,QAAQ,IAAI,IAAI,OAAO,EAAE,CAAC;CAC1B,QAAQ,IAAI;AACd;AAEA,MAAa,WAAc,UAAyB;CAClD,OAAO,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK;AAC5D;AAEA,MAAa,iBAAiB,SAAiB;CAC7C,MAAM,MAAM,QAAQ,IAAI;CAMxB,QAAA,GAAA,GAAA,WAAA,EAAA,GAAA,KAAA,KAAA,CALkC,KAAK,KAAK,CACjB,KAAA,GAAA,KAAA,KAAA,CAClB,KAAK,OAAO,IAAI,KAAA,GAAA,KAAA,KAAA,CAChB,KAAK,IAAI;AAGpB"}
|
|
1
|
+
{"version":3,"file":"helpers.cjs","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { dirname, join, relative, resolve, parse } from 'path';\nimport { REPLACERS } from './constants';\nimport { FileAnalysis, type CodebaseAnalysis } 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 );\n\n return out;\n};\n\nexport const pathToJsonKey = (relativePath: string, name: string) => {\n const parts = relativePath.split('/');\n parts.pop();\n parts.push(name);\n return parts.join('/');\n};\n\nexport const writeFileAnalysis = (\n fileAnalysis: FileAnalysis,\n folderPath: string,\n) => {\n const relativePath = fileAnalysis.relativePath;\n // Create the destination path in root maintaining the structure\n const destPath = join(folderPath, relativePath);\n const parsed = parse(destPath);\n\n try {\n // Create the destination folder if necessary\n mkdirSync(parsed.dir, { recursive: true });\n let fileContent = fileAnalysis.text;\n\n REPLACERS.init.forEach(([search, replace]) => {\n fileContent = fileContent.replaceAll(search, replace);\n });\n\n // Write the types file content\n writeFileSync(destPath, fileContent, 'utf8');\n console.log(` โ
${relativePath}`);\n return pathToJsonKey(relativePath, parsed.name);\n } catch (error) {\n return console.error(` โ Error for ${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\n ? join(cwd, 'src', root)\n : join(cwd, root);\n\n return folderPath;\n};\n\nexport const createTypesStructure = (\n folderPath: string,\n CODEBASE_ANALYSIS: CodebaseAnalysis,\n) => {\n const entries = Object.entries(CODEBASE_ANALYSIS).filter(([key]) => {\n return key.endsWith('types') || key.endsWith('constants');\n });\n\n const PATHS: string[] = [];\n\n console.log(\n `๐ง Creating types structure (${entries.length} files)...`,\n );\n\n for (const [, fileAnalysis] of entries) {\n try {\n const file = writeFileAnalysis(fileAnalysis, folderPath);\n if (file) PATHS.push(file);\n } catch {\n console.error(\n `โ Error creating the file ${fileAnalysis.relativePath}:`,\n );\n }\n }\n\n console.log(`โ
Types structure successfully created!`);\n return PATHS;\n};\n"],"mappings":";;;;;AAWA,MAAa,mBAAmB,EAC9B,MAAM,QAAQ,IAAI,GAClB,cACA,sBACyB;CAMzB,QAAA,GAAA,KAAA,SAAA,CAJE,MAAA,GAAA,KAAA,QAAA,EAAA,GAAA,KAAA,QAAA,CACgB,YAAY,GAAG,eAAe,CAGvC;AACX;AAEA,MAAa,iBAAiB,cAAsB,SAAiB;CACnE,MAAM,QAAQ,aAAa,MAAM,GAAG;CACpC,MAAM,IAAI;CACV,MAAM,KAAK,IAAI;CACf,OAAO,MAAM,KAAK,GAAG;AACvB;AAEA,MAAa,qBACX,cACA,eACG;CACH,MAAM,eAAe,aA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,MAAM,GAAG,iBAAiB,SAC7B,IAAI;EACF,MAAM,OAAO,kBAAkB,cAAc,UAAU;EACvD,IAAI,MAAM,MAAM,KAAK,IAAI;CAC3B,QAAQ;EACN,QAAQ,MACN,6BAA6B,aAAa,aAAa,EACzD;CACF;CAGF,QAAQ,IAAI,yCAAyC;CACrD,OAAO;AACT"}
|
package/lib/helpers.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FileAnalysis } from './schemas';
|
|
1
|
+
import { FileAnalysis, type CodebaseAnalysis } from './schemas';
|
|
2
2
|
export type TransformModuleArgs = {
|
|
3
3
|
cwd?: string;
|
|
4
4
|
relativePath: string;
|
|
@@ -10,4 +10,5 @@ export declare const writeFileAnalysis: (fileAnalysis: FileAnalysis, folderPath:
|
|
|
10
10
|
export declare const consoleStars: () => void;
|
|
11
11
|
export declare const toArray: <T>(value?: T | T[]) => T[];
|
|
12
12
|
export declare const getFolderPath: (root: string) => string;
|
|
13
|
+
export declare const createTypesStructure: (folderPath: string, CODEBASE_ANALYSIS: CodebaseAnalysis) => string[];
|
|
13
14
|
//# sourceMappingURL=helpers.d.ts.map
|
package/lib/helpers.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEhE,MAAM,MAAM,mBAAmB,GAAG;IAChC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,yCAI7B,mBAAmB,WAOrB,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,cAAc,MAAM,EAAE,MAAM,MAAM,WAK/D,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAC5B,cAAc,YAAY,EAC1B,YAAY,MAAM,kBAuBnB,CAAC;AAEF,eAAO,MAAM,YAAY,YAIxB,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,KAAG,CAAC,EAE7C,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,WAQzC,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAC/B,YAAY,MAAM,EAClB,mBAAmB,gBAAgB,aAyBpC,CAAC"}
|
package/lib/helpers.js
CHANGED
|
@@ -40,7 +40,22 @@ const getFolderPath = (root) => {
|
|
|
40
40
|
const cwd = process.cwd();
|
|
41
41
|
return existsSync(join(cwd, "src")) ? join(cwd, "src", root) : join(cwd, root);
|
|
42
42
|
};
|
|
43
|
+
const createTypesStructure = (folderPath, CODEBASE_ANALYSIS) => {
|
|
44
|
+
const entries = Object.entries(CODEBASE_ANALYSIS).filter(([key]) => {
|
|
45
|
+
return key.endsWith("types") || key.endsWith("constants");
|
|
46
|
+
});
|
|
47
|
+
const PATHS = [];
|
|
48
|
+
console.log(`๐ง Creating types structure (${entries.length} files)...`);
|
|
49
|
+
for (const [, fileAnalysis] of entries) try {
|
|
50
|
+
const file = writeFileAnalysis(fileAnalysis, folderPath);
|
|
51
|
+
if (file) PATHS.push(file);
|
|
52
|
+
} catch {
|
|
53
|
+
console.error(`โ Error creating the file ${fileAnalysis.relativePath}:`);
|
|
54
|
+
}
|
|
55
|
+
console.log(`โ
Types structure successfully created!`);
|
|
56
|
+
return PATHS;
|
|
57
|
+
};
|
|
43
58
|
//#endregion
|
|
44
|
-
export { consoleStars, getFolderPath, pathToJsonKey, toArray, transformModule, writeFileAnalysis };
|
|
59
|
+
export { consoleStars, createTypesStructure, getFolderPath, pathToJsonKey, toArray, transformModule, writeFileAnalysis };
|
|
45
60
|
|
|
46
61
|
//# sourceMappingURL=helpers.js.map
|
package/lib/helpers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { dirname, join, relative, resolve, parse } from '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 );\n\n return out;\n};\n\nexport const pathToJsonKey = (relativePath: string, name: string) => {\n const parts = relativePath.split('/');\n parts.pop();\n parts.push(name);\n return parts.join('/');\n};\n\nexport const writeFileAnalysis = (\n fileAnalysis: FileAnalysis,\n folderPath: string,\n) => {\n const relativePath = fileAnalysis.relativePath;\n // Create the destination path in root maintaining the structure\n const destPath = join(folderPath, relativePath);\n const parsed = parse(destPath);\n\n try {\n // Create the destination folder if necessary\n mkdirSync(parsed.dir, { recursive: true });\n let fileContent = fileAnalysis.text;\n\n REPLACERS.init.forEach(([search, replace]) => {\n fileContent = fileContent.replaceAll(search, replace);\n });\n\n // Write the types file content\n writeFileSync(destPath, fileContent, 'utf8');\n console.log(` โ
${relativePath}`);\n return pathToJsonKey(relativePath, parsed.name);\n } catch (error) {\n return console.error(` โ Error for ${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\n ? join(cwd, 'src', root)\n : join(cwd, root);\n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"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { dirname, join, relative, resolve, parse } from 'path';\nimport { REPLACERS } from './constants';\nimport { FileAnalysis, type CodebaseAnalysis } 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 );\n\n return out;\n};\n\nexport const pathToJsonKey = (relativePath: string, name: string) => {\n const parts = relativePath.split('/');\n parts.pop();\n parts.push(name);\n return parts.join('/');\n};\n\nexport const writeFileAnalysis = (\n fileAnalysis: FileAnalysis,\n folderPath: string,\n) => {\n const relativePath = fileAnalysis.relativePath;\n // Create the destination path in root maintaining the structure\n const destPath = join(folderPath, relativePath);\n const parsed = parse(destPath);\n\n try {\n // Create the destination folder if necessary\n mkdirSync(parsed.dir, { recursive: true });\n let fileContent = fileAnalysis.text;\n\n REPLACERS.init.forEach(([search, replace]) => {\n fileContent = fileContent.replaceAll(search, replace);\n });\n\n // Write the types file content\n writeFileSync(destPath, fileContent, 'utf8');\n console.log(` โ
${relativePath}`);\n return pathToJsonKey(relativePath, parsed.name);\n } catch (error) {\n return console.error(` โ Error for ${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\n ? join(cwd, 'src', root)\n : join(cwd, root);\n\n return folderPath;\n};\n\nexport const createTypesStructure = (\n folderPath: string,\n CODEBASE_ANALYSIS: CodebaseAnalysis,\n) => {\n const entries = Object.entries(CODEBASE_ANALYSIS).filter(([key]) => {\n return key.endsWith('types') || key.endsWith('constants');\n });\n\n const PATHS: string[] = [];\n\n console.log(\n `๐ง Creating types structure (${entries.length} files)...`,\n );\n\n for (const [, fileAnalysis] of entries) {\n try {\n const file = writeFileAnalysis(fileAnalysis, folderPath);\n if (file) PATHS.push(file);\n } catch {\n console.error(\n `โ Error creating the file ${fileAnalysis.relativePath}:`,\n );\n }\n }\n\n console.log(`โ
Types structure successfully created!`);\n return PATHS;\n};\n"],"mappings":";;;;AAWA,MAAa,mBAAmB,EAC9B,MAAM,QAAQ,IAAI,GAClB,cACA,sBACyB;CAMzB,OALY,SACV,KACA,QAAQ,QAAQ,YAAY,GAAG,eAAe,CAGvC;AACX;AAEA,MAAa,iBAAiB,cAAsB,SAAiB;CACnE,MAAM,QAAQ,aAAa,MAAM,GAAG;CACpC,MAAM,IAAI;CACV,MAAM,KAAK,IAAI;CACf,OAAO,MAAM,KAAK,GAAG;AACvB;AAEA,MAAa,qBACX,cACA,eACG;CACH,MAAM,eAAe,aAAa;CAElC,MAAM,WAAW,KAAK,YAAY,YAAY;CAC9C,MAAM,SAAS,MAAM,QAAQ;CAE7B,IAAI;EAEF,UAAU,OAAO,KAAK,EAAE,WAAW,KAAK,CAAC;EACzC,IAAI,cAAc,aAAa;EAE/B,UAAU,KAAK,SAAS,CAAC,QAAQ,aAAa;GAC5C,cAAc,YAAY,WAAW,QAAQ,OAAO;EACtD,CAAC;EAGD,cAAc,UAAU,aAAa,MAAM;EAC3C,QAAQ,IAAI,OAAO,cAAc;EACjC,OAAO,cAAc,cAAc,OAAO,IAAI;CAChD,SAAS,OAAO;EACd,OAAO,QAAQ,MAAM,iBAAiB,aAAa,IAAI,KAAK;CAC9D;AACF;AAEA,MAAa,qBAAqB;CAChC,QAAQ,IAAI;CACZ,QAAQ,IAAI,IAAI,OAAO,EAAE,CAAC;CAC1B,QAAQ,IAAI;AACd;AAEA,MAAa,WAAc,UAAyB;CAClD,OAAO,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK;AAC5D;AAEA,MAAa,iBAAiB,SAAiB;CAC7C,MAAM,MAAM,QAAQ,IAAI;CAMxB,OALkB,WAAW,KAAK,KAAK,KAAK,CACjB,IACvB,KAAK,KAAK,OAAO,IAAI,IACrB,KAAK,KAAK,IAAI;AAGpB;AAEA,MAAa,wBACX,YACA,sBACG;CACH,MAAM,UAAU,OAAO,QAAQ,iBAAiB,CAAC,CAAC,QAAQ,CAAC,SAAS;EAClE,OAAO,IAAI,SAAS,OAAO,KAAK,IAAI,SAAS,WAAW;CAC1D,CAAC;CAED,MAAM,QAAkB,CAAC;CAEzB,QAAQ,IACN,gCAAgC,QAAQ,OAAO,WACjD;CAEA,KAAK,MAAM,GAAG,iBAAiB,SAC7B,IAAI;EACF,MAAM,OAAO,kBAAkB,cAAc,UAAU;EACvD,IAAI,MAAM,MAAM,KAAK,IAAI;CAC3B,QAAQ;EACN,QAAQ,MACN,6BAA6B,aAAa,aAAa,EACzD;CACF;CAGF,QAAQ,IAAI,yCAAyC;CACrD,OAAO;AACT"}
|
package/lib/imports.cjs
CHANGED
|
@@ -5,7 +5,7 @@ let ts_morph = require("ts-morph");
|
|
|
5
5
|
/**
|
|
6
6
|
* Resolves the moduleSpecifier using the tsconfig paths if it starts with "#"
|
|
7
7
|
*/
|
|
8
|
-
const
|
|
8
|
+
const _resolveModuleSpecifier = (sourceFile, moduleSpecifier) => {
|
|
9
9
|
const paths = sourceFile.getProject().getCompilerOptions().paths;
|
|
10
10
|
if (!paths) return moduleSpecifier;
|
|
11
11
|
const baseUrl = sourceFile.getProject().getCompilerOptions().baseUrl;
|
|
@@ -24,6 +24,9 @@ const resolveModuleSpecifier = (sourceFile, moduleSpecifier) => {
|
|
|
24
24
|
}
|
|
25
25
|
return moduleSpecifier;
|
|
26
26
|
};
|
|
27
|
+
const resolveModuleSpecifier = (sourceFile, moduleSpecifier) => {
|
|
28
|
+
return _resolveModuleSpecifier(sourceFile, moduleSpecifier).replace(/\.tsx?$/, "");
|
|
29
|
+
};
|
|
27
30
|
/**
|
|
28
31
|
* Analyzes a file's imports
|
|
29
32
|
*/
|
package/lib/imports.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"imports.cjs","names":["SyntaxKind"],"sources":["../src/imports.ts"],"sourcesContent":["import { join, relative } from 'path';\nimport { SourceFile, SyntaxKind } from 'ts-morph';\nimport type { ImportInfo } from './schemas';\n\n/**\n * Resolves the moduleSpecifier using the tsconfig paths if it starts with \"#\"\n */\nconst
|
|
1
|
+
{"version":3,"file":"imports.cjs","names":["SyntaxKind"],"sources":["../src/imports.ts"],"sourcesContent":["import { join, relative } from 'path';\nimport { SourceFile, SyntaxKind } from 'ts-morph';\nimport type { ImportInfo } from './schemas';\n\n/**\n * Resolves the moduleSpecifier using the tsconfig paths if it starts with \"#\"\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 // Find the match in paths\n for (const [pattern, mappings] of paths2) {\n // Replace * with a regex to match\n const regexPattern = pattern.replace(/\\*/g, '(.*)');\n const regex = new RegExp(`^${regexPattern}$`);\n const match = moduleSpecifier.match(regex);\n\n if (match) {\n // Take the first available mapping\n const first = mappings[0];\n\n // Resolve the absolute path\n let relativedPath = baseUrl ? join(baseUrl, first) : first;\n\n if (match[1]) {\n relativedPath = relativedPath.replace('*', match[1]);\n }\n\n // Calculate the relative path from the current source file\n const sourceFileDir = relative(\n process.cwd(),\n sourceFile.getDirectoryPath(),\n );\n const relativePath = relative(sourceFileDir, relativedPath);\n\n // Make sure the relative path starts with ./ or ../\n const resolved = relativePath.startsWith('.')\n ? relativePath\n : `./${relativePath}`;\n\n return resolved;\n }\n }\n\n return moduleSpecifier;\n};\n\nconst resolveModuleSpecifier = (\n sourceFile: SourceFile,\n moduleSpecifier: string,\n): string => {\n return _resolveModuleSpecifier(sourceFile, moduleSpecifier).replace(\n /\\.tsx?$/,\n '',\n );\n};\n\n/**\n * Analyzes a file's imports\n */\nexport const analyzeImports = (\n sourceFile: SourceFile,\n): 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 (\n !defaultImport &&\n !namespaceImport &&\n namedImports.length === 0\n ) {\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 (\n callExpr.getExpression().getKind() === SyntaxKind.ImportKeyword\n ) {\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,2BACJ,YACA,oBACW;CACX,MAAM,QAAQ,WAAW,WAAW,CAAC,CAAC,mBAAmB,CAAC,CAAC;CAE3D,IAAI,CAAC,OAAO,OAAO;CAEnB,MAAM,UAAU,WAAW,WAAW,CAAC,CAAC,mBAAmB,CAAC,CAAC;CAC7D,MAAM,SAAS,OAAO,QAAQ,KAAK;CAGnC,KAAK,MAAM,CAAC,SAAS,aAAa,QAAQ;EAExC,MAAM,eAAe,QAAQ,QAAQ,OAAO,MAAM;EAClD,MAAM,QAAQ,IAAI,OAAO,IAAI,aAAa,EAAE;EAC5C,MAAM,QAAQ,gBAAgB,MAAM,KAAK;EAEzC,IAAI,OAAO;GAET,MAAM,QAAQ,SAAS;GAGvB,IAAI,gBAAgB,WAAA,GAAA,KAAA,KAAA,CAAe,SAAS,KAAK,IAAI;GAErD,IAAI,MAAM,IACR,gBAAgB,cAAc,QAAQ,KAAK,MAAM,EAAE;GAQrD,MAAM,gBAAA,GAAA,KAAA,SAAA,EAAA,GAAA,KAAA,SAAA,CAHJ,QAAQ,IAAI,GACZ,WAAW,iBAAiB,CAEY,GAAG,aAAa;GAO1D,OAJiB,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C;GACH;EACF;CACF,CAAC;CAEH,OAAO;AACT;AAEA,MAAa,sBAAsB,YAA0B;CAC3D,OAAO,QAAQ,KAAI,QAAO;EACxB,QAAQ,IAAI,MAAZ;GACE,KAAK,SAAS;IACZ,MAAM,eAAe,IAAI,cAAc,KAAK,IAAI,KAAK;IACrD,OAAO,UAAU,IAAI,aAAa,UAAU,GAAG,IAAI,aAAa,WAAW,IAAI,gBAAgB;GACjG;GACA,KAAK,aACH,OAAO,UAAU,IAAI,aAAa,UAAU,GAAG,OAAO,IAAI,QAAQ,SAAS,IAAI,gBAAgB;GACjG,KAAK;IACH,IAAI,IAAI,WACN,OAAO,8BAA8B,IAAI,gBAAgB;IAE3D,OAAO,WAAW,IAAI,gBAAgB;GAGxC,KAAK,WACH,OAAO,UAAU,IAAI,aAAa,UAAU,KAAK,IAAI,QAAQ,SAAS,IAAI,gBAAgB;GAC5F,SACE,OAAO;EACX;CACF,CAAC;AACH"}
|
package/lib/imports.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"imports.d.ts","sourceRoot":"","sources":["../src/imports.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAc,MAAM,UAAU,CAAC;AAClD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"imports.d.ts","sourceRoot":"","sources":["../src/imports.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAc,MAAM,UAAU,CAAC;AAClD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AA+D5C;;GAEG;AACH,eAAO,MAAM,cAAc,GACzB,YAAY,UAAU,KACrB,UAAU,EAqFZ,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,SAAS,UAAU,EAAE,aAsBvD,CAAC"}
|
package/lib/imports.js
CHANGED
|
@@ -4,7 +4,7 @@ import { SyntaxKind } from "ts-morph";
|
|
|
4
4
|
/**
|
|
5
5
|
* Resolves the moduleSpecifier using the tsconfig paths if it starts with "#"
|
|
6
6
|
*/
|
|
7
|
-
const
|
|
7
|
+
const _resolveModuleSpecifier = (sourceFile, moduleSpecifier) => {
|
|
8
8
|
const paths = sourceFile.getProject().getCompilerOptions().paths;
|
|
9
9
|
if (!paths) return moduleSpecifier;
|
|
10
10
|
const baseUrl = sourceFile.getProject().getCompilerOptions().baseUrl;
|
|
@@ -23,6 +23,9 @@ const resolveModuleSpecifier = (sourceFile, moduleSpecifier) => {
|
|
|
23
23
|
}
|
|
24
24
|
return moduleSpecifier;
|
|
25
25
|
};
|
|
26
|
+
const resolveModuleSpecifier = (sourceFile, moduleSpecifier) => {
|
|
27
|
+
return _resolveModuleSpecifier(sourceFile, moduleSpecifier).replace(/\.tsx?$/, "");
|
|
28
|
+
};
|
|
26
29
|
/**
|
|
27
30
|
* Analyzes a file's imports
|
|
28
31
|
*/
|
package/lib/imports.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"imports.js","names":[],"sources":["../src/imports.ts"],"sourcesContent":["import { join, relative } from 'path';\nimport { SourceFile, SyntaxKind } from 'ts-morph';\nimport type { ImportInfo } from './schemas';\n\n/**\n * Resolves the moduleSpecifier using the tsconfig paths if it starts with \"#\"\n */\nconst
|
|
1
|
+
{"version":3,"file":"imports.js","names":[],"sources":["../src/imports.ts"],"sourcesContent":["import { join, relative } from 'path';\nimport { SourceFile, SyntaxKind } from 'ts-morph';\nimport type { ImportInfo } from './schemas';\n\n/**\n * Resolves the moduleSpecifier using the tsconfig paths if it starts with \"#\"\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 // Find the match in paths\n for (const [pattern, mappings] of paths2) {\n // Replace * with a regex to match\n const regexPattern = pattern.replace(/\\*/g, '(.*)');\n const regex = new RegExp(`^${regexPattern}$`);\n const match = moduleSpecifier.match(regex);\n\n if (match) {\n // Take the first available mapping\n const first = mappings[0];\n\n // Resolve the absolute path\n let relativedPath = baseUrl ? join(baseUrl, first) : first;\n\n if (match[1]) {\n relativedPath = relativedPath.replace('*', match[1]);\n }\n\n // Calculate the relative path from the current source file\n const sourceFileDir = relative(\n process.cwd(),\n sourceFile.getDirectoryPath(),\n );\n const relativePath = relative(sourceFileDir, relativedPath);\n\n // Make sure the relative path starts with ./ or ../\n const resolved = relativePath.startsWith('.')\n ? relativePath\n : `./${relativePath}`;\n\n return resolved;\n }\n }\n\n return moduleSpecifier;\n};\n\nconst resolveModuleSpecifier = (\n sourceFile: SourceFile,\n moduleSpecifier: string,\n): string => {\n return _resolveModuleSpecifier(sourceFile, moduleSpecifier).replace(\n /\\.tsx?$/,\n '',\n );\n};\n\n/**\n * Analyzes a file's imports\n */\nexport const analyzeImports = (\n sourceFile: SourceFile,\n): 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 (\n !defaultImport &&\n !namespaceImport &&\n namedImports.length === 0\n ) {\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 (\n callExpr.getExpression().getKind() === SyntaxKind.ImportKeyword\n ) {\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,2BACJ,YACA,oBACW;CACX,MAAM,QAAQ,WAAW,WAAW,CAAC,CAAC,mBAAmB,CAAC,CAAC;CAE3D,IAAI,CAAC,OAAO,OAAO;CAEnB,MAAM,UAAU,WAAW,WAAW,CAAC,CAAC,mBAAmB,CAAC,CAAC;CAC7D,MAAM,SAAS,OAAO,QAAQ,KAAK;CAGnC,KAAK,MAAM,CAAC,SAAS,a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;GACN;EACF,CAAC;CAEL,CAAC;CAGD,WACG,qBAAqB,WAAW,cAAc,CAAC,CAC/C,SAAQ,aAAY;EACnB,IACE,SAAS,cAAc,CAAC,CAAC,QAAQ,MAAM,WAAW,eAClD;GACA,MAAM,MAAM,SAAS,aAAa,CAAC,CAAC;GACpC,IAAI,OAAO,IAAI,QAAQ,MAAM,WAAW,eAAe;IAErD,MAAM,kBAAkB,uBACtB,YAFyB,IAAI,QAAQ,CAAC,CAAC,QAAQ,SAAS,EAGvC,CACnB;IACA,QAAQ,KAAK;KACX;KACA,MAAM;KACN,WAAW;IACb,CAAC;GACH;EACF;CACF,CAAC;CAEH,OAAO;AACT;AAEA,MAAa,sBAAsB,YAA0B;CAC3D,OAAO,QAAQ,KAAI,QAAO;EACxB,QAAQ,IAAI,MAAZ;GACE,KAAK,SAAS;IACZ,MAAM,eAAe,IAAI,cAAc,KAAK,IAAI,KAAK;IACrD,OAAO,UAAU,IAAI,aAAa,UAAU,GAAG,IAAI,aAAa,WAAW,IAAI,gBAAgB;GACjG;GACA,KAAK,aACH,OAAO,UAAU,IAAI,aAAa,UAAU,GAAG,OAAO,IAAI,QAAQ,SAAS,IAAI,gBAAgB;GACjG,KAAK;IACH,IAAI,IAAI,WACN,OAAO,8BAA8B,IAAI,gBAAgB;IAE3D,OAAO,WAAW,IAAI,gBAAgB;GAGxC,KAAK,WACH,OAAO,UAAU,IAAI,aAAa,UAAU,KAAK,IAAI,QAAQ,SAAS,IAAI,gBAAgB;GAC5F,SACE,OAAO;EACX;CACF,CAAC;AACH"}
|
package/lib/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ const require_analyse = require("./analyse.cjs");
|
|
|
5
5
|
const require_functions_add = require("./functions/add.cjs");
|
|
6
6
|
const require_functions_generate = require("./functions/generate.cjs");
|
|
7
7
|
const require_functions_init = require("./functions/init.cjs");
|
|
8
|
+
const require_functions_lift = require("./functions/lift.cjs");
|
|
8
9
|
const require_functions_remove = require("./functions/remove.cjs");
|
|
9
10
|
require("./functions/index.cjs");
|
|
10
11
|
exports.AnalysisStatsSchema = require_schemas.AnalysisStatsSchema;
|
|
@@ -17,9 +18,9 @@ exports.ImportInfoSchema = require_schemas.ImportInfoSchema;
|
|
|
17
18
|
exports.add = require_functions_add.add;
|
|
18
19
|
exports.analyze = require_analyse.analyze;
|
|
19
20
|
exports.consoleStars = require_helpers.consoleStars;
|
|
20
|
-
exports.createTypesStructure = require_functions_init.createTypesStructure;
|
|
21
21
|
exports.generate = require_functions_generate.generate;
|
|
22
22
|
exports.getFolderPath = require_helpers.getFolderPath;
|
|
23
23
|
exports.init = require_functions_init.init;
|
|
24
|
+
exports.lift = require_functions_lift.lift;
|
|
24
25
|
exports.remove = require_functions_remove.remove;
|
|
25
26
|
exports.transformJSON = require_functions_generate.transformJSON;
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,8 @@ import { consoleStars, getFolderPath } from "./helpers.js";
|
|
|
3
3
|
import { analyze } from "./analyse.js";
|
|
4
4
|
import { add } from "./functions/add.js";
|
|
5
5
|
import { generate, transformJSON } from "./functions/generate.js";
|
|
6
|
-
import {
|
|
6
|
+
import { init } from "./functions/init.js";
|
|
7
|
+
import { lift } from "./functions/lift.js";
|
|
7
8
|
import { remove } from "./functions/remove.js";
|
|
8
9
|
import "./functions/index.js";
|
|
9
|
-
export { AnalysisStatsSchema, CodeAnalysisFileSchema, CodebaseAnalysisSchema, DeclarationKindSchema, ExportInfoSchema, FileAnalysisSchema, ImportInfoSchema, add, analyze, consoleStars,
|
|
10
|
+
export { AnalysisStatsSchema, CodeAnalysisFileSchema, CodebaseAnalysisSchema, DeclarationKindSchema, ExportInfoSchema, FileAnalysisSchema, ImportInfoSchema, add, analyze, consoleStars, generate, getFolderPath, init, lift, remove, transformJSON };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemedev/codebase",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "The CLI for to generate codebase, and import partially a library. From @bemedev.",
|
|
5
5
|
"author": {
|
|
6
6
|
"email": "bri_lvi@icloud.com",
|
|
@@ -72,10 +72,10 @@
|
|
|
72
72
|
}
|
|
73
73
|
],
|
|
74
74
|
"devDependencies": {
|
|
75
|
-
"@bemedev/dev-utils": "^0.8.
|
|
75
|
+
"@bemedev/dev-utils": "^0.8.3",
|
|
76
76
|
"@size-limit/file": "^12.1.0",
|
|
77
|
-
"@types/node": "^
|
|
78
|
-
"rolldown": "1.1.
|
|
77
|
+
"@types/node": "^26.0.1",
|
|
78
|
+
"rolldown": "1.1.3",
|
|
79
79
|
"size-limit": "^12.1.0",
|
|
80
80
|
"tsx": "^4.22.4",
|
|
81
81
|
"typescript": "^6.0.3"
|