(contest: Contest, template: Template, contest_path: string, log: boolean = false)
| 86 | * @param log default=false trueなら通常ログを標準出力に表示させる falseの場合はエラーログのみをエラー出力に表示 |
| 87 | */ |
| 88 | export async function installContestTemplate(contest: Contest, template: Template, contest_path: string, log: boolean = false) { |
| 89 | const contest_template = template.contest !== undefined ? template.contest : {}; |
| 90 | // 現在のディレクトリを記憶しつつ展開先ディレクトリに移動する |
| 91 | const pwd = process.cwd(); |
| 92 | process.chdir(contest_path); |
| 93 | const template_dir = resolve(await getConfigDirectory(), template.name); |
| 94 | const fs = await importFsExtra(); |
| 95 | |
| 96 | // 静的ファイルのコピー |
| 97 | // 同名ファイルが存在した場合は上書きされる |
| 98 | if (contest_template.static !== undefined) { |
| 99 | for (const file of contest_template.static) { |
| 100 | const source = resolve(template_dir, typeof file === "string" ? file : file[0]); |
| 101 | const dest = resolve(process.cwd(), typeof file === "string" ? file : formatContestDirname(file[1], contest)); |
| 102 | try { |
| 103 | await fs.copy(source, dest); |
| 104 | } catch (e) { |
| 105 | console.error(e.toString()); |
| 106 | } |
| 107 | if (log) console.log(`"${source}" -> "${dest}"`) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // コマンドの実行 |
| 112 | if (contest_template.cmd !== undefined) { |
| 113 | if (log) console.log(`Command:\n exec \`${contest_template.cmd}\``); |
| 114 | // 環境変数としてパラメータを利用可能にする |
| 115 | const env = { |
| 116 | ...process.env, |
| 117 | TEMPLATE_DIR: template_dir, |
| 118 | CONTEST_DIR: contest_path, |
| 119 | CONTEST_ID: contest.id |
| 120 | }; |
| 121 | const {stdout, stderr} = await promisify(child_process.exec)(contest_template.cmd, {env}); |
| 122 | if (log && stdout !== "") console.log(stdout); |
| 123 | if (stderr !== "") console.error(stderr); |
| 124 | } |
| 125 | |
| 126 | // もとのディレクトリに戻る |
| 127 | process.chdir(pwd); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * 問題テンプレートを展開する |
no test coverage detected