Setting the file. One moment.
Seed Members · Wix Headless Templates · wix/skills · Skills Docs
ContentsBack to the top of the page members/seed/seed-members.mjs
members/seed/ seed-members.mjs
JavaScript · 92 lines · 4 KB
// Seeding is ADDITIVE — never deletes or overwrites existing content. The appDefId below is the
15 // Members Area app's App Market id; the identity/profile split is described beside it.
16 import { seedSiteId } from "../../shared/seed/site-context.mjs" ;
17 import { wixToken } from "../../shared/seed/wix-cli.mjs" ;
18 import { isMain } from "../../shared/seed/main.mjs" ;
19 import { readFileSync } from "node:fs" ;
20
21 const API = "https://www.wixapis.com" ;
22 // The Wix Members Area app (the profile layer). Not in the "Apps Created by Wix" table —
23 // the GUID comes from the App Market listing.
24 // Installing it pulls in its Site-Members dependency automatically.
25 const MEMBERS_AREA_APP_ID = "14cc59bc-f0b7-15b8-e1c7-89ce41d0e0c9" ;
26
27 export function makeCtx ({ cwd = process. cwd () } = {}) {
28 // The content site: the config's site, or the parent on a migration preview (site-context.mjs stops
29 // a seed there unless --allow-parent is passed after the user confirmed).
30 const siteId = seedSiteId ({ cwd, argv: process.argv });
31 const token = wixToken (siteId, cwd);
32 return { token, siteId };
33 }
34
35 async function req ( ctx , path , { method = "POST" , body } = {}) {
36 const res = await fetch ( API + path, {
37 method,
38 headers: {
39 Authorization: `Bearer ${ ctx . token }` ,
40 "wix-site-id" : ctx.siteId,
41 "Content-Type" : "application/json" ,
42 },
43 body: body ? JSON . stringify (body) : undefined ,
44 });
45 const json = await res. json (). catch (() => ({}));
46 if ( ! res.ok) throw new Error ( `${ method } ${ path } -> ${ res . status }: ${ JSON . stringify ( json ). slice ( 0 , 400 ) }` );
47 return json;
48 }
49
50 // ---- operations ----------------------------------------------------------------------------------
51
52 /**
53 * Install the Wix Members Area app (idempotent in effect: re-running against a site that
54 * already has it errors — recorded in the result, not thrown, since "already installed" and
55 * a real failure aren't distinguishable from the response).
56 * docs: https://dev.wix.com/docs/api-reference/articles/work-with-wix-apis/platform/about-apps-created-by-wix.md
57 */
58 export async function installMembersAreaApp ( ctx ) {
59 try {
60 await req (ctx, "/apps-installer-service/v1/app-instance/install" , { body: {
61 tenant: { tenantType: "SITE" , id: ctx.siteId },
62 appInstance: { appDefId: MEMBERS_AREA_APP_ID , enabled: true },
63 } });
64 return { requested: true , note: null };
65 } catch (e) {
66 return { requested: false , note: String (e.message). slice ( 0 , 300 ) };
67 }
68 }
69
70 /** ONE-CALL seed: install the Members Area app (the profile layer). The default path. */
71 export async function setupMembers ( ctx , { installMembersArea = true } = {}) {
72 return {
73 membersAreaAppId: MEMBERS_AREA_APP_ID ,
74 membersAreaInstall: installMembersArea ? await installMembersAreaApp (ctx) : { skipped: true },
75 membersCreated: 0 , // members self-register — never created at build time
76 };
77 }
78
79 // ---- CLI entry ----------------------------------------------------------------------------------
80
81 const invokedDirectly = isMain ( import . meta .url);
82 if (invokedDirectly) {
83 const planPath = process.argv[ 2 ];
84 const plan = planPath ? JSON . parse ( readFileSync (planPath, "utf8" )) : {};
85 const ctx = makeCtx ();
86 setupMembers (ctx, plan)
87 . then (( result ) => console. log ( JSON . stringify (result, null , 2 )))
88 . catch (( e ) => {
89 console. error (e.message);
90 process. exit ( 1 );
91 });
92 }