1+ /**
2+ * CloudCannon Configuration Build Script
3+ *
4+ * This script automates the generation of CloudCannon configuration for Hugolify.
5+ * It performs the following operations:
6+ *
7+ * 1. Executes the Hugo build command (yarn build)
8+ * 2. Generates archetypes from the archetypes.json file
9+ * - Creates .md files in the /archetypes folder with default fields
10+ * 3. Copies the generated configuration file to cloudcannon.config.yml
11+ *
12+ * Usage: node scripts/build-config-cloudcannon.js
13+ *
14+ * @requires child_process
15+ * @requires fs
16+ * @requires path
17+ */
18+
19+ const { exec } = require ( 'child_process' ) ;
20+ const fs = require ( 'fs' ) ;
21+ const path = require ( 'path' ) ;
22+
23+ // Function to execute a shell command
24+ function runCommand ( command ) {
25+ return new Promise ( ( resolve , reject ) => {
26+ console . log ( `Executing: ${ command } ` ) ;
27+ const process = exec ( command ) ;
28+
29+ process . stdout . on ( 'data' , ( data ) => {
30+ console . log ( data . toString ( ) ) ;
31+ } ) ;
32+
33+ process . stderr . on ( 'data' , ( data ) => {
34+ console . error ( data . toString ( ) ) ;
35+ } ) ;
36+
37+ process . on ( 'close' , ( code ) => {
38+ if ( code === 0 ) {
39+ resolve ( ) ;
40+ } else {
41+ reject ( new Error ( `Command failed with code ${ code } ` ) ) ;
42+ }
43+ } ) ;
44+ } ) ;
45+ }
46+
47+ // Main function
48+ async function buildAndCopy ( ) {
49+ try {
50+ // Step 1: Run the build
51+ console . log ( '📦 Starting build...' ) ;
52+ await runCommand ( 'yarn build' ) ;
53+ console . log ( '✅ Build completed successfully' ) ;
54+
55+ const projectRoot = path . join ( __dirname , '..' ) ;
56+
57+ // Step 2: Generate Archetypes
58+ console . log ( '\n🏗️ Generating archetypes...' ) ;
59+ const archetypesPath = path . join ( projectRoot , 'public' , 'admin' , 'config' , 'archetypes.json' ) ;
60+ if ( fs . existsSync ( archetypesPath ) ) {
61+ const archetypesData = JSON . parse ( fs . readFileSync ( archetypesPath , 'utf8' ) ) ;
62+ const archetypesDir = path . join ( projectRoot , 'archetypes' ) ;
63+
64+ if ( ! fs . existsSync ( archetypesDir ) ) {
65+ fs . mkdirSync ( archetypesDir , { recursive : true } ) ;
66+ }
67+
68+ for ( const [ collection , fields ] of Object . entries ( archetypesData ) ) {
69+
70+ const archetypeFile = path . join ( archetypesDir , `${ collection } .md` ) ;
71+ let content = '---\n' ;
72+ fields . forEach ( item => {
73+ if ( item . default !== "" ) {
74+ content += `${ item . field } : ${ item . default } \n` ;
75+ } else {
76+ content += `${ item . field } :\n` ;
77+ }
78+ } ) ;
79+ content += '---\n' ;
80+
81+ fs . writeFileSync ( archetypeFile , content ) ;
82+ console . log ( `Created archetype: ${ archetypeFile } ` ) ;
83+ }
84+ } else {
85+ console . warn ( `⚠️ Archetypes JSON not found at ${ archetypesPath } ` ) ;
86+ }
87+
88+ // Step 3: Copy the file
89+ console . log ( '\n📋 Copying config.yml file...' ) ;
90+ const sourcePath = path . join ( projectRoot , 'public' , 'admin' , 'config.yml' ) ;
91+ const destPath = path . join ( projectRoot , 'cloudcannon.config.yml' ) ;
92+
93+ if ( ! fs . existsSync ( sourcePath ) ) {
94+ throw new Error ( `Source file does not exist: ${ sourcePath } ` ) ;
95+ }
96+
97+ fs . copyFileSync ( sourcePath , destPath ) ;
98+ console . log ( `✅ File copied: ${ sourcePath } -> ${ destPath } ` ) ;
99+
100+ console . log ( '\n🎉 Process completed successfully!' ) ;
101+ } catch ( error ) {
102+ console . error ( '\n❌ Error:' , error . message ) ;
103+ process . exit ( 1 ) ;
104+ }
105+ }
106+
107+ // Run the script
108+ buildAndCopy ( ) ;
0 commit comments