diff --git a/config/rollup.js b/config/rollup.js index 7a37295..3924a8a 100644 --- a/config/rollup.js +++ b/config/rollup.js @@ -10,7 +10,6 @@ import replace from 'rollup-plugin-replace'; import serve from 'rollup-plugin-serve'; import { terser } from 'rollup-plugin-terser'; import typescript from 'rollup-plugin-typescript2'; -import virtual from 'rollup-plugin-virtual'; import visualizer from 'rollup-plugin-visualizer'; import yaml from 'rollup-plugin-yaml'; @@ -111,12 +110,6 @@ const bundle = { PACKAGE_VERSION: metadata.version, }, }), - /* - virtual({ - 'universal-user-agent': - 'export function getUserAgent() {return `Node.js/${process.version.substr(1)} (${process.platform}); ${process.arch})`}', - }), - */ alias({ resolve: ['.tsx', '.ts'], entries: { diff --git a/docs/.gitattributes b/docs/.gitattributes new file mode 100644 index 0000000..7cea7b0 --- /dev/null +++ b/docs/.gitattributes @@ -0,0 +1 @@ +api/* linguist-generated=true diff --git a/src/main.ts b/src/main.ts index c30f5c9..85d423d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,8 @@ -import { InvalidArgumentError } from '@apextoaster/js-utils'; +import { InvalidArgumentError, isNil } from '@apextoaster/js-utils'; +import { createSchema } from '@apextoaster/js-yaml-schema'; +import { existsSync, readFileSync, realpathSync } from 'fs'; +import { DEFAULT_SAFE_SCHEMA, safeLoad } from 'js-yaml'; +import { join } from 'path'; import { ConfigData } from './config'; import { Commands, createParser } from './config/args'; @@ -15,25 +19,34 @@ export { syncIssues, syncLabels } from './sync'; const SLICE_ARGS = 2; +async function loadConfig(path: string): Promise { + const schema = createSchema({ + include: { + exists: existsSync, + join, + read: readFileSync, + resolve: realpathSync, + schema: DEFAULT_SAFE_SCHEMA, + } + }); + const rawConfig = readFileSync(path, { + encoding: 'utf-8', + }); + const config = safeLoad(rawConfig, { schema }); + + if (isNil(config) || typeof config === 'string') { + throw new Error(); + } + + return config as ConfigData; +} + export async function main(argv: Array): Promise { // get arguments let mode = Commands.UNKNOWN as Commands; const parser = createParser((argMode) => mode = argMode as Commands); const args = parser.parse(argv.slice(SLICE_ARGS)); - - // load config - const config: ConfigData = { - projects: [{ - colors: [], - flags: [], - name: '', - remote: { - data: {}, - type: '', - }, - states: [], - }], - }; + const config = await loadConfig('/home/ssube/.cautious-journey.yml'); /* eslint-disable-next-line no-console */ console.log({ @@ -45,26 +58,27 @@ export async function main(argv: Array): Promise { // create logger // create remote - const remote = new GithubRemote({ - data: {}, - type: '', - }); - // mode switch - const options: SyncOptions = { - config, - project: '', - remote, - }; - switch (mode) { - case Commands.ISSUES: - await syncIssues(options); - break; - case Commands.LABELS: - await syncLabels(options); - break; - default: - throw new InvalidArgumentError('unknown mode'); + for (const project of config.projects) { + const remote = new GithubRemote(project.remote); + await remote.connect(); + + // mode switch + const options: SyncOptions = { + config, + project: project.name, + remote, + }; + switch (mode) { + case Commands.ISSUES: + await syncIssues(options); + break; + case Commands.LABELS: + await syncLabels(options); + break; + default: + throw new InvalidArgumentError('unknown mode'); + } } return 0; diff --git a/src/remote/github.ts b/src/remote/github.ts index f2f355e..68eaf80 100644 --- a/src/remote/github.ts +++ b/src/remote/github.ts @@ -11,35 +11,11 @@ export class GithubRemote implements Remote { protected options: RemoteOptions; protected request?: Octokit; - /* eslint-disable-next-line no-useless-constructor */ constructor(options: RemoteOptions) { this.options = options; } public async connect() { - /* eslint-disable-next-line no-console */ - console.log('connecting to github', { - auth: this.options.data, - authStrategy: createAppAuth, - }); - - /* - const auth = createAppAuth({ - id: parseInt(mustExist(this.options.data.id), 10), - privateKey: mustExist(this.options.data.privateKey), - }); - - const install = await auth({ - installationId: parseInt(mustExist(this.options.data.installationId), 10), - type: 'installation', - }); - - this.request = new Octokit({ - auth: install, - authStrategy: createAppAuth, - }); - */ - this.request = new Octokit({ auth: { id: parseInt(mustExist(this.options.data.id), 10), @@ -95,9 +71,6 @@ export class GithubRemote implements Remote { }); } - /* eslint-disable-next-line no-console */ - console.log('list issues:', path, issues); - return issues; }