building out frontend and docker

This commit is contained in:
C-West8
2026-10-23 06:52:20 -05:00
parent 42a1ce7d30
commit c9ce46d7bc
58 changed files with 2665 additions and 50 deletions

18
.dockerignore Normal file
View File

@@ -0,0 +1,18 @@
.git
.svelte-kit
.vscode
build
node_modules
docs
.env
.env.*
!.env.example
*.db
*.db-shm
*.db-wal
Dockerfile
docker-compose.yml
.dockerignore

View File

@@ -1,3 +1,12 @@
# "live" (real SharePoint/ACC) or "simulated" (in-memory demo; nothing persists)
SYNC_BACKEND=simulated
# Admin pages are disabled unless both are set
ADMIN_USERNAME=admin
ADMIN_PASSWORD=change-me
# Optional; without it admin logins end when the server restarts
SESSION_SECRET=
DATABASE_PATH=local.db DATABASE_PATH=local.db
POLL_INTERVAL_SECONDS=180 POLL_INTERVAL_SECONDS=180
MANUAL_RUN_COOLDOWN_SECONDS=30 MANUAL_RUN_COOLDOWN_SECONDS=30

2
.gitignore vendored
View File

@@ -23,3 +23,5 @@ vite.config.js.timestamp-*
vite.config.ts.timestamp-* vite.config.ts.timestamp-*
# SQLite # SQLite
*.db *.db
*.db-shm
*.db-wal

34
Dockerfile Normal file
View File

