2021-01-29 20:45:11 +02:00
|
|
|
import { execCommand2, rootDir } from './tool-utils';
|
|
|
|
|
|
|
|
const sqlts = require('@rmp135/sql-ts').default;
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
// Run the CLI app once so as to generate the database file
|
|
|
|
process.chdir(`${rootDir}/packages/app-cli`);
|
2022-06-15 13:49:23 +02:00
|
|
|
await execCommand2('yarn start version');
|
2021-01-29 20:45:11 +02:00
|
|
|
|
|
|
|
const sqlTsConfig = {
|
|
|
|
'client': 'sqlite3',
|
|
|
|
'connection': {
|
2021-05-13 18:57:37 +02:00
|
|
|
'filename': `${require('os').homedir()}/.config/joplindev/database.sqlite`,
|
2021-01-29 20:45:11 +02:00
|
|
|
},
|
|
|
|
'tableNameCasing': 'pascal',
|
|
|
|
'singularTableNames': true,
|
|
|
|
'useNullAsDefault': true, // To disable warning "sqlite does not support inserting default values"
|
|
|
|
'excludedTables': [
|
|
|
|
'main.notes_fts',
|
|
|
|
'main.notes_fts_segments',
|
|
|
|
'main.notes_fts_segdir',
|
|
|
|
'main.notes_fts_docsize',
|
|
|
|
'main.notes_fts_stat',
|
2021-08-17 13:03:19 +02:00
|
|
|
'main.master_keys',
|
2021-01-29 20:45:11 +02:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const definitions = await sqlts.toObject(sqlTsConfig);
|
|
|
|
|
|
|
|
definitions.tables = definitions.tables.map((t: any) => {
|
|
|
|
t.columns.push({
|
|
|
|
nullable: false,
|
|
|
|
name: 'type_',
|
|
|
|
type: 'int',
|
|
|
|
optional: true,
|
|
|
|
isEnum: false,
|
|
|
|
propertyName: 'type_',
|
|
|
|
propertyType: 'number',
|
|
|
|
});
|
|
|
|
|
|
|
|
return t;
|
|
|
|
});
|
|
|
|
|
2022-06-15 13:54:00 +02:00
|
|
|
definitions.tables = definitions.tables.map((table: any) => {
|
|
|
|
table.columns = table.columns.map((column: any) => {
|
|
|
|
return {
|
|
|
|
...column,
|
|
|
|
optional: true,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return table;
|
|
|
|
});
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
const tsString = sqlts.fromObject(definitions, sqlTsConfig)
|
|
|
|
.replace(/": /g, '"?: ');
|
|
|
|
const header = `// AUTO-GENERATED BY ${__filename.substr(rootDir.length + 1)}`;
|
|
|
|
|
|
|
|
const targetFile = `${rootDir}/packages/lib/services/database/types.ts`;
|
|
|
|
console.info(`Writing type definitions to ${targetFile}...`);
|
2021-06-15 18:17:12 +02:00
|
|
|
|
|
|
|
const existingContent = (await fs.pathExists(targetFile)) ? await fs.readFile(targetFile, 'utf8') : '';
|
|
|
|
const splitted = existingContent.split('// AUTO-GENERATED BY');
|
|
|
|
const staticContent = splitted[0];
|
|
|
|
|
|
|
|
await fs.writeFile(targetFile, `${staticContent}\n\n${header}\n\n${tsString}`, 'utf8');
|
2021-01-29 20:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
});
|