Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove prepublish script when creating template with Semaphore CLI #882

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))}/..`
Expand Down Expand Up @@ -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`)
Expand All @@ -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`)
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/src/removePrePublishScript.ts
Original file line number Diff line number Diff line change
@@ -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
}
}