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
This commit is contained in:
Shikhar Singh
2024-10-24 03:07:48 +07:00
committed by GitHub
parent 82cdc60af6
commit 3be17268ab
2 changed files with 24 additions and 1 deletions

View File

@@ -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`)

View File

@@ -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
}
}