@nocode-factory/sanity-kit 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ncf-kit.mjs +201 -0
- package/dist/index.d.ts +1643 -0
- package/dist/index.js +6632 -0
- package/dist/index.js.map +1 -0
- package/dist/web-server.d.ts +46 -0
- package/dist/web-server.js +51 -0
- package/dist/web-server.js.map +1 -0
- package/dist/web.d.ts +27 -0
- package/dist/web.js +117 -0
- package/dist/web.js.map +1 -0
- package/package.json +90 -0
- package/scripts/use-client.mjs +20 -0
- package/src/config/defaults.ts +74 -0
- package/src/config/resolve.ts +153 -0
- package/src/config/types.ts +290 -0
- package/src/config/validate.ts +193 -0
- package/src/context/BoardLayoutContext.tsx +24 -0
- package/src/context/KitContext.tsx +30 -0
- package/src/context/PreviewTargetContext.tsx +27 -0
- package/src/hooks/useAnnotations.ts +60 -0
- package/src/hooks/useBoardLayout.ts +398 -0
- package/src/hooks/useColumnOptions.ts +47 -0
- package/src/hooks/useDocumentColumns.ts +61 -0
- package/src/hooks/useHistory.ts +89 -0
- package/src/hooks/useInternalColumns.ts +19 -0
- package/src/hooks/useKitClient.ts +15 -0
- package/src/hooks/useOccurrences.ts +62 -0
- package/src/hooks/usePageContents.ts +65 -0
- package/src/hooks/usePages.ts +41 -0
- package/src/hooks/usePreviewTarget.ts +162 -0
- package/src/hooks/useQueryData.ts +103 -0
- package/src/hooks/useRegistration.ts +50 -0
- package/src/index.ts +280 -0
- package/src/lib/blocks.ts +38 -0
- package/src/lib/colors.ts +103 -0
- package/src/lib/columns.ts +137 -0
- package/src/lib/diff.ts +200 -0
- package/src/lib/elkLayout.ts +124 -0
- package/src/lib/graph.ts +238 -0
- package/src/lib/grouping.ts +116 -0
- package/src/lib/links.ts +70 -0
- package/src/lib/occurrences.ts +93 -0
- package/src/lib/pageThumbnail.ts +43 -0
- package/src/lib/pages.ts +199 -0
- package/src/lib/paths.ts +68 -0
- package/src/lib/query.ts +113 -0
- package/src/lib/schemaColumns.ts +162 -0
- package/src/lib/schemaTypes.ts +75 -0
- package/src/lib/sectionsMap.ts +83 -0
- package/src/lib/service.ts +88 -0
- package/src/lib/siteRoutes.ts +43 -0
- package/src/lib/status.ts +122 -0
- package/src/lib/studioLinks.ts +137 -0
- package/src/schema/boardLayout.ts +190 -0
- package/src/tools/components/BlockCard.tsx +79 -0
- package/src/tools/components/ComponentsTool.tsx +171 -0
- package/src/tools/components/map/SectionNode.tsx +109 -0
- package/src/tools/components/map/SectionsMap.tsx +339 -0
- package/src/tools/createToolComponent.tsx +67 -0
- package/src/tools/history/ChangeRow.tsx +87 -0
- package/src/tools/history/DiffPanel.tsx +227 -0
- package/src/tools/history/HistoryTool.tsx +230 -0
- package/src/tools/more/MoreTool.tsx +82 -0
- package/src/tools/pages/ColumnHeader.tsx +216 -0
- package/src/tools/pages/ColumnVisibility.tsx +173 -0
- package/src/tools/pages/FieldValue.tsx +327 -0
- package/src/tools/pages/PagesTable.tsx +394 -0
- package/src/tools/pages/PagesTool.tsx +333 -0
- package/src/tools/pages/PagesToolbar.tsx +53 -0
- package/src/tools/pages/PathCell.tsx +71 -0
- package/src/tools/pages/PreviewTarget.tsx +226 -0
- package/src/tools/pages/TagCell.tsx +124 -0
- package/src/tools/pages/ViewTabs.tsx +57 -0
- package/src/tools/pages/board/BoardView.tsx +661 -0
- package/src/tools/pages/board/PageNode.tsx +306 -0
- package/src/tools/pages/board/PagePanel.tsx +228 -0
- package/src/tools/presentation/PreviewHeader.tsx +75 -0
- package/src/tools/shared/AnnotationNode.tsx +126 -0
- package/src/ui/ActorName.tsx +20 -0
- package/src/ui/ConfigIssues.tsx +41 -0
- package/src/ui/InlineCode.tsx +22 -0
- package/src/ui/Popover.tsx +85 -0
- package/src/ui/ProjectStatusNotice.tsx +58 -0
- package/src/ui/QueryError.tsx +16 -0
- package/src/ui/QuietInput.tsx +64 -0
- package/src/ui/StatusBadge.tsx +11 -0
- package/src/ui/ToolPlaceholder.tsx +23 -0
- package/src/ui/formatDate.ts +14 -0
- package/src/ui/tokens.ts +54 -0
- package/src/version.ts +5 -0
- package/src/web/PreviewBanner.tsx +115 -0
- package/src/web/draftModeToggle.ts +56 -0
- package/src/web/environment.ts +21 -0
- package/src/web/index.ts +15 -0
- package/src/web/server.ts +11 -0
package/bin/ncf-kit.mjs
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Commande d'installation du kit : `npx ncf-kit setup`, depuis le dossier du
|
|
3
|
+
// studio. En une passe :
|
|
4
|
+
// 1. active le projet chez NoCode Factory (fonction `activate`, authentifiée
|
|
5
|
+
// par le jeton Sanity de l'appelant, qui doit administrer le projet) ;
|
|
6
|
+
// 2. écrit la clé de lecture dans .env (SANITY_STUDIO_NCF_PROJECT_KEY) ;
|
|
7
|
+
// 3. crée le webhook du journal via l'API de gestion Sanity, avec la bonne
|
|
8
|
+
// forme (brouillons inclus, projection, secret), sans jamais supprimer un
|
|
9
|
+
// webhook existant : au quota, elle s'arrête et le dit.
|
|
10
|
+
// Aucune dépendance : Node 22 suffit.
|
|
11
|
+
import {readFileSync, writeFileSync, existsSync} from 'node:fs'
|
|
12
|
+
import {join, resolve} from 'node:path'
|
|
13
|
+
import {homedir} from 'node:os'
|
|
14
|
+
|
|
15
|
+
const DEFAULT_ENDPOINT = 'https://dkoxgivytgsmvqmjvmjt.supabase.co/functions/v1'
|
|
16
|
+
const SANITY_API = 'https://api.sanity.io'
|
|
17
|
+
const ENV_KEY = 'SANITY_STUDIO_NCF_PROJECT_KEY'
|
|
18
|
+
const WEBHOOK_NAME = 'NCF Kit : journal des modifications'
|
|
19
|
+
const PROJECTION = '{"id": _id, "type": _type, "rev": _rev, "at": _updatedAt, "actorId": identity(), "doc": @}'
|
|
20
|
+
|
|
21
|
+
const args = process.argv.slice(2)
|
|
22
|
+
const command = args[0]
|
|
23
|
+
const flag = (name) => {
|
|
24
|
+
const i = args.indexOf(`--${name}`)
|
|
25
|
+
return i >= 0 ? args[i + 1] : undefined
|
|
26
|
+
}
|
|
27
|
+
const has = (name) => args.includes(`--${name}`)
|
|
28
|
+
|
|
29
|
+
function usage(code = 0) {
|
|
30
|
+
console.log(`Usage : npx ncf-kit setup --types page,homePage [options]
|
|
31
|
+
|
|
32
|
+
Depuis le dossier du studio (celui de sanity.config.ts et de .env).
|
|
33
|
+
|
|
34
|
+
Options :
|
|
35
|
+
--types <a,b> Types de documents journalisés (obligatoire) : les types de pages.
|
|
36
|
+
--name <nom> Nom du client dans le registre (défaut : nom du projet Sanity).
|
|
37
|
+
--dir <chemin> Dossier du studio (défaut : dossier courant).
|
|
38
|
+
--dataset <nom> Dataset (défaut : SANITY_STUDIO_DATASET de .env, sinon production).
|
|
39
|
+
--endpoint <url> Service du kit (défaut : ${DEFAULT_ENDPOINT}).
|
|
40
|
+
--dry-run Active le projet mais n'écrit rien en local et ne crée pas de webhook.
|
|
41
|
+
|
|
42
|
+
Jeton Sanity : SANITY_AUTH_TOKEN, sinon celui de \`npx sanity login\`.`)
|
|
43
|
+
process.exit(code)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const fail = (message) => {
|
|
47
|
+
console.error(`\n✖ ${message}`)
|
|
48
|
+
process.exit(1)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** .env minimal : KEY=value, KEY="value", commentaires ignorés. */
|
|
52
|
+
function parseEnv(text) {
|
|
53
|
+
const out = {}
|
|
54
|
+
for (const line of text.split('\n')) {
|
|
55
|
+
const m = line.match(/^\s*(?:export\s+)?([A-Z0-9_]+)\s*=\s*(.*)\s*$/)
|
|
56
|
+
if (!m) continue
|
|
57
|
+
let value = m[2].trim()
|
|
58
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) value = value.slice(1, -1)
|
|
59
|
+
out[m[1]] = value
|
|
60
|
+
}
|
|
61
|
+
return out
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function upsertEnv(file, key, value) {
|
|
65
|
+
const line = `${key}="${value}"`
|
|
66
|
+
if (!existsSync(file)) {
|
|
67
|
+
writeFileSync(file, `${line}\n`)
|
|
68
|
+
return 'créé'
|
|
69
|
+
}
|
|
70
|
+
const text = readFileSync(file, 'utf8')
|
|
71
|
+
const re = new RegExp(`^\\s*${key}\\s*=.*$`, 'm')
|
|
72
|
+
const next = re.test(text) ? text.replace(re, line) : `${text.replace(/\n?$/, '\n')}${line}\n`
|
|
73
|
+
writeFileSync(file, next)
|
|
74
|
+
return re.test(text) ? 'mis à jour' : 'ajouté'
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function sanityToken() {
|
|
78
|
+
if (process.env.SANITY_AUTH_TOKEN) return process.env.SANITY_AUTH_TOKEN
|
|
79
|
+
try {
|
|
80
|
+
const config = JSON.parse(readFileSync(join(homedir(), '.config', 'sanity', 'config.json'), 'utf8'))
|
|
81
|
+
if (config.authToken) return config.authToken
|
|
82
|
+
} catch {
|
|
83
|
+
/* pas connecté */
|
|
84
|
+
}
|
|
85
|
+
return null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function request(url, options = {}) {
|
|
89
|
+
const response = await fetch(url, options)
|
|
90
|
+
const text = await response.text()
|
|
91
|
+
let body = {}
|
|
92
|
+
try {
|
|
93
|
+
body = text ? JSON.parse(text) : {}
|
|
94
|
+
} catch {
|
|
95
|
+
body = {raw: text}
|
|
96
|
+
}
|
|
97
|
+
return {ok: response.ok, status: response.status, body}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function setup() {
|
|
101
|
+
const dir = resolve(flag('dir') ?? process.cwd())
|
|
102
|
+
const types = (flag('types') ?? '')
|
|
103
|
+
.split(',')
|
|
104
|
+
.map((t) => t.trim())
|
|
105
|
+
.filter(Boolean)
|
|
106
|
+
if (!types.length) fail('--types est obligatoire : la liste des types de pages à journaliser, ex. --types page,homePage')
|
|
107
|
+
if (types.some((t) => !/^[A-Za-z_][A-Za-z0-9_.]*$/.test(t))) fail('--types : noms de types Sanity uniquement (lettres, chiffres, _ et .).')
|
|
108
|
+
|
|
109
|
+
const envFile = join(dir, '.env')
|
|
110
|
+
if (!existsSync(envFile)) fail(`Aucun .env dans ${dir}. Lance la commande depuis le dossier du studio, ou passe --dir.`)
|
|
111
|
+
const env = parseEnv(readFileSync(envFile, 'utf8'))
|
|
112
|
+
const projectId = env.SANITY_STUDIO_PROJECT_ID
|
|
113
|
+
const dataset = flag('dataset') ?? env.SANITY_STUDIO_DATASET ?? 'production'
|
|
114
|
+
if (!projectId) fail('SANITY_STUDIO_PROJECT_ID manquant dans .env.')
|
|
115
|
+
|
|
116
|
+
const token = sanityToken()
|
|
117
|
+
if (!token) fail('Aucun jeton Sanity : lance `npx sanity login`, ou exporte SANITY_AUTH_TOKEN.')
|
|
118
|
+
|
|
119
|
+
const endpoint = (flag('endpoint') ?? DEFAULT_ENDPOINT).replace(/\/$/, '')
|
|
120
|
+
const dryRun = has('dry-run')
|
|
121
|
+
|
|
122
|
+
console.log(`\nNCF Kit — projet ${projectId}, dataset ${dataset}${dryRun ? ' (simulation)' : ''}`)
|
|
123
|
+
|
|
124
|
+
/* 1. Activation et clés */
|
|
125
|
+
const activation = await request(`${endpoint}/activate`, {
|
|
126
|
+
method: 'POST',
|
|
127
|
+
headers: {'content-type': 'application/json', authorization: `Bearer ${token}`},
|
|
128
|
+
body: JSON.stringify({projectId, dataset, name: flag('name')}),
|
|
129
|
+
})
|
|
130
|
+
if (!activation.ok) {
|
|
131
|
+
const {error, hint} = activation.body
|
|
132
|
+
if (error === 'not_an_admin' || error === 'not_a_member') fail(`Activation refusée : ${hint ?? error}. Il faut un compte administrateur du projet Sanity.`)
|
|
133
|
+
if (error === 'project_blocked') fail('Ce projet est bloqué chez NoCode Factory : activation manuelle uniquement.')
|
|
134
|
+
fail(`Activation impossible (${activation.status}) : ${hint ?? error ?? JSON.stringify(activation.body)}`)
|
|
135
|
+
}
|
|
136
|
+
const {readKey, ingestSecret, name} = activation.body
|
|
137
|
+
console.log(`✔ Projet actif chez NoCode Factory${name ? ` (${name})` : ''}`)
|
|
138
|
+
|
|
139
|
+
/* 2. Clé de lecture dans .env */
|
|
140
|
+
const targets = [envFile, join(dir, '.env.production.local')].filter((f, i) => i === 0 || existsSync(f))
|
|
141
|
+
for (const file of targets) {
|
|
142
|
+
if (dryRun) console.log(` (simulation) ${ENV_KEY} dans ${file}`)
|
|
143
|
+
else console.log(`✔ ${ENV_KEY} ${upsertEnv(file, ENV_KEY, readKey)} dans ${file.replace(dir, '.')}`)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/* 3. Webhook du journal */
|
|
147
|
+
const hooksUrl = `${SANITY_API}/v2021-10-04/hooks/projects/${projectId}`
|
|
148
|
+
const ingestUrl = `${endpoint}/ingest?project=${encodeURIComponent(projectId)}&dataset=${encodeURIComponent(dataset)}`
|
|
149
|
+
const list = await request(hooksUrl, {headers: {authorization: `Bearer ${token}`}})
|
|
150
|
+
if (!list.ok) fail(`Impossible de lister les webhooks Sanity (${list.status}). Le jeton administre-t-il ce projet ?`)
|
|
151
|
+
const hooks = Array.isArray(list.body) ? list.body : []
|
|
152
|
+
const existing = hooks.find((h) => typeof h.url === 'string' && h.url.split('?')[0] === ingestUrl.split('?')[0] && h.url.includes(`project=${projectId}`) && h.dataset === dataset)
|
|
153
|
+
|
|
154
|
+
if (existing) {
|
|
155
|
+
console.log(`✔ Webhook déjà en place (« ${existing.name} », id ${existing.id}) : rien à créer.`)
|
|
156
|
+
if (existing.includeDrafts === false) console.log(' ⚠ includeDrafts est désactivé sur ce webhook : les brouillons ne seront pas journalisés. À corriger sur sanity.io/manage.')
|
|
157
|
+
} else if (dryRun) {
|
|
158
|
+
console.log(` (simulation) création du webhook « ${WEBHOOK_NAME} » vers ${ingestUrl}`)
|
|
159
|
+
} else {
|
|
160
|
+
const created = await request(hooksUrl, {
|
|
161
|
+
method: 'POST',
|
|
162
|
+
headers: {'content-type': 'application/json', authorization: `Bearer ${token}`},
|
|
163
|
+
body: JSON.stringify({
|
|
164
|
+
name: WEBHOOK_NAME,
|
|
165
|
+
description: 'Journal des modifications (NCF Kit). Brouillons inclus.',
|
|
166
|
+
url: ingestUrl,
|
|
167
|
+
dataset,
|
|
168
|
+
type: 'document',
|
|
169
|
+
rule: {on: ['create', 'update', 'delete'], filter: `_type in [${types.map((t) => JSON.stringify(t)).join(', ')}]`, projection: PROJECTION},
|
|
170
|
+
includeDrafts: true,
|
|
171
|
+
httpMethod: 'POST',
|
|
172
|
+
apiVersion: 'v2021-03-25',
|
|
173
|
+
secret: ingestSecret,
|
|
174
|
+
}),
|
|
175
|
+
})
|
|
176
|
+
if (!created.ok) {
|
|
177
|
+
const message = JSON.stringify(created.body)
|
|
178
|
+
if (/quota|limit|exceed|maximum/i.test(message)) {
|
|
179
|
+
fail(`Quota de webhooks atteint sur ce plan Sanity (${hooks.length} existant(s)). Aucun webhook n'a été supprimé : libère-en un ou change de plan, puis relance.`)
|
|
180
|
+
}
|
|
181
|
+
fail(`Création du webhook refusée (${created.status}) : ${message}`)
|
|
182
|
+
}
|
|
183
|
+
console.log(`✔ Webhook créé (id ${created.body.id}) : ${types.join(', ')}, brouillons inclus.`)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
console.log(`
|
|
187
|
+
Reste à faire :
|
|
188
|
+
1. Dans sanity.config.ts : history: {projectKey: process.env.${ENV_KEY}}
|
|
189
|
+
2. Redéployer le studio : npx sanity deploy
|
|
190
|
+
3. Modifier un document, puis vérifier l'onglet Historique.
|
|
191
|
+
Le journal part de maintenant : rien n'est reconstitué en arrière.`)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (command === 'setup') {
|
|
195
|
+
setup().catch((error) => fail(error?.message ?? String(error)))
|
|
196
|
+
} else if (!command || command === 'help' || has('help')) {
|
|
197
|
+
usage(command ? 0 : 1)
|
|
198
|
+
} else {
|
|
199
|
+
console.error(`Commande inconnue : ${command}`)
|
|
200
|
+
usage(1)
|
|
201
|
+
}
|