2024-01-04 19:18:57 +02:00
|
|
|
// Takes a directory of TypeScript files and generate an index from it.
|
|
|
|
|
2021-09-04 14:43:25 +02:00
|
|
|
const utils = require('../utils');
|
|
|
|
const glob = require('glob');
|
|
|
|
const rootDir = utils.rootDir();
|
|
|
|
|
2024-01-04 19:18:57 +02: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 21:04:32 +02:00
|
|
|
const tsFiles = glob.sync('{*.ts,*.tsx}', {
|
2021-09-04 14:43:25 +02:00
|
|
|
cwd: dir,
|
2024-02-26 12:27:34 +02: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 14:43:25 +02:00
|
|
|
|
2023-07-01 13:56:34 +02:00
|
|
|
tsFiles.sort();
|
|
|
|
|
2021-09-04 14:43:25 +02:00
|
|
|
const fileContent = [];
|
|
|
|
|
|
|
|
for (const tsFile of tsFiles) {
|
2021-09-04 16:15:25 +02:00
|
|
|
const f = utils.getFilename(tsFile);
|
2024-01-04 19:18:57 +02:00
|
|
|
fileContent.push(`import ${importNameTemplate.replace(/FILE_NAME/g, f)} from './${f}';`);
|
2021-09-04 14:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fileContent.push('');
|
|
|
|
|
2024-01-04 19:18:57 +02:00
|
|
|
if (imports) {
|
|
|
|
fileContent.push(imports);
|
|
|
|
fileContent.push('');
|
|
|
|
}
|
|
|
|
|
2024-01-05 11:46:53 +02:00
|
|
|
fileContent.push(`const index: ${typeScriptType}[] = [`);
|
2021-09-04 14:43:25 +02:00
|
|
|
|
|
|
|
for (const tsFile of tsFiles) {
|
2021-09-04 16:15:25 +02:00
|
|
|
const f = utils.getFilename(tsFile);
|
2024-01-04 19:18:57 +02:00
|
|
|
fileContent.push(`\t${exportNameTemplate.replace(/FILE_NAME/g, f)},`);
|
2021-09-04 14:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fileContent.push('];');
|
|
|
|
|
|
|
|
fileContent.push('');
|
|
|
|
|
|
|
|
fileContent.push('export default index;');
|
|
|
|
|
2024-01-04 19:18:57 +02:00
|
|
|
console.info(`Generating ${indexFilePath}...`);
|
2021-09-04 14:43:25 +02:00
|
|
|
|
|
|
|
await utils.insertContentIntoFile(
|
2024-01-04 19:18:57 +02:00
|
|
|
indexFilePath,
|
|
|
|
'// AUTO-GENERATED using `gulp buildScriptIndexes`',
|
2021-09-04 14:43:25 +02:00
|
|
|
fileContent.join('\n'),
|
2023-08-22 12:58:53 +02:00
|
|
|
true,
|
2021-09-04 14:43:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
src: '',
|
|
|
|
fn: async function() {
|
|
|
|
await processDirectory(`${rootDir}/packages/app-desktop/commands`);
|
2024-11-08 17:32:05 +02:00
|
|
|
await processDirectory(`${rootDir}/packages/app-desktop/gui/WindowCommandsAndDialogs/commands`);
|
2021-09-04 14:43:25 +02:00
|
|
|
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 21:04:32 +02:00
|
|
|
await processDirectory(`${rootDir}/packages/app-mobile/commands`);
|
2024-12-11 14:31:05 +02:00
|
|
|
await processDirectory(`${rootDir}/packages/app-mobile/components/screens/Note/commands`);
|
2021-09-04 14:43:25 +02:00
|
|
|
await processDirectory(`${rootDir}/packages/lib/commands`);
|
2024-01-04 19:18:57 +02:00
|
|
|
|
|
|
|
await processDirectory(
|
|
|
|
`${rootDir}/packages/lib/services/database/migrations`,
|
|
|
|
null,
|
|
|
|
'Migration',
|
|
|
|
'import { Migration } from \'../types\';',
|
|
|
|
'migrationFILE_NAME',
|
|
|
|
'migrationFILE_NAME',
|
|
|
|
);
|
2021-09-04 14:43:25 +02:00
|
|
|
},
|
|
|
|
};
|