l7note
Version:
Access your notion notes quick
83 lines (72 loc) • 2.16 kB
text/typescript
import chalk from 'chalk';
import { exit } from 'process';
import prompts, { Choice, PromptObject } from 'prompts';
import { createDataBase } from './noteActions/createDatabase.js';
import { getDatabaseList } from './noteActions/getDatabaseList.js';
import { globalConfig, saveConfig } from './setup.js';
const setupQuestions: PromptObject[] = [
{
type: 'text',
name: 'token',
message: 'Enter your integration secret token:',
validate: input =>
/secret_.*/.test(input) ? true : 'This input was not the right token',
},
{
type: 'select',
name: 'select',
message: 'Where is the database:',
choices: [
{ title: 'Use a existing database', value: 0 },
{ title: 'Create a new database on a page', value: 1 },
],
},
];
const pageIdQuestions: PromptObject[] = [
{
type: 'text',
name: 'pageId',
message: 'Enter your Page Id',
validate: input =>
/.{32}/.test(input) ? true : 'This input was not a real Id',
},
];
const configureSettings = async () => {
const res = await prompts(setupQuestions);
if (!res.token) {
exit(1);
}
globalConfig.token = res.token;
if (res.select == 0) {
//
const dbList = await getDatabaseList();
const dbChoiseList = dbList.map(db => {
return { title: db.title, value: db.id };
});
const dataBaseSelectQuestions: PromptObject[] = [
{
type: 'select',
name: 'databaseId',
choices: dbChoiseList,
message: 'Select the default database:',
validate: input =>
/.{32}/.test(input) ? true : 'This input was not a real Id',
},
];
const dbSelectResponse = await prompts(dataBaseSelectQuestions);
const index = dbList.findIndex(
index => index.id == dbSelectResponse.databaseId
);
if ('Done' in dbList[index].properties) {
globalConfig.dbId = dbSelectResponse.databaseId;
} else {
console.log(chalk.red('Selected database has the wrong properties '));
exit(1);
}
} else {
const pageResponse = await prompts(pageIdQuestions);
globalConfig.dbId = await createDataBase(pageResponse.pageId);
}
saveConfig();
};
export { configureSettings };