From 3be17268ab21e5cfca7183d7185d6e933345914c Mon Sep 17 00:00:00 2001 From: Shikhar Singh Date: Thu, 24 Oct 2024 03:07:48 +0700 Subject: [PATCH] feat: remove prepublish script when creating template with Semaphore CLI (#882) * feat(cli): remove @semaphore-protocol/cli prepublish script The idea is to remove the prepublish script from the scripts object of the package.json file of every cli template when the template is downloaded using the CLI. BREAKING CHANGE: n * refactor(cli): add comment * refactor(cli): create seperate file for removePrePublishScript function * refactor(cli): using updatedPackageJsonContent var instead of calling readFileSync again --- packages/cli/src/index.ts | 9 ++++++++- packages/cli/src/removePrePublishScript.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/removePrePublishScript.ts diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index c349afa52..28d1e7f1e 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -13,6 +13,7 @@ import checkLatestVersion from "./checkLatestVersion.js" import getGroupIds from "./getGroupIds.js" import { getGroupId, getProjectName, getSupportedNetwork, getSupportedTemplate } from "./inquirerPrompts.js" import Spinner from "./spinner.js" +import removePrePublishScript from "./removePrePublishScript.js" // Define the path to the package.json file to extract metadata for the CLI. const packagePath = `${dirname(fileURLToPath(import.meta.url))}/..` @@ -103,6 +104,12 @@ program // Create an empty yarn.lock file to install dependencies successfully writeFileSync(`${currentDirectory}/${projectDirectory}/yarn.lock`, "") + // Read and modify package.json to remove prepublish script + const packageJsonPath = `${currentDirectory}/${projectDirectory}/package.json` + const packageJsonContent = readFileSync(packageJsonPath, "utf8") + const updatedPackageJsonContent = removePrePublishScript(packageJsonContent) + writeFileSync(packageJsonPath, updatedPackageJsonContent) + spinner.stop() console.info(`\n ${logSymbols.success}`, `Your project is ready!\n`) @@ -111,7 +118,7 @@ program console.info(` ${chalk.cyan("yarn install")}\n`) // Read the package.json to list available npm scripts. - const { scripts } = JSON.parse(readFileSync(`${currentDirectory}/${projectDirectory}/package.json`, "utf8")) + const { scripts } = JSON.parse(updatedPackageJsonContent) if (scripts) { console.info(` Available scripts:\n`) diff --git a/packages/cli/src/removePrePublishScript.ts b/packages/cli/src/removePrePublishScript.ts new file mode 100644 index 000000000..f69a36860 --- /dev/null +++ b/packages/cli/src/removePrePublishScript.ts @@ -0,0 +1,16 @@ +// Remove the prepublish script from the package.json file when creating a new project using the Semaphore CLI. +export default function removePrePublishScript(packageJsonContent: string): string { + try { + const packageJson = JSON.parse(packageJsonContent) + if (packageJson.scripts && "prepublish" in packageJson.scripts) { + delete packageJson.scripts.prepublish + if (Object.keys(packageJson.scripts).length === 0) { + delete packageJson.scripts + } + } + return JSON.stringify(packageJson, null, 2) + } catch (error) { + console.error("Error processing package.json:", error) + return packageJsonContent + } +}