import { BACKEND_URL_ENV, envFile, integrationDependencies, offlineInstruction, scriptImports, scriptsProperty, sortImports, spaBuildOptions, vendorInstruction, } from './source.ts'; import type { BoilerplateOptions, BoilerplateTemplate } from './types.ts'; /** The link that opens the preference dialog, without any script. */ export const PREFERENCES_LINK = 'Privacy settings'; /** * A minimal page for a project without an `index.html`. * @param title Page title. * @param head Extra lines inside ``, already indented by two tabs. * @param body Extra lines after the footer, already indented by two tabs. * @returns The page. */ export const htmlPage = (title: string, head: string, body: string): string => ` ${title} ${head} ${body} `; /** * Generate the JavaScript (ESM) quickstart: `vite.config.ts`, `src/main.ts` * and a privacy settings link in `index.html`. * @param options Mode and vendor selections. * @returns Files relative to the project root. */ export const generateJavaScriptBoilerplate = ( options: BoilerplateOptions ): BoilerplateTemplate => { const mode = options.mode === 'offline' ? 'local' : 'manifest'; return { dependencies: [ '@c15t/browser', 'c15t', ...integrationDependencies(options), ], files: { ...envFile(BACKEND_URL_ENV.vite, options), 'index.html': htmlPage( 'c15t with JavaScript', '', '\t\t\n' ), 'src/main.ts': `${sortImports([ `import { init, ${mode} } from '@c15t/browser';`, ...scriptImports(options.scripts), ])} init({ mode: ${mode}(), ${scriptsProperty(options.scripts, '\t')}}); `, 'vite.config.ts': `import { consentManifest } from 'c15t/build'; import { defineConfig } from 'vite'; export default defineConfig({ plugins: [consentManifest(${spaBuildOptions(options)})], }); `, }, instructions: [ 'Call init() once from the script your page loads. The client opens the preference dialog for any link to #c15t-preferences.', ...vendorInstruction(options, 'src/main.ts'), ...offlineInstruction(options), ], merge: { 'index.html': { inserts: [{ before: '', content: PREFERENCES_LINK }], marker: '#c15t-preferences', type: 'insert', }, }, }; };