11import path from 'path' ;
2- import { existsSync } from 'fs' ;
32import { mkdir , writeFile , rm } from 'fs/promises' ;
43import { htmlTemplate } from './html' ;
54import { onePageTemplate } from './one-page' ;
@@ -8,6 +7,8 @@ import { babelTemplate } from './babel';
87import { packageTemplate } from './package' ;
98import { indexTemplate } from './index' ;
109import { tsconfigTemplate } from './tsconfig' ;
10+ import { gitignoreTemplate } from './gitignore' ;
11+ import { readmeTemplate } from './readme' ;
1112
1213export type FileOptions = {
1314 snakeCaseName : string ;
@@ -27,46 +28,52 @@ export const writeWebpackProjectFiles = async ({
2728 spectacleVersion
2829} : FileOptions ) => {
2930 const outPath = path . resolve ( process . cwd ( ) , snakeCaseName ) ;
31+ const pathFor = ( file : string ) => path . join ( outPath , file ) ;
3032
3133 await rm ( outPath , { recursive : true , force : true } ) ;
3234
33- if ( existsSync ( outPath ) ) {
34- throw new Error ( `Directory named ${ snakeCaseName } already exists.` ) ;
35- }
3635 await mkdir ( outPath , { recursive : true } ) ;
37- await writeFile ( ` ${ snakeCaseName } / index.html` , htmlTemplate ( { name, lang } ) ) ;
36+ await writeFile ( pathFor ( ' index.html' ) , htmlTemplate ( { name, lang } ) ) ;
3837 await writeFile (
39- ` ${ snakeCaseName } / webpack.config.js` ,
38+ pathFor ( ' webpack.config.js' ) ,
4039 webpackTemplate ( { port, usesTypeScript : enableTypeScriptSupport } )
4140 ) ;
4241 await writeFile (
43- ` ${ snakeCaseName } / .babelrc` ,
42+ pathFor ( ' .babelrc' ) ,
4443 babelTemplate ( { enableTypeScriptSupport } )
4544 ) ;
4645 await writeFile (
47- ` ${ snakeCaseName } / package.json` ,
46+ pathFor ( ' package.json' ) ,
4847 packageTemplate ( {
4948 usesTypeScript : enableTypeScriptSupport ,
5049 name : snakeCaseName ,
5150 spectacleVersion
5251 } )
5352 ) ;
5453 await writeFile (
55- ` ${ snakeCaseName } / index.${ enableTypeScriptSupport ? 'tsx' : 'jsx' } `,
54+ pathFor ( ` index.${ enableTypeScriptSupport ? 'tsx' : 'jsx' } `) ,
5655 indexTemplate ( {
5756 usesTypeScript : enableTypeScriptSupport ,
5857 name
5958 } )
6059 ) ;
60+ await writeFile ( pathFor ( '.gitignore' ) , gitignoreTemplate ( ) ) ;
61+ await writeFile (
62+ pathFor ( 'README.md' ) ,
63+ readmeTemplate ( { name, enableTypeScriptSupport } )
64+ ) ;
6165
6266 enableTypeScriptSupport &&
63- ( await writeFile ( ` ${ snakeCaseName } / tsconfig.json` , tsconfigTemplate ( ) ) ) ;
67+ ( await writeFile ( pathFor ( ' tsconfig.json' ) , tsconfigTemplate ( ) ) ) ;
6468} ;
6569
6670export const writeOnePageHTMLFile = async ( {
6771 snakeCaseName,
6872 name,
6973 lang
7074} : FileOptions ) => {
71- await writeFile ( `${ snakeCaseName } .html` , onePageTemplate ( { name, lang } ) ) ;
75+ await writeFile (
76+ path . resolve ( process . cwd ( ) , `${ snakeCaseName } .html` ) ,
77+ onePageTemplate ( { name, lang } )
78+ ) ;
7279} ;
0 commit comments