@a35hie/ts-pkg 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +43 -21
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -278,6 +278,15 @@ ${deps.map((d) => `${indent}'${d}',`).join(`
|
|
|
278
278
|
`)}
|
|
279
279
|
]`;
|
|
280
280
|
}
|
|
281
|
+
function formatDepsObject(deps, indent = " ") {
|
|
282
|
+
const entries = Object.entries(deps).sort(([a], [b]) => a.localeCompare(b));
|
|
283
|
+
if (entries.length === 0)
|
|
284
|
+
return "{}";
|
|
285
|
+
return `{
|
|
286
|
+
${entries.map(([name, version]) => `${indent}'${name}': '${version}',`).join(`
|
|
287
|
+
`)}
|
|
288
|
+
}`;
|
|
289
|
+
}
|
|
281
290
|
async function syncDependencies(options = {}) {
|
|
282
291
|
const {
|
|
283
292
|
packageJsonPath = "package.json",
|
|
@@ -318,9 +327,9 @@ async function syncDependencies(options = {}) {
|
|
|
318
327
|
const devDeps = depsObjectToArray(packageJson.devDependencies);
|
|
319
328
|
const peerDeps = depsObjectToArray(packageJson.peerDependencies);
|
|
320
329
|
let updatedContent = configContent;
|
|
321
|
-
updatedContent = updateDepsInConfig(updatedContent, "dependencies", deps);
|
|
322
|
-
updatedContent = updateDepsInConfig(updatedContent, "devDependencies", devDeps);
|
|
323
|
-
updatedContent = updateDepsInConfig(updatedContent, "peerDependencies", peerDeps);
|
|
330
|
+
updatedContent = updateDepsInConfig(updatedContent, "dependencies", deps, packageJson.dependencies);
|
|
331
|
+
updatedContent = updateDepsInConfig(updatedContent, "devDependencies", devDeps, packageJson.devDependencies);
|
|
332
|
+
updatedContent = updateDepsInConfig(updatedContent, "peerDependencies", peerDeps, packageJson.peerDependencies);
|
|
324
333
|
await writeFile(absoluteConfigPath, updatedContent);
|
|
325
334
|
if (!quiet) {
|
|
326
335
|
console.log(`✨ Synced dependencies to ${configPath}`);
|
|
@@ -332,12 +341,12 @@ async function syncDependencies(options = {}) {
|
|
|
332
341
|
console.log(` peerDependencies: ${peerDeps.length} packages`);
|
|
333
342
|
}
|
|
334
343
|
}
|
|
335
|
-
function
|
|
344
|
+
function findBracketEnd(content, startIndex, openChar, closeChar) {
|
|
336
345
|
let depth = 0;
|
|
337
346
|
for (let i = startIndex;i < content.length; i++) {
|
|
338
|
-
if (content[i] ===
|
|
347
|
+
if (content[i] === openChar)
|
|
339
348
|
depth++;
|
|
340
|
-
else if (content[i] ===
|
|
349
|
+
else if (content[i] === closeChar) {
|
|
341
350
|
depth--;
|
|
342
351
|
if (depth === 0)
|
|
343
352
|
return i;
|
|
@@ -345,22 +354,33 @@ function findArrayEnd(content, startIndex) {
|
|
|
345
354
|
}
|
|
346
355
|
return -1;
|
|
347
356
|
}
|
|
348
|
-
function updateDepsInConfig(content, depType,
|
|
349
|
-
const
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
const arrayStart =
|
|
354
|
-
const arrayEnd =
|
|
357
|
+
function updateDepsInConfig(content, depType, depsArray, depsObject) {
|
|
358
|
+
const arrayPattern = new RegExp(`(${depType}:\\s*)\\[`);
|
|
359
|
+
const arrayMatch = arrayPattern.exec(content);
|
|
360
|
+
if (arrayMatch && arrayMatch[1]) {
|
|
361
|
+
const formattedDeps = formatDepsArray(depsArray);
|
|
362
|
+
const arrayStart = arrayMatch.index + arrayMatch[1].length;
|
|
363
|
+
const arrayEnd = findBracketEnd(content, arrayStart, "[", "]");
|
|
355
364
|
if (arrayEnd !== -1) {
|
|
356
365
|
return content.slice(0, arrayStart) + formattedDeps + content.slice(arrayEnd + 1);
|
|
357
366
|
}
|
|
358
367
|
}
|
|
359
|
-
|
|
368
|
+
const objectPattern = new RegExp(`(${depType}:\\s*)\\{`);
|
|
369
|
+
const objectMatch = objectPattern.exec(content);
|
|
370
|
+
if (objectMatch && objectMatch[1]) {
|
|
371
|
+
const formattedDeps = formatDepsObject(depsObject || {});
|
|
372
|
+
const objStart = objectMatch.index + objectMatch[1].length;
|
|
373
|
+
const objEnd = findBracketEnd(content, objStart, "{", "}");
|
|
374
|
+
if (objEnd !== -1) {
|
|
375
|
+
return content.slice(0, objStart) + formattedDeps + content.slice(objEnd + 1);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (depsArray.length > 0) {
|
|
379
|
+
const formattedDeps = formatDepsArray(depsArray);
|
|
360
380
|
const insertPatterns = [
|
|
361
|
-
{ pattern: /peerDependencies:\s
|
|
362
|
-
{ pattern: /devDependencies:\s
|
|
363
|
-
{ pattern: /dependencies:\s
|
|
381
|
+
{ pattern: /peerDependencies:\s*[\[{]/, after: true },
|
|
382
|
+
{ pattern: /devDependencies:\s*[\[{]/, after: true },
|
|
383
|
+
{ pattern: /dependencies:\s*[\[{]/, after: true },
|
|
364
384
|
{ pattern: /(\n\s*)(conditions:\s*\[)/, before: true },
|
|
365
385
|
{ pattern: /(\n\s*)(engines:\s*\{)/, before: true }
|
|
366
386
|
];
|
|
@@ -368,10 +388,12 @@ function updateDepsInConfig(content, depType, deps) {
|
|
|
368
388
|
const m = pattern.exec(content);
|
|
369
389
|
if (m) {
|
|
370
390
|
if (after) {
|
|
371
|
-
const
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
391
|
+
const startChar = content[m.index + m[0].length - 1];
|
|
392
|
+
const isArray = startChar === "[";
|
|
393
|
+
const bracketStart = m.index + m[0].length - 1;
|
|
394
|
+
const bracketEnd = findBracketEnd(content, bracketStart, isArray ? "[" : "{", isArray ? "]" : "}");
|
|
395
|
+
if (bracketEnd !== -1) {
|
|
396
|
+
let insertPos = bracketEnd + 1;
|
|
375
397
|
let char = content[insertPos];
|
|
376
398
|
while (insertPos < content.length && char && /[,\s]/.test(char) && char !== `
|
|
377
399
|
`) {
|