create-nevela 0.1.3 → 0.1.5

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.
@@ -0,0 +1,118 @@
1
+ <?php
2
+
3
+ namespace Nevela\Laravel\Support;
4
+
5
+ /**
6
+ * What an app remembers about its dashboard, in apps/web/.nevela.json.
7
+ *
8
+ * - `template`: the version of the dashboard the app is on.
9
+ * - `files`: a fingerprint of every template file at that version. Comparing the app's
10
+ * files against these is how an update tells "you changed this" from "you didn't",
11
+ * with nothing to download and nothing to guess.
12
+ * - `dependencies`: the template's package.json dependencies at that version, for the
13
+ * same question about each dependency.
14
+ * - `history`: what each update did.
15
+ *
16
+ * Commit the file. Backups and incoming files live beside it in .nevela/, which ignores
17
+ * itself.
18
+ */
19
+ final class DashboardState
20
+ {
21
+ /** Files the installer personalises for every app (its name); never "changed by you". */
22
+ public const PERSONALISED = ['package.json', 'lib/site.ts'];
23
+
24
+ /** @param array<string, string>|null $files @param array<string, array<string, string>>|null $dependencies @param list<array<string, mixed>> $history */
25
+ public function __construct(
26
+ public ?string $template = null,
27
+ public ?array $files = null,
28
+ public ?array $dependencies = null,
29
+ public array $history = [],
30
+ ) {}
31
+
32
+ public static function path(string $web): string
33
+ {
34
+ return rtrim($web, '/\\').'/.nevela.json';
35
+ }
36
+
37
+ public static function read(string $web): self
38
+ {
39
+ $data = json_decode((string) @file_get_contents(self::path($web)), true);
40
+ if (! is_array($data)) {
41
+ return new self;
42
+ }
43
+
44
+ return new self(
45
+ isset($data['template']) ? (string) $data['template'] : null,
46
+ isset($data['files']) && is_array($data['files']) ? $data['files'] : null,
47
+ isset($data['dependencies']) && is_array($data['dependencies']) ? $data['dependencies'] : null,
48
+ isset($data['history']) && is_array($data['history']) ? array_values($data['history']) : [],
49
+ );
50
+ }
51
+
52
+ public function write(string $web): void
53
+ {
54
+ $files = $this->files ?? [];
55
+ ksort($files);
56
+ file_put_contents(self::path($web), json_encode(array_filter([
57
+ 'template' => $this->template,
58
+ 'files' => $files ?: null,
59
+ 'dependencies' => $this->dependencies,
60
+ 'history' => $this->history ?: null,
61
+ ], fn ($value) => $value !== null), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n");
62
+ }
63
+
64
+ /**
65
+ * Record a template as the one this app is now on.
66
+ *
67
+ * @param array<string, string> $templateFiles path => contents
68
+ */
69
+ public function adopt(string $version, array $templateFiles): void
70
+ {
71
+ $this->template = $version;
72
+ $this->files = DashboardUpdate::hashes($templateFiles);
73
+ $package = json_decode($templateFiles['package.json'] ?? '{}', true) ?: [];
74
+ $this->dependencies = array_filter([
75
+ 'dependencies' => $package['dependencies'] ?? null,
76
+ 'devDependencies' => $package['devDependencies'] ?? null,
77
+ ]);
78
+ }
79
+
80
+ /** The old template's package.json, as far as merging needs it, or null when it wasn't recorded. */
81
+ public function packageJson(): ?string
82
+ {
83
+ return $this->dependencies === null ? null : json_encode($this->dependencies);
84
+ }
85
+
86
+ /**
87
+ * The app's own fingerprints for a set of paths.
88
+ *
89
+ * @param iterable<string> $paths
90
+ * @return array<string, string|null>
91
+ */
92
+ public static function fingerprints(string $web, iterable $paths): array
93
+ {
94
+ $web = rtrim($web, '/\\');
95
+ $yours = [];
96
+ foreach ($paths as $path) {
97
+ $yours[$path] = is_file("{$web}/{$path}") ? DashboardUpdate::hash((string) file_get_contents("{$web}/{$path}")) : null;
98
+ }
99
+
100
+ return $yours;
101
+ }
102
+
103
+ /**
104
+ * The dashboard files this app has made its own, leaving out the two the installer
105
+ * personalises for everyone.
106
+ *
107
+ * @return array{changed: list<string>, deleted: list<string>}|null null when no fingerprints were recorded
108
+ */
109
+ public function yourChanges(string $web): ?array
110
+ {
111
+ if ($this->files === null) {
112
+ return null;
113
+ }
114
+ $base = array_diff_key($this->files, array_flip(self::PERSONALISED));
115
+
116
+ return DashboardUpdate::changes($base, self::fingerprints($web, array_keys($base)));
117
+ }
118
+ }
@@ -6,7 +6,7 @@ namespace Nevela\Laravel\Support;
6
6
  * Works out how to bring an app's dashboard up to a newer template without losing what
7
7
  * the developer changed.
8
8
  *
9
- * Three versions of every file are compared: the template the app was created from
9
+ * Three versions of every file are compared, by fingerprint: the template the app is on
10
10
  * (base), the newer template (next) and what is in the app now (yours). Only a file the
11
11
  * developer hasn't touched is replaced. Pure on purpose: no filesystem, no network.
12
12
  */
@@ -18,19 +18,37 @@ final class DashboardUpdate
18
18
  public const REMOVED = 'removed'; // gone from the template; you still have it unchanged
19
19
 
20
20
  /**
21
- * @param array<string, string> $base path => contents, the template the app started from
22
- * @param array<string, string> $next path => contents, the template to move to
23
- * @param array<string, string|null> $yours path => contents in the app, null when the file is missing
21
+ * A file's fingerprint. Line endings are ignored: git on Windows may have rewritten
22
+ * them, and that is not a change anyone made.
23
+ */
24
+ public static function hash(string $contents): string
25
+ {
26
+ return sha1(str_replace("\r\n", "\n", $contents));
27
+ }
28
+
29
+ /**
30
+ * @param array<string, string> $contents path => contents
31
+ * @return array<string, string> path => fingerprint
32
+ */
33
+ public static function hashes(array $contents): array
34
+ {
35
+ return array_map(self::hash(...), $contents);
36
+ }
37
+
38
+ /**
39
+ * @param array<string, string> $base path => fingerprint, the template the app is on
40
+ * @param array<string, string> $next path => fingerprint, the template to move to
41
+ * @param array<string, string|null> $yours path => fingerprint of the app's file, null when it is missing
24
42
  * @return array<string, string> path => one of the constants above. Files needing nothing are left out.
25
43
  */
26
44
  public static function plan(array $base, array $next, array $yours): array
27
45
  {
28
46
  $plan = [];
29
- foreach ($next as $path => $contents) {
47
+ foreach ($next as $path => $wanted) {
30
48
  $mine = $yours[$path] ?? null;
31
49
  $before = $base[$path] ?? null;
32
50
 
33
- if ($mine !== null && self::same($mine, $contents)) {
51
+ if ($mine === $wanted) {
34
52
  continue; // already there
35
53
  }
36
54
  if ($mine === null) {
@@ -41,15 +59,14 @@ final class DashboardUpdate
41
59
 
42
60
  continue;
43
61
  }
44
- if ($before !== null && self::same($before, $contents)) {
62
+ if ($before === $wanted) {
45
63
  continue; // the template didn't change this file; your version stands
46
64
  }
47
- $plan[$path] = $before !== null && self::same($mine, $before) ? self::UPDATE : self::CONFLICT;
65
+ $plan[$path] = $mine === $before ? self::UPDATE : self::CONFLICT;
48
66
  }
49
67
 
50
- foreach ($base as $path => $contents) {
51
- $mine = $yours[$path] ?? null;
52
- if (! isset($next[$path]) && $mine !== null && self::same($mine, $contents)) {
68
+ foreach ($base as $path => $before) {
69
+ if (! isset($next[$path]) && ($yours[$path] ?? null) === $before) {
53
70
  $plan[$path] = self::REMOVED;
54
71
  }
55
72
  }
@@ -59,19 +76,45 @@ final class DashboardUpdate
59
76
  return $plan;
60
77
  }
61
78
 
79
+ /**
80
+ * The files of the template that differ in the app: what the developer has made their own.
81
+ *
82
+ * @param array<string, string> $base path => fingerprint, the template the app is on
83
+ * @param array<string, string|null> $yours path => fingerprint of the app's file, null when it is missing
84
+ * @return array{changed: list<string>, deleted: list<string>}
85
+ */
86
+ public static function changes(array $base, array $yours): array
87
+ {
88
+ $changed = [];
89
+ $deleted = [];
90
+ foreach ($base as $path => $before) {
91
+ $mine = $yours[$path] ?? null;
92
+ if ($mine === null) {
93
+ $deleted[] = $path;
94
+ } elseif ($mine !== $before) {
95
+ $changed[] = $path;
96
+ }
97
+ }
98
+ sort($changed);
99
+ sort($deleted);
100
+
101
+ return ['changed' => $changed, 'deleted' => $deleted];
102
+ }
103
+
62
104
  /**
63
105
  * Move the app's package.json to the template's dependency versions, keeping its name
64
106
  * and anything else the developer added.
65
107
  *
66
108
  * A dependency is moved when the app still has the version the old template had, or
67
109
  * doesn't have it at all. One the developer pinned differently is left and reported.
110
+ * Without the old template (`$base` null) only missing dependencies are added.
68
111
  *
69
112
  * @return array{json: string, changed: array<string, string>, kept: array<string, string>}
70
113
  */
71
- public static function mergePackageJson(string $yours, string $base, string $next): array
114
+ public static function mergePackageJson(string $yours, ?string $base, string $next): array
72
115
  {
73
116
  $app = json_decode($yours, true, 512, JSON_THROW_ON_ERROR);
74
- $old = json_decode($base, true, 512, JSON_THROW_ON_ERROR);
117
+ $old = $base === null ? null : json_decode($base, true, 512, JSON_THROW_ON_ERROR);
75
118
  $new = json_decode($next, true, 512, JSON_THROW_ON_ERROR);
76
119
  $changed = [];
77
120
  $kept = [];
@@ -83,7 +126,7 @@ final class DashboardUpdate
83
126
  continue;
84
127
  }
85
128
  $before = $old[$section][$name] ?? null;
86
- if ($mine === null || $mine === $before) {
129
+ if ($mine === null || ($old !== null && $mine === $before)) {
87
130
  $app[$section][$name] = $version;
88
131
  $changed[$name] = ($mine ?? 'not installed').' → '.$version;
89
132
  } else {
@@ -101,10 +144,4 @@ final class DashboardUpdate
101
144
 
102
145
  return ['json' => $json."\n", 'changed' => $changed, 'kept' => $kept];
103
146
  }
104
-
105
- /** Equal apart from line endings: git on Windows may have rewritten them. */
106
- public static function same(string $a, string $b): bool
107
- {
108
- return $a === $b || str_replace("\r\n", "\n", $a) === str_replace("\r\n", "\n", $b);
109
- }
110
147
  }
@@ -0,0 +1,144 @@
1
+ <?php
2
+
3
+ namespace Nevela\Laravel\Support;
4
+
5
+ use Illuminate\Support\Facades\Http;
6
+ use PharData;
7
+ use RecursiveDirectoryIterator;
8
+ use RecursiveIteratorIterator;
9
+ use RuntimeException;
10
+ use Throwable;
11
+
12
+ /**
13
+ * The dashboard as it shipped in a given version of Nevela.
14
+ *
15
+ * Tried in order, so an update never depends on one place being up to date:
16
+ *
17
+ * 1. NEVELA_TEMPLATE_DIR, a folder of create-nevela-<version>.tgz files. For working on
18
+ * Nevela itself, and for updating without a connection.
19
+ * 2. The create-nevela package on npm, which carries the dashboard.
20
+ * 3. The repository's own tag on GitHub. Tags are pushed before anything is published,
21
+ * so this works in the gap between a release and its npm package.
22
+ */
23
+ final class TemplateSource
24
+ {
25
+ private const NPM = 'https://registry.npmjs.org/create-nevela/-/create-nevela-%s.tgz';
26
+
27
+ private const GITHUB = 'https://codeload.github.com/MarkColeMukisa/nevela/tar.gz/refs/tags/v%s';
28
+
29
+ /** Never part of an app's dashboard: build output, installs and local settings. */
30
+ private const NEVER = ['node_modules', '.next', '.env.local', 'tsconfig.tsbuildinfo', 'next-env.d.ts'];
31
+
32
+ /**
33
+ * @return array{files: array<string, string>, from: string} path => contents, and where it came from
34
+ */
35
+ public static function fetch(string $version): array
36
+ {
37
+ $dir = rtrim(sys_get_temp_dir(), '/\\').DIRECTORY_SEPARATOR.'nevela-template-'.$version.'-'.bin2hex(random_bytes(4));
38
+ mkdir($dir, 0775, true);
39
+ $archive = $dir.DIRECTORY_SEPARATOR.'template.tgz';
40
+ $tried = [];
41
+
42
+ try {
43
+ $local = rtrim((string) env('NEVELA_TEMPLATE_DIR', ''), '/\\');
44
+ if ($local !== '' && is_file("{$local}/create-nevela-{$version}.tgz")) {
45
+ copy("{$local}/create-nevela-{$version}.tgz", $archive);
46
+
47
+ return ['files' => self::read($archive, $dir, 'package/template/web', false), 'from' => 'NEVELA_TEMPLATE_DIR'];
48
+ }
49
+
50
+ foreach (['npm' => [self::NPM, 'package/template/web', false], 'GitHub' => [self::GITHUB, "nevela-{$version}/apps/web", true]] as $name => [$url, $root, $filter]) {
51
+ try {
52
+ $response = Http::timeout(60)->get(sprintf($url, $version));
53
+ if (! $response->successful()) {
54
+ $tried[] = "{$name}: HTTP {$response->status()}";
55
+
56
+ continue;
57
+ }
58
+ file_put_contents($archive, $response->body());
59
+
60
+ return ['files' => self::read($archive, $dir.DIRECTORY_SEPARATOR.strtolower($name), $root, $filter), 'from' => $name];
61
+ } catch (Throwable $e) {
62
+ $tried[] = "{$name}: ".$e->getMessage();
63
+ }
64
+ }
65
+
66
+ throw new RuntimeException("version {$version} was not found (".implode('; ', $tried).')');
67
+ } finally {
68
+ self::remove($dir);
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Unpack an archive and read the dashboard out of it.
74
+ *
75
+ * @param bool $filter Whether this is the repository's own apps/web, whose example resources aren't part of the template
76
+ * @return array<string, string>
77
+ */
78
+ private static function read(string $archive, string $into, string $root, bool $filter): array
79
+ {
80
+ if (! is_dir($into)) {
81
+ mkdir($into, 0775, true);
82
+ }
83
+ (new PharData($archive))->extractTo($into, null, true);
84
+ $base = $into.DIRECTORY_SEPARATOR.str_replace('/', DIRECTORY_SEPARATOR, $root);
85
+ if (! is_dir($base)) {
86
+ throw new RuntimeException('the archive has no dashboard in it');
87
+ }
88
+
89
+ $files = [];
90
+ foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($base, RecursiveDirectoryIterator::SKIP_DOTS)) as $file) {
91
+ $path = str_replace('\\', '/', substr($file->getPathname(), strlen($base) + 1));
92
+ if (array_intersect(explode('/', $path), self::NEVER) !== []) {
93
+ continue;
94
+ }
95
+ // npm can't carry .gitignore files, so its template holds them as _gitignore.
96
+ $path = preg_replace('#(^|/)_gitignore$#', '$1.gitignore', $path);
97
+ $files[$path] = (string) file_get_contents($file->getPathname());
98
+ }
99
+
100
+ return $filter ? self::withoutExamples($files) : $files;
101
+ }
102
+
103
+ /**
104
+ * The repository's dashboard has example resources in it (a Product, say). They are
105
+ * generated from Laravel, not part of what a new app is given.
106
+ *
107
+ * @param array<string, string> $files
108
+ * @return array<string, string>
109
+ */
110
+ public static function withoutExamples(array $files): array
111
+ {
112
+ $slugs = [];
113
+ foreach ($files as $path => $contents) {
114
+ if (preg_match('#^resources/[^/]+\.resource\.ts$#', $path) && preg_match('/slug:\s*"([^"]+)"/', $contents, $m)) {
115
+ $slugs[] = $m[1];
116
+ }
117
+ }
118
+
119
+ return array_filter($files, function (string $path) use ($slugs) {
120
+ if (preg_match('#^resources/[^/]+\.resource\.ts$#', $path) || $path === 'resources/index.ts') {
121
+ return false;
122
+ }
123
+ foreach ($slugs as $slug) {
124
+ if (str_starts_with($path, "app/dashboard/{$slug}/")) {
125
+ return false;
126
+ }
127
+ }
128
+
129
+ return true;
130
+ }, ARRAY_FILTER_USE_KEY);
131
+ }
132
+
133
+ private static function remove(string $dir): void
134
+ {
135
+ if (! is_dir($dir)) {
136
+ return;
137
+ }
138
+ $items = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
139
+ foreach ($items as $item) {
140
+ $item->isDir() ? @rmdir($item->getPathname()) : @unlink($item->getPathname());
141
+ }
142
+ @rmdir($dir);
143
+ }
144
+ }
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
 
3
- import { useState } from "react";
3
+ import { useState, useSyncExternalStore } from "react";
4
4
  import Link from "next/link";
5
5
  import { BellIcon, MoonIcon, SunIcon } from "lucide-react";
6
6
  import { Button } from "@/components/ui/button";
@@ -30,9 +30,31 @@ function applyTheme(theme: Theme) {
30
30
  if (theme !== "system") root.classList.add(theme);
31
31
  }
32
32
 
33
+ const DARK_QUERY = "(prefers-color-scheme: dark)";
34
+
35
+ function watchSystemTheme(onChange: () => void) {
36
+ const query = window.matchMedia(DARK_QUERY);
37
+ query.addEventListener("change", onChange);
38
+ return () => query.removeEventListener("change", onChange);
39
+ }
40
+
41
+ /**
42
+ * Whether the device prefers dark. The server can't know, so it and the first render in
43
+ * the browser both say no, and the real answer follows straight after: reading it during
44
+ * that first render would give the browser a different page from the one the server sent.
45
+ */
46
+ function useSystemDark() {
47
+ return useSyncExternalStore(
48
+ watchSystemTheme,
49
+ () => window.matchMedia(DARK_QUERY).matches,
50
+ () => false,
51
+ );
52
+ }
53
+
33
54
  export function DashboardHeader({ user, initialTheme, notices = [] }: { user: DashboardUser; initialTheme: Theme; notices?: Notice[] }) {
34
55
  const [theme, setTheme] = useState<Theme>(initialTheme);
35
- const dark = theme === "dark" || (theme === "system" && typeof window !== "undefined" && window.matchMedia("(prefers-color-scheme: dark)").matches);
56
+ const systemDark = useSystemDark();
57
+ const dark = theme === "dark" || (theme === "system" && systemDark);
36
58
  const needsAttention = notices.some((notice) => notice.tone === "warning");
37
59
 
38
60
  return (
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nevela-web",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "private": true,
5
5
  "type": "module",
6
6
  "scripts": {
@@ -22,7 +22,7 @@
22
22
  "cmdk": "^1.1.1",
23
23
  "date-fns": "^4.4.0",
24
24
  "lucide-react": "^1.46.0",
25
- "next": "16.0.4",
25
+ "next": "16.3.8",
26
26
  "radix-ui": "^1.6.7",
27
27
  "react": "19.3.0",
28
28
  "react-day-picker": "^10.0.1",