typescript-project-generator
Version:
A TypeScript initial configuration for web projects
78 lines (73 loc) • 2.03 kB
JavaScript
const writeFile = require('../utils/writeFile')
/**
* @param {PackageJsonConfig} args
*/
module.exports = async ({
author,
projectName,
projectDescription,
version,
license,
mainFile,
testFramework
}) => {
const data = {
content: '',
file: 'package.json'
}
// Generate test scripts based on the chosen framework
const testScripts =
testFramework === 'vitest'
? {
'test:ci': 'vitest run',
'test:local': 'vitest run --reporter=verbose',
'test:watch': 'vitest'
}
: {
'test:ci': 'jest --ci -i',
'test:local': 'jest --setupFiles dotenv/config --ci -i --forceExit',
'test:watch': 'jest --setupFiles dotenv/config --watch'
}
data.content = `{
"name": "${projectName.toLowerCase().replace(/ /g, '-')}",
"version": "${version}",
"main": "${mainFile}",
"description": "${projectDescription}",
"scripts": {
"lint": "biome check --write src/",
"dev": "nodemon",
"start": "ts-node src/index.ts",
"release": "standard-version",
${Object.entries(testScripts)
.map(([key, value]) => `"${key}": "${value}"`)
.join(',\n ')}
},
"author": "${author}",
"license": "${license.toUpperCase()}",
"dependencies": {},
"devDependencies": {},
"nodemonConfig": {
"watch": [
".env",
"src"
],
"ext": "ts",
"ignore": [
"src/**/*.test.ts"
],
"exec": "npx ts-node -r dotenv/config ./src/index"
}
}
`
await writeFile(`${projectName}/${data.file}`, data.content)
}
/**
* @typedef {Object} PackageJsonConfig configuration to create the package.json
* @property {String} author author of the project
* @property {String} projectName project name
* @property {String} projectDescription project description
* @property {String} version project initial version
* @property {String} license project license
* @property {String} mainFile project main file
* @property {String} testFramework test framework to use (jest or vitest)
*/