From 1f222c7f4f790196b8e82a243950a1c13d16efe3 Mon Sep 17 00:00:00 2001 From: Amio Date: Sun, 26 May 2019 12:28:59 +0800 Subject: [PATCH] Refactor gen-examples script --- libs/examples.ts | 26 ++++++++++++++++++++++++++ tools/gen-examples.ts | 35 +++-------------------------------- 2 files changed, 29 insertions(+), 32 deletions(-) create mode 100644 libs/examples.ts diff --git a/libs/examples.ts b/libs/examples.ts new file mode 100644 index 0000000..b61b3b9 --- /dev/null +++ b/libs/examples.ts @@ -0,0 +1,26 @@ +import path from 'path' +import { examples as staticBadgeExamples } from '../endpoints/badge' + +const rel = (...args) => path.resolve(__dirname, ...args) + +// sort live badge examples manually +const liveBadgeExampleList = [ + 'github', + 'amo', + 'homebrew' +] + +export async function loadExamples () { + const liveBadgeExamples = await Promise.all(liveBadgeExampleList.map(async name => { + const { title, examples } = await import(rel('../endpoints', name)) + return { + title: title || name, + examples + } + })) + + return { + live: liveBadgeExamples, + static: staticBadgeExamples + } +} diff --git a/tools/gen-examples.ts b/tools/gen-examples.ts index d4f5caf..129ae60 100644 --- a/tools/gen-examples.ts +++ b/tools/gen-examples.ts @@ -1,43 +1,14 @@ import path from 'path' import fse from 'fs-extra' +import { loadExamples } from '../libs/examples' const rel = (...args) => path.resolve(__dirname, ...args) async function main () { - const staticExamples = (await import(rel('../endpoints/badge'))).examples - - // sort badges manually - const liveBadgeList = { - github: 'github', - // npm: 'npm', - // david: 'david-dm', - // packagephobia: 'packagephobia', - // bundlephobia: 'bundlephobia', - // xo: 'xo', - // crates: 'crates', - // docker: 'docker', - homebrew: 'homebrew' - } - - // @ts-ignore - const liveExamples = await Promise.all(Object.entries(liveBadgeList).map(async ([name, title]) => { - const { examples, handlers } = await import(rel('../endpoints', name)) - return { - title, - examples, - schemes: Object.keys(handlers) - } - })) - - - const examples = { - live: liveExamples, - static: staticExamples - } + const examples = await loadExamples() + console.log(examples) await fse.outputJson(rel('../static/.gen/examples.json'), examples) - - console.log(examples.live) } main()