| 
									
										
										
										
											2022-10-28 09:35:21 +02:00
										 |  |  | const { | 
					
						
							|  |  |  |   file: { | 
					
						
							|  |  |  |     providers: { createLocalFileDestinationProvider }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   strapi: { | 
					
						
							|  |  |  |     providers: { createLocalStrapiSourceProvider }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   engine: { createTransferEngine }, | 
					
						
							|  |  |  | } = require('@strapi/data-transfer'); | 
					
						
							| 
									
										
										
										
											2023-12-11 22:00:48 +01:00
										 |  |  | const { strapiFactory } = require('@strapi/strapi'); | 
					
						
							| 
									
										
										
										
											2022-10-28 09:35:21 +02:00
										 |  |  | const { ALLOWED_CONTENT_TYPES } = require('../constants'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Export the data from a strapi project. | 
					
						
							|  |  |  |  * This script should be run as `node <path-to>/dts-export.js [exportFilePath]` from the | 
					
						
							|  |  |  |  * root directory of a strapi project e.g. `/examples/kitchensink`. Remember to import | 
					
						
							|  |  |  |  * the `with-admin` tar file into the project first because the tests rely on the data. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const exportData = async () => { | 
					
						
							|  |  |  |   let args = process.argv.slice(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (args.length !== 1) { | 
					
						
							|  |  |  |     console.error('Please provide the export file name as a parameter.'); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const strapi = await createStrapiInstance(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const source = createSourceProvider(strapi); | 
					
						
							|  |  |  |   const destination = createDestinationProvider(args[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const engine = createTransferEngine(source, destination, { | 
					
						
							|  |  |  |     versionStrategy: 'ignore', // for an export to file, versionStrategy will always be skipped
 | 
					
						
							|  |  |  |     schemaStrategy: 'ignore', // for an export to file, schemaStrategy will always be skipped
 | 
					
						
							| 
									
										
										
										
											2024-01-18 10:22:06 +00:00
										 |  |  |     only: ['content', 'files'], | 
					
						
							| 
									
										
										
										
											2022-10-28 09:35:21 +02:00
										 |  |  |     transforms: { | 
					
						
							|  |  |  |       links: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           filter(link) { | 
					
						
							|  |  |  |             return ( | 
					
						
							|  |  |  |               ALLOWED_CONTENT_TYPES.includes(link.left.type) && | 
					
						
							|  |  |  |               ALLOWED_CONTENT_TYPES.includes(link.right.type) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       entities: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           filter(entity) { | 
					
						
							|  |  |  |             return ALLOWED_CONTENT_TYPES.includes(entity.type); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   engine.diagnostics.onDiagnostic(console.log); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     const results = await engine.transfer(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     console.log(JSON.stringify(results.engine, null, 2)); | 
					
						
							|  |  |  |   } catch { | 
					
						
							|  |  |  |     console.error('Export process failed.'); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   process.exit(0); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const createSourceProvider = (strapi) => | 
					
						
							|  |  |  |   createLocalStrapiSourceProvider({ | 
					
						
							|  |  |  |     async getStrapi() { | 
					
						
							|  |  |  |       return strapi; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const createDestinationProvider = (filePath) => | 
					
						
							|  |  |  |   createLocalFileDestinationProvider({ | 
					
						
							|  |  |  |     file: { path: filePath }, | 
					
						
							|  |  |  |     encryption: { enabled: false }, | 
					
						
							|  |  |  |     compression: { enabled: false }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const createStrapiInstance = async (logLevel = 'error') => { | 
					
						
							| 
									
										
										
										
											2023-12-11 22:00:48 +01:00
										 |  |  |   const appContext = await strapiFactory.compile(); | 
					
						
							|  |  |  |   const app = strapiFactory(appContext); | 
					
						
							| 
									
										
										
										
											2022-10-28 09:35:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   app.log.level = logLevel; | 
					
						
							|  |  |  |   const loadedApp = await app.load(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return loadedApp; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exportData(); |