From d069e01c7cbe5edee9828fd05b5761503a9dcaf5 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Mon, 15 Jan 2024 21:59:22 +0100 Subject: [PATCH] feat: improve logging --- src/append_to_group.ts | 23 ++++++++++++++--------- src/create_users.ts | 25 ++++++++++++++++++++----- utils.ts | 35 ++++++++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src/append_to_group.ts b/src/append_to_group.ts index 1848a0d..158fd1b 100644 --- a/src/append_to_group.ts +++ b/src/append_to_group.ts @@ -1,8 +1,12 @@ -import { Redmine } from 'bluemine'; -import { CsvEntry } from '../type.ts'; -import { toLogin } from '../utils.ts'; +import { Redmine } from 'bluemine' +import { CsvEntry } from '../type.ts' +import { log, toLogin } from '../utils.ts' -async function addUsersToGroup(redmine: Redmine, csv: CsvEntry[]) { +async function addUsersToGroup( + redmine: Redmine, + csv: CsvEntry[], + groupName: string, +) { const users: Redmine['users']['List']['Result']['users'] = [] for (const { firstname, lastname } of csv) { @@ -12,10 +16,11 @@ async function addUsersToGroup(redmine: Redmine, csv: CsvEntry[]) { const list = await redmine.users.list({ limit: 1, name: login }) const user = list.users[0] - console.log(`get user: ${user.login}#${user.id}`) + log.info(`get id of ${user.login}#${user.id}`) users.push(user) - } catch { - console.error(`error list: ${login}`) + } catch (error) { + log.error(`get id of ${login}`) + console.error(String(error)) } } @@ -25,10 +30,10 @@ async function addUsersToGroup(redmine: Redmine, csv: CsvEntry[]) { for (const user of users) { try { - console.error(`ok: ${user.login}#${user.id}`) + log.info(`add ${user.login}#${user.id} to "${groupName}"`) await redmine.unstableGroups.addUser(id, user) } catch { - console.error(`error: ${user.login}`) + log.error(`add ${user.login}#${user.id} to "${groupName}"`) } } } diff --git a/src/create_users.ts b/src/create_users.ts index c6b7cf6..3e0a662 100644 --- a/src/create_users.ts +++ b/src/create_users.ts @@ -1,8 +1,12 @@ import { Redmine } from 'bluemine' import { CsvEntry } from '../type.ts' -import { capitalize, sanitize, toLogin } from '../utils.ts' +import { capitalize, log, sanitize, toLogin } from '../utils.ts' -async function createUsers(redmine: Redmine, csv: CsvEntry[]) { +async function createUsers( + redmine: Redmine, + csv: CsvEntry[], + groupName: string, +) { const userIds: number[] = [] for (const { firstname, lastname, mail } of csv) { @@ -18,11 +22,22 @@ async function createUsers(redmine: Redmine, csv: CsvEntry[]) { generatePassword: true, }) + log.info(`create user ${login}#${user.id}`) userIds.push(user.id) - } catch { - console.error(login) + } catch (error) { + log.error(`create user ${login}`) + console.error(error) } } - await redmine.unstableGroups.create({ name: 'entrepreneur-01-24', userIds }) + try { + log.info(`add to "${groupName}" users [${userIds.join(', ')}]`) + await redmine.unstableGroups.create({ + name: 'entrepreneur-01-24', + userIds, + }) + } catch (error) { + log.error(`add to "${groupName}" users [${userIds.join(', ')}]`) + console.error(error) + } } diff --git a/utils.ts b/utils.ts index 50c8293..a79839d 100644 --- a/utils.ts +++ b/utils.ts @@ -1,15 +1,36 @@ export function sanitize(str: string): string { - return str.replaceAll(' ', '_') + return str.replaceAll(' ', '_') } export function capitalize(str: string): string { - return str - .split(' ') - .map(word => word.split('')) - .map(([firstLetter, ...tail]) => `${firstLetter.toLocaleUpperCase()}${tail.join().toLocaleLowerCase()}`) - .join(' ') + return str + .split(' ') + .map((word) => word.split('')) + .map(([firstLetter, ...tail]) => + `${firstLetter.toLocaleUpperCase()}${tail.join().toLocaleLowerCase()}` + ) + .join(' ') } export function toLogin(firstname: string, lastname: string): string { - return `${sanitize(firstname.toLocaleLowerCase())}.${sanitize(lastname.toLocaleLowerCase())}` + return `${sanitize(firstname.toLocaleLowerCase())}.${ + sanitize(lastname.toLocaleLowerCase()) + }` +} + +export const log = { + info(message: string) { + console.log( + `%c[info]%c ${message}`, + 'color: cyan; font-weight: bold', + 'color: white', + ) + }, + error(message: string) { + console.error( + `%c[error]%c ${message}`, + 'color: red; font-weight: bold', + 'color: white', + ) + }, }