Last active
May 2, 2022 03:33
-
-
Save ken7253/f8fdca601c643884acd0302f6de58633 to your computer and use it in GitHub Desktop.
Selecting slides with interactive UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inquirer from 'inquirer'; | |
import fs from 'node:fs'; | |
import path from 'node:path'; | |
import { spawn } from 'node:child_process'; | |
/** 設定情報 */ | |
const config = { | |
/** スライドを格納しているディレクトリ */ | |
slideRoot: 'slides', | |
}; | |
/** | |
* 格納されたすべてのスライドのファイル名を取得する関数 | |
* @returns 取得できたファイル名の配列 | |
*/ | |
const fetchAllSlide = () => { | |
const slideDir = path.join(process.cwd(), config.slideRoot); | |
const ls = fs.readdirSync(slideDir); | |
const markdownFiles = ls.filter((file) => { | |
return path.extname(file) === '.md'; | |
}); | |
return markdownFiles; | |
}; | |
/** 選択されたスライドのパス */ | |
const getSlide = (fileName: string) => { | |
const slideDir = path.join(config.slideRoot, fileName); | |
return slideDir; | |
}; | |
/** メインメソッド */ | |
const exec = () => { | |
console.log(`Searching for slides.\n at ${path.join(process.cwd(), config.slideRoot)}`); | |
if (fetchAllSlide().length === 0) { | |
// スライドが存在しない場合 | |
console.log('\u001b[31m' + '[ERROR] No slides were available.' + '\u001b[0m'); | |
return; | |
} | |
inquirer | |
.prompt({ | |
name: 'select', | |
type: 'list', | |
choices: fetchAllSlide(), | |
}) | |
.then((value) => { | |
const slidev = spawn('npm', ['x' ,'-p', 'slidev', getSlide(value.select)]); | |
slidev.stdout.on('data', (data) => { | |
console.log(`[LOG] ${data}`); | |
}); | |
slidev.stderr.on('data', (data) => { | |
console.error(`[ERROR] ${data}`); | |
}); | |
slidev.on('close', (code) => { | |
console.log(`[LOG] Child process exited with code ${code}`); | |
}); | |
}); | |
}; | |
exec(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment