From a281163353964ce6ea37f2520469acaeee508f4b Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Thu, 4 Jan 2024 17:18:57 +0000 Subject: [PATCH] Chore: Automatically build migration index --- .eslintignore | 1 + .gitignore | 1 + package.json | 2 +- packages/app-desktop/commands/index.ts | 4 +- .../gui/MainScreen/commands/index.ts | 4 +- .../gui/NoteEditor/commands/index.ts | 4 +- .../gui/NoteList/commands/index.ts | 4 +- .../gui/NoteListControls/commands/index.ts | 4 +- .../app-desktop/gui/Sidebar/commands/index.ts | 4 +- packages/app-desktop/gulpfile.ts | 4 +- packages/lib/JoplinDatabase.ts | 14 +------ packages/lib/commands/index.ts | 4 +- .../lib/services/database/migrations/index.ts | 17 ++++++++ ...dCommandIndex.js => buildScriptIndexes.js} | 39 ++++++++++++++----- ...ndIndexRun.js => buildScriptIndexesRun.js} | 4 +- 15 files changed, 69 insertions(+), 41 deletions(-) create mode 100644 packages/lib/services/database/migrations/index.ts rename packages/tools/gulp/tasks/{buildCommandIndex.js => buildScriptIndexes.js} (50%) rename packages/tools/gulp/tasks/{buildCommandIndexRun.js => buildScriptIndexesRun.js} (52%) diff --git a/.eslintignore b/.eslintignore index bb23ded58..21772c4b1 100644 --- a/.eslintignore +++ b/.eslintignore @@ -781,6 +781,7 @@ packages/lib/services/database/migrations/42.js packages/lib/services/database/migrations/43.js packages/lib/services/database/migrations/44.js packages/lib/services/database/migrations/45.js +packages/lib/services/database/migrations/index.js packages/lib/services/database/sqlStringToLines.js packages/lib/services/database/types.js packages/lib/services/debug/populateDatabase.js diff --git a/.gitignore b/.gitignore index d716b1062..dbc4d20bb 100644 --- a/.gitignore +++ b/.gitignore @@ -761,6 +761,7 @@ packages/lib/services/database/migrations/42.js packages/lib/services/database/migrations/43.js packages/lib/services/database/migrations/44.js packages/lib/services/database/migrations/45.js +packages/lib/services/database/migrations/index.js packages/lib/services/database/sqlStringToLines.js packages/lib/services/database/types.js packages/lib/services/debug/populateDatabase.js diff --git a/package.json b/package.json index 4d03ba713..98a08f152 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ }, "scripts": { "buildApiDoc": "yarn workspace joplin start apidoc ../../readme/api/references/rest_api.md", - "buildCommandIndex": "node packages/tools/gulp/tasks/buildCommandIndexRun.js", + "buildScriptIndexes": "node packages/tools/gulp/tasks/buildScriptIndexesRun.js", "buildParallel": "yarn workspaces foreach --verbose --interlaced --parallel --jobs 2 --topological run build && yarn run tsc", "buildPluginDoc": "cd packages/generate-plugin-doc && yarn run buildPluginDoc_", "buildSequential": "yarn workspaces foreach --verbose --interlaced --topological run build && yarn run tsc", diff --git a/packages/app-desktop/commands/index.ts b/packages/app-desktop/commands/index.ts index 79354c90c..99c080b44 100644 --- a/packages/app-desktop/commands/index.ts +++ b/packages/app-desktop/commands/index.ts @@ -1,4 +1,4 @@ -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` import * as copyDevCommand from './copyDevCommand'; import * as editProfileConfig from './editProfileConfig'; import * as exportFolders from './exportFolders'; @@ -36,4 +36,4 @@ const index:any[] = [ ]; export default index; -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` diff --git a/packages/app-desktop/gui/MainScreen/commands/index.ts b/packages/app-desktop/gui/MainScreen/commands/index.ts index d496cef63..85b7972d9 100644 --- a/packages/app-desktop/gui/MainScreen/commands/index.ts +++ b/packages/app-desktop/gui/MainScreen/commands/index.ts @@ -1,4 +1,4 @@ -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` import * as addProfile from './addProfile'; import * as commandPalette from './commandPalette'; import * as deleteFolder from './deleteFolder'; @@ -92,4 +92,4 @@ const index:any[] = [ ]; export default index; -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` diff --git a/packages/app-desktop/gui/NoteEditor/commands/index.ts b/packages/app-desktop/gui/NoteEditor/commands/index.ts index 129382cb6..cfa1d884b 100644 --- a/packages/app-desktop/gui/NoteEditor/commands/index.ts +++ b/packages/app-desktop/gui/NoteEditor/commands/index.ts @@ -1,4 +1,4 @@ -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` import * as focusElementNoteBody from './focusElementNoteBody'; import * as focusElementNoteTitle from './focusElementNoteTitle'; import * as pasteAsText from './pasteAsText'; @@ -14,4 +14,4 @@ const index:any[] = [ ]; export default index; -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` diff --git a/packages/app-desktop/gui/NoteList/commands/index.ts b/packages/app-desktop/gui/NoteList/commands/index.ts index aeb87d0c1..5493c6e2f 100644 --- a/packages/app-desktop/gui/NoteList/commands/index.ts +++ b/packages/app-desktop/gui/NoteList/commands/index.ts @@ -1,4 +1,4 @@ -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` import * as focusElementNoteList from './focusElementNoteList'; const index:any[] = [ @@ -6,4 +6,4 @@ const index:any[] = [ ]; export default index; -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` diff --git a/packages/app-desktop/gui/NoteListControls/commands/index.ts b/packages/app-desktop/gui/NoteListControls/commands/index.ts index cc09efc79..5815ab0f6 100644 --- a/packages/app-desktop/gui/NoteListControls/commands/index.ts +++ b/packages/app-desktop/gui/NoteListControls/commands/index.ts @@ -1,4 +1,4 @@ -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` import * as focusSearch from './focusSearch'; const index:any[] = [ @@ -6,4 +6,4 @@ const index:any[] = [ ]; export default index; -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` diff --git a/packages/app-desktop/gui/Sidebar/commands/index.ts b/packages/app-desktop/gui/Sidebar/commands/index.ts index 5a3f58f9b..84c916b63 100644 --- a/packages/app-desktop/gui/Sidebar/commands/index.ts +++ b/packages/app-desktop/gui/Sidebar/commands/index.ts @@ -1,4 +1,4 @@ -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` import * as focusElementSideBar from './focusElementSideBar'; const index:any[] = [ @@ -6,4 +6,4 @@ const index:any[] = [ ]; export default index; -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` diff --git a/packages/app-desktop/gulpfile.ts b/packages/app-desktop/gulpfile.ts index 871cec44d..a399bbca9 100644 --- a/packages/app-desktop/gulpfile.ts +++ b/packages/app-desktop/gulpfile.ts @@ -39,7 +39,7 @@ const tasks = { }, tsc: require('@joplin/tools/gulp/tasks/tsc'), updateIgnoredTypeScriptBuild: require('@joplin/tools/gulp/tasks/updateIgnoredTypeScriptBuild'), - buildCommandIndex: require('@joplin/tools/gulp/tasks/buildCommandIndex'), + buildScriptIndexes: require('@joplin/tools/gulp/tasks/buildScriptIndexes'), compileSass: { fn: async () => { await compileSass( @@ -58,7 +58,7 @@ const buildBeforeStartParallel = [ 'copyPluginAssets', 'copyApplicationAssets', 'updateIgnoredTypeScriptBuild', - 'buildCommandIndex', + 'buildScriptIndexes', 'compileSass', ]; diff --git a/packages/lib/JoplinDatabase.ts b/packages/lib/JoplinDatabase.ts index c41d9ad26..138772cfc 100644 --- a/packages/lib/JoplinDatabase.ts +++ b/packages/lib/JoplinDatabase.ts @@ -1,13 +1,10 @@ import Resource from './models/Resource'; import shim from './shim'; import Database from './database'; -import migration42 from './services/database/migrations/42'; -import migration43 from './services/database/migrations/43'; -import migration44 from './services/database/migrations/44'; -import migration45 from './services/database/migrations/45'; -import { SqlQuery, Migration } from './services/database/types'; +import { SqlQuery } from './services/database/types'; import addMigrationFile from './services/database/addMigrationFile'; import sqlStringToLines from './services/database/sqlStringToLines'; +import migrations from './services/database/migrations'; const { sprintf } = require('sprintf-js'); @@ -125,13 +122,6 @@ CREATE TABLE version ( INSERT INTO version (version) VALUES (1); `; -const migrations: Migration[] = [ - migration42, - migration43, - migration44, - migration45, -]; - export interface TableField { name: string; type: number; diff --git a/packages/lib/commands/index.ts b/packages/lib/commands/index.ts index cc5746fd5..2def7bddc 100644 --- a/packages/lib/commands/index.ts +++ b/packages/lib/commands/index.ts @@ -1,4 +1,4 @@ -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` import * as historyBackward from './historyBackward'; import * as historyForward from './historyForward'; import * as openMasterPasswordDialog from './openMasterPasswordDialog'; @@ -12,4 +12,4 @@ const index:any[] = [ ]; export default index; -// AUTO-GENERATED using `gulp buildCommandIndex` +// AUTO-GENERATED using `gulp buildScriptIndexes` diff --git a/packages/lib/services/database/migrations/index.ts b/packages/lib/services/database/migrations/index.ts new file mode 100644 index 000000000..f165cc050 --- /dev/null +++ b/packages/lib/services/database/migrations/index.ts @@ -0,0 +1,17 @@ +// AUTO-GENERATED using `gulp buildScriptIndexes` +import migration42 from './42'; +import migration43 from './43'; +import migration44 from './44'; +import migration45 from './45'; + +import { Migration } from '../types'; + +const index: Migration[] = [ + migration42, + migration43, + migration44, + migration45, +]; + +export default index; +// AUTO-GENERATED using `gulp buildScriptIndexes` diff --git a/packages/tools/gulp/tasks/buildCommandIndex.js b/packages/tools/gulp/tasks/buildScriptIndexes.js similarity index 50% rename from packages/tools/gulp/tasks/buildCommandIndex.js rename to packages/tools/gulp/tasks/buildScriptIndexes.js index 767d1528a..a0928e498 100644 --- a/packages/tools/gulp/tasks/buildCommandIndex.js +++ b/packages/tools/gulp/tasks/buildScriptIndexes.js @@ -1,11 +1,18 @@ +// Takes a directory of TypeScript files and generate an index from it. + const utils = require('../utils'); const glob = require('glob'); const rootDir = utils.rootDir(); -async function processDirectory(dir) { +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'; + const tsFiles = glob.sync('{**/*.ts,**/*.tsx}', { cwd: dir, - }).filter(f => f !== 'index.ts'); + }).filter(f => `${dir}/${f}` !== indexFilePath); tsFiles.sort(); @@ -13,16 +20,21 @@ async function processDirectory(dir) { for (const tsFile of tsFiles) { const f = utils.getFilename(tsFile); - fileContent.push(`import * as ${f} from './${f}';`); + fileContent.push(`import ${importNameTemplate.replace(/FILE_NAME/g, f)} from './${f}';`); } fileContent.push(''); - fileContent.push('const index:any[] = ['); + if (imports) { + fileContent.push(imports); + fileContent.push(''); + } + + fileContent.push(`const index:${typeScriptType}[] = [`); for (const tsFile of tsFiles) { const f = utils.getFilename(tsFile); - fileContent.push(`\t${f},`); + fileContent.push(`\t${exportNameTemplate.replace(/FILE_NAME/g, f)},`); } fileContent.push('];'); @@ -31,13 +43,11 @@ async function processDirectory(dir) { fileContent.push('export default index;'); - const destFile = `${dir}/index.ts`; - - console.info(`Generating ${destFile}...`); + console.info(`Generating ${indexFilePath}...`); await utils.insertContentIntoFile( - destFile, - '// AUTO-GENERATED using `gulp buildCommandIndex`', + indexFilePath, + '// AUTO-GENERATED using `gulp buildScriptIndexes`', fileContent.join('\n'), true, ); @@ -53,5 +63,14 @@ module.exports = { await processDirectory(`${rootDir}/packages/app-desktop/gui/NoteListControls/commands`); await processDirectory(`${rootDir}/packages/app-desktop/gui/Sidebar/commands`); await processDirectory(`${rootDir}/packages/lib/commands`); + + await processDirectory( + `${rootDir}/packages/lib/services/database/migrations`, + null, + 'Migration', + 'import { Migration } from \'../types\';', + 'migrationFILE_NAME', + 'migrationFILE_NAME', + ); }, }; diff --git a/packages/tools/gulp/tasks/buildCommandIndexRun.js b/packages/tools/gulp/tasks/buildScriptIndexesRun.js similarity index 52% rename from packages/tools/gulp/tasks/buildCommandIndexRun.js rename to packages/tools/gulp/tasks/buildScriptIndexesRun.js index bbda5fd8d..c01286fa7 100644 --- a/packages/tools/gulp/tasks/buildCommandIndexRun.js +++ b/packages/tools/gulp/tasks/buildScriptIndexesRun.js @@ -1,6 +1,6 @@ -// Allow running that task "buildCommandIndex" without gulp +// Allow running that task "buildScriptIndexes" without gulp -const task = require('./buildCommandIndex.js'); +const task = require('./buildScriptIndexes.js'); const main = async () => { await task.fn();