Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc6fd31d

Browse files
committed
feat: generate start and test NPM scripts (#303)
1 parent6773f73 commitc6fd31d

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
importgetLanguagefrom'./utils/getLanguage'
1919
importrenderEslintfrom'./utils/renderEslint'
2020
import{FILES_TO_FILTER}from'./utils/filterList'
21+
importaddNpmScriptfrom'./utils/addNpmScript'
2122

2223
functionisValidPackageName(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
constuserAgent=process.env.npm_config_user_agent??''
492493
constpackageManager=/pnpm/.test(userAgent) ?'pnpm' :/yarn/.test(userAgent) ?'yarn' :'npm'
493494

495+
// Extend the package.json with the test script
496+
constpackageJsonPath=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+
}elseif(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'inpackageJSON.scripts||'test:e2e'inpackageJSON.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*asfsfrom'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+
functionaddNpmScript(
13+
scriptName:string,
14+
packageManager:string,
15+
invokedScriptName:string,
16+
packageJsonPath:string
17+
){
18+
constpackageJson=JSON.parse(fs.readFileSync(packageJsonPath,'utf-8'))
19+
constcommand=
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+
returnresult
29+
},{})
30+
fs.writeFileSync(packageJsonPath,JSON.stringify(packageJson,null,2)+'\n')
31+
}
32+
33+
exportdefaultaddNpmScript

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp