UNPKG

create-typescript-project-scaffolding

Version:
195 lines (194 loc) 9.6 kB
import * as fs from 'fs'; import * as path from 'path'; import PackageVersions from './packageVersions.js'; export default class BuildPackageJson { constructor(name, answers) { this.json = {}; this.packageVersionClass = new PackageVersions(); this.name = name; this.answers = answers; this.addDefaultParameter(); this.addDependencies(); this.addProjectDependencies(); this.additional(); } getPackageJson() { return this.json; } depVer(name) { return PackageVersions.STATIC_DEPENDENCY_VERSIONS[name] || 'latest'; } getScripts() { var _a; const testObj = {}; if (((_a = this.answers['project-testing-dependencies']) === null || _a === void 0 ? void 0 : _a.length) > 0) { if (this.answers['project-testing-dependencies'].includes('mocha')) testObj['test:unit'] = 'mocha --exit -r ts-node/register test/*.ts'; else if (this.answers['project-testing-dependencies'].includes('jest')) testObj['test:unit'] = 'jest test/*.ts'; if (this.answers['project-testing-dependencies'].includes('nyc') && this.answers['project-testing-dependencies'].includes('mocha')) testObj['test:coverage'] = 'nyc --reporter cobertura --reporter=html --reporter=text mocha -r ts-node/register --require source-map-support/register --recursive test/*.ts --reporter mocha-junit-reporter --reporter=spec'; else if (this.answers['project-testing-dependencies'].includes('nyc') && this.answers['project-testing-dependencies'].includes('jest')) testObj['test:coverage'] = 'nyc --reporter cobertura --reporter=html --reporter=text jest --reporter=spec'; if (testObj['test:unit']) testObj['test'] = 'npm run test:unit'; if (testObj['test:coverage']) { if (testObj['test']) testObj['test'] += ' && npm run test:coverage'; else testObj['test'] += 'npm run test:coverage'; } else testObj['test'] = 'echo "Error: no test specified" && exit 1'; } if (this.answers['project-additional-dependencies'].includes('ts-node-dev')) testObj['dev'] = 'ts-node-dev --respawn --no-deps --rs --cls ./src/index.ts'; if (this.answers['project-additional-dependencies'].includes('eslint_prettier')) testObj['eslint'] = 'eslint src/**/*.ts'; return Object.assign({ start: 'node .', build: 'tsc' }, testObj); } addDefaultParameter() { this.json = { name: this.name, version: '0.0.0-dev-01', description: this.name, main: './dist/index.js', scripts: this.getScripts(), keywords: [ this.name, 'created-with-typescript-new-project-scaffolding', 'template', 'typescript', 'node', 'nodejs', ], author: { email: 'info@example.com', name: 'Change me', }, license: 'MIT', devDependencies: {}, dependencies: {}, }; } addDependencies() { var _a, _b, _c, _d, _e, _f; if (!this.json['devDependencies']) this.json['devDependencies'] = {}; if (!this.json['dependencies']) this.json['dependencies'] = {}; this.json['devDependencies']['typescript'] = this.depVer('typescript'); this.json['devDependencies']['@types/node'] = this.depVer('@types/node'); if (this.answers['project-additional-dependencies'].includes('convict')) { this.json['dependencies']['convict'] = this.depVer('convict'); this.json['devDependencies']['@types/convict'] = this.depVer('@types/convict'); } if (this.answers['project-additional-dependencies'].includes('eslint_prettier')) { this.json['devDependencies']['@trickfilm400/eslint-shared-config'] = this.depVer('@trickfilm400/eslint-shared-config'); } if (this.answers['project-additional-dependencies'].includes('ts-node-dev')) { this.json['devDependencies']['ts-node'] = this.depVer('ts-node'); this.json['devDependencies']['ts-node-dev'] = this.depVer('ts-node-dev'); } if (this.answers['project-additional-dependencies'].includes('winston')) { this.json['dependencies']['winston'] = this.depVer('winston'); } if (this.answers['project-additional-dependencies'].includes('joi')) { this.json['dependencies']['joi'] = this.depVer('joi'); } if (this.answers['project-additional-dependencies'].includes('mqtt')) { this.json['dependencies']['mqtt'] = this.depVer('mqtt'); } if (this.answers['project-additional-dependencies'].includes('amqp')) { this.json['dependencies']['amqp-connection-manager'] = this.depVer('amqp-connection-manager'); this.json['dependencies']['amqplib'] = this.depVer('amqplib'); this.json['devDependencies']['@types/amqplib'] = this.depVer('@types/amqplib'); } if ((_a = this.answers['project-testing-dependencies']) === null || _a === void 0 ? void 0 : _a.includes('nyc')) { this.json['devDependencies']['nyc'] = this.depVer('nyc'); } if ((_b = this.answers['project-testing-dependencies']) === null || _b === void 0 ? void 0 : _b.includes('jest')) { this.json['devDependencies']['jest'] = this.depVer('jest'); } if ((_c = this.answers['project-testing-dependencies']) === null || _c === void 0 ? void 0 : _c.includes('chai-http')) { this.json['devDependencies']['chai'] = this.depVer('chai'); this.json['devDependencies']['chai-http'] = this.depVer('chai-http'); } if ((_d = this.answers['project-testing-dependencies']) === null || _d === void 0 ? void 0 : _d.includes('cypress')) { this.json['devDependencies']['cypress'] = this.depVer('cypress'); } if ((_e = this.answers['project-testing-dependencies']) === null || _e === void 0 ? void 0 : _e.includes('mocha')) { this.json['devDependencies']['mocha'] = this.depVer('mocha'); this.json['devDependencies']['@types/mocha'] = this.depVer('@types/mocha'); if (this.answers['project-cicd-pipeline'] == 'gitlab') this.json['devDependencies']['mocha-junit-reporter'] = this.depVer('mocha-junit-reporter'); } if ((_f = this.answers['project-testing-dependencies']) === null || _f === void 0 ? void 0 : _f.includes('vitest')) { this.json['devDependencies']['vitest'] = this.depVer('vitest'); } if (this.answers['project-database-driver'].includes('mongoose')) { this.json['dependencies']['mongoose'] = this.depVer('mongoose'); } if (this.answers['project-database-driver'].includes('typeorm')) { this.json['dependencies']['typeorm'] = this.depVer('typeorm'); } if (this.answers['project-database-driver'].includes('mysql')) { this.json['dependencies']['mysql'] = this.depVer('mysql'); this.json['devDependencies']['@types/mysql'] = this.depVer('@types/mysql'); } if (this.answers['project-database-driver'].includes('mysql2')) { this.json['dependencies']['mysql2'] = this.depVer('mysql2'); } if (this.answers['project-database-driver'].includes('mongodb')) { this.json['dependencies']['mongodb'] = this.depVer('mongodb'); } if (this.answers['project-database-driver'].includes('redis')) { this.json['dependencies']['redis'] = this.depVer('redis'); } } addProjectDependencies() { if (!this.json['devDependencies']) this.json['devDependencies'] = {}; if (!this.json['dependencies']) this.json['dependencies'] = {}; if (this.answers['project-type'] == 'http-api@express-utils') { this.json['dependencies']['@kopf02/express-utils'] = this.depVer('@kopf02/express-utils'); this.json['dependencies']['@types/express'] = this.depVer('@types/express'); } if (this.answers['project-type'] == 'websocket-server') { this.json['dependencies']['ws'] = this.depVer('ws'); } if (this.answers['project-type'] == 'socket-io-server') { this.json['dependencies']['socket.io'] = this.depVer('socket.io'); } } additional() { if (this.answers['project-type'] === 'npm-package') { this.json.files = ['dist', 'typings']; this.json.typings = 'types/index.d.ts'; } } fetchLatestPackageVersions() { return new Promise((resolve, reject) => { this.packageVersionClass .runNCU(JSON.stringify(this.json)) .then((res) => { this.json = res; resolve(void 0); }) .catch((err) => { console.log(err); reject(err); }); }); } save(filePath) { if (!fs.existsSync(filePath)) { fs.mkdirSync(filePath); } fs.writeFileSync(path.join(filePath, 'package.json'), JSON.stringify(this.json, null, 4)); } }