feat(logger): add bunyan without DI
This commit is contained in:
parent
b2fac3494b
commit
25b375d6ac
|
@ -0,0 +1,11 @@
|
|||
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
|
||||
|
||||
[Home](./index.md) > [cautious-journey](./cautious-journey.md) > [RemoteOptions](./cautious-journey.remoteoptions.md) > [logger](./cautious-journey.remoteoptions.logger.md)
|
||||
|
||||
## RemoteOptions.logger property
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```typescript
|
||||
logger: Logger;
|
||||
```
|
|
@ -16,5 +16,6 @@ export interface RemoteOptions
|
|||
| --- | --- | --- |
|
||||
| [data](./cautious-journey.remoteoptions.data.md) | Record<string, string> | Arbitrary key-value data for this remote, usually credentials and base URLs. |
|
||||
| [dryrun](./cautious-journey.remoteoptions.dryrun.md) | boolean | If set, do not make any real changes. |
|
||||
| [logger](./cautious-journey.remoteoptions.logger.md) | Logger | |
|
||||
| [type](./cautious-journey.remoteoptions.type.md) | string | Remote class/type name. |
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
|
||||
|
||||
[Home](./index.md) > [cautious-journey](./cautious-journey.md) > [SyncOptions](./cautious-journey.syncoptions.md) > [logger](./cautious-journey.syncoptions.logger.md)
|
||||
|
||||
## SyncOptions.logger property
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```typescript
|
||||
logger: Logger;
|
||||
```
|
|
@ -15,6 +15,7 @@ export interface SyncOptions
|
|||
| Property | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| [flags](./cautious-journey.syncoptions.flags.md) | Array<[FlagLabel](./cautious-journey.flaglabel.md)<!-- -->> | |
|
||||
| [logger](./cautious-journey.syncoptions.logger.md) | Logger | |
|
||||
| [project](./cautious-journey.syncoptions.project.md) | string | |
|
||||
| [remote](./cautious-journey.syncoptions.remote.md) | [Remote](./cautious-journey.remote.md) | |
|
||||
| [states](./cautious-journey.syncoptions.states.md) | Array<[StateLabel](./cautious-journey.statelabel.md)<!-- -->> | |
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { LogLevel } from 'noicejs';
|
||||
|
||||
import { FlagLabel, StateLabel } from '../labels';
|
||||
import { RemoteOptions } from '../remote';
|
||||
|
||||
|
@ -5,6 +7,10 @@ import { RemoteOptions } from '../remote';
|
|||
* Config data for the app, loaded from CLI or DOM.
|
||||
*/
|
||||
export interface ConfigData {
|
||||
logger: {
|
||||
level: LogLevel;
|
||||
name: string;
|
||||
};
|
||||
projects: Array<{
|
||||
/**
|
||||
* Color palette for labels without their own.
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
import { constructorName } from '@apextoaster/js-utils';
|
||||
import bunyan from 'bunyan';
|
||||
import { Logger } from 'noicejs';
|
||||
|
||||
/**
|
||||
* Attach bunyan to the Logger. Does very little, since bunyan matches the Logger interface.
|
||||
*/
|
||||
export class BunyanLogger {
|
||||
public static create(options: bunyan.LoggerOptions): Logger {
|
||||
return bunyan.createLogger({
|
||||
...options,
|
||||
serializers: {
|
||||
...bunyan.stdSerializers,
|
||||
container: constructorName,
|
||||
logger: constructorName,
|
||||
module: constructorName,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
10
src/main.ts
10
src/main.ts
|
@ -6,6 +6,7 @@ import { join } from 'path';
|
|||
|
||||
import { ConfigData } from './config';
|
||||
import { Commands, createParser } from './config/args';
|
||||
import { BunyanLogger } from './logger/BunyanLogger';
|
||||
import { GithubRemote } from './remote/github';
|
||||
import { syncIssues, syncLabels, SyncOptions } from './sync';
|
||||
import { VERSION_INFO } from './version';
|
||||
|
@ -46,26 +47,27 @@ export async function main(argv: Array<string>): Promise<number> {
|
|||
const parser = createParser((argMode) => mode = argMode as Commands);
|
||||
const args = parser.parse(argv.slice(SLICE_ARGS));
|
||||
const config = await loadConfig(args.config);
|
||||
// TODO: create logger
|
||||
const logger = BunyanLogger.create(config.logger);
|
||||
|
||||
/* eslint-disable-next-line no-console */
|
||||
console.log({
|
||||
logger.info({
|
||||
args,
|
||||
config,
|
||||
mode,
|
||||
version: VERSION_INFO,
|
||||
});
|
||||
}, 'startup environment');
|
||||
|
||||
for (const project of config.projects) {
|
||||
const remote = new GithubRemote({
|
||||
...project.remote,
|
||||
dryrun: args.dryrun,
|
||||
logger,
|
||||
});
|
||||
await remote.connect();
|
||||
|
||||
// mode switch
|
||||
const options: SyncOptions = {
|
||||
flags: project.flags,
|
||||
logger,
|
||||
project: project.name,
|
||||
remote,
|
||||
states: project.states,
|
||||
|
|
|
@ -18,11 +18,11 @@ export class GithubRemote implements Remote {
|
|||
constructor(options: RemoteOptions) {
|
||||
this.options = options;
|
||||
|
||||
/* eslint-disable-next-line */
|
||||
console.log('options:', options);
|
||||
options.logger.debug({ options }, 'github remote');
|
||||
}
|
||||
|
||||
public async connect() {
|
||||
this.options.logger.info('connecting to github');
|
||||
this.request = new Octokit({
|
||||
auth: {
|
||||
id: parseInt(mustExist(this.options.data.id), 10),
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { Logger } from 'noicejs';
|
||||
|
||||
export interface ProjectQuery {
|
||||
project: string;
|
||||
}
|
||||
|
@ -35,6 +37,8 @@ export interface RemoteOptions {
|
|||
*/
|
||||
dryrun: boolean;
|
||||
|
||||
logger: Logger;
|
||||
|
||||
/**
|
||||
* Remote class/type name.
|
||||
*/
|
||||
|
|
24
src/sync.ts
24
src/sync.ts
|
@ -1,15 +1,14 @@
|
|||
import { doesExist, mustExist } from '@apextoaster/js-utils';
|
||||
import { Logger } from 'noicejs';
|
||||
|
||||
import { FlagLabel, getLabelNames, StateLabel, valueName } from './labels';
|
||||
import { LabelUpdate, Remote } from './remote';
|
||||
import { resolveLabels } from './resolve';
|
||||
import { defaultTo } from './utils';
|
||||
|
||||
// TODO: turn this back on/remove the disable pragma
|
||||
/* eslint-disable no-console */
|
||||
|
||||
export interface SyncOptions {
|
||||
flags: Array<FlagLabel>;
|
||||
logger: Logger;
|
||||
project: string;
|
||||
remote: Remote;
|
||||
states: Array<StateLabel>;
|
||||
|
@ -21,7 +20,7 @@ export async function syncIssues(options: SyncOptions): Promise<unknown> {
|
|||
});
|
||||
|
||||
for (const issue of issues) {
|
||||
console.log('issue:', issue);
|
||||
options.logger.info({ issue }, 'issue');
|
||||
|
||||
const resolution = resolveLabels({
|
||||
flags: options.flags,
|
||||
|
@ -31,7 +30,7 @@ export async function syncIssues(options: SyncOptions): Promise<unknown> {
|
|||
|
||||
// TODO: prompt user to update this particular issue
|
||||
if (resolution.changes.length > 0) {
|
||||
console.log('updating issue:', issue, resolution);
|
||||
options.logger.info({ issue, resolution }, 'updating issue');
|
||||
await options.remote.updateIssue({
|
||||
...issue,
|
||||
labels: resolution.labels,
|
||||
|
@ -54,14 +53,19 @@ export async function syncLabels(options: SyncOptions): Promise<unknown> {
|
|||
for (const label of combined) {
|
||||
const exists = present.has(label);
|
||||
const expected = desired.has(label);
|
||||
console.log('label:', label, exists, expected);
|
||||
|
||||
options.logger.info({
|
||||
exists,
|
||||
expected,
|
||||
label,
|
||||
}, 'label');
|
||||
|
||||
if (exists) {
|
||||
if (expected) {
|
||||
const data = mustExist(labels.find((l) => l.name === label));
|
||||
await syncSingleLabel(options, data);
|
||||
} else {
|
||||
console.log('remove label:', label);
|
||||
options.logger.warn({ label }, 'remove label');
|
||||
await options.remote.deleteLabel({
|
||||
name: label,
|
||||
project: options.project,
|
||||
|
@ -69,7 +73,7 @@ export async function syncLabels(options: SyncOptions): Promise<unknown> {
|
|||
}
|
||||
} else {
|
||||
if (expected) {
|
||||
console.log('create label:', label);
|
||||
options.logger.info({ label }, 'create label');
|
||||
await createLabel(options, label);
|
||||
} else {
|
||||
// skip
|
||||
|
@ -122,11 +126,11 @@ export async function syncLabelDiff(options: SyncOptions, current: LabelUpdate,
|
|||
project: options.project,
|
||||
};
|
||||
|
||||
console.log('update label:', current, expected, body);
|
||||
options.logger.debug({ body, current, expected }, 'update label');
|
||||
|
||||
const resp = await options.remote.updateLabel(body);
|
||||
|
||||
console.log('update resp:', resp);
|
||||
options.logger.debug({ resp }, 'update resp');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue