@aneuhold/core-ts-lib 2.1.8 → 2.1.10
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/lib/services/FileSystemService/FileSystemService.spec.ts +20 -4
- package/lib/services/PackageService.d.ts +16 -2
- package/lib/services/PackageService.d.ts.map +1 -1
- package/lib/services/PackageService.js +98 -4
- package/lib/services/PackageService.js.map +1 -1
- package/lib/services/PackageService.ts +132 -5
- package/package.json +1 -1
|
@@ -36,10 +36,12 @@ describe('FileSystemService', () => {
|
|
|
36
36
|
);
|
|
37
37
|
|
|
38
38
|
// Assert
|
|
39
|
-
const expectedContents =
|
|
40
|
-
await FileSystemService.getAllFilePathsRelative(sourceFolderPath)
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
const expectedContents = (
|
|
40
|
+
await FileSystemService.getAllFilePathsRelative(sourceFolderPath)
|
|
41
|
+
).sort();
|
|
42
|
+
const actualContents = (
|
|
43
|
+
await FileSystemService.getAllFilePathsRelative(targetFolderPath)
|
|
44
|
+
).sort();
|
|
43
45
|
expect(actualContents).toEqual(expectedContents);
|
|
44
46
|
});
|
|
45
47
|
|
|
@@ -91,6 +93,12 @@ describe('FileSystemService', () => {
|
|
|
91
93
|
});
|
|
92
94
|
});
|
|
93
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Creates test files and folders based on the provided structure
|
|
98
|
+
*
|
|
99
|
+
* @param folderPath The path where the files and folders should be created
|
|
100
|
+
* @param fileStructure An object representing the file structure to create
|
|
101
|
+
*/
|
|
94
102
|
async function createTestFiles(folderPath: string, fileStructure: object) {
|
|
95
103
|
await FileSystemService.checkOrCreateFolder(folderPath);
|
|
96
104
|
|
|
@@ -106,12 +114,20 @@ async function createTestFiles(folderPath: string, fileStructure: object) {
|
|
|
106
114
|
);
|
|
107
115
|
}
|
|
108
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Creates a test folder within the main test directory
|
|
119
|
+
*
|
|
120
|
+
* @param folderName The name of the folder to create
|
|
121
|
+
*/
|
|
109
122
|
async function createTestFolder(folderName: string): Promise<string> {
|
|
110
123
|
const testFolderPath = path.join(TEST_FOLDER_NAME, folderName);
|
|
111
124
|
await FileSystemService.checkOrCreateFolder(testFolderPath);
|
|
112
125
|
return testFolderPath;
|
|
113
126
|
}
|
|
114
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Deletes the main test folder and all its contents
|
|
130
|
+
*/
|
|
115
131
|
async function deleteTestFolder(): Promise<void> {
|
|
116
132
|
await FileSystemService.deleteFolder(TEST_FOLDER_NAME);
|
|
117
133
|
}
|
|
@@ -15,10 +15,11 @@ export default class PackageService {
|
|
|
15
15
|
* Updates the jsr.json file from the package.json file and resolves wildcard
|
|
16
16
|
* dependencies in package.json for JSR compatibility.
|
|
17
17
|
*/
|
|
18
|
-
static updateJsrFromPackageJson
|
|
18
|
+
private static updateJsrFromPackageJson;
|
|
19
19
|
/**
|
|
20
20
|
* Resolves wildcard dependencies in package.json by replacing them with actual
|
|
21
|
-
* version constraints (e.g., "*1.2.3") for JSR compatibility.
|
|
21
|
+
* version constraints (e.g., "*1.2.3") for JSR compatibility. This will
|
|
22
|
+
* write the modified package.json back to disk.
|
|
22
23
|
*
|
|
23
24
|
* @param packageJsonData The package.json data to modify
|
|
24
25
|
* @param packageJsonPath The path to the package.json file
|
|
@@ -35,6 +36,19 @@ export default class PackageService {
|
|
|
35
36
|
* @returns true if the publish was successful, false otherwise.
|
|
36
37
|
*/
|
|
37
38
|
private static publishJsr;
|
|
39
|
+
/**
|
|
40
|
+
* Replaces imports of monorepo dependencies with npm: specifiers in all TypeScript files.
|
|
41
|
+
* This is needed for JSR compatibility when publishing packages that depend on other packages in the monorepo.
|
|
42
|
+
*
|
|
43
|
+
* This helps prevent the following error that comes up as of 5/26/2025:
|
|
44
|
+
*
|
|
45
|
+
* ```sh
|
|
46
|
+
* error: Failed to publish @aneuhold/be-ts-lib@2.0.67
|
|
47
|
+
* Caused by: Failed to publish @aneuhold/be-ts-lib at 2.0.67: failed to build module graph: Module not found "file:///src/services/@aneuhold/core-ts-lib".
|
|
48
|
+
* at file:///src/services/GitHubService.ts:1:20
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
private static replaceMonorepoImportsWithNpmSpecifiers;
|
|
38
52
|
private static revertGitChanges;
|
|
39
53
|
}
|
|
40
54
|
//# sourceMappingURL=PackageService.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PackageService.d.ts","sourceRoot":"./src/","sources":["services/PackageService.ts"],"names":[],"mappings":"AAaA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,cAAc;IACjC;;;;;OAKG;WACU,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"PackageService.d.ts","sourceRoot":"./src/","sources":["services/PackageService.ts"],"names":[],"mappings":"AAaA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,cAAc;IACjC;;;;;OAKG;WACU,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;WAiBnC,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAiB1C;;;OAGG;mBACkB,wBAAwB;IAgD7C;;;;;;;OAOG;mBACkB,wCAAwC;IA+C7D;;;OAGG;mBACkB,gBAAgB;IAmBrC;;;;OAIG;mBACkB,UAAU;IAkB/B;;;;;;;;;;;OAWG;mBACkB,uCAAuC;mBA4GvC,gBAAgB;CAYtC"}
|
|
@@ -23,6 +23,7 @@ export default class PackageService {
|
|
|
23
23
|
DR.logger.error('Please commit or stash your changes before publishing.');
|
|
24
24
|
process.exit(1);
|
|
25
25
|
}
|
|
26
|
+
await PackageService.replaceMonorepoImportsWithNpmSpecifiers();
|
|
26
27
|
await PackageService.updateJsrFromPackageJson();
|
|
27
28
|
const successfulDryRun = await PackageService.publishJsrDryRun();
|
|
28
29
|
await PackageService.revertGitChanges();
|
|
@@ -38,6 +39,7 @@ export default class PackageService {
|
|
|
38
39
|
DR.logger.error('Please commit or stash your changes before publishing.');
|
|
39
40
|
process.exit(1);
|
|
40
41
|
}
|
|
42
|
+
await PackageService.replaceMonorepoImportsWithNpmSpecifiers();
|
|
41
43
|
await PackageService.updateJsrFromPackageJson();
|
|
42
44
|
const result = await PackageService.publishJsr();
|
|
43
45
|
await PackageService.revertGitChanges();
|
|
@@ -87,7 +89,8 @@ export default class PackageService {
|
|
|
87
89
|
}
|
|
88
90
|
/**
|
|
89
91
|
* Resolves wildcard dependencies in package.json by replacing them with actual
|
|
90
|
-
* version constraints (e.g., "*1.2.3") for JSR compatibility.
|
|
92
|
+
* version constraints (e.g., "*1.2.3") for JSR compatibility. This will
|
|
93
|
+
* write the modified package.json back to disk.
|
|
91
94
|
*
|
|
92
95
|
* @param packageJsonData The package.json data to modify
|
|
93
96
|
* @param packageJsonPath The path to the package.json file
|
|
@@ -164,13 +167,104 @@ export default class PackageService {
|
|
|
164
167
|
});
|
|
165
168
|
});
|
|
166
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Replaces imports of monorepo dependencies with npm: specifiers in all TypeScript files.
|
|
172
|
+
* This is needed for JSR compatibility when publishing packages that depend on other packages in the monorepo.
|
|
173
|
+
*
|
|
174
|
+
* This helps prevent the following error that comes up as of 5/26/2025:
|
|
175
|
+
*
|
|
176
|
+
* ```sh
|
|
177
|
+
* error: Failed to publish @aneuhold/be-ts-lib@2.0.67
|
|
178
|
+
* Caused by: Failed to publish @aneuhold/be-ts-lib at 2.0.67: failed to build module graph: Module not found "file:///src/services/@aneuhold/core-ts-lib".
|
|
179
|
+
* at file:///src/services/GitHubService.ts:1:20
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
static async replaceMonorepoImportsWithNpmSpecifiers() {
|
|
183
|
+
const rootDir = process.cwd();
|
|
184
|
+
const srcDir = path.join(rootDir, 'src');
|
|
185
|
+
try {
|
|
186
|
+
await access(srcDir);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
DR.logger.error('No src directory found in the current directory.');
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
// Get all packages in the monorepo to know which ones to replace
|
|
193
|
+
const childPackages = await DependencyService.getChildPackageJsons('../');
|
|
194
|
+
const packageNames = Object.keys(childPackages);
|
|
195
|
+
if (packageNames.length === 0) {
|
|
196
|
+
DR.logger.info('No monorepo packages found to replace.');
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
// Create maps for package specifiers and pre-compiled regex patterns
|
|
200
|
+
const packageSpecifierMap = new Map();
|
|
201
|
+
const packageRegexMap = new Map();
|
|
202
|
+
for (const [packageName, packageInfo] of Object.entries(childPackages)) {
|
|
203
|
+
const version = packageInfo.packageJsonContents.version;
|
|
204
|
+
// The below HAS to be a carrot (^) to ensure that the version is compatible with JSR.
|
|
205
|
+
// As of 5/26/2025, inline imports with versions don't seem to support the *
|
|
206
|
+
// wildcard, so we use a caret (^) to allow for minor version updates. This
|
|
207
|
+
// can cause problems, but it is accepted for now.
|
|
208
|
+
// Here is an example error that comes up if we don't use a caret:
|
|
209
|
+
// ```sh
|
|
210
|
+
// error: Invalid package specifier 'npm:@aneuhold/core-ts-lib@*2.1.7'
|
|
211
|
+
// 0: Invalid specifier version requirement
|
|
212
|
+
// 1: Unexpected character.
|
|
213
|
+
// 2.1.7
|
|
214
|
+
// ~
|
|
215
|
+
// at file:///Users/aneuhold/Development/GithubRepos/ts-libs/packages/be-ts-lib/src/services/ConfigService/ConfigService.ts:1:20
|
|
216
|
+
// ```
|
|
217
|
+
const npmSpecifier = `npm:${packageName}@^${version}`;
|
|
218
|
+
packageSpecifierMap.set(packageName, npmSpecifier);
|
|
219
|
+
// Pre-compile regex pattern for this package
|
|
220
|
+
const importRegex = new RegExp(`(import\\s+[^;]+from\\s+['"])${packageName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(['"])`, 'g');
|
|
221
|
+
packageRegexMap.set(packageName, importRegex);
|
|
222
|
+
}
|
|
223
|
+
// Get all TypeScript files in the src directory
|
|
224
|
+
const allFiles = await FileSystemService.getAllFilePathsRelative(srcDir);
|
|
225
|
+
const tsFiles = allFiles
|
|
226
|
+
.filter((filePath) => filePath.endsWith('.ts') && !filePath.endsWith('.spec.ts'))
|
|
227
|
+
.map((filePath) => path.join(srcDir, filePath));
|
|
228
|
+
DR.logger.info(`Replacing monorepo imports with npm: specifiers in ${tsFiles.length} TypeScript files...`);
|
|
229
|
+
let totalReplacements = 0;
|
|
230
|
+
for (const filePath of tsFiles) {
|
|
231
|
+
try {
|
|
232
|
+
const content = await readFile(filePath, 'utf-8');
|
|
233
|
+
let newContent = content;
|
|
234
|
+
let fileReplacements = 0;
|
|
235
|
+
for (const [packageName, npmSpecifier] of packageSpecifierMap) {
|
|
236
|
+
// Use the pre-compiled regex pattern
|
|
237
|
+
const importRegex = packageRegexMap.get(packageName);
|
|
238
|
+
if (!importRegex)
|
|
239
|
+
continue;
|
|
240
|
+
// Reset the regex lastIndex to ensure it starts from the beginning
|
|
241
|
+
importRegex.lastIndex = 0;
|
|
242
|
+
const matches = newContent.match(importRegex);
|
|
243
|
+
if (matches) {
|
|
244
|
+
newContent = newContent.replace(importRegex, `$1${npmSpecifier}$2`);
|
|
245
|
+
fileReplacements += matches.length;
|
|
246
|
+
DR.logger.info(` Replaced ${packageName} with ${npmSpecifier} (${matches.length} occurrences)`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (fileReplacements > 0) {
|
|
250
|
+
await writeFile(filePath, newContent);
|
|
251
|
+
totalReplacements += fileReplacements;
|
|
252
|
+
DR.logger.info(` Updated: ${path.relative(rootDir, filePath)} (${fileReplacements} replacements)`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
DR.logger.error(`Failed to process file ${filePath}: ${ErrorUtils.getErrorString(error)}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
DR.logger.info(`Completed monorepo import replacement: ${totalReplacements} total replacements made`);
|
|
260
|
+
}
|
|
167
261
|
static async revertGitChanges() {
|
|
168
|
-
DR.logger.info('Reverting changes made to jsr.json
|
|
262
|
+
DR.logger.info('Reverting changes made to jsr.json, package.json, and source files');
|
|
169
263
|
try {
|
|
170
|
-
await execAsync('git checkout jsr.json package.json');
|
|
264
|
+
await execAsync('git checkout -- jsr.json package.json src/');
|
|
171
265
|
}
|
|
172
266
|
catch (error) {
|
|
173
|
-
DR.logger.error(`Failed to revert changes
|
|
267
|
+
DR.logger.error(`Failed to revert changes: ${ErrorUtils.getErrorString(error)}`);
|
|
174
268
|
}
|
|
175
269
|
}
|
|
176
270
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PackageService.js","sourceRoot":"./src/","sources":["services/PackageService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAGjC,OAAO,UAAU,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,EAAE,EAAE,MAAM,yBAAyB,CAAC;AAC7C,OAAO,iBAAiB,MAAM,wBAAwB,CAAC;AACvD,OAAO,iBAAiB,MAAM,0CAA0C,CAAC;AAEzE,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;AAElC;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,cAAc;IACjC;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,kBAAkB;QAC7B,IAAI,MAAM,iBAAiB,CAAC,iBAAiB,EAAE,EAAE,CAAC;YAChD,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,wDAAwD,CAAC,CAAC;YAC1E,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;QACD,MAAM,cA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IAAI,gBAAgB,GAAG,CAAC,EAAE,CAAC;oBACzB,MAAM,SAAS,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;oBACtC,iBAAiB,IAAI,gBAAgB,CAAC;oBACtC,EAAE,CAAC,MAAM,CAAC,IAAI,CACZ,cAAc,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAK,gBAAgB,gBAAgB,CACpF,CAAC;gBACJ,CAAC;YACH,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,EAAE,CAAC,MAAM,CAAC,KAAK,CACb,0BAA0B,QAAQ,KAAK,UAAU,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,CAC1E,CAAC;YACJ,CAAC;QACH,CAAC;QAED,EAAE,CAAC,MAAM,CAAC,IAAI,CACZ,0CAA0C,iBAAiB,0BAA0B,CACtF,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,KAAK,CAAC,gBAAgB;QACnC,EAAE,CAAC,MAAM,CAAC,IAAI,CACZ,oEAAoE,CACrE,CAAC;QACF,IAAI,CAAC;YACH,MAAM,SAAS,CAAC,4CAA4C,CAAC,CAAC;QAChE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,EAAE,CAAC,MAAM,CAAC,KAAK,CACb,6BAA6B,UAAU,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,CAChE,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -27,9 +27,11 @@ export default class PackageService {
|
|
|
27
27
|
DR.logger.error('Please commit or stash your changes before publishing.');
|
|
28
28
|
process.exit(1);
|
|
29
29
|
}
|
|
30
|
+
await PackageService.replaceMonorepoImportsWithNpmSpecifiers();
|
|
30
31
|
await PackageService.updateJsrFromPackageJson();
|
|
31
32
|
const successfulDryRun = await PackageService.publishJsrDryRun();
|
|
32
33
|
await PackageService.revertGitChanges();
|
|
34
|
+
|
|
33
35
|
if (!successfulDryRun) {
|
|
34
36
|
process.exit(1);
|
|
35
37
|
} else {
|
|
@@ -42,9 +44,11 @@ export default class PackageService {
|
|
|
42
44
|
DR.logger.error('Please commit or stash your changes before publishing.');
|
|
43
45
|
process.exit(1);
|
|
44
46
|
}
|
|
47
|
+
await PackageService.replaceMonorepoImportsWithNpmSpecifiers();
|
|
45
48
|
await PackageService.updateJsrFromPackageJson();
|
|
46
49
|
const result = await PackageService.publishJsr();
|
|
47
50
|
await PackageService.revertGitChanges();
|
|
51
|
+
|
|
48
52
|
if (!result) {
|
|
49
53
|
process.exit(1);
|
|
50
54
|
} else {
|
|
@@ -56,7 +60,7 @@ export default class PackageService {
|
|
|
56
60
|
* Updates the jsr.json file from the package.json file and resolves wildcard
|
|
57
61
|
* dependencies in package.json for JSR compatibility.
|
|
58
62
|
*/
|
|
59
|
-
static async updateJsrFromPackageJson(): Promise<void> {
|
|
63
|
+
private static async updateJsrFromPackageJson(): Promise<void> {
|
|
60
64
|
const rootDir = process.cwd();
|
|
61
65
|
const packageJsonPath = path.join(rootDir, 'package.json');
|
|
62
66
|
const jsrJsonPath = path.join(rootDir, 'jsr.json');
|
|
@@ -106,7 +110,8 @@ export default class PackageService {
|
|
|
106
110
|
|
|
107
111
|
/**
|
|
108
112
|
* Resolves wildcard dependencies in package.json by replacing them with actual
|
|
109
|
-
* version constraints (e.g., "*1.2.3") for JSR compatibility.
|
|
113
|
+
* version constraints (e.g., "*1.2.3") for JSR compatibility. This will
|
|
114
|
+
* write the modified package.json back to disk.
|
|
110
115
|
*
|
|
111
116
|
* @param packageJsonData The package.json data to modify
|
|
112
117
|
* @param packageJsonPath The path to the package.json file
|
|
@@ -204,13 +209,135 @@ export default class PackageService {
|
|
|
204
209
|
});
|
|
205
210
|
}
|
|
206
211
|
|
|
212
|
+
/**
|
|
213
|
+
* Replaces imports of monorepo dependencies with npm: specifiers in all TypeScript files.
|
|
214
|
+
* This is needed for JSR compatibility when publishing packages that depend on other packages in the monorepo.
|
|
215
|
+
*
|
|
216
|
+
* This helps prevent the following error that comes up as of 5/26/2025:
|
|
217
|
+
*
|
|
218
|
+
* ```sh
|
|
219
|
+
* error: Failed to publish @aneuhold/be-ts-lib@2.0.67
|
|
220
|
+
* Caused by: Failed to publish @aneuhold/be-ts-lib at 2.0.67: failed to build module graph: Module not found "file:///src/services/@aneuhold/core-ts-lib".
|
|
221
|
+
* at file:///src/services/GitHubService.ts:1:20
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
private static async replaceMonorepoImportsWithNpmSpecifiers(): Promise<void> {
|
|
225
|
+
const rootDir = process.cwd();
|
|
226
|
+
const srcDir = path.join(rootDir, 'src');
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
await access(srcDir);
|
|
230
|
+
} catch {
|
|
231
|
+
DR.logger.error('No src directory found in the current directory.');
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Get all packages in the monorepo to know which ones to replace
|
|
236
|
+
const childPackages = await DependencyService.getChildPackageJsons('../');
|
|
237
|
+
const packageNames = Object.keys(childPackages);
|
|
238
|
+
|
|
239
|
+
if (packageNames.length === 0) {
|
|
240
|
+
DR.logger.info('No monorepo packages found to replace.');
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Create maps for package specifiers and pre-compiled regex patterns
|
|
245
|
+
const packageSpecifierMap = new Map<string, string>();
|
|
246
|
+
const packageRegexMap = new Map<string, RegExp>();
|
|
247
|
+
|
|
248
|
+
for (const [packageName, packageInfo] of Object.entries(childPackages)) {
|
|
249
|
+
const version = packageInfo.packageJsonContents.version;
|
|
250
|
+
// The below HAS to be a carrot (^) to ensure that the version is compatible with JSR.
|
|
251
|
+
// As of 5/26/2025, inline imports with versions don't seem to support the *
|
|
252
|
+
// wildcard, so we use a caret (^) to allow for minor version updates. This
|
|
253
|
+
// can cause problems, but it is accepted for now.
|
|
254
|
+
// Here is an example error that comes up if we don't use a caret:
|
|
255
|
+
// ```sh
|
|
256
|
+
// error: Invalid package specifier 'npm:@aneuhold/core-ts-lib@*2.1.7'
|
|
257
|
+
// 0: Invalid specifier version requirement
|
|
258
|
+
// 1: Unexpected character.
|
|
259
|
+
// 2.1.7
|
|
260
|
+
// ~
|
|
261
|
+
// at file:///Users/aneuhold/Development/GithubRepos/ts-libs/packages/be-ts-lib/src/services/ConfigService/ConfigService.ts:1:20
|
|
262
|
+
// ```
|
|
263
|
+
const npmSpecifier = `npm:${packageName}@^${version}`;
|
|
264
|
+
packageSpecifierMap.set(packageName, npmSpecifier);
|
|
265
|
+
|
|
266
|
+
// Pre-compile regex pattern for this package
|
|
267
|
+
const importRegex = new RegExp(
|
|
268
|
+
`(import\\s+[^;]+from\\s+['"])${packageName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(['"])`,
|
|
269
|
+
'g'
|
|
270
|
+
);
|
|
271
|
+
packageRegexMap.set(packageName, importRegex);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Get all TypeScript files in the src directory
|
|
275
|
+
const allFiles = await FileSystemService.getAllFilePathsRelative(srcDir);
|
|
276
|
+
const tsFiles = allFiles
|
|
277
|
+
.filter(
|
|
278
|
+
(filePath) => filePath.endsWith('.ts') && !filePath.endsWith('.spec.ts')
|
|
279
|
+
)
|
|
280
|
+
.map((filePath) => path.join(srcDir, filePath));
|
|
281
|
+
|
|
282
|
+
DR.logger.info(
|
|
283
|
+
`Replacing monorepo imports with npm: specifiers in ${tsFiles.length} TypeScript files...`
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
let totalReplacements = 0;
|
|
287
|
+
|
|
288
|
+
for (const filePath of tsFiles) {
|
|
289
|
+
try {
|
|
290
|
+
const content = await readFile(filePath, 'utf-8');
|
|
291
|
+
let newContent = content;
|
|
292
|
+
let fileReplacements = 0;
|
|
293
|
+
|
|
294
|
+
for (const [packageName, npmSpecifier] of packageSpecifierMap) {
|
|
295
|
+
// Use the pre-compiled regex pattern
|
|
296
|
+
const importRegex = packageRegexMap.get(packageName);
|
|
297
|
+
if (!importRegex) continue;
|
|
298
|
+
|
|
299
|
+
// Reset the regex lastIndex to ensure it starts from the beginning
|
|
300
|
+
importRegex.lastIndex = 0;
|
|
301
|
+
|
|
302
|
+
const matches = newContent.match(importRegex);
|
|
303
|
+
|
|
304
|
+
if (matches) {
|
|
305
|
+
newContent = newContent.replace(importRegex, `$1${npmSpecifier}$2`);
|
|
306
|
+
fileReplacements += matches.length;
|
|
307
|
+
DR.logger.info(
|
|
308
|
+
` Replaced ${packageName} with ${npmSpecifier} (${matches.length} occurrences)`
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (fileReplacements > 0) {
|
|
314
|
+
await writeFile(filePath, newContent);
|
|
315
|
+
totalReplacements += fileReplacements;
|
|
316
|
+
DR.logger.info(
|
|
317
|
+
` Updated: ${path.relative(rootDir, filePath)} (${fileReplacements} replacements)`
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
} catch (error) {
|
|
321
|
+
DR.logger.error(
|
|
322
|
+
`Failed to process file ${filePath}: ${ErrorUtils.getErrorString(error)}`
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
DR.logger.info(
|
|
328
|
+
`Completed monorepo import replacement: ${totalReplacements} total replacements made`
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
|
|
207
332
|
private static async revertGitChanges(): Promise<void> {
|
|
208
|
-
DR.logger.info(
|
|
333
|
+
DR.logger.info(
|
|
334
|
+
'Reverting changes made to jsr.json, package.json, and source files'
|
|
335
|
+
);
|
|
209
336
|
try {
|
|
210
|
-
await execAsync('git checkout jsr.json package.json');
|
|
337
|
+
await execAsync('git checkout -- jsr.json package.json src/');
|
|
211
338
|
} catch (error) {
|
|
212
339
|
DR.logger.error(
|
|
213
|
-
`Failed to revert changes
|
|
340
|
+
`Failed to revert changes: ${ErrorUtils.getErrorString(error)}`
|
|
214
341
|
);
|
|
215
342
|
}
|
|
216
343
|
}
|
package/package.json
CHANGED