unpeek 1.14.0 → 1.15.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.
@@ -280,6 +280,70 @@ function detectIndentation(rawJson) {
280
280
  if (indent.includes(" ")) return " ";
281
281
  return indent.length;
282
282
  }
283
+ function isNext16OrAbove(versionStr) {
284
+ if (!versionStr) return false;
285
+ const clean = versionStr.replace(/^[^\d]*/, "");
286
+ const match = clean.match(/^(\d+)\.(\d+)/);
287
+ if (!match) return false;
288
+ const major = parseInt(match[1], 10);
289
+ const minor = parseInt(match[2], 10);
290
+ if (major > 16) return true;
291
+ if (major === 16 && minor >= 2) return true;
292
+ return false;
293
+ }
294
+ var NEXT_CONFIG_FILES2 = ["next.config.ts", "next.config.mjs", "next.config.js", "next.config.cjs"];
295
+ function wireNextConfig(rootDir, dryRun) {
296
+ let targetFile;
297
+ for (const f of NEXT_CONFIG_FILES2) {
298
+ if (import_node_fs2.default.existsSync(import_node_path2.default.join(rootDir, f))) {
299
+ targetFile = f;
300
+ break;
301
+ }
302
+ }
303
+ if (!targetFile) {
304
+ const defaultMjs = `import { withUnpeek } from 'unpeek/next';
305
+
306
+ /** @type {import('next').NextConfig} */
307
+ const nextConfig = {};
308
+
309
+ export default withUnpeek(nextConfig);
310
+ `;
311
+ if (!dryRun) {
312
+ import_node_fs2.default.writeFileSync(import_node_path2.default.join(rootDir, "next.config.mjs"), defaultMjs, "utf8");
313
+ }
314
+ return { updated: true, filename: "next.config.mjs" };
315
+ }
316
+ const filePath = import_node_path2.default.join(rootDir, targetFile);
317
+ const content = import_node_fs2.default.readFileSync(filePath, "utf8");
318
+ if (content.includes("withUnpeek")) {
319
+ return { updated: false, filename: targetFile };
320
+ }
321
+ const isCjs = targetFile.endsWith(".cjs") || targetFile.endsWith(".js") && !content.includes("import ") && !content.includes("export ");
322
+ let modified = content;
323
+ if (isCjs) {
324
+ const importStmt = `const { withUnpeek } = require('unpeek/next');
325
+ `;
326
+ modified = `${importStmt}${modified}`;
327
+ if (/module\.exports\s*=\s*([a-zA-Z0-9_$]+)\s*;?/.test(modified)) {
328
+ modified = modified.replace(/module\.exports\s*=\s*([a-zA-Z0-9_$]+)\s*;?/, "module.exports = withUnpeek($1);");
329
+ } else if (/module\.exports\s*=\s*/.test(modified)) {
330
+ modified = modified.replace(/module\.exports\s*=\s*([\s\S]+?);?\s*$/, "module.exports = withUnpeek($1);");
331
+ }
332
+ } else {
333
+ const importStmt = `import { withUnpeek } from 'unpeek/next';
334
+ `;
335
+ modified = `${importStmt}${modified}`;
336
+ if (/export\s+default\s+([a-zA-Z0-9_$]+)\s*;?/.test(modified)) {
337
+ modified = modified.replace(/export\s+default\s+([a-zA-Z0-9_$]+)\s*;?/, "export default withUnpeek($1);");
338
+ } else if (/export\s+default\s+/.test(modified)) {
339
+ modified = modified.replace(/export\s+default\s+([\s\S]+?);?\s*$/, "export default withUnpeek($1);");
340
+ }
341
+ }
342
+ if (!dryRun && modified !== content) {
343
+ import_node_fs2.default.writeFileSync(filePath, modified, "utf8");
344
+ }
345
+ return { updated: modified !== content, filename: targetFile };
346
+ }
283
347
  function initProject(cwd = process.cwd(), options = {}) {
284
348
  const dryRun = typeof options === "boolean" ? options : options.dryRun ?? false;
285
349
  const rootDir = import_node_path2.default.resolve(cwd);
@@ -287,11 +351,26 @@ function initProject(cwd = process.cwd(), options = {}) {
287
351
  const pkgPath = import_node_path2.default.join(rootDir, "package.json");
288
352
  let updatedPackageJson = false;
289
353
  let updatedGitignore = false;
354
+ let updatedNextConfig = false;
355
+ let isNext16 = false;
356
+ let nextVersion;
357
+ let pkg = {};
290
358
  if (import_node_fs2.default.existsSync(pkgPath)) {
359
+ try {
360
+ const raw = import_node_fs2.default.readFileSync(pkgPath, "utf8");
361
+ pkg = JSON.parse(raw);
362
+ nextVersion = pkg.dependencies?.next || pkg.devDependencies?.next;
363
+ } catch {
364
+ }
365
+ }
366
+ if (framework === "nextjs" && isNext16OrAbove(nextVersion)) {
367
+ isNext16 = true;
368
+ const configRes = wireNextConfig(rootDir, dryRun);
369
+ updatedNextConfig = configRes.updated;
370
+ } else if (import_node_fs2.default.existsSync(pkgPath)) {
291
371
  const raw = import_node_fs2.default.readFileSync(pkgPath, "utf8");
292
372
  const indent = detectIndentation(raw);
293
373
  const trailingNewline = raw.endsWith("\r\n") ? "\r\n" : raw.endsWith("\n") ? "\n" : "";
294
- const pkg = JSON.parse(raw);
295
374
  if (!pkg.scripts || typeof pkg.scripts !== "object") {
296
375
  pkg.scripts = {};
297
376
  }
@@ -326,17 +405,29 @@ function initProject(cwd = process.cwd(), options = {}) {
326
405
  updatedGitignore = true;
327
406
  }
328
407
  let message;
329
- if (dryRun) {
330
- message = `[dry-run] Detected framework: ${framework}. Would configure postbuild hook and .unpeek-maps/ vault.`;
331
- } else if (updatedPackageJson || updatedGitignore) {
332
- message = `Detected framework: ${framework}. Initialized unpeek: configured postbuild hook and .unpeek-maps/ vault.`;
408
+ if (isNext16) {
409
+ if (dryRun) {
410
+ message = `[dry-run] Detected Next.js 16.2+ (${nextVersion}). Would configure withUnpeek adapter in next.config and .unpeek-maps/ vault.`;
411
+ } else if (updatedNextConfig || updatedGitignore) {
412
+ message = `Detected Next.js 16.2+ (${nextVersion}). Initialized unpeek: configured withUnpeek adapter in next.config and .unpeek-maps/ vault.`;
413
+ } else {
414
+ message = `Detected Next.js 16.2+ (${nextVersion}). unpeek is already initialized in this project.`;
415
+ }
333
416
  } else {
334
- message = `Detected framework: ${framework}. unpeek is already initialized in this project.`;
417
+ if (dryRun) {
418
+ message = `[dry-run] Detected framework: ${framework}. Would configure postbuild hook and .unpeek-maps/ vault.`;
419
+ } else if (updatedPackageJson || updatedGitignore) {
420
+ message = `Detected framework: ${framework}. Initialized unpeek: configured postbuild hook and .unpeek-maps/ vault.`;
421
+ } else {
422
+ message = `Detected framework: ${framework}. unpeek is already initialized in this project.`;
423
+ }
335
424
  }
336
425
  return {
337
426
  updatedPackageJson,
338
427
  updatedGitignore,
428
+ updatedNextConfig,
339
429
  framework,
430
+ isNext162Plus: isNext16,
340
431
  message
341
432
  };
342
433
  }
@@ -5783,7 +5874,7 @@ ${finalCode}`;
5783
5874
  }
5784
5875
 
5785
5876
  // src/cli/index.ts
5786
- var VERSION = "1.14.0";
5877
+ var VERSION = "1.15.0";
5787
5878
  function getPackageVersion() {
5788
5879
  try {
5789
5880
  const pkgUrl = new URL("../../package.json", importMetaUrl);
@@ -5901,23 +5992,26 @@ function printSummaryTable(summary) {
5901
5992
  }
5902
5993
  }
5903
5994
  function printInitResult(result, dryRun) {
5904
- const postbuild = result.updatedPackageJson ? dryRun ? "would be added" : "unpeek protect (added)" : "already configured";
5995
+ const postbuild = result.isNext162Plus ? "n/a (Build Adapter)" : result.updatedPackageJson ? dryRun ? "would be added" : "unpeek protect (added)" : "already configured";
5996
+ const adapterStatus = result.isNext162Plus ? result.updatedNextConfig ? dryRun ? "withUnpeek (would be configured)" : "withUnpeek (configured)" : "withUnpeek (already configured)" : void 0;
5905
5997
  const vault = result.updatedGitignore ? dryRun ? "would be ignored" : ".unpeek-maps/ (added)" : "already ignored";
5998
+ const rows = [
5999
+ ["framework", result.framework ?? "unknown"]
6000
+ ];
6001
+ if (adapterStatus) {
6002
+ rows.push(["next.config", adapterStatus]);
6003
+ }
6004
+ rows.push(["postbuild", postbuild]);
6005
+ rows.push(["vault", vault]);
5906
6006
  console.log("");
5907
6007
  console.log(brandBlock());
5908
6008
  console.log("");
5909
- console.log(
5910
- sectionBlock("PROJECT", [
5911
- ["framework", result.framework ?? "unknown"],
5912
- ["postbuild", postbuild],
5913
- ["vault", vault]
5914
- ])
5915
- );
6009
+ console.log(sectionBlock("PROJECT", rows));
5916
6010
  console.log("");
5917
6011
  if (dryRun) {
5918
6012
  console.log(` ${dim(result.message)}`);
5919
- } else if (result.updatedPackageJson || result.updatedGitignore) {
5920
- console.log(` ${dim("postbuild wired. maps stay private from the first build.")}`);
6013
+ } else if (result.updatedPackageJson || result.updatedGitignore || result.updatedNextConfig) {
6014
+ console.log(` ${dim("unpeek wired. maps stay private from the first build.")}`);
5921
6015
  } else {
5922
6016
  console.log(` ${dim(result.message)}`);
5923
6017
  }