Skip to content

Commit

Permalink
feat: generate start and test NPM scripts (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
szgabsz91 committed Nov 25, 2023
1 parent 6773f73 commit c6fd31d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
15 changes: 15 additions & 0 deletions index.ts
Expand Up @@ -18,6 +18,7 @@ import getCommand from './utils/getCommand'
import getLanguage from './utils/getLanguage'
import renderEslint from './utils/renderEslint'
import { FILES_TO_FILTER } from './utils/filterList'
import addNpmScript from './utils/addNpmScript'

function isValidPackageName(projectName) {
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(projectName)
Expand Down Expand Up @@ -491,6 +492,20 @@ async function init() {
const userAgent = process.env.npm_config_user_agent ?? ''
const packageManager = /pnpm/.test(userAgent) ? 'pnpm' : /yarn/.test(userAgent) ? 'yarn' : 'npm'

// Extend the package.json with the test script
const packageJsonPath = path.resolve(root, 'package.json')
addNpmScript('start', packageManager, 'dev', packageJsonPath)
if (
(needsCypress || needsNightwatch || needsPlaywright) &&
!needsVitest &&
!needsCypressCT &&
!needsNightwatchCT
) {
addNpmScript('test', packageManager, 'test:e2e', packageJsonPath)
} else if (needsVitest || needsCypressCT || needsNightwatchCT) {
addNpmScript('test', packageManager, 'test:unit', packageJsonPath)
}

// README generation
fs.writeFileSync(
path.resolve(root, 'README.md'),
Expand Down
5 changes: 5 additions & 0 deletions scripts/test.mjs
Expand Up @@ -37,4 +37,9 @@ for (const projectName of projects) {
console.log(`Running unit tests in ${projectName}`)
await $`pnpm test:unit`
}

if ('test:unit' in packageJSON.scripts || 'test:e2e' in packageJSON.scripts) {
console.log(`Running the test script in ${projectName}`)
await $`pnpm test`
}
}
33 changes: 33 additions & 0 deletions utils/addNpmScript.ts
@@ -0,0 +1,33 @@
import * as fs from 'node:fs'

/**
* Adds a new NPM script to the generated package.json file.
* The new script invokes another existing script with the package manager used by the developer,
* and is placed directly after the referenced NPM script.
* @param {string} scriptName the name of the new NPM script
* @param {string} packageManager the name of the used package manager, e.g. npm, pnpm or yarn
* @param {string} invokedScriptName the name of the invoked NPM script
* @param {string} packageJsonPath the path of the destination package.json file
*/
function addNpmScript(
scriptName: string,
packageManager: string,
invokedScriptName: string,
packageJsonPath: string
) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
const command =
packageManager === 'npm'
? `npm run ${invokedScriptName}`
: `${packageManager} ${invokedScriptName}`
packageJson.scripts = Object.entries(packageJson.scripts).reduce((result, entry) => {
result[entry[0]] = entry[1]
if (entry[0] === invokedScriptName) {
result[scriptName] = command
}
return result
}, {})
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n')
}

export default addNpmScript

0 comments on commit c6fd31d

Please sign in to comment.