| 
									
										
										
										
											2024-01-04 17:18:57 +00:00
										 |  |  | // Takes a directory of TypeScript files and generate an index from it.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | const utils = require('../utils'); | 
					
						
							|  |  |  | const glob = require('glob'); | 
					
						
							|  |  |  | const rootDir = utils.rootDir(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 17:18:57 +00:00
										 |  |  | async function processDirectory(dir, indexFilePath = null, typeScriptType = null, imports = null, importNameTemplate = null, exportNameTemplate = null) { | 
					
						
							|  |  |  | 	if (!indexFilePath) indexFilePath = `${dir}/index.ts`; | 
					
						
							|  |  |  | 	if (!typeScriptType) typeScriptType = 'any'; | 
					
						
							|  |  |  | 	if (!importNameTemplate) importNameTemplate = '* as FILE_NAME'; | 
					
						
							|  |  |  | 	if (!exportNameTemplate) exportNameTemplate = 'FILE_NAME'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 12:04:32 -07:00
										 |  |  | 	const tsFiles = glob.sync('{*.ts,*.tsx}', { | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 		cwd: dir, | 
					
						
							| 
									
										
										
										
											2024-02-26 10:27:34 +00:00
										 |  |  | 	}).filter(f => `${dir}/${f}` !== indexFilePath) | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// Exclude Jest test files to
 | 
					
						
							|  |  |  | 	// not include them in index.ts
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 		.filter(f => !f.endsWith('.test.ts')); | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-01 12:56:34 +01:00
										 |  |  | 	tsFiles.sort(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 	const fileContent = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (const tsFile of tsFiles) { | 
					
						
							| 
									
										
										
										
											2021-09-04 15:15:25 +01:00
										 |  |  | 		const f = utils.getFilename(tsFile); | 
					
						
							| 
									
										
										
										
											2024-01-04 17:18:57 +00:00
										 |  |  | 		fileContent.push(`import ${importNameTemplate.replace(/FILE_NAME/g, f)} from './${f}';`); | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fileContent.push(''); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 17:18:57 +00:00
										 |  |  | 	if (imports) { | 
					
						
							|  |  |  | 		fileContent.push(imports); | 
					
						
							|  |  |  | 		fileContent.push(''); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 09:46:53 +00:00
										 |  |  | 	fileContent.push(`const index: ${typeScriptType}[] = [`); | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (const tsFile of tsFiles) { | 
					
						
							| 
									
										
										
										
											2021-09-04 15:15:25 +01:00
										 |  |  | 		const f = utils.getFilename(tsFile); | 
					
						
							| 
									
										
										
										
											2024-01-04 17:18:57 +00:00
										 |  |  | 		fileContent.push(`\t${exportNameTemplate.replace(/FILE_NAME/g, f)},`); | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fileContent.push('];'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fileContent.push(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fileContent.push('export default index;'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 17:18:57 +00:00
										 |  |  | 	console.info(`Generating ${indexFilePath}...`); | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	await utils.insertContentIntoFile( | 
					
						
							| 
									
										
										
										
											2024-01-04 17:18:57 +00:00
										 |  |  | 		indexFilePath, | 
					
						
							|  |  |  | 		'// AUTO-GENERATED using `gulp buildScriptIndexes`', | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 		fileContent.join('\n'), | 
					
						
							| 
									
										
										
										
											2023-08-22 11:58:53 +01:00
										 |  |  | 		true, | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 	); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  | 	src: '', | 
					
						
							|  |  |  | 	fn: async function() { | 
					
						
							|  |  |  | 		await processDirectory(`${rootDir}/packages/app-desktop/commands`); | 
					
						
							|  |  |  | 		await processDirectory(`${rootDir}/packages/app-desktop/gui/MainScreen/commands`); | 
					
						
							|  |  |  | 		await processDirectory(`${rootDir}/packages/app-desktop/gui/NoteEditor/commands`); | 
					
						
							|  |  |  | 		await processDirectory(`${rootDir}/packages/app-desktop/gui/NoteList/commands`); | 
					
						
							|  |  |  | 		await processDirectory(`${rootDir}/packages/app-desktop/gui/NoteListControls/commands`); | 
					
						
							|  |  |  | 		await processDirectory(`${rootDir}/packages/app-desktop/gui/Sidebar/commands`); | 
					
						
							| 
									
										
										
										
											2024-03-14 12:04:32 -07:00
										 |  |  | 		await processDirectory(`${rootDir}/packages/app-mobile/commands`); | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 		await processDirectory(`${rootDir}/packages/lib/commands`); | 
					
						
							| 
									
										
										
										
											2024-01-04 17:18:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		await processDirectory( | 
					
						
							|  |  |  | 			`${rootDir}/packages/lib/services/database/migrations`, | 
					
						
							|  |  |  | 			null, | 
					
						
							|  |  |  | 			'Migration', | 
					
						
							|  |  |  | 			'import { Migration } from \'../types\';', | 
					
						
							|  |  |  | 			'migrationFILE_NAME', | 
					
						
							|  |  |  | 			'migrationFILE_NAME', | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-09-04 13:43:25 +01:00
										 |  |  | 	}, | 
					
						
							|  |  |  | }; |