Skip to content

Commit c6fd31d

Browse files
committed
feat: generate start and test NPM scripts (#303)
1 parent 6773f73 commit c6fd31d

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import getCommand from './utils/getCommand'
1818
import getLanguage from './utils/getLanguage'
1919
import renderEslint from './utils/renderEslint'
2020
import { FILES_TO_FILTER } from './utils/filterList'
21+
import addNpmScript from './utils/addNpmScript'
2122

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

495+
// Extend the package.json with the test script
496+
const packageJsonPath = path.resolve(root, 'package.json')
497+
addNpmScript('start', packageManager, 'dev', packageJsonPath)
498+
if (
499+
(needsCypress || needsNightwatch || needsPlaywright) &&
500+
!needsVitest &&
501+
!needsCypressCT &&
502+
!needsNightwatchCT
503+
) {
504+
addNpmScript('test', packageManager, 'test:e2e', packageJsonPath)
505+
} else if (needsVitest || needsCypressCT || needsNightwatchCT) {
506+
addNpmScript('test', packageManager, 'test:unit', packageJsonPath)
507+
}
508+
494509
// README generation
495510
fs.writeFileSync(
496511
path.resolve(root, 'README.md'),

scripts/test.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ for (const projectName of projects) {
3737
console.log(`Running unit tests in ${projectName}`)
3838
await $`pnpm test:unit`
3939
}
40+
41+
if ('test:unit' in packageJSON.scripts || 'test:e2e' in packageJSON.scripts) {
42+
console.log(`Running the test script in ${projectName}`)
43+
await $`pnpm test`
44+
}
4045
}

utils/addNpmScript.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as fs from 'node:fs'
2+
3+
/**
4+
* Adds a new NPM script to the generated package.json file.
5+
* The new script invokes another existing script with the package manager used by the developer,
6+
* and is placed directly after the referenced NPM script.
7+
* @param {string} scriptName the name of the new NPM script
8+
* @param {string} packageManager the name of the used package manager, e.g. npm, pnpm or yarn
9+
* @param {string} invokedScriptName the name of the invoked NPM script
10+
* @param {string} packageJsonPath the path of the destination package.json file
11+
*/
12+
function addNpmScript(
13+
scriptName: string,
14+
packageManager: string,
15+
invokedScriptName: string,
16+
packageJsonPath: string
17+
) {
18+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
19+
const command =
20+
packageManager === 'npm'
21+
? `npm run ${invokedScriptName}`
22+
: `${packageManager} ${invokedScriptName}`
23+
packageJson.scripts = Object.entries(packageJson.scripts).reduce((result, entry) => {
24+
result[entry[0]] = entry[1]
25+
if (entry[0] === invokedScriptName) {
26+
result[scriptName] = command
27+
}
28+
return result
29+
}, {})
30+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n')
31+
}
32+
33+
export default addNpmScript

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy