From 6ae5c5d13e2cf165f42c79469752d223f6ca7f29 Mon Sep 17 00:00:00 2001 From: Gabor Szabo Date: Sat, 25 Nov 2023 12:23:32 +0100 Subject: [PATCH] feat: generate start and test NPM scripts (#303) --- index.ts | 15 +++++++++++++++ scripts/test.mjs | 5 +++++ utils/addNpmScript.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 utils/addNpmScript.ts diff --git a/index.ts b/index.ts index 38b9598f..cf7a54d2 100755 --- a/index.ts +++ b/index.ts @@ -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) @@ -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'), diff --git a/scripts/test.mjs b/scripts/test.mjs index cab7b948..14caf3cd 100644 --- a/scripts/test.mjs +++ b/scripts/test.mjs @@ -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` + } } diff --git a/utils/addNpmScript.ts b/utils/addNpmScript.ts new file mode 100644 index 00000000..df29014f --- /dev/null +++ b/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