@@ -0,0 +1,34 @@
# syntax=docker/dockerfile:1
FROM node:24-bookworm-slim AS base
WORKDIR /app
# Build stage. better-sqlite3 normally downloads a prebuilt binary; the compilers
# are only a fallback for when no prebuilt one matches.
FROM base AS build
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 make g++ \
&& rm -rf /var/lib/apt/lists/*
COPY package.json package-lock.json .npmrc ./
RUN npm ci
COPY . .
# The address users open the app at, when served over plain HTTP (see docs/runbook.md).
ARG ORIGIN
RUN npm run build && npm prune --omit=dev
# Runtime stage: the built server, production dependencies and the migrations.
FROM base
ENV NODE_ENV=production \
PORT=3000 \
DATABASE_PATH=/data/sync.db
COPY --from=build /app/package.json ./
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/build ./build
COPY --from=build /app/drizzle ./drizzle
RUN mkdir /data && chown node:node /data
USER node
VOLUME /data
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
CMD ["node", "-e", "fetch(`http://127.0.0.1:${process.env.PORT}/healthz`).then((r) => process.exit(r.ok ? 0 : 1), () => process.exit(1))"]
CMD ["node", "build"]

View File

@@ -14,10 +14,30 @@ There is also a small web page that shows each project's file tree, sync status,
## Status ## Status
In development. The sync core and database layer are built and tested against simulated SharePoint and ACC. The real SharePoint/ACC connections and the web UI are next. See: In development. The sync engine, database, scheduler and web UI are built, and you can try them end to end in **simulated mode**. The real SharePoint/ACC connections come next, once credentials arrive. See:
- [docs/design.md](docs/design.md): architecture, interfaces, data model and build order - [docs/design.md](docs/design.md): architecture, interfaces, data model and build order
- [docs/access-setup.md](docs/access-setup.md): what IT and the ACC admin need to set up - [docs/access-setup.md](docs/access-setup.md): what IT and the ACC admin need to set up
- [docs/runbook.md](docs/runbook.md): deploying with Docker/Portainer, updating, backups, troubleshooting
## Running it locally
```sh
npm install
cp .env.example .env # simulated mode, admin login admin / change-me
npm run dev
```
In simulated mode, SharePoint and ACC are in-memory stand-ins. A demo project is created at startup, filled with sample files, and edited at random every 45 seconds so there's something to watch. One file (`Corrupt model.rvt`) is always rejected, so the issues list has something in it. Nothing persists: a restart starts over.
| Command | What it does |
| --------------- | ------------------------------------------------------------------------------------------------------------------- |
| `npm test` | Unit and integration tests (Vitest) |
| `npm run check` | Type check |
| `npm run lint` | Prettier + ESLint |
| `npm run build` | Production build into `build/` (`node build` runs it; run it from the repo root so `drizzle/` migrations are found) |
To try the Docker image locally: put `ADMIN_USERNAME`, `ADMIN_PASSWORD`, `SESSION_SECRET`, `SYNC_BACKEND=simulated` and `ORIGIN=http://localhost:3000` in `.env`, then run `docker compose up -d --build`.
## Stack ## Stack

31
docker-compose.yml Normal file
View File

@@ -0,0 +1,31 @@
# Portainer stack (or `docker compose up -d --build`). Set the variables below in
# Portainer's stack environment, or in a .env file next to this one. See docs/runbook.md.
services:
sync:
build:
context: .
args:
# Only when serving plain HTTP: the exact address users open, e.g. http://sync-server:3000
ORIGIN: ${ORIGIN:-}
image: sharepoint-acc-sync:latest
restart: unless-stopped
ports:
- '${HOST_PORT:-3000}:3000'
environment:
SYNC_BACKEND: ${SYNC_BACKEND:-live}
ADMIN_USERNAME: ${ADMIN_USERNAME:?Set ADMIN_USERNAME}
ADMIN_PASSWORD: ${ADMIN_PASSWORD:?Set ADMIN_PASSWORD}
SESSION_SECRET: ${SESSION_SECRET:?Set SESSION_SECRET to a long random string}
POLL_INTERVAL_SECONDS: ${POLL_INTERVAL_SECONDS:-180}
MANUAL_RUN_COOLDOWN_SECONDS: ${MANUAL_RUN_COOLDOWN_SECONDS:-30}
MAX_ATTEMPTS: ${MAX_ATTEMPTS:-3}
HISTORY_RETENTION_DAYS: ${HISTORY_RETENTION_DAYS:-7}
# Times on the status pages are shown in this zone
TZ: ${TZ:-America/Chicago}
volumes:
- sync-data:/data
# Lets a sync in progress finish when the container is stopped or updated
stop_grace_period: 2m
volumes:
sync-data:

View File

@@ -7,7 +7,7 @@
| Direction | One-way, SharePoint → ACC | | Direction | One-way, SharePoint → ACC |
| Latency | Poll every ~3 min (configurable); "Run now" for immediate sync | | Latency | Poll every ~3 min (configurable); "Run now" for immediate sync |
| Operations | Create, update (new ACC version), rename, move, delete | | Operations | Create, update (new ACC version), rename, move, delete |
| Scope | Many projects per server; each project is a 1:1 pair of SharePoint root ↔ ACC root | | Scope | Many projects per server; each project is a 1:1 pair of SharePoint root and ACC root |
| ACC ownership | Only touch ACC items this tool created; hand-added ACC content is left alone | | ACC ownership | Only touch ACC items this tool created; hand-added ACC content is left alone |
| Ignored files | Temp/lock/system files are skipped silently (not issues) | | Ignored files | Temp/lock/system files are skipped silently (not issues) |
| Retries | 3 attempts across runs, then surface as an issue | | Retries | 3 attempts across runs, then surface as an issue |
@@ -164,14 +164,42 @@ MAX_ATTEMPTS=3
HISTORY_RETENTION_DAYS=7 HISTORY_RETENTION_DAYS=7
``` ```
Env vars are declared and validated with `zod` in `src/env.ts` (SvelteKit's `defineEnvVars`); the server refuses to start on bad config. Only the variables in use so far are declared; the Graph, APS and admin ones get added along with the code that reads them. Run exactly **one** replica, because the locks live in-process. Env vars are declared and validated with `zod` in `src/env.ts` (SvelteKit's `defineEnvVars`); the server refuses to start on bad config. Only the variables in use so far are declared; the Graph and APS ones get added along with the adapters that read them. Run exactly **one** replica, because the locks live in-process.
`SYNC_BACKEND` picks the SharePoint/ACC connection:
- **`live`**: the real Graph/APS adapters. Until those exist, a `NotConnectedBackend` stands in. Runs fail as an outage (no attempts charged) and the add-project form explains why.
- **`simulated`**: `FakeDrive`/`FakeAcc` behind a `SimulatedBackend` that resolves any pasted path, seeds sample files and makes random edits. The database is in-memory in this mode, so the fake services and the sync state never disagree.
`ADMIN_USERNAME`/`ADMIN_PASSWORD` turn the admin pages on; `SESSION_SECRET` keeps logins valid across restarts.
Two settings live outside `src/env.ts`:
- **`ORIGIN`** is read at build time (`paths.origin` in `vite.config.ts`). SvelteKit 3 fixes the app's address at build time, and adapter-node otherwise assumes HTTPS. So over plain HTTP, form posts fail the CSRF check, and the session cookie is marked `Secure`, unless `ORIGIN` is set. Behind an HTTPS reverse proxy, leave it unset.
- **`TZ`** is the container's time zone. Times are formatted on the server first, so it should match the users' zone.
### Deployment
- `Dockerfile`: a multi-stage build on `node:24-bookworm-slim`. The runtime stage holds `build/`, production `node_modules` (`better-sqlite3`, `zod`; everything else is bundled) and `drizzle/`, runs as `node`, and keeps the database at `/data/sync.db`.
- `docker-compose.yml` is the Portainer stack: one service, the `sync-data` volume, required settings enforced with `${VAR:?}`, and `stop_grace_period: 2m` so a run in progress can finish.
- `GET /healthz` backs the image's `HEALTHCHECK`.
- On startup, runs left without an outcome (the process was killed mid-run) are closed as failed. The next run carries on from the saved item state.
- Operations are covered in [runbook.md](runbook.md).
### Code conventions
- **Imports:** never `../`. Anything outside the current folder goes through the `#lib/...` alias (`package.json` `imports`). Every local import carries its `.ts` extension, which SvelteKit 3's tsconfig requires.
- **Internal links:** always built with `resolve()` from `$app/paths` (enforced by lint).
## Source layout ## Source layout
``` ```
src/env.ts # env var declarations + validation src/env.ts # env var declarations + validation
src/hooks.server.ts # init → startApp(config); admin route guard
src/lib/server/ src/lib/server/
container.ts # (todo) composition root: the only place adapters are chosen container.ts # composition root: the only place adapters are chosen
app.ts # holds the running services (survives dev reloads)
forms.ts logger.ts
sync/ sync/
core/ # pure TS, no SvelteKit imports core/ # pure TS, no SvelteKit imports
model.ts ports.ts errors.ts model.ts ports.ts errors.ts
@@ -182,16 +210,22 @@ src/lib/server/
tree.ts ignore.ts tree.ts ignore.ts
coordinator.ts # per-project lock + manual cooldown coordinator.ts # per-project lock + manual cooldown
scheduler.ts # non-overlapping interval tasks scheduler.ts # non-overlapping interval tasks
testing/ # FakeDrive, FakeAcc, harness (real engine + in-memory SQLite) project-service.ts # admin operations: create/edit/reset/delete, retry
testing/ # harness: real engine + in-memory SQLite + fakes
status/ # read models for the UI: summaries, issues, file tree
auth/ # admin credentials, signed session tokens, cookie helpers
adapters/ adapters/
sqlite/ # Drizzle schema, migrations runner, repositories sqlite/ # Drizzle schema, migrations runner, repositories
graph/ # (todo) GraphChangeSource, auth (msal-node) simulated/ # FakeDrive, FakeAcc, SimulatedBackend (demo + tests)
aps/ # (todo) ApsDocumentTarget, auth, upload flow not-connected.ts # placeholder for live mode until Graph/APS exist
graph/ # (todo) GraphChangeSource + SourceRootResolver, auth (msal-node)
aps/ # (todo) ApsDocumentTarget + TargetRootResolver, upload flow
http/ # (todo) shared fetch wrapper: retry, backoff, Retry-After http/ # (todo) shared fetch wrapper: retry, backoff, Retry-After
auth/ # (todo) admin session cookie src/lib/components/ # Badge, FileTree, RunNowButton, ProjectFields, TimeAgo, AutoRefresh
src/lib/styles/app.css # design tokens + shared styles
src/routes/ # projects, project status + history, admin, healthz
drizzle/ # generated SQL migrations (npm run db:generate) drizzle/ # generated SQL migrations (npm run db:generate)
src/hooks.server.ts # (todo) init → container → scheduler; admin route guard Dockerfile docker-compose.yml
src/routes/ # (todo) UI
``` ```
## Open questions (resolved by spikes) ## Open questions (resolved by spikes)
@@ -205,11 +239,11 @@ src/routes/ # (todo) UI
0. **Access requests:** start now; they're the slowest part. See [access-setup.md](access-setup.md). 0. **Access requests:** start now; they're the slowest part. See [access-setup.md](access-setup.md).
1. **Spikes** against a sandbox ACC project and test SharePoint site. Answer the open questions above and record the results here. 1. **Spikes** against a sandbox ACC project and test SharePoint site. Answer the open questions above and record the results here.
2. ✅ **Scaffold:** SvelteKit 3 + adapter-node, TypeScript strict, Drizzle + better-sqlite3, zod, Vitest, ESLint/Prettier. (Dockerfile moves to step 9.) 2. **Scaffold (done):** SvelteKit 3 + adapter-node, TypeScript strict, Drizzle + better-sqlite3, zod, Vitest, ESLint/Prettier. (Dockerfile moves to step 9.)
3. ✅ **Core:** ports, IgnorePolicy, ingest, planner, reconciler and SyncEngine, tested against FakeDrive/FakeAcc. 3. **Core (done):** ports, IgnorePolicy, ingest, planner, reconciler and SyncEngine, tested against FakeDrive/FakeAcc.
4. ✅ **SQLite repositories** and migrations. 4. **SQLite repositories and migrations (done).**
5. **GraphChangeSource.** 5. **GraphChangeSource** + SharePoint URL resolver.
6. **ApsDocumentTarget**, including the fallbacks chosen in step 1. 6. **ApsDocumentTarget** + ACC folder URL resolver, including the fallbacks chosen in step 1.
7. **Wiring:** ✅ coordinator, scheduler. Still to do: composition root, `init` hook, retention job schedule. 7. **Wiring (done):** coordinator, scheduler, composition root, `init` hook, history pruning, simulated mode.
8. **UI:** status pages, Run now, admin login, project add/edit/reset. 8. **UI (done):** projects list, project status (title block, issues with retry, file tree), 7-day history with per-run detail, Run now with cooldown, admin login, add/edit/resume/reset/remove.
9. **Ship:** Docker image, Portainer stack file, runbook. 9. **Ship (done):** Docker image, Portainer stack file, runbook. The image was tested locally. A first Portainer deploy is still to do.

84
docs/runbook.md Normal file
View File

@@ -0,0 +1,84 @@
# Runbook
How to deploy, update, back up and troubleshoot the sync server.
## What runs
- **One container**, built from the `Dockerfile`. Never run more than one copy: the locks that stop two syncs of the same project overlapping live inside the process.
- **One volume**, `sync-data`, mounted at `/data`. It holds `sync.db`, the SQLite database with the projects, the record of which ACC item belongs to which SharePoint item, and the last 7 days of history.
- **Port 3000** inside the container. `HOST_PORT` sets the port on the server (default 3000).
- **Health check:** `GET /healthz` answers `{"status":"ok"}` once the server has started. Docker checks it every 30 seconds, and Portainer shows the result as healthy or unhealthy.
- **Logs:** one JSON object per line on stdout. View them in Portainer under the container's logs.
## Choose how people will reach it
Decide this before the first deploy, because it changes how the image is built.
- **Behind an HTTPS reverse proxy** (recommended). The proxy must pass the original `Host` header on, which most do by default. Leave `ORIGIN` empty.
- **Plain HTTP** on the VPN, for example `http://sync-server:3000`. Set `ORIGIN` to exactly the address people will type, with no trailing slash. `ORIGIN` is fixed when the image is built, so the app only works at that one address: not by IP if `ORIGIN` uses a name, and not on a different port.
If `ORIGIN` is wrong or missing over plain HTTP, pages still load, but every button that submits something fails with "Cross-site POST form submissions are forbidden".
## Settings
Set these in the Portainer stack's **Environment variables**, or in a `.env` file next to `docker-compose.yml` when using the command line. Never put them in the repository.
| Variable | Required | Default | What it is |
| ----------------------------- | --------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ADMIN_USERNAME` | yes | | Login for the admin pages |
| `ADMIN_PASSWORD` | yes | | Password for the admin pages |
| `SESSION_SECRET` | yes | | Signs admin logins. Any long random string, e.g. the output of `node -e "console.log(crypto.randomBytes(32).toString('hex'))"`. Changing it logs admins out |
| `ORIGIN` | see above | | The app's address when served over plain HTTP. Build time only |
| `HOST_PORT` | no | `3000` | Port on the server |
| `SYNC_BACKEND` | no | `live` | `simulated` runs the in-memory demo instead (nothing is saved) |
| `TZ` | no | `America/Chicago` | Time zone the status pages show times in |
| `POLL_INTERVAL_SECONDS` | no | `180` | How often every project is checked for changes |
| `MANUAL_RUN_COOLDOWN_SECONDS` | no | `30` | Minimum gap between "Run now" presses for one project |
| `MAX_ATTEMPTS` | no | `3` | Failed tries before a file is reported as an issue |
| `HISTORY_RETENTION_DAYS` | no | `7` | How long sync history is kept |
The Microsoft Graph and APS credentials will be added to this table when the live connections are built.
## First deploy (Portainer)
1. **Stacks → Add stack**, give it a name such as `sharepoint-acc-sync`, and choose **Repository**.
2. Enter the repository URL and the branch (`refs/heads/main`). The compose path is `docker-compose.yml`. If the repository is private, turn on authentication and use a read-only access token.
3. Under **Environment variables**, add the settings above.
4. **Deploy the stack.** Portainer builds the image from the `Dockerfile`, which takes a few minutes the first time.
5. Check it's working:
- the container shows as **healthy** in Portainer;
- the logs show `Sync server started` with the expected `backend`;
- the projects page opens at the chosen address, and the admin login works.
From the command line on the Docker host, the equivalent is `docker compose up -d --build` in a clone of the repository, with the settings in `.env`.
## Updating
1. In Portainer, open the stack and use **Pull and redeploy**. Make sure the image is rebuilt, not reused: after the first update, check that the change is actually live.
2. On the command line: `git pull`, then `docker compose up -d --build`.
When the container stops, a sync in progress gets up to 2 minutes to finish. If it doesn't finish in time, it's marked in the history as stopped by a restart, and the next sync picks up whatever was left. Nothing needs cleaning up by hand.
## Backups
Back up `sync.db`. Losing it means the tool forgets which ACC files it created. It would then treat every SharePoint file as new, and could not tidy up the ACC copies it made before.
To take a copy while the server is running:
```sh
docker exec <container> node -e "require('better-sqlite3')('/data/sync.db').backup('/data/backup.db').then(() => console.log('backed up'))"
docker cp <container>:/data/backup.db ./sync-backup.db
```
To restore one: stop the stack, put the backup in the volume as `sync.db`, delete any `sync.db-wal` and `sync.db-shm` beside it, then start the stack.
## Troubleshooting
| Symptom | Likely cause and fix |
| ------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------- |
| Deploy fails with "required variable … is missing a value" | A required setting isn't set. Add it to the stack's environment variables |
| Buttons fail with "Cross-site POST form submissions are forbidden" | Plain HTTP without the right `ORIGIN`, or the app was opened at a different address. Set `ORIGIN` and rebuild |
| Admin login appears to work but doesn't stick | Plain HTTP without `ORIGIN`: the login cookie is marked HTTPS-only, so the browser drops it. Set `ORIGIN` and rebuild |
| Container is unhealthy or keeps restarting | Check the logs. Bad settings are reported by name at startup |
| Times on the pages are off by some hours | `TZ` doesn't match the office's time zone |
| A project shows **Paused** | Its SharePoint folder was deleted or couldn't be found. Nothing was deleted in ACC. Fix the folder, then **Resume** it on the admin page |

4
src/app.d.ts vendored
View File

@@ -3,7 +3,9 @@
declare global { declare global {
namespace App { namespace App {
// interface Error {} // interface Error {}
// interface Locals {} interface Locals {
isAdmin: boolean;
}
// interface PageData {} // interface PageData {}
// interface PageState {} // interface PageState {}
// interface Platform {} // interface Platform {}

View File

@@ -4,6 +4,12 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="text-scale" content="scale" /> <meta name="text-scale" content="scale" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin="anonymous" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Barlow:wght@400;500;600&family=Barlow+Semi+Condensed:wght@500;600&display=swap"
/>
%sveltekit.head% %sveltekit.head%
</head> </head>
<body data-sveltekit-preload-data="hover"> <body data-sveltekit-preload-data="hover">

View File

@@ -4,7 +4,30 @@ import { z } from 'zod';
const positiveInt = (fallback: number) => const positiveInt = (fallback: number) =>
z.coerce.number<string | undefined>().int().positive().default(fallback); z.coerce.number<string | undefined>().int().positive().default(fallback);
const optional = z
.string()
.optional()
.transform((value) => value || undefined);
export const variables = defineEnvVars({ export const variables = defineEnvVars({
SYNC_BACKEND: {
description:
'"live" for real SharePoint/ACC, or "simulated" for an in-memory demo (nothing is persisted).',
schema: z.enum(['live', 'simulated']).default('live')
},
ADMIN_USERNAME: {
description: 'Username for the admin pages. Admin pages are disabled when unset.',
schema: optional
},
ADMIN_PASSWORD: {
description: 'Password for the admin pages.',
schema: optional
},
SESSION_SECRET: {
description:
'Signs admin sessions. If unset, a random secret is used and logins end on restart.',
schema: optional
},
DATABASE_PATH: { DATABASE_PATH: {
description: 'Path to the SQLite database file (mount a volume here in Docker).', description: 'Path to the SQLite database file (mount a volume here in Docker).',
schema: z.string().min(1).default('local.db') schema: z.string().min(1).default('local.db')

37
src/hooks.server.ts Normal file
View File

@@ -0,0 +1,37 @@
import { redirect } from '@sveltejs/kit';
import type { Handle, ServerInit } from '@sveltejs/kit/hooks';
import { building } from '$app/env';
import * as env from '$app/env/private';
import { services, startApp, stopApp } from '#lib/server/app.ts';
import { SESSION_COOKIE } from '#lib/server/auth/admin-auth.ts';
export const init: ServerInit = async () => {
if (building) return;
await startApp({
backend: env.SYNC_BACKEND,
databasePath: env.DATABASE_PATH,
pollIntervalSeconds: env.POLL_INTERVAL_SECONDS,
manualRunCooldownSeconds: env.MANUAL_RUN_COOLDOWN_SECONDS,
maxAttempts: env.MAX_ATTEMPTS,
historyRetentionDays: env.HISTORY_RETENTION_DAYS,
admin: {
username: env.ADMIN_USERNAME,
password: env.ADMIN_PASSWORD,
sessionSecret: env.SESSION_SECRET
}
});
// adapter-node emits this on SIGINT/SIGTERM; let in-flight runs finish first.
process.once('sveltekit:shutdown', () => void stopApp());
};
const PUBLIC_ADMIN_PATHS = new Set(['/admin/login']);
export const handle: Handle = async ({ event, resolve }) => {
event.locals.isAdmin = services().auth.verifyToken(event.cookies.get(SESSION_COOKIE));
const { pathname, search } = event.url;
if (pathname.startsWith('/admin') && !PUBLIC_ADMIN_PATHS.has(pathname) && !event.locals.isAdmin) {
redirect(303, `/admin/login?next=${encodeURIComponent(pathname + search)}`);
}
return resolve(event);
};

View File

@@ -0,0 +1,13 @@
<script lang="ts">
import { invalidateAll } from '$app/navigation';
/** Reloads the page's data on an interval while the tab is visible. */
let { intervalMs }: { intervalMs: number } = $props();
$effect(() => {
const timer = setInterval(() => {
if (document.visibilityState === 'visible') void invalidateAll();
}, intervalMs);
return () => clearInterval(timer);
});
</script>

View File

@@ -0,0 +1,7 @@
<script lang="ts">
import type { Tone } from '#lib/format.ts';
let { tone, label }: { tone: Tone; label: string } = $props();
</script>
<span class="badge tone-{tone}">{label}</span>

View File

@@ -0,0 +1,179 @@
<script lang="ts">
import { SvelteSet } from 'svelte/reactivity';
import Badge from '#lib/components/Badge.svelte';
import TimeAgo from '#lib/components/TimeAgo.svelte';
import { ITEM_STATUS } from '#lib/format.ts';
import type { TreeNode } from '#lib/server/status/tree-view.ts';
let { nodes }: { nodes: TreeNode[] } = $props();
const collapsed = new SvelteSet<string>();
let attentionOnly = $state(false);
interface Row {
node: TreeNode;
depth: number;
}
const needsAttention = (node: TreeNode): boolean =>
node.status !== 'synced' || node.children.some(needsAttention);
const rows = $derived.by(() => {
const result: Row[] = [];
const walk = (list: TreeNode[], depth: number) => {
for (const node of list) {
if (attentionOnly && !needsAttention(node)) continue;
result.push({ node, depth });
if (node.isFolder && !collapsed.has(node.sourceId)) walk(node.children, depth + 1);
}
};
walk(nodes, 0);
return result;
});
function toggle(id: string) {
if (collapsed.has(id)) collapsed.delete(id);
else collapsed.add(id);
}
function statusOf(node: TreeNode) {
if (node.waiting) return { label: 'Waiting on folder', tone: 'info' as const };
return ITEM_STATUS[node.status];
}
</script>
<div class="toolbar">
<label class="check small">
<input type="checkbox" bind:checked={attentionOnly} />
Only show files that need attention
</label>
</div>
{#if rows.length === 0}
<p class="empty muted">
{attentionOnly
? 'Everything is synced.'
: 'No files yet. They appear here after the first sync.'}
</p>
{:else}
<div class="table-wrap">
<table class="register">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Changed in SharePoint</th>
<th>Last synced</th>
</tr>
</thead>
<tbody>
{#each rows as { node, depth } (node.sourceId)}
{@const status = statusOf(node)}
<tr class:folder={node.isFolder}>
<td class="name" style:--depth={depth}>
{#if node.isFolder}
<button
type="button"
class="twisty"
aria-expanded={!collapsed.has(node.sourceId)}
aria-label="{collapsed.has(node.sourceId) ? 'Expand' : 'Collapse'} {node.name}"
onclick={() => toggle(node.sourceId)}
>
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M6 4l4 4-4 4" /></svg>
</button>
<svg class="icon" viewBox="0 0 16 16" aria-hidden="true">
<path d="M1.5 3.5h5l1.5 1.5h6.5v8h-13z" />
</svg>
{:else}
<span class="twisty-spacer"></span>
<svg class="icon" viewBox="0 0 16 16" aria-hidden="true">
<path d="M3.5 1.5h6l3 3v10h-9z M9.5 1.5v3h3" />
</svg>
{/if}
<span title={node.error ?? undefined}>{node.name}</span>
{#if node.problems > 0}
<span class="problems">{node.problems} failed inside</span>
{/if}
</td>
<td><Badge {...status} /></td>
<td class="small"><TimeAgo date={node.isFolder ? null : node.modifiedAt} empty="" /></td
>
<td class="small"><TimeAgo date={node.syncedAt} empty="Not yet" /></td>
</tr>
{/each}
</tbody>
</table>
</div>
{/if}
<style>
.toolbar {
display: flex;
justify-content: flex-end;
}
.empty {
padding: 1.5rem;
border: 1px dashed var(--rule);
text-align: center;
}
.name {
display: flex;
align-items: center;
gap: 0.35rem;
padding-left: calc(0.5rem + var(--depth) * 1.25rem);
min-width: 18rem;
}
.folder .name {
font-weight: 600;
}
.twisty,
.twisty-spacer {
flex: none;
width: 1.25rem;
height: 1.25rem;
}
.twisty {
display: grid;
place-items: center;
padding: 0;
border: 0;
border-radius: var(--radius);
background: none;
color: var(--pencil);
cursor: pointer;
}
.twisty svg {
width: 0.85rem;
height: 0.85rem;
fill: none;
stroke: currentColor;
stroke-width: 1.75;
transition: transform 120ms ease;
}
.twisty[aria-expanded='true'] svg {
transform: rotate(90deg);
}
.icon {
flex: none;
width: 1rem;
height: 1rem;
fill: none;
stroke: var(--pencil);
stroke-width: 1.1;
}
.problems {
margin-left: 0.25rem;
font-size: var(--step--1);
font-weight: 500;
color: var(--danger);
}
</style>

View File

@@ -0,0 +1,62 @@
<script lang="ts">
/** The shared add/edit project form fields. */
let {
values,
rootsLocked = false,
current
}: {
values: { name?: string; sourceInput?: string; targetInput?: string };
/** After the first sync the roots can't change until the project is reset. */
rootsLocked?: boolean;
/** Current root paths, shown when editing. */
current?: { source: string; target: string };
} = $props();
</script>
<div class="field">
<label for="name">Project name</label>
<input id="name" name="name" type="text" required maxlength="100" value={values.name ?? ''} />
</div>
{#if rootsLocked && current}
<div class="notice">
<p><strong>From SharePoint:</strong> {current.source}</p>
<p><strong>To ACC:</strong> {current.target}</p>
<p class="small muted">
These folders are locked because the project has already synced. Reset the project to change
them.
</p>
</div>
{:else}
<div class="field">
<label for="sourceInput">SharePoint folder</label>
<input
id="sourceInput"
name="sourceInput"
type="text"
required={!current}
value={values.sourceInput ?? ''}
placeholder={current?.source}
/>
<span class="hint">
Open the folder in SharePoint and paste the address from your browser. Everything inside it is
synced.
{#if current}Leave blank to keep the current folder.{/if}
</span>
</div>
<div class="field">
<label for="targetInput">ACC folder</label>
<input
id="targetInput"
name="targetInput"
type="text"
required={!current}
value={values.targetInput ?? ''}
placeholder={current?.target}
/>
<span class="hint">
Open the destination folder in ACC Docs and paste the address from your browser.
{#if current}Leave blank to keep the current folder.{/if}
</span>
</div>
{/if}

View File

@@ -0,0 +1,81 @@
<script lang="ts">
import { enhance } from '$app/forms';
import type { RunRequestResult } from '#lib/server/sync/coordinator.ts';
let {
running,
cooldownSeconds,
unavailable = null
}: {
running: boolean;
/** Matches the server's per-project cooldown, so the button disables in step with it. */
cooldownSeconds: number;
/** Why a run can't start (disabled or paused project), if it can't. */
unavailable?: string | null;
} = $props();
let cooldownUntil = $state(0);
let tick = $state(Date.now());
let message = $state<string | null>(null);
const remaining = $derived(Math.max(0, Math.ceil((cooldownUntil - tick) / 1000)));
$effect(() => {
const until = cooldownUntil;
if (until <= Date.now()) return;
const timer = setInterval(() => (tick = Date.now()), 250);
return () => clearInterval(timer);
});
function startCooldown(seconds: number) {
tick = Date.now();
cooldownUntil = tick + seconds * 1000;
}
const MESSAGES: Record<RunRequestResult['status'], (r: RunRequestResult) => string> = {
started: () => 'Sync started.',
'already-running': () => 'A sync is already running for this project.',
cooldown: () => 'Run now was used moments ago. It will be available again shortly.',
unavailable: (r) => (r.status === 'unavailable' ? r.reason : '')
};
</script>
<form
method="POST"
action="?/run"
class="run-now"
use:enhance={() =>
async ({ result, update }) => {
if (result.type === 'success' && result.data?.run) {
const run = result.data.run as RunRequestResult;
message = MESSAGES[run.status](run);
if (run.status === 'started') startCooldown(cooldownSeconds);
if (run.status === 'cooldown') startCooldown(run.retryAfterSeconds);
}
await update({ reset: false });
}}
>
<button class="button primary" disabled={running || remaining > 0 || !!unavailable}>
{#if running}
Syncing…
{:else if remaining > 0}
Run now ({remaining}s)
{:else}
Run now
{/if}
</button>
<p class="small muted" role="status">{unavailable ?? message ?? ''}</p>
</form>
<style>
.run-now {
display: grid;
justify-items: end;
gap: 0.3rem;
}
.run-now p {
max-width: 22rem;
text-align: right;
min-height: 1.2em;
}
</style>

View File

@@ -0,0 +1,14 @@
<script lang="ts">
import { formatDateTime, timeAgo } from '#lib/format.ts';
import { now } from '#lib/now.svelte.ts';
let { date, empty = 'Never' }: { date: Date | null; empty?: string } = $props();
</script>
{#if date}
<time datetime={date.toISOString()} title={formatDateTime(date)}
>{timeAgo(date, now.current)}</time
>
{:else}
<span class="muted">{empty}</span>
{/if}

80
src/lib/format.ts Normal file
View File

@@ -0,0 +1,80 @@
import type { ProjectHealth } from '#lib/server/status/status-service.ts';
import type {
EventAction,
ItemStatus,
RunCounts,
RunOutcome
} from '#lib/server/sync/core/model.ts';
/** Visual tone for a badge; maps to a colour token in app.css. */
export type Tone = 'ok' | 'info' | 'warn' | 'danger' | 'muted';
export const HEALTH: Record<ProjectHealth, { label: string; tone: Tone }> = {
healthy: { label: 'Healthy', tone: 'ok' },
running: { label: 'Syncing…', tone: 'info' },
pending: { label: 'Changes pending', tone: 'info' },
issues: { label: 'Needs attention', tone: 'danger' },
paused: { label: 'Paused', tone: 'warn' },
disabled: { label: 'Disabled', tone: 'muted' },
'never-run': { label: 'Not synced yet', tone: 'muted' }
};
export const ITEM_STATUS: Record<ItemStatus, { label: string; tone: Tone }> = {
synced: { label: 'Synced', tone: 'ok' },
pending: { label: 'Pending', tone: 'info' },
failed: { label: 'Failed', tone: 'danger' },
deleted: { label: 'Deleted', tone: 'muted' }
};
export const OUTCOME: Record<RunOutcome, { label: string; tone: Tone }> = {
success: { label: 'Success', tone: 'ok' },
partial: { label: 'Partial', tone: 'warn' },
failed: { label: 'Failed', tone: 'danger' }
};
export const EVENT: Record<EventAction, { label: string; tone: Tone }> = {
created: { label: 'Created', tone: 'ok' },
updated: { label: 'New version', tone: 'ok' },
renamed: { label: 'Renamed', tone: 'info' },
moved: { label: 'Moved', tone: 'info' },
deleted: { label: 'Deleted', tone: 'muted' },
restored: { label: 'Restored', tone: 'ok' },
failed: { label: 'Failed', tone: 'danger' }
};
/** "3 created, 1 updated" — only non-zero counts, or "No changes". */
export function describeCounts(counts: RunCounts): string {
const parts = (Object.keys(EVENT) as EventAction[])
.filter((action) => counts[action] > 0)
.map((action) => `${counts[action]} ${EVENT[action].label.toLowerCase()}`);
return parts.length > 0 ? parts.join(', ') : 'No changes';
}
const relative = new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' });
const UNITS: Array<[Intl.RelativeTimeFormatUnit, number]> = [
['day', 86_400_000],
['hour', 3_600_000],
['minute', 60_000],
['second', 1000]
];
/** "3 minutes ago", "just now". */
export function timeAgo(date: Date | null, now: Date = new Date()): string {
if (!date) return 'Never';
const diff = date.getTime() - now.getTime();
if (Math.abs(diff) < 10_000) return 'just now';
const [unit, ms] = UNITS.find(([, size]) => Math.abs(diff) >= size) ?? UNITS.at(-1)!;
return relative.format(Math.round(diff / ms), unit);
}
const dateTime = new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' });
export function formatDateTime(date: Date | null): string {
return date ? dateTime.format(date) : '—';
}
export function formatDuration(start: Date, end: Date | null): string {
if (!end) return 'Running';
const seconds = Math.max(0, Math.round((end.getTime() - start.getTime()) / 1000));
return seconds < 60 ? `${seconds}s` : `${Math.floor(seconds / 60)}m ${seconds % 60}s`;
}

19
src/lib/now.svelte.ts Normal file
View File

@@ -0,0 +1,19 @@
import { browser } from '$app/env';
/**
* A shared "now" for relative times. In the browser it is reactive and ticks
* every few seconds; on the server it is simply the current time.
*/
class Now {
#tick = $state(new Date());
constructor(intervalMs: number) {
if (browser) setInterval(() => (this.#tick = new Date()), intervalMs);
}
get current(): Date {
return browser ? this.#tick : new Date();
}
}
export const now = new Now(5000);

View File

@@ -132,7 +132,11 @@ export class FakeDrive implements ChangeSource {
/** The node followed by its ancestors up to the drive root. */ /** The node followed by its ancestors up to the drive root. */
private *lineage(node: DriveNode): Generator<DriveNode> { private *lineage(node: DriveNode): Generator<DriveNode> {
for (let n: DriveNode | undefined = node; n; n = n.parentId ? this.nodes.get(n.parentId) : undefined) { for (
let n: DriveNode | undefined = node;
n;
n = n.parentId ? this.nodes.get(n.parentId) : undefined
) {
yield n; yield n;
} }
} }

View File

@@ -0,0 +1,42 @@
import { describe, expect, it } from 'vitest';
import { REJECTED_FILE, SimulatedBackend } from './simulated-backend.ts';
describe('SimulatedBackend', () => {
it('resolves the last segment of a pasted URL and reuses the folder', async () => {
const backend = new SimulatedBackend();
const url = 'https://contoso.sharepoint.com/sites/A/Shared%20Documents/Tower%20B?web=1';
const first = await backend.resolveSourceRoot(url);
const second = await backend.resolveSourceRoot('Tower B');
expect(first.displayPath).toBe('Simulated SharePoint / Tower B');
expect(second.root).toEqual(first.root);
});
it('seeds new SharePoint folders with sample files', async () => {
const backend = new SimulatedBackend();
const { root } = await backend.resolveSourceRoot('Demo');
const names = backend.drive.filesUnder(root.rootItemId).map((id) => backend.drive.nameOf(id));
expect(names).toContain('A-101 Floor Plan.pdf');
expect(names).toContain(REJECTED_FILE);
});
it('rejects a blank location', async () => {
await expect(new SimulatedBackend().resolveTargetRoot(' / ')).rejects.toThrow(
'Enter a folder path or URL.'
);
});
it('simulates activity as new change-feed entries', async () => {
const backend = new SimulatedBackend(() => 0.5);
const { root } = await backend.resolveSourceRoot('Demo');
const { cursor } = await backend.drive.getChanges(root, null);
backend.simulateActivity();
const { changes } = await backend.drive.getChanges(root, cursor);
expect(changes).toHaveLength(1);
});
});

View File

@@ -65,7 +65,9 @@ export class SimulatedBackend implements SourceRootResolver, TargetRootResolver
const roots = [...this.sourceFolders.values()]; const roots = [...this.sourceFolders.values()];
if (roots.length === 0) return; if (roots.length === 0) return;
const root = this.pick(roots); const root = this.pick(roots);
const files = this.drive.filesUnder(root).filter((id) => this.drive.nameOf(id) !== REJECTED_FILE); const files = this.drive
.filesUnder(root)
.filter((id) => this.drive.nameOf(id) !== REJECTED_FILE);
const roll = this.random(); const roll = this.random();
if (files.length === 0 || roll < 0.25) { if (files.length === 0 || roll < 0.25) {

View File

@@ -3,7 +3,7 @@ import { drizzle, type BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'
import { migrate } from 'drizzle-orm/better-sqlite3/migrator'; import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
import * as schema from './schema.ts'; import * as schema from './schema.ts';
export type Db = BetterSQLite3Database<typeof schema>; export type Db = BetterSQLite3Database<typeof schema> & { $client: Database.Database };
/** Opens (or creates) the database and brings its schema up to date. */ /** Opens (or creates) the database and brings its schema up to date. */
export function openDatabase(path: string, migrationsFolder = 'drizzle'): Db { export function openDatabase(path: string, migrationsFolder = 'drizzle'): Db {

View File

@@ -73,11 +73,7 @@ export class SqliteProjectRepository implements ProjectRepository {
private set(id: string, values: Partial<ProjectRow>): void { private set(id: string, values: Partial<ProjectRow>): void {
const defined = Object.entries(values).filter(([, value]) => value !== undefined); const defined = Object.entries(values).filter(([, value]) => value !== undefined);
if (defined.length === 0) return; if (defined.length === 0) return;
this.db this.db.update(projects).set(Object.fromEntries(defined)).where(eq(projects.id, id)).run();
.update(projects)
.set(Object.fromEntries(defined))
.where(eq(projects.id, id))
.run();
} }
} }

View File

@@ -25,4 +25,23 @@ describe('SqliteRunRepository', () => {
expect(run).toMatchObject({ runId, outcome: null, finishedAt: null }); expect(run).toMatchObject({ runId, outcome: null, finishedAt: null });
}); });
it('fails runs left unfinished, leaving finished ones alone', async () => {
const h = await createHarness();
const finished = await h.sync();
const unfinished = await h.runs.start(h.project.id, 'schedule', new Date());
const at = new Date();
expect(await h.runs.failUnfinished(at, 'Interrupted')).toBe(1);
const byId = new Map(
(await h.runs.listSince(h.project.id, new Date(0))).map((r) => [r.runId, r])
);
expect(byId.get(unfinished)).toMatchObject({
outcome: 'failed',
error: 'Interrupted',
finishedAt: at
});
expect(byId.get(finished.runId)).toMatchObject({ outcome: 'success', error: null });
});
}); });

View File

@@ -1,4 +1,4 @@
import { and, asc, desc, eq, gte, lt } from 'drizzle-orm'; import { and, asc, desc, eq, gte, isNull, lt } from 'drizzle-orm';
import type { import type {
RunCounts, RunCounts,
RunEvent, RunEvent,
@@ -71,6 +71,14 @@ export class SqliteRunRepository implements RunRepository {
async pruneBefore(cutoff: Date): Promise<number> { async pruneBefore(cutoff: Date): Promise<number> {
return this.db.delete(runs).where(lt(runs.startedAt, cutoff)).run().changes; return this.db.delete(runs).where(lt(runs.startedAt, cutoff)).run().changes;
} }
async failUnfinished(at: Date, error: string): Promise<number> {
return this.db
.update(runs)
.set({ finishedAt: at, outcome: 'failed', error })
.where(isNull(runs.outcome))
.run().changes;
}
} }
function toRunRecord(row: RunRow): RunRecord { function toRunRecord(row: RunRow): RunRecord {

24
src/lib/server/app.ts Normal file
View File

@@ -0,0 +1,24 @@
import { createServices, type AppConfig, type AppServices } from './container.ts';
// Kept on globalThis so a dev-server reload of this module can still find and
// stop the previous instance instead of leaking its timers and DB handle.
const holder = globalThis as typeof globalThis & { __syncApp?: AppServices };
export async function startApp(config: AppConfig): Promise<void> {
await stopApp();
const services = createServices(config);
holder.__syncApp = services;
await services.start();
}
export async function stopApp(): Promise<void> {
const current = holder.__syncApp;
holder.__syncApp = undefined;
await current?.stop();
}
/** The running app's services. Only valid after `startApp` (the server `init` hook). */
export function services(): AppServices {
if (!holder.__syncApp) throw new Error('The sync app has not been started.');
return holder.__syncApp;
}

View File

@@ -4,10 +4,10 @@ import type { Clock } from '#lib/server/sync/core/ports.ts';
export const SESSION_COOKIE = 'admin_session'; export const SESSION_COOKIE = 'admin_session';
export interface AdminAuthConfig { export interface AdminAuthConfig {
username: string | undefined; username?: string;
password: string | undefined; password?: string;
/** Signs session tokens. If omitted, a random one is used and sessions end on restart. */ /** Signs session tokens. If omitted, a random one is used and sessions end on restart. */
sessionSecret: string | undefined; sessionSecret?: string;
sessionTtlMs: number; sessionTtlMs: number;
clock: Clock; clock: Clock;
} }
@@ -24,6 +24,10 @@ export class AdminAuth {
return Boolean(this.config.username && this.config.password); return Boolean(this.config.username && this.config.password);
} }
get sessionTtlSeconds(): number {
return Math.floor(this.config.sessionTtlMs / 1000);
}
checkCredentials(username: string, password: string): boolean { checkCredentials(username: string, password: string): boolean {
if (!this.enabled) return false; if (!this.enabled) return false;
// Evaluate both so timing doesn't reveal which one was wrong. // Evaluate both so timing doesn't reveal which one was wrong.

View File

@@ -0,0 +1,23 @@
import type { Cookies } from '@sveltejs/kit';
import { SESSION_COOKIE, type AdminAuth } from './admin-auth.ts';
export function startSession(cookies: Cookies, url: URL, auth: AdminAuth): void {
cookies.set(SESSION_COOKIE, auth.issueToken(), {
path: '/',
httpOnly: true,
sameSite: 'strict',
// Behind the VPN the app may be served over plain HTTP.
secure: url.protocol === 'https:',
maxAge: auth.sessionTtlSeconds
});
}
export function endSession(cookies: Cookies): void {
cookies.delete(SESSION_COOKIE, { path: '/' });
}
/** The `next` query parameter if it's a same-site path, otherwise the admin home. */
export function safeNext(url: URL): string {
const next = url.searchParams.get('next');
return next && next.startsWith('/') && !next.startsWith('//') ? next : '/admin';
}

177
src/lib/server/container.ts Normal file
View File

@@ -0,0 +1,177 @@
import { NotConnectedBackend } from '#lib/server/adapters/not-connected.ts';
import { SimulatedBackend } from '#lib/server/adapters/simulated/simulated-backend.ts';
import { openDatabase } from '#lib/server/adapters/sqlite/database.ts';
import { SqliteItemRepository } from '#lib/server/adapters/sqlite/item-repository.ts';
import { SqliteProjectRepository } from '#lib/server/adapters/sqlite/project-repository.ts';
import { SqliteRunRepository } from '#lib/server/adapters/sqlite/run-repository.ts';
import { AdminAuth } from '#lib/server/auth/admin-auth.ts';
import { consoleLogger } from '#lib/server/logger.ts';
import { StatusService } from '#lib/server/status/status-service.ts';
import { SyncCoordinator } from '#lib/server/sync/coordinator.ts';
import { SyncEngine } from '#lib/server/sync/core/engine.ts';
import { PatternIgnorePolicy } from '#lib/server/sync/core/ignore.ts';
import type {
ChangeSource,
DocumentTarget,
Logger,
SourceRootResolver,
TargetRootResolver
} from '#lib/server/sync/core/ports.ts';
import { Reconciler } from '#lib/server/sync/core/reconciler.ts';
import { ProjectService } from '#lib/server/sync/project-service.ts';
import { IntervalScheduler } from '#lib/server/sync/scheduler.ts';
export interface AppConfig {
backend: 'live' | 'simulated';
databasePath: string;
pollIntervalSeconds: number;
manualRunCooldownSeconds: number;
maxAttempts: number;
historyRetentionDays: number;
admin: { username?: string; password?: string; sessionSecret?: string };
}
export interface AppServices {
coordinator: SyncCoordinator;
status: StatusService;
projectService: ProjectService;
auth: AdminAuth;
config: AppConfig;
start(): Promise<void>;
stop(): Promise<void>;
}
interface Connection {
source: ChangeSource;
target: DocumentTarget;
sourceResolver: SourceRootResolver;
targetResolver: TargetRootResolver;
}
const DAY_MS = 86_400_000;
const PRUNE_INTERVAL_MS = 6 * 3_600_000;
const SIMULATED_ACTIVITY_MS = 45_000;
const ADMIN_SESSION_TTL_MS = 12 * 3_600_000;
const INTERRUPTED_RUN =
'The server stopped before this sync finished. Anything left over is picked up by the next sync.';
/** The composition root: the only place concrete adapters are chosen and wired. */
export function createServices(config: AppConfig, logger: Logger = consoleLogger): AppServices {
const clock = () => new Date();
const simulated = config.backend === 'simulated' ? new SimulatedBackend() : null;
const connection = simulated ? simulatedConnection(simulated) : notConnected();
// Simulated SharePoint/ACC live in memory, so their sync state must too.
const db = openDatabase(simulated ? ':memory:' : config.databasePath);
const projects = new SqliteProjectRepository(db, clock);
const items = new SqliteItemRepository(db);
const runs = new SqliteRunRepository(db);
const { maxAttempts } = config;
const reconciler = new Reconciler({ ...connection, items, runs, clock, maxAttempts });
const engine = new SyncEngine({
source: connection.source,
projects,
items,
runs,
reconciler,
ignore: new PatternIgnorePolicy(),
clock,
logger,
maxAttempts
});
const coordinator = new SyncCoordinator({
runner: engine,
projects,
clock,
logger,
manualCooldownMs: config.manualRunCooldownSeconds * 1000
});
const projectService = new ProjectService({
projects,
items,
sourceResolver: connection.sourceResolver,
targetResolver: connection.targetResolver,
activity: coordinator,
maxAttempts
});
const status = new StatusService({
projects,
items,
runs,
activity: coordinator,
clock,
retentionDays: config.historyRetentionDays
});
const auth = new AdminAuth({
...config.admin,
sessionTtlMs: ADMIN_SESSION_TTL_MS,
clock
});
const scheduler = new IntervalScheduler(logger);
return {
coordinator,
status,
projectService,
auth,
config,
async start() {
const interrupted = await runs.failUnfinished(clock(), INTERRUPTED_RUN);
if (interrupted > 0) logger.warn('Closed syncs interrupted by a restart', { interrupted });
if (simulated && (await projects.list()).length === 0) {
await projectService.create({
name: 'Demo project',
sourceInput: 'Demo Project',
targetInput: 'Demo Project'
});
}
scheduler.every(
'sync',
config.pollIntervalSeconds * 1000,
() => coordinator.runScheduledPass(),
{
immediate: true
}
);
scheduler.every(
'prune-history',
PRUNE_INTERVAL_MS,
() => runs.pruneBefore(new Date(clock().getTime() - config.historyRetentionDays * DAY_MS)),
{ immediate: true }
);
if (simulated) {
scheduler.every('simulated-activity', SIMULATED_ACTIVITY_MS, async () =>
simulated.simulateActivity()
);
}
logger.info('Sync server started', {
backend: config.backend,
pollIntervalSeconds: config.pollIntervalSeconds,
adminEnabled: auth.enabled
});
},
async stop() {
scheduler.stop();
await coordinator.drain();
db.$client.close();
}
};
}
function simulatedConnection(backend: SimulatedBackend): Connection {
return {
source: backend.drive,
target: backend.acc,
sourceResolver: backend,
targetResolver: backend
};
}
function notConnected(): Connection {
const backend = new NotConnectedBackend();
return { source: backend, target: backend, sourceResolver: backend, targetResolver: backend };
}

27
src/lib/server/forms.ts Normal file
View File

@@ -0,0 +1,27 @@
import { fail, type ActionFailure } from '@sveltejs/kit';
import { InvalidRequestError } from '#lib/server/sync/core/errors.ts';
/**
* Runs a form action's work, turning user-facing errors into a 400 with
* `{ error, ...echo }` so the form can show the message and keep what was
* typed. Anything else still throws.
*/
export async function attempt<T, E extends object = object>(
work: () => Promise<T>,
echo?: E
): Promise<T | ActionFailure<{ error: string } & E>> {
try {
return await work();
} catch (error) {
if (error instanceof InvalidRequestError) {
return fail(400, { error: error.message, ...(echo as E) });
}
throw error;
}
}
/** A form field as a string (empty if missing or a file). */
export function field(data: FormData, name: string): string {
const value = data.get(name);
return typeof value === 'string' ? value : '';
}

View File

@@ -0,0 +1,70 @@
import { describe, expect, it } from 'vitest';
import { createHarness } from '#lib/server/sync/testing/harness.ts';
import type { TreeNode } from './tree-view.ts';
const outline = (nodes: TreeNode[], depth = 0): string[] =>
nodes.flatMap((node) => [
`${' '.repeat(depth)}${node.name}${node.isFolder ? '/' : ''} [${node.status}${node.problems ? `, ${node.problems} problem` : ''}]`,
...outline(node.children, depth + 1)
]);
describe('StatusService', () => {
it('reports a never-run project', async () => {
const h = await createHarness();
const [summary] = await h.status.listProjects();
expect(summary).toMatchObject({ health: 'never-run', issueCount: 0, lastRun: null });
});
it('builds a sorted tree with folder problem counts and lists item issues', async () => {
const h = await createHarness({ maxAttempts: 1 });
const docs = h.drive.addFolder(h.rootId, 'Docs');
h.drive.addFile(docs, 'b.pdf');
h.drive.addFile(docs, 'a 10.pdf');
h.drive.addFile(docs, 'a 9.pdf');
h.drive.addFile(h.rootId, 'readme.txt');
h.drive.addFile(h.rootId, '~$readme.txt');
h.acc.failOn('b.pdf', new Error('Rejected'));
await h.sync();
const detail = (await h.status.getProject(h.project.id))!;
expect(outline(detail.tree)).toEqual([
'Docs/ [synced, 1 problem]',
' a 9.pdf [synced]',
' a 10.pdf [synced]',
' b.pdf [failed]',
'readme.txt [synced]'
]);
expect(detail).toMatchObject({ health: 'issues', issueCount: 1 });
expect(detail.issues).toEqual([
expect.objectContaining({ path: 'Docs/b.pdf', message: 'Rejected', attempts: 1 })
]);
});
it('surfaces a paused project as a project-level issue', async () => {
const h = await createHarness();
await h.projects.pause(h.project.id, 'Root deleted');
const detail = (await h.status.getProject(h.project.id))!;
expect(detail.health).toBe('paused');
expect(detail.issues).toEqual([
expect.objectContaining({ sourceId: null, message: 'Paused: Root deleted' })
]);
});
it('reports running projects as running', async () => {
const h = await createHarness();
h.running.add(h.project.id);
expect((await h.status.listProjects())[0]).toMatchObject({ running: true, health: 'running' });
});
it('only shows events for runs of the requested project', async () => {
const h = await createHarness();
h.drive.addFile(h.rootId, 'a.txt');
const run = await h.sync();
expect(await h.status.runEvents(h.project.id, run.runId)).toHaveLength(1);
expect(await h.status.runEvents('other-project', run.runId)).toEqual([]);
});
});

View File

@@ -1,9 +1,4 @@
import type { import type { Project, RunEvent, RunRecord, StatusCounts } from '#lib/server/sync/core/model.ts';
Project,
RunEvent,
RunRecord,
StatusCounts
} from '#lib/server/sync/core/model.ts';
import type { import type {
Clock, Clock,
ItemRepository, ItemRepository,
@@ -14,13 +9,7 @@ import { ItemTree } from '#lib/server/sync/core/tree.ts';
import { buildTree, type TreeNode } from './tree-view.ts'; import { buildTree, type TreeNode } from './tree-view.ts';
export type ProjectHealth = export type ProjectHealth =
| 'disabled' 'disabled' | 'paused' | 'running' | 'issues' | 'pending' | 'never-run' | 'healthy';
| 'paused'
| 'running'
| 'issues'
| 'pending'
| 'never-run'
| 'healthy';
export interface ProjectSummary { export interface ProjectSummary {
project: Project; project: Project;
@@ -63,6 +52,11 @@ export class StatusService {
return Promise.all(projects.map((project) => this.summarize(project))); return Promise.all(projects.map((project) => this.summarize(project)));
} }
async getSummary(id: string): Promise<ProjectSummary | null> {
const project = await this.deps.projects.get(id);
return project ? this.summarize(project) : null;
}
async getProject(id: string): Promise<ProjectDetail | null> { async getProject(id: string): Promise<ProjectDetail | null> {
const project = await this.deps.projects.get(id); const project = await this.deps.projects.get(id);
if (!project) return null; if (!project) return null;

View File

@@ -108,6 +108,8 @@ export interface RunRepository {
listEvents(runId: string): Promise<RunEvent[]>; listEvents(runId: string): Promise<RunEvent[]>;
/** Delete runs (and their events) started before `cutoff`; returns how many runs were removed. */ /** Delete runs (and their events) started before `cutoff`; returns how many runs were removed. */
pruneBefore(cutoff: Date): Promise<number>; pruneBefore(cutoff: Date): Promise<number>;
/** Close every run left unfinished (e.g. by a restart) as failed; returns how many were closed. */
failUnfinished(at: Date, error: string): Promise<number>;
} }
export type Clock = () => Date; export type Clock = () => Date;

View File

@@ -0,0 +1,96 @@
import { describe, expect, it } from 'vitest';
import { createHarness } from './testing/harness.ts';
import { BUSY, ROOTS_LOCKED } from './project-service.ts';
describe('ProjectService', () => {
it('creates a project from pasted locations', async () => {
const h = await createHarness();
const project = await h.projectService.create({
name: ' Tower B ',
sourceInput: 'Tower B',
targetInput: 'Tower B Docs'
});
expect(project).toMatchObject({
name: 'Tower B',
enabled: true,
sourceDisplayPath: 'SharePoint/Tower B',
targetDisplayPath: 'ACC/Tower B Docs'
});
});
it('rejects blank names and locations', async () => {
const h = await createHarness();
await expect(
h.projectService.create({ name: ' ', sourceInput: 'a', targetInput: 'b' })
).rejects.toThrow('Name is required.');
await expect(
h.projectService.create({ name: 'x', sourceInput: '', targetInput: 'b' })
).rejects.toThrow('SharePoint folder is required.');
});
it('lets roots change before the first sync but locks them afterwards', async () => {
const h = await createHarness();
const edit = { name: 'Project X', enabled: true, sourceInput: 'Elsewhere' };
await h.projectService.update(h.project.id, edit);
expect((await h.projects.get(h.project.id))!.sourceDisplayPath).toBe('SharePoint/Elsewhere');
await h.engine.runProject(h.project.id, 'manual');
await expect(h.projectService.update(h.project.id, edit)).rejects.toThrow(ROOTS_LOCKED);
await h.projectService.update(h.project.id, { name: 'Renamed', enabled: false });
expect(await h.projects.get(h.project.id)).toMatchObject({ name: 'Renamed', enabled: false });
});
it('unlocks roots and forgets sync state on reset', async () => {
const h = await createHarness();
h.drive.addFile(h.rootId, 'a.txt');
await h.sync();
await h.projectService.reset(h.project.id);
expect(await h.projects.get(h.project.id)).toMatchObject({
firstSyncedAt: null,
deltaCursor: null
});
expect(await h.items.listByProject(h.project.id)).toEqual([]);
});
it('refuses to reset, delete or retry while a run is in progress', async () => {
const h = await createHarness();
h.running.add(h.project.id);
await expect(h.projectService.reset(h.project.id)).rejects.toThrow(BUSY);
await expect(h.projectService.delete(h.project.id)).rejects.toThrow(BUSY);
await expect(h.projectService.retryItem(h.project.id, 'x')).rejects.toThrow(BUSY);
});
it('deletes a project with its items and history', async () => {
const h = await createHarness();
h.drive.addFile(h.rootId, 'a.txt');
const run = await h.sync();
await h.projectService.delete(h.project.id);
expect(await h.projects.get(h.project.id)).toBeNull();
expect(await h.items.listByProject(h.project.id)).toEqual([]);
expect(await h.runs.listEvents(run.runId)).toEqual([]);
});
it('gives a failed item fresh attempts on retry', async () => {
const h = await createHarness({ maxAttempts: 1 });
h.drive.addFile(h.rootId, 'bad.dwg');
h.acc.failOn('bad.dwg', new Error('nope'), 1);
await h.sync();
const failed = await h.itemNamed('bad.dwg');
expect(failed.status).toBe('failed');
await h.projectService.retryItem(h.project.id, failed.sourceId);
expect(await h.itemNamed('bad.dwg')).toMatchObject({ status: 'pending', attemptCount: 0 });
await h.sync();
expect((await h.itemNamed('bad.dwg')).status).toBe('synced');
});
});

View File

@@ -4,8 +4,14 @@ import { SqliteProjectRepository } from '#lib/server/adapters/sqlite/project-rep
import { SqliteRunRepository } from '#lib/server/adapters/sqlite/run-repository.ts'; import { SqliteRunRepository } from '#lib/server/adapters/sqlite/run-repository.ts';
import { SyncEngine } from '#lib/server/sync/core/engine.ts'; import { SyncEngine } from '#lib/server/sync/core/engine.ts';
import { PatternIgnorePolicy } from '#lib/server/sync/core/ignore.ts'; import { PatternIgnorePolicy } from '#lib/server/sync/core/ignore.ts';
import type { Logger } from '#lib/server/sync/core/ports.ts'; import type {
Logger,
SourceRootResolver,
TargetRootResolver
} from '#lib/server/sync/core/ports.ts';
import { Reconciler } from '#lib/server/sync/core/reconciler.ts'; import { Reconciler } from '#lib/server/sync/core/reconciler.ts';
import { ProjectService } from '#lib/server/sync/project-service.ts';
import { StatusService } from '#lib/server/status/status-service.ts';
import { FakeAcc } from '#lib/server/adapters/simulated/fake-acc.ts'; import { FakeAcc } from '#lib/server/adapters/simulated/fake-acc.ts';
import { FakeDrive } from '#lib/server/adapters/simulated/fake-drive.ts'; import { FakeDrive } from '#lib/server/adapters/simulated/fake-drive.ts';
@@ -53,6 +59,40 @@ export async function createHarness({ maxAttempts = 3 } = {}) {
targetDisplayPath: 'Project X/Project Files' targetDisplayPath: 'Project X/Project Files'
}); });
/** Stands in for the coordinator: tests add project IDs to mark them as running. */
const running = new Set<string>();
const activity = { isRunning: (id: string) => running.has(id) };
/** Resolves any input to a new folder of that name on each side. */
const resolver: SourceRootResolver & TargetRootResolver = {
resolveSourceRoot: async (input) => ({
root: {
siteId: 'site',
driveId: 'drive',
rootItemId: drive.addFolder(drive.driveRootId, input)
},
displayPath: `SharePoint/${input}`
}),
resolveTargetRoot: async (input) => ({
root: {
hubId: 'hub',
projectId: 'acc-project',
rootFolderId: acc.addFolderByHand(acc.rootFolderId, input)
},
displayPath: `ACC/${input}`
})
};
const projectService = new ProjectService({
projects,
items,
sourceResolver: resolver,
targetResolver: resolver,
activity,
maxAttempts
});
const status = new StatusService({ projects, items, runs, activity, clock, retentionDays: 7 });
return { return {
db, db,
drive, drive,
@@ -62,6 +102,9 @@ export async function createHarness({ maxAttempts = 3 } = {}) {
items, items,
runs, runs,
engine, engine,
projectService,
status,
running,
project, project,
sync: () => engine.runProject(project.id, 'schedule'), sync: () => engine.runProject(project.id, 'schedule'),
itemNamed: async (name: string) => itemNamed: async (name: string) =>

316
src/lib/styles/app.css Normal file
View File

@@ -0,0 +1,316 @@
/*
* Drafting-room palette: paper, graphite and rule lines, one marker blue for
* actions, and signal colours reserved for status.
*/
:root {
--paper: #fafaf7;
--sheet: #ffffff;
--graphite: #23272c;
--pencil: #5d6670;
--rule: #d3d7dc;
--rule-strong: #23272c;
--marker: #1d5ba6;
--marker-ink: #ffffff;
--ok: #1f7a4d;
--info: #1d5ba6;
--warn: #a86a12;
--danger: #bf3a30;
--muted: #6b737c;
--tint: 12%;
--font-ui: 'Barlow', system-ui, sans-serif;
--font-display: 'Barlow Semi Condensed', 'Barlow', system-ui, sans-serif;
--step--1: 0.8125rem;
--step-0: 0.9375rem;
--step-1: 1.125rem;
--step-2: 1.5rem;
--step-3: 2.25rem;
--radius: 3px;
--gutter: clamp(1rem, 3vw, 2rem);
color-scheme: light;
}
@media (prefers-color-scheme: dark) {
:root {
--paper: #181b1f;
--sheet: #202429;
--graphite: #e4e7ea;
--pencil: #a3abb4;
--rule: #363c43;
--rule-strong: #8b949e;
--marker: #7fb0ec;
--marker-ink: #10243c;
--ok: #5cc28d;
--info: #7fb0ec;
--warn: #e0a64a;
--danger: #f07c72;
--muted: #8b949e;
--tint: 18%;
color-scheme: dark;
}
}
*,
*::before,
*::after {
box-sizing: border-box;
}
html {
-webkit-text-size-adjust: 100%;
}
body {
margin: 0;
background: var(--paper);
color: var(--graphite);
font-family: var(--font-ui);
font-size: var(--step-0);
line-height: 1.5;
}
h1,
h2,
h3 {
font-family: var(--font-display);
font-weight: 600;
line-height: 1.15;
margin: 0;
}
h1 {
font-size: var(--step-3);
letter-spacing: -0.01em;
}
h2 {
font-size: var(--step-2);
}
h3 {
font-size: var(--step-1);
}
p {
margin: 0;
}
a {
color: var(--marker);
text-underline-offset: 0.2em;
}
:focus-visible {
outline: 2px solid var(--marker);
outline-offset: 2px;
}
.page {
max-width: 76rem;
margin: 0 auto;
padding: var(--gutter);
display: grid;
gap: 2rem;
}
.section {
display: grid;
gap: 0.75rem;
}
.section-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 1rem;
flex-wrap: wrap;
}
.muted {
color: var(--pencil);
}
.small {
font-size: var(--step--1);
}
/* Buttons */
.button {
display: inline-flex;
align-items: center;
gap: 0.4rem;
min-height: 2.25rem;
padding: 0.35rem 0.9rem;
border: 1px solid var(--rule-strong);
border-radius: var(--radius);
background: var(--sheet);
color: var(--graphite);
font: 500 var(--step-0) / 1 var(--font-ui);
text-decoration: none;
cursor: pointer;
}
.button:hover {
background: color-mix(in srgb, var(--graphite) 6%, var(--sheet));
}
.button.primary {
background: var(--marker);
border-color: var(--marker);
color: var(--marker-ink);
}
.button.primary:hover {
background: color-mix(in srgb, var(--marker) 88%, black);
}
.button.danger {
color: var(--danger);
border-color: var(--danger);
}
.button.compact {
min-height: 1.75rem;
padding: 0.2rem 0.6rem;
font-size: var(--step--1);
}
.button:disabled {
opacity: 0.55;
cursor: not-allowed;
}
/* Status badges: a tinted chip with a solid dot, colour from --tone. */
.badge {
--tone: var(--muted);
display: inline-flex;
align-items: center;
gap: 0.4em;
padding: 0.1em 0.55em;
border-radius: 999px;
background: color-mix(in srgb, var(--tone) var(--tint), transparent);
color: var(--tone);
font-size: var(--step--1);
font-weight: 600;
white-space: nowrap;
}
.badge::before {
content: '';
width: 0.5em;
height: 0.5em;
border-radius: 50%;
background: currentColor;
}
.tone-ok {
--tone: var(--ok);
}
.tone-info {
--tone: var(--info);
}
.tone-warn {
--tone: var(--warn);
}
.tone-danger {
--tone: var(--danger);
}
.tone-muted {
--tone: var(--muted);
}
/* Register tables: the drawing-index look used for lists. */
.register {
width: 100%;
border-collapse: collapse;
background: var(--sheet);
border: 1px solid var(--rule);
}
.register th {
text-align: left;
font-weight: 600;
font-size: var(--step--1);
color: var(--pencil);
padding: 0.5rem 0.75rem;
border-bottom: 1px solid var(--rule-strong);
}
.register td {
padding: 0.55rem 0.75rem;
border-bottom: 1px solid var(--rule);
vertical-align: top;
}
.register tr:last-child td {
border-bottom: 0;
}
.table-wrap {
overflow-x: auto;
}
/* Forms */
.form {
display: grid;
gap: 1.1rem;
max-width: 40rem;
}
.field {
display: grid;
gap: 0.3rem;
}
.field label {
font-weight: 600;
}
.field input[type='text'],
.field input[type='password'],
.field input[type='url'] {
width: 100%;
padding: 0.5rem 0.65rem;
border: 1px solid var(--rule-strong);
border-radius: var(--radius);
background: var(--sheet);
color: var(--graphite);
font: inherit;
}
.field .hint {
font-size: var(--step--1);
color: var(--pencil);
}
.check {
display: flex;
align-items: center;
gap: 0.5rem;
}
.notice {
--tone: var(--info);
padding: 0.65rem 0.9rem;
border-left: 3px solid var(--tone);
background: color-mix(in srgb, var(--tone) var(--tint), var(--sheet));
}
.actions {
display: flex;
gap: 0.6rem;
flex-wrap: wrap;
align-items: center;
}
@media (prefers-reduced-motion: reduce) {
* {
animation: none !important;
transition: none !important;
}
}

View File

@@ -0,0 +1,7 @@
import { services } from '#lib/server/app.ts';
import type { LayoutServerLoad } from './$types';
export const load: LayoutServerLoad = ({ locals }) => ({
isAdmin: locals.isAdmin,
simulated: services().config.backend === 'simulated'
});

View File

@@ -1,12 +1,101 @@
<script lang="ts"> <script lang="ts">
import '#lib/styles/app.css';
import favicon from '#lib/assets/favicon.svg'; import favicon from '#lib/assets/favicon.svg';
import { resolve } from '$app/paths';
import { page } from '$app/state';
import type { LayoutProps } from './$types'; import type { LayoutProps } from './$types';
let { children }: LayoutProps = $props(); let { data, children }: LayoutProps = $props();
const current = (prefix: string) =>
prefix === '/'
? page.url.pathname === '/' || page.url.pathname.startsWith('/projects')
: page.url.pathname.startsWith(prefix);
</script> </script>
<svelte:head> <svelte:head>
<link rel="icon" href={favicon} /> <link rel="icon" href={favicon} />
<title>SharePoint to ACC sync</title>
</svelte:head> </svelte:head>
{@render children()} <header class="masthead">
<a class="brand" href={resolve('/')}
>SharePoint <span aria-hidden="true">⟶</span><span class="sr-only">to</span> ACC sync</a
>
<nav aria-label="Main">
<a href={resolve('/')} aria-current={current('/') ? 'page' : undefined}>Projects</a>
<a href={resolve('/admin')} aria-current={current('/admin') ? 'page' : undefined}>
{data.isAdmin ? 'Admin' : 'Admin login'}
</a>
</nav>
</header>
{#if data.simulated}
<div class="simulated">
Simulated mode: SharePoint and ACC are in-memory stand-ins with sample files. Nothing here is
real, and everything resets when the server restarts.
</div>
{/if}
<main>
{@render children()}
</main>
<style>
.masthead {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
flex-wrap: wrap;
padding: 0.75rem var(--gutter);
border-bottom: 2px solid var(--rule-strong);
background: var(--sheet);
}
.brand {
font-family: var(--font-display);
font-size: var(--step-1);
font-weight: 600;
color: var(--graphite);
text-decoration: none;
}
.brand span {
color: var(--marker);
}
nav {
display: flex;
gap: 1.25rem;
}
nav a {
color: var(--pencil);
text-decoration: none;
font-weight: 500;
padding-bottom: 0.15rem;
border-bottom: 2px solid transparent;
}
nav a[aria-current='page'] {
color: var(--graphite);
border-bottom-color: var(--marker);
}
.simulated {
padding: 0.5rem var(--gutter);
background: color-mix(in srgb, var(--warn) var(--tint), var(--paper));
border-bottom: 1px solid color-mix(in srgb, var(--warn) 40%, transparent);
font-size: var(--step--1);
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
</style>

View File

@@ -0,0 +1,6 @@
import { services } from '#lib/server/app.ts';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async () => ({
projects: await services().status.listProjects()
});

View File

@@ -1,2 +1,83 @@
<h1>Welcome to SvelteKit</h1> <script lang="ts">
<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p> import { resolve } from '$app/paths';
import AutoRefresh from '#lib/components/AutoRefresh.svelte';
import Badge from '#lib/components/Badge.svelte';
import TimeAgo from '#lib/components/TimeAgo.svelte';
import { HEALTH } from '#lib/format.ts';
import type { PageProps } from './$types';
let { data }: PageProps = $props();
const anyRunning = $derived(data.projects.some((p) => p.running));
</script>
<svelte:head>
<title>Projects | SharePoint to ACC sync</title>
</svelte:head>
<AutoRefresh intervalMs={anyRunning ? 3000 : 15000} />
<div class="page">
<div class="section-head">
<h1>Projects</h1>
<p class="muted">Each project mirrors one SharePoint folder into one ACC folder.</p>
</div>
{#if data.projects.length === 0}
<div class="notice">
No projects yet. An admin can add one from the <a href={resolve('/admin')}>admin page</a>.
</div>
{:else}
<div class="table-wrap">
<table class="register">
<thead>
<tr>
<th>Project</th>
<th>Status</th>
<th>Files</th>
<th>Last sync</th>
</tr>
</thead>
<tbody>
{#each data.projects as summary (summary.project.id)}
{@const { project, counts } = summary}
<tr>
<td>
<a class="project" href={resolve('/projects/[id]', { id: project.id })}
>{project.name}</a
>
<div class="small muted">{project.sourceDisplayPath}</div>
</td>
<td>
<Badge {...HEALTH[summary.health]} />
{#if summary.issueCount > 0}
<div class="small issues">
{summary.issueCount}
{summary.issueCount === 1 ? 'issue' : 'issues'}
</div>
{/if}
</td>
<td class="small">
{counts.synced} synced{#if counts.pending}, {counts.pending} pending{/if}{#if counts.failed},
<span class="issues">{counts.failed} failed</span>{/if}
</td>
<td class="small"><TimeAgo date={summary.lastRun?.startedAt ?? null} /></td>
</tr>
{/each}
</tbody>
</table>
</div>
{/if}
</div>
<style>
.project {
font-family: var(--font-display);
font-size: var(--step-1);
font-weight: 600;
color: var(--graphite);
}
.issues {
color: var(--danger);
}
</style>

View File

@@ -0,0 +1,15 @@
import { redirect } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import { endSession } from '#lib/server/auth/session-cookie.ts';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async () => ({
projects: await services().status.listProjects()
});
export const actions: Actions = {
logout: ({ cookies }) => {
endSession(cookies);
redirect(303, '/');
}
};

View File

@@ -0,0 +1,55 @@
<script lang="ts">
import { resolve } from '$app/paths';
import Badge from '#lib/components/Badge.svelte';
import { HEALTH } from '#lib/format.ts';
import type { PageProps } from './$types';
let { data }: PageProps = $props();
</script>
<svelte:head>
<title>Admin | SharePoint to ACC sync</title>
</svelte:head>
<div class="page">
<div class="section-head">
<h1>Manage projects</h1>
<div class="actions">
<a class="button primary" href={resolve('/admin/projects/new')}>Add project</a>
<form method="POST" action="?/logout">
<button class="button">Log out</button>
</form>
</div>
</div>
{#if data.projects.length === 0}
<div class="notice">
No projects yet. Add one to start syncing a SharePoint folder into ACC.
</div>
{:else}
<div class="table-wrap">
<table class="register">
<thead>
<tr>
<th>Project</th>
<th>From SharePoint</th>
<th>To ACC</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{#each data.projects as { project, health } (project.id)}
<tr>
<td>
<a href={resolve('/admin/projects/[id]', { id: project.id })}>{project.name}</a>
</td>
<td class="small">{project.sourceDisplayPath}</td>
<td class="small">{project.targetDisplayPath}</td>
<td><Badge {...HEALTH[health]} /></td>
</tr>
{/each}
</tbody>
</table>
</div>
{/if}
</div>

View File

@@ -0,0 +1,23 @@
import { fail, redirect } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import { safeNext, startSession } from '#lib/server/auth/session-cookie.ts';
import { field } from '#lib/server/forms.ts';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = ({ locals, url }) => {
if (locals.isAdmin) redirect(303, safeNext(url));
return { enabled: services().auth.enabled };
};
export const actions: Actions = {
default: async ({ request, cookies, url }) => {
const data = await request.formData();
const username = field(data, 'username');
const { auth } = services();
if (!auth.checkCredentials(username, field(data, 'password'))) {
return fail(400, { error: 'That username and password combination is incorrect.', username });
}
startSession(cookies, url, auth);
redirect(303, safeNext(url));
}
};

View File

@@ -0,0 +1,54 @@
<script lang="ts">
import { enhance } from '$app/forms';
import type { PageProps } from './$types';
let { data, form }: PageProps = $props();
</script>
<svelte:head>
<title>Admin login | SharePoint to ACC sync</title>
</svelte:head>
<div class="page">
<div class="section">
<h1>Admin login</h1>
<p class="muted">Admins can add projects, change their settings, and reset or remove them.</p>
</div>
{#if !data.enabled}
<div class="notice tone-warn">
Admin login is turned off. Set <code>ADMIN_USERNAME</code> and <code>ADMIN_PASSWORD</code> on the
server to turn it on.
</div>
{:else}
<form method="POST" class="form" use:enhance>
{#if form?.error}
<div class="notice tone-danger" role="alert">{form.error}</div>
{/if}
<div class="field">
<label for="username">Username</label>
<input
id="username"
name="username"
type="text"
autocomplete="username"
required
value={form?.username ?? ''}
/>
</div>
<div class="field">
<label for="password">Password</label>
<input
id="password"
name="password"
type="password"
autocomplete="current-password"
required
/>
</div>
<div class="actions">
<button class="button primary">Log in</button>
</div>
</form>
{/if}
</div>

View File

@@ -0,0 +1,47 @@
import { error, redirect } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import { attempt, field } from '#lib/server/forms.ts';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const summary = await services().status.getSummary(params.id);
if (!summary) error(404, 'Project not found');
return { summary };
};
export const actions: Actions = {
save: async ({ params, request }) => {
const data = await request.formData();
const values = {
name: field(data, 'name'),
enabled: data.has('enabled'),
sourceInput: field(data, 'sourceInput'),
targetInput: field(data, 'targetInput')
};
return attempt(
async () => {
await services().projectService.update(params.id, values);
return { saved: true };
},
{ values }
);
},
resume: ({ params }) =>
attempt(async () => {
await services().projectService.resume(params.id);
return { resumed: true };
}),
reset: ({ params }) =>
attempt(async () => {
await services().projectService.reset(params.id);
return { reset: true };
}),
delete: ({ params }) =>
attempt(async () => {
await services().projectService.delete(params.id);
redirect(303, '/admin');
})
};

View File

@@ -0,0 +1,148 @@
<script lang="ts">
import { enhance, type SubmitFunction } from '$app/forms';
import { resolve } from '$app/paths';
import ProjectFields from '#lib/components/ProjectFields.svelte';
import type { PageProps } from './$types';
let { data, form }: PageProps = $props();
const project = $derived(data.summary.project);
const values = $derived(form && 'values' in form && form.values ? form.values : project);
const enabled = $derived(
form && 'values' in form && form.values ? form.values.enabled : project.enabled
);
/** Asks before a destructive action, then submits without resetting the form. */
const confirmFirst =
(question: string): SubmitFunction =>
({ cancel }) => {
if (!confirm(question)) cancel();
return ({ update }) => update({ reset: false });
};
</script>
<svelte:head>
<title>Edit {project.name} | SharePoint to ACC sync</title>
</svelte:head>
<div class="page">
<div class="section">
<a class="small" href={resolve('/admin')}>Back to projects</a>
<h1>{project.name}</h1>
<a class="small" href={resolve('/projects/[id]', { id: project.id })}>View sync status</a>
</div>
{#if form?.error}
<div class="notice tone-danger" role="alert">{form.error}</div>
{:else if form && 'saved' in form}
<div class="notice tone-ok" role="status">Changes saved.</div>
{:else if form && 'reset' in form}
<div class="notice tone-ok" role="status">
Project reset. The next sync reads the SharePoint folder from scratch.
</div>
{:else if form && 'resumed' in form}
<div class="notice tone-ok" role="status">Project resumed. It syncs again on the next run.</div>
{/if}
{#if project.pausedReason}
<section class="section">
<h2>Paused</h2>
<div class="notice tone-warn">
<p>{project.pausedReason}</p>
<p class="small">
Nothing in ACC was changed. If the SharePoint folder is back, resume the project. If it
moved for good, reset the project and point it at the new folder.
</p>
</div>
<form method="POST" action="?/resume" use:enhance>
<button class="button">Resume syncing</button>
</form>
</section>
{/if}
<section class="section">
<h2>Settings</h2>
<form
method="POST"
action="?/save"
class="form"
use:enhance={() =>
({ update }) =>
update({ reset: false })}
>
<ProjectFields
values={{ name: values.name }}
rootsLocked={project.firstSyncedAt !== null}
current={{ source: project.sourceDisplayPath, target: project.targetDisplayPath }}
/>
<label class="check">
<input type="checkbox" name="enabled" checked={enabled} />
Sync this project
</label>
<div class="actions">
<button class="button primary">Save changes</button>
</div>
</form>
</section>
<section class="section danger-zone">
<h2>Reset or remove</h2>
<div class="option">
<div>
<h3>Reset project</h3>
<p class="muted">
Forgets what has been synced and unlocks the folder settings. Files already in ACC stay
there; the next sync matches SharePoint again from scratch.
</p>
</div>
<form
method="POST"
action="?/reset"
use:enhance={confirmFirst(
`Reset ${project.name}? The next sync re-reads the SharePoint folder from scratch.`
)}
>
<button class="button danger">Reset project</button>
</form>
</div>
<div class="option">
<div>
<h3>Remove project</h3>
<p class="muted">
Stops syncing and deletes this project's history. Nothing is deleted from SharePoint or
ACC.
</p>
</div>
<form
method="POST"
action="?/delete"
use:enhance={confirmFirst(`Remove ${project.name}? This can't be undone.`)}
>
<button class="button danger">Remove project</button>
</form>
</div>
</section>
</div>
<style>
.danger-zone {
border-top: 1px solid var(--rule);
padding-top: 1.5rem;
}
.option {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 1rem;
flex-wrap: wrap;
padding: 0.9rem 1rem;
border: 1px solid var(--rule);
background: var(--sheet);
}
.option div {
display: grid;
gap: 0.25rem;
max-width: 60ch;
}
</style>

View File

@@ -0,0 +1,22 @@
import { redirect } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import { attempt, field } from '#lib/server/forms.ts';
import type { Actions } from './$types';
export const actions: Actions = {
default: async ({ request }) => {
const data = await request.formData();
const values = {
name: field(data, 'name'),
sourceInput: field(data, 'sourceInput'),
targetInput: field(data, 'targetInput')
};
return attempt(
async () => {
const project = await services().projectService.create(values);
redirect(303, `/projects/${project.id}`);
},
{ values }
);
}
};

View File

@@ -0,0 +1,47 @@
<script lang="ts">
import { enhance } from '$app/forms';
import { resolve } from '$app/paths';
import ProjectFields from '#lib/components/ProjectFields.svelte';
import type { PageProps } from './$types';
let { form }: PageProps = $props();
let saving = $state(false);
</script>
<svelte:head>
<title>Add project | SharePoint to ACC sync</title>
</svelte:head>
<div class="page">
<div class="section">
<a class="small" href={resolve('/admin')}>Back to projects</a>
<h1>Add project</h1>
<p class="muted">
Pick the SharePoint folder to copy from and the ACC folder to copy into. The first sync starts
within a few minutes, or right away with Run now.
</p>
</div>
<form
method="POST"
class="form"
use:enhance={() => {
saving = true;
return async ({ update }) => {
await update({ reset: false });
saving = false;
};
}}
>
{#if form?.error}
<div class="notice tone-danger" role="alert">{form.error}</div>
{/if}
<ProjectFields values={form?.values ?? {}} />
<div class="actions">
<button class="button primary" disabled={saving}>
{saving ? 'Checking folders…' : 'Add project'}
</button>
<a class="button" href={resolve('/admin')}>Cancel</a>
</div>
</form>
</div>

View File

@@ -0,0 +1,9 @@
import { json } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import type { RequestHandler } from './$types';
/** Liveness check for Docker's HEALTHCHECK: answers once the sync app has started. */
export const GET: RequestHandler = () => {
services();
return json({ status: 'ok' }, { headers: { 'cache-control': 'no-store' } });
};

View File

@@ -0,0 +1,25 @@
import { error } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import { attempt, field } from '#lib/server/forms.ts';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const { status, config } = services();
const detail = await status.getProject(params.id);
if (!detail) error(404, 'Project not found');
return { detail, cooldownSeconds: config.manualRunCooldownSeconds };
};
export const actions: Actions = {
run: async ({ params }) => ({
run: await services().coordinator.requestManualRun(params.id)
}),
retry: async ({ params, request }) => {
const sourceId = field(await request.formData(), 'sourceId');
return attempt(async () => {
await services().projectService.retryItem(params.id, sourceId);
return { retried: sourceId };
});
}
};

View File

@@ -0,0 +1,193 @@
<script lang="ts">
import { enhance } from '$app/forms';
import { resolve } from '$app/paths';
import AutoRefresh from '#lib/components/AutoRefresh.svelte';
import Badge from '#lib/components/Badge.svelte';
import FileTree from '#lib/components/FileTree.svelte';
import RunNowButton from '#lib/components/RunNowButton.svelte';
import TimeAgo from '#lib/components/TimeAgo.svelte';
import { HEALTH, OUTCOME, describeCounts } from '#lib/format.ts';
import type { PageProps } from './$types';
let { data, form }: PageProps = $props();
const detail = $derived(data.detail);
const project = $derived(detail.project);
const unavailable = $derived(
!project.enabled
? 'This project is disabled.'
: project.pausedReason
? 'Paused. An admin needs to resume it.'
: null
);
</script>
<svelte:head>
<title>{project.name} | SharePoint to ACC sync</title>
</svelte:head>
<AutoRefresh intervalMs={detail.running ? 2000 : 15000} />
<div class="page">
<section class="title-block" aria-label="Project details">
<div class="head">
<div class="title">
<h1>{project.name}</h1>
<Badge {...HEALTH[detail.health]} />
</div>
<RunNowButton running={detail.running} cooldownSeconds={data.cooldownSeconds} {unavailable} />
</div>
<dl>
<div class="cell">
<dt>From SharePoint</dt>
<dd>{project.sourceDisplayPath}</dd>
</div>
<div class="cell">
<dt>To ACC</dt>
<dd>{project.targetDisplayPath}</dd>
</div>
<div class="cell">
<dt>Last sync</dt>
<dd>
{#if detail.lastRun}
<TimeAgo date={detail.lastRun.startedAt} />
{#if detail.lastRun.outcome}
<Badge {...OUTCOME[detail.lastRun.outcome]} />
{/if}
<div class="small muted">{describeCounts(detail.lastRun.counts)}</div>
{:else}
<span class="muted">Not synced yet</span>
{/if}
<a class="small" href={resolve('/projects/[id]/history', { id: project.id })}
>See the last 7 days</a
>
</dd>
</div>
<div class="cell">
<dt>Files</dt>
<dd>
{detail.counts.synced} synced
<div class="small muted">
{detail.counts.pending} pending, {detail.counts.failed} failed
</div>
</dd>
</div>
</dl>
</section>
{#if detail.issues.length > 0}
<section class="section" aria-labelledby="issues-heading">
<h2 id="issues-heading">Issues</h2>
{#if form?.error}
<div class="notice tone-danger" role="alert">{form.error}</div>
{/if}
<ul class="issues">
{#each detail.issues as issue (issue.sourceId ?? issue.message)}
<li>
<div class="issue-text">
<strong>{issue.path}</strong>
<p>{issue.message}</p>
{#if issue.sourceId}
<p class="small muted">
Tried {issue.attempts}
{issue.attempts === 1 ? 'time' : 'times'}, last <TimeAgo
date={issue.lastAttemptAt}
/>. It won't be tried again until it changes in SharePoint or you retry it.
</p>
{/if}
</div>
{#if issue.sourceId}
<form method="POST" action="?/retry" use:enhance>
<input type="hidden" name="sourceId" value={issue.sourceId} />
<button class="button compact" disabled={detail.running}>Retry on next sync</button>
</form>
{/if}
</li>
{/each}
</ul>
</section>
{/if}
<section class="section" aria-labelledby="files-heading">
<h2 id="files-heading">Files</h2>
<FileTree nodes={detail.tree} />
</section>
</div>
<style>
.title-block {
border: 2px solid var(--rule-strong);
background: var(--sheet);
}
.head {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 1rem;
flex-wrap: wrap;
padding: 1rem 1.25rem;
border-bottom: 1px solid var(--rule-strong);
}
.title {
display: grid;
gap: 0.4rem;
justify-items: start;
}
dl {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr));
margin: 0;
overflow: hidden;
}
.cell {
padding: 0.6rem 1.25rem 0.75rem;
border-right: 1px solid var(--rule);
border-bottom: 1px solid var(--rule);
margin: 0 -1px -1px 0;
}
dt {
font-size: var(--step--1);
color: var(--pencil);
}
dd {
display: grid;
justify-items: start;
gap: 0.2rem;
margin: 0.15rem 0 0;
font-weight: 500;
overflow-wrap: anywhere;
}
.issues {
list-style: none;
margin: 0;
padding: 0;
border: 1px solid var(--rule);
background: var(--sheet);
}
.issues li {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 1rem;
flex-wrap: wrap;
padding: 0.75rem 1rem;
border-left: 3px solid var(--danger);
}
.issues li + li {
border-top: 1px solid var(--rule);
}
.issue-text {
display: grid;
gap: 0.15rem;
max-width: 60ch;
}
</style>

View File

@@ -0,0 +1,15 @@
import { error } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const { status, config } = services();
const summary = await status.getSummary(params.id);
if (!summary) error(404, 'Project not found');
return {
project: summary.project,
running: summary.running,
runs: await status.history(params.id),
retentionDays: config.historyRetentionDays
};
};

View File

@@ -0,0 +1,84 @@
<script lang="ts">
import { resolve } from '$app/paths';
import AutoRefresh from '#lib/components/AutoRefresh.svelte';
import Badge from '#lib/components/Badge.svelte';
import TimeAgo from '#lib/components/TimeAgo.svelte';
import { OUTCOME, describeCounts, formatDateTime, formatDuration } from '#lib/format.ts';
import type { PageProps } from './$types';
let { data }: PageProps = $props();
</script>
<svelte:head>
<title>History for {data.project.name} | SharePoint to ACC sync</title>
</svelte:head>
<AutoRefresh intervalMs={data.running ? 2000 : 15000} />
<div class="page">
<div class="section">
<a class="small" href={resolve('/projects/[id]', { id: data.project.id })}>
Back to {data.project.name}
</a>
<h1>Sync history</h1>
<p class="muted">
Every sync of {data.project.name} in the last {data.retentionDays} days. Older runs are removed
automatically.
</p>
</div>
{#if data.runs.length === 0}
<div class="notice">No syncs in the last {data.retentionDays} days.</div>
{:else}
<div class="table-wrap">
<table class="register">
<thead>
<tr>
<th>Started</th>
<th>Triggered by</th>
<th>Result</th>
<th>Changes</th>
<th>Took</th>
</tr>
</thead>
<tbody>
{#each data.runs as run (run.runId)}
<tr>
<td>
<a
href={resolve('/projects/[id]/history/[runId]', {
id: data.project.id,
runId: run.runId
})}
>
{formatDateTime(run.startedAt)}
</a>
<div class="small muted"><TimeAgo date={run.startedAt} /></div>
</td>
<td>{run.trigger === 'manual' ? 'Run now' : 'Schedule'}</td>
<td>
{#if run.outcome}
<Badge {...OUTCOME[run.outcome]} />
{:else}
<Badge tone="info" label="Running" />
{/if}
{#if run.error}
<div class="small error">{run.error}</div>
{/if}
</td>
<td class="small">{describeCounts(run.counts)}</td>
<td class="small">{formatDuration(run.startedAt, run.finishedAt)}</td>
</tr>
{/each}
</tbody>
</table>
</div>
{/if}
</div>
<style>
.error {
color: var(--danger);
max-width: 40ch;
}
</style>

View File

@@ -0,0 +1,15 @@
import { error } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const { status } = services();
const [summary, runs, events] = await Promise.all([
status.getSummary(params.id),
status.history(params.id),
status.runEvents(params.id, params.runId)
]);
const run = runs.find((r) => r.runId === params.runId);
if (!summary || !run) error(404, 'Sync run not found. It may be older than the history window.');
return { project: summary.project, run, events };
};

View File

@@ -0,0 +1,72 @@
<script lang="ts">
import { resolve } from '$app/paths';
import Badge from '#lib/components/Badge.svelte';
import { EVENT, OUTCOME, describeCounts, formatDateTime, formatDuration } from '#lib/format.ts';
import type { PageProps } from './$types';
let { data }: PageProps = $props();
const timeOnly = new Intl.DateTimeFormat(undefined, { timeStyle: 'medium' });
</script>
<svelte:head>
<title>Sync on {formatDateTime(data.run.startedAt)} | {data.project.name}</title>
</svelte:head>
<div class="page">
<div class="section">
<a class="small" href={resolve('/projects/[id]/history', { id: data.project.id })}>
Back to sync history
</a>
<h1>Sync on {formatDateTime(data.run.startedAt)}</h1>
<div class="actions">
{#if data.run.outcome}
<Badge {...OUTCOME[data.run.outcome]} />
{/if}
<span class="muted">
{data.run.trigger === 'manual' ? 'Started with Run now' : 'Started by the schedule'}, took
{formatDuration(data.run.startedAt, data.run.finishedAt)}. {describeCounts(
data.run.counts
)}.
</span>
</div>
{#if data.run.error}
<div class="notice tone-danger">{data.run.error}</div>
{/if}
</div>
{#if data.events.length === 0}
<div class="notice">Nothing needed to change in ACC during this sync.</div>
{:else}
<div class="table-wrap">
<table class="register">
<thead>
<tr>
<th>Time</th>
<th>What happened</th>
<th>File</th>
</tr>
</thead>
<tbody>
{#each data.events as event, i (i)}
<tr>
<td class="small">{timeOnly.format(event.at)}</td>
<td><Badge {...EVENT[event.action]} /></td>
<td>
{event.path}
{#if event.message}
<div class="small error">{event.message}</div>
{/if}
</td>
</tr>
{/each}
</tbody>
</table>
</div>
{/if}
</div>
<style>
.error {
color: var(--danger);
}
</style>

View File

@@ -10,7 +10,13 @@ export default defineConfig({
runes: ({ filename }) => runes: ({ filename }) =>
filename.split(/[/\\]/).includes('node_modules') ? undefined : true filename.split(/[/\\]/).includes('node_modules') ? undefined : true
}, },
adapter: adapter() adapter: adapter(),
paths: {
// Kit 3 fixes the site's address at build time. When unset, requests are assumed to
// arrive over HTTPS (e.g. through a reverse proxy). Set ORIGIN (e.g. http://sync:3000)
// when serving plain HTTP, or form posts are rejected as cross-site.
origin: process.env.ORIGIN || undefined
}
}) })
], ],
test: { test: {