UNPKG

create-typescript-project-scaffolding

Version:
114 lines (113 loc) 6.38 kB
import * as fs from 'fs'; import * as path from 'path'; import { fileURLToPath } from 'url'; export class CreateNewProjectFiles { constructor(answers, projectPath, projectName) { this.answers = answers; this.path = projectPath; this.projectName = projectName; this.staticPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..', 'static'); } run() { if (!fs.existsSync(this.path)) { fs.mkdirSync(this.path); } if (this.answers['project-type'] === 'http-api@express-utils') { fs.cpSync(path.join(this.staticPath, 'example_src', 'express-utils-template'), path.join(this.path), { recursive: true, }); return; } if (this.answers['project-dockerfile-enabled']) { this.addDocker(); } this.addEslintPrettier(); this.copyProjectPresetFiles(); this.addGenericFiles(); } addGenericFiles() { var _a, _b, _c, _d; if (((_a = this.answers['project-idea']) === null || _a === void 0 ? void 0 : _a.length) > 0 && !fs.existsSync(path.join(this.path, '.idea'))) { fs.mkdirSync(path.join(this.path, '.idea')); } if ((_b = this.answers['project-idea']) === null || _b === void 0 ? void 0 : _b.includes('prettier')) { fs.copyFileSync(path.join(this.staticPath, 'idea', 'prettier.xml'), path.join(this.path, '.idea', 'prettier.xml')); } if (((_c = this.answers['project-idea']) === null || _c === void 0 ? void 0 : _c.length) > 0 && !fs.existsSync(path.join(this.path, '.idea', 'jsLinters'))) { fs.mkdirSync(path.join(this.path, '.idea', 'jsLinters')); } if ((_d = this.answers['project-idea']) === null || _d === void 0 ? void 0 : _d.includes('eslint')) { fs.copyFileSync(path.join(this.staticPath, 'idea', 'jsLinters', 'eslint.xml'), path.join(this.path, '.idea', 'jsLinters', 'eslint.xml')); } const eslint = this.answers['project-additional-dependencies'].includes('eslint_prettier'); const testing = this.answers['project-testing-dependencies'].length > 0; let templateFile = 'only'; if (testing) templateFile = 'test'; if (eslint) templateFile = 'eslint'; if (testing && eslint) templateFile = 'eslint_test'; if (this.answers['project-cicd-pipeline'] !== 'none') { if (this.answers['project-cicd-pipeline'] === 'github') fs.mkdirSync(path.join(this.path, '.github', 'workflows'), { recursive: true }); fs.copyFileSync(path.join(this.staticPath, 'cicd', this.answers['project-type'] === 'npm-package' ? 'npm_package' : 'docker', `${this.answers['project-cicd-pipeline'] === 'github' ? 'github' : 'gitlab'}_build_${templateFile}.yml`), this.answers['project-cicd-pipeline'] === 'github' ? path.join(this.path, '.github', 'workflows', 'ci.yml') : path.join(this.path, '.gitlab-ci.yml')); } if (this.answers['project-additional-dependencies'].includes('winston') && this.answers['project-type'] !== 'http-api@express-utils') { fs.copyFileSync(path.join(this.staticPath, 'ts', 'logger.ts'), path.join(this.path, 'src', 'logger.ts')); } fs.writeFileSync(path.join(this.path, 'readme.md'), `# ${this.projectName}\n\n© 2023\n\nCreated with ♥ by [typescript-project-scaffolding](https://github.com/Trickfilm400/typescript-project-scaffolding)`); fs.copyFileSync(path.join(this.staticPath, 'gitignore'), path.join(this.path, '.gitignore')); switch (this.answers['project-type']) { case 'npm-package': fs.copyFileSync(path.join(this.staticPath, 'tsconfig_npm_module.json'), path.join(this.path, 'tsconfig.json')); break; default: fs.copyFileSync(path.join(this.staticPath, 'tsconfig.json'), path.join(this.path, 'tsconfig.json')); break; } } addDocker() { fs.copyFileSync(path.join(this.staticPath, 'dockerignore'), path.join(this.path, '.dockerignore')); fs.copyFileSync(path.join(this.staticPath, 'Dockerfile'), path.join(this.path, 'Dockerfile')); } addEslintPrettier() { if (this.answers['project-additional-dependencies'].includes('eslint_prettier')) { fs.copyFileSync(path.join(this.staticPath, 'prettierrc'), path.join(this.path, '.prettierrc')); fs.copyFileSync(path.join(this.staticPath, 'eslint.config.mjs'), path.join(this.path, 'eslint.config.mjs')); } } copyProjectPresetFiles() { switch (this.answers['project-type']) { case 'npm-package': fs.cpSync(path.join(this.staticPath, 'example_src', 'npm_package'), path.join(this.path, 'src'), { recursive: true, }); fs.copyFileSync(path.join(this.staticPath, 'npmignore'), path.join(this.path, '.npmignore')); break; case 'http-api@express-utils': fs.cpSync(path.join(this.staticPath, 'example_src', 'express-utils-template', 'src'), path.join(this.path, 'src'), { recursive: true }); fs.cpSync(path.join(this.staticPath, 'example_src', 'express-utils-template', 'test'), path.join(this.path, 'test'), { recursive: true }); fs.cpSync(path.join(this.staticPath, 'example_src', 'express-utils-template', 'Dockerfile'), path.join(this.path, 'Dockerfile'), { recursive: true }); break; case 'empty-project': fs.cpSync(path.join(this.staticPath, 'example_src', 'empty_project'), path.join(this.path, 'src'), { recursive: true, }); break; case 'socket-io-server': fs.cpSync(path.join(this.staticPath, 'example_src', 'socket_io_server'), path.join(this.path, 'src'), { recursive: true, }); break; case 'websocket-server': fs.cpSync(path.join(this.staticPath, 'example_src', 'websocket_server'), path.join(this.path, 'src'), { recursive: true, }); break; } } }