mirror of
https://github.com/semaphore-protocol/semaphore.git
synced 2026-01-10 07:08:17 -05:00
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:
@@ -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`)
|
||||
|
||||
16
packages/cli/src/removePrePublishScript.ts
Normal file
16
packages/cli/src/removePrePublishScript.ts
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user