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

internal(rwfw) Add bins as scripts #8754

Merged
merged 12 commits into from Jun 28, 2023
1 change: 0 additions & 1 deletion packages/core/package.json
Expand Up @@ -14,7 +14,6 @@
"jest": "./dist/bins/jest.js",
"nodemon": "./dist/bins/nodemon.js",
"redwood": "./dist/bins/redwood.js",
"rimraf": "./dist/bins/rimraf.js",
"rw": "./dist/bins/redwood.js",
"rw-api-server-watch": "./dist/bins/rw-api-server-watch.js",
"rw-gen": "./dist/bins/rw-gen.js",
Expand Down
8 changes: 0 additions & 8 deletions packages/core/src/bins/rimraf.ts

This file was deleted.

6 changes: 5 additions & 1 deletion tasks/framework-tools/frameworkFilesToProject.mjs
Expand Up @@ -2,7 +2,10 @@
/* eslint-env node */
// @ts-check

import { copyFrameworkFilesToProject } from './lib/project.mjs'
import {
copyFrameworkFilesToProject,
fixProjectBinaries,
} from './lib/project.mjs'

async function main() {
const redwoodProjectPath = process.argv?.[2] ?? process.env.RWJS_CWD
Expand All @@ -19,6 +22,7 @@ async function main() {

try {
await copyFrameworkFilesToProject(redwoodProjectPath)
fixProjectBinaries(redwoodProjectPath)
} catch (e) {
console.error('Error:', e.message)
process.exitCode = 1
Expand Down
33 changes: 31 additions & 2 deletions tasks/framework-tools/lib/project.mjs
Expand Up @@ -22,6 +22,11 @@ import {
export function fixProjectBinaries(projectPath) {
const bins = getFrameworkPackagesBins()

// Read the existing package.json scripts
const packageJsonPath = path.join(projectPath, 'package.json')
const packageJson = fs.readJSONSync(packageJsonPath)
const scripts = packageJson.scripts ?? {}

for (let [binName, binPath] of Object.entries(bins)) {
// if the binPath doesn't exist, create it.
const binSymlink = path.join(projectPath, 'node_modules/.bin', binName)
Expand Down Expand Up @@ -49,7 +54,32 @@ export function fixProjectBinaries(projectPath) {
console.log('chmod +x', terminalLink(binName, binPath))
fs.chmodSync(binSymlink, '755')
fs.chmodSync(binPath, '755')

// Finally we need to add all bins as scripts to the project's package.json
// otherwise newly added bins won't work.
//
// From a yarn maintainer:
// > The node_modules/.bin folder is pretty much unused by Yarn, and is
// > only there for compatibility purpose with the tools that expect it to
// > exist.
// https://github.com/yarnpkg/berry/issues/2416#issuecomment-768271751
//
// Adding them as scripts works around this issue

let posixPath = binPath

// When writing windows paths to package.json, we need to convert them to
// posix paths (or double-escape them).
if (process.platform === 'win32') {
posixPath = posixPath.replace(/\\/g, '/')
}

scripts[binName] = `node ${posixPath}`
}

// Write the updated project.json which includes the full list of scripts.
packageJson.scripts = scripts
fs.writeJSONSync(packageJsonPath, packageJson, { spaces: 2 })
}

/**
Expand Down Expand Up @@ -116,8 +146,7 @@ export async function copyFrameworkFilesToProject(
projectPath,
packageJsonPaths = getFrameworkPackageJsonPaths()
) {
// Loop over every package, delete all existing files, copy over the new files,
// and fix binaries.
// Loop over every package, delete all existing files and copy over the new files
const packagesFiles = await getFrameworkPackagesFiles(packageJsonPaths)

const packageNamesToPaths = packageJsonPaths.reduce(
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Expand Up @@ -7497,7 +7497,6 @@ __metadata:
jest: ./dist/bins/jest.js
nodemon: ./dist/bins/nodemon.js
redwood: ./dist/bins/redwood.js
rimraf: ./dist/bins/rimraf.js
rw: ./dist/bins/redwood.js
rw-api-server-watch: ./dist/bins/rw-api-server-watch.js
rw-gen: ./dist/bins/rw-gen.js
Expand Down