From c3172b878357107f382182c5af654340c8165943 Mon Sep 17 00:00:00 2001 From: rocketraccoon Date: Mon, 4 Aug 2025 12:13:10 +0700 Subject: [PATCH] fix(create-testplane): fix scripts test --- .gitignore | 1 + src/fsUtils.ts | 8 +++++++- src/package.ts | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8305ab5..b5a14e7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules build build-spec .DS_Store +.idea diff --git a/src/fsUtils.ts b/src/fsUtils.ts index dca0407..ee77ab0 100644 --- a/src/fsUtils.ts +++ b/src/fsUtils.ts @@ -123,4 +123,10 @@ export const writeJson = async (filePath: string, obj: Record): return fs.promises.writeFile(filePath, JSON.stringify(obj, null, 4)); }; -export default { exists, ensureDirectory, writeTestplaneConfig, writeTest, writeJson }; +export const readJson = async (filePath: string): Promise> => { + return new Promise(resolve => { + fs.readFile(filePath, "utf8", (_, data: string) => resolve(JSON.parse(data))); + }); +}; + +export default { exists, ensureDirectory, writeTestplaneConfig, writeTest, writeJson, readJson }; diff --git a/src/package.ts b/src/package.ts index c79bb68..094644c 100644 --- a/src/package.ts +++ b/src/package.ts @@ -61,6 +61,10 @@ const initNodeProject = (dirPath: string, packageManager: PackageManager): Promi ); }); +interface MinimalPackageJson { + scripts?: { [key: string]: string }; +} + export const initApp = async (dirPath: string, extraQuestions: boolean): Promise => { await fsUtils.ensureDirectory(dirPath); @@ -79,6 +83,18 @@ export const initApp = async (dirPath: string, extraQuestions: boolean): Promise await initNodeProject(dirPath, packageManager); } + const packageJson: MinimalPackageJson = await fsUtils.readJson(path.resolve(dirPath, PACKAGE_JSON)); + + if (packageJson && (!packageJson?.scripts?.test || !isPackageJsonExist)) { + packageJson.scripts = { + ...(packageJson?.scripts || {}), + test: "testplane", + "test:gui": "testplane gui", + }; + + await fsUtils.writeJson(path.resolve(dirPath, PACKAGE_JSON), packageJson as Record); + } + return packageManager; };