|
| 1 | +import path from 'path'; |
| 2 | +import { existsSync } from 'fs'; |
| 3 | +import { mkdir, writeFile, rm } from 'fs/promises'; |
| 4 | +import { htmlTemplate } from './html'; |
| 5 | +import { onePageTemplate } from './one-page'; |
| 6 | +import { webpackTemplate } from './webpack'; |
| 7 | +import { babelTemplate } from './babel'; |
| 8 | +import { packageTemplate } from './package'; |
| 9 | +import { indexTemplate } from './index'; |
| 10 | +import { tsconfigTemplate } from './tsconfig'; |
| 11 | + |
| 12 | +export type FileOptions = { |
| 13 | + snakeCaseName: string; |
| 14 | + name: string; |
| 15 | + lang: string; |
| 16 | + port: number; |
| 17 | + enableTypeScriptSupport: boolean; |
| 18 | + spectacleVersion: string; |
| 19 | +}; |
| 20 | + |
| 21 | +export const writeWebpackProjectFiles = async ({ |
| 22 | + snakeCaseName, |
| 23 | + name, |
| 24 | + lang, |
| 25 | + port, |
| 26 | + enableTypeScriptSupport, |
| 27 | + spectacleVersion |
| 28 | +}: FileOptions) => { |
| 29 | + const outPath = path.resolve(process.cwd(), snakeCaseName); |
| 30 | + |
| 31 | + await rm(outPath, { recursive: true, force: true }); |
| 32 | + |
| 33 | + if (existsSync(outPath)) { |
| 34 | + throw new Error(`Directory named ${snakeCaseName} already exists.`); |
| 35 | + } |
| 36 | + await mkdir(outPath, { recursive: true }); |
| 37 | + await writeFile(`${snakeCaseName}/index.html`, htmlTemplate({ name, lang })); |
| 38 | + await writeFile( |
| 39 | + `${snakeCaseName}/webpack.config.js`, |
| 40 | + webpackTemplate({ port, usesTypeScript: enableTypeScriptSupport }) |
| 41 | + ); |
| 42 | + await writeFile( |
| 43 | + `${snakeCaseName}/.babelrc`, |
| 44 | + babelTemplate({ enableTypeScriptSupport }) |
| 45 | + ); |
| 46 | + await writeFile( |
| 47 | + `${snakeCaseName}/package.json`, |
| 48 | + packageTemplate({ |
| 49 | + usesTypeScript: enableTypeScriptSupport, |
| 50 | + name: snakeCaseName, |
| 51 | + spectacleVersion |
| 52 | + }) |
| 53 | + ); |
| 54 | + await writeFile( |
| 55 | + `${snakeCaseName}/index.${enableTypeScriptSupport ? 'tsx' : 'jsx'}`, |
| 56 | + indexTemplate({ |
| 57 | + usesTypeScript: enableTypeScriptSupport, |
| 58 | + name |
| 59 | + }) |
| 60 | + ); |
| 61 | + |
| 62 | + enableTypeScriptSupport && |
| 63 | + (await writeFile(`${snakeCaseName}/tsconfig.json`, tsconfigTemplate())); |
| 64 | +}; |
| 65 | + |
| 66 | +export const writeOnePageHTMLFile = async ({ |
| 67 | + snakeCaseName, |
| 68 | + name, |
| 69 | + lang |
| 70 | +}: FileOptions) => { |
| 71 | + await writeFile(`${snakeCaseName}.html`, onePageTemplate({ name, lang })); |
| 72 | +}; |
0 commit comments