Skip to content

Commit

Permalink
internal(rwfw) Add bins as scripts (#8754)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh GM Walker <[email protected]>
  • Loading branch information
Tobbe and Josh-Walker-GM authored Jun 28, 2023
1 parent 6903d3b commit 0fae3fb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 0fae3fb

Please sign in to comment.