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

Improved yarn rwt link, with selective copy #2122

Merged
merged 5 commits into from
Mar 31, 2021
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"build:types": "ttsc --build --verbose",
"build:clean": "rimraf ./packages/**/dist",
"build:watch": "lerna run build:watch --parallel; ttsc --build",
"build:link": "lerna run 'build:link' --parallel",
"test": "lerna run test --stream -- --colors --maxWorkers=4",
"lint": "cross-env __REDWOOD__CONFIG_PATH=packages/create-redwood-app/template eslint -c .eslintrc.js packages",
"lint:fix": "cross-env __REDWOOD__CONFIG_PATH=packages/create-redwood-app/template eslint -c .eslintrc.js --fix packages"
Expand Down Expand Up @@ -41,6 +42,7 @@
"lerna": "^3.20.2",
"msw": "0.21.2",
"nodemon": "^2.0.6",
"npm-packlist": "^2.1.5",
"rimraf": "^3.0.2",
"ttypescript": "^1.5.12",
"typescript": "^4.1.3",
Expand Down
1 change: 1 addition & 0 deletions packages/api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build && yarn fix:permissions\"",
"build:link": "../../tasks/build-link",
"fix:permissions": "chmod +x dist/index.js; chmod +x dist/watch.js"
},
"gitHead": "c235e7d7186e5e258764699c0e0e1d5cc0fdd0b5"
Expand Down
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:types": "ttsc --build --verbose",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",
"build:link": "../../tasks/build-link",
"test": "jest",
"test:watch": "yarn test --watch"
},
Expand Down
1 change: 1 addition & 0 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"build": "yarn build:js && yarn build:types",
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:link": "../../tasks/build-link",
"build:types": "ttsc --build --verbose",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",
"test": "jest",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"prepublishOnly": "yarn build",
"build:clean-dist": "yarn rimraf 'dist/**/*/__tests__'",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\" --copy-files --no-copy-ignored && yarn build:clean-dist",
"build:link": "../../tasks/build-link",
"fix:permissions": "chmod +x dist/index.js dist/redwood-tools.js",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx,template\" --ignore dist --exec \"yarn build && yarn fix:permissions\"",
"test": "jest",
Expand Down
182 changes: 99 additions & 83 deletions packages/cli/src/redwood-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,61 +153,20 @@ const rwtLink = async (yargs) => {

console.log(`\n Redwood Framework Path: ${c.info(frameworkPath)}`)

const frameworkPackagesPath = path.join(frameworkPath, 'packages')

const symLinkPath = path.join(getPaths().base, 'redwood')

// Make sure we don't double create the symlink
// Also makes sure we use the latest path passed to link
if (fs.existsSync(symLinkPath)) {
if (fs.lstatSync(symLinkPath).isSymbolicLink()) {
console.log(c.info(' 🔗 Removing old symlink. Will recreate a new one'))
rimraf.sync(symLinkPath)
} else {
// Throw an error if it looks like there was a file/folder called redwood
console.error(
c.error(
"\n 🛑 Looks like there's something called `redwood` at the root of your project."
) +
'\n This is where we symlink redwood packages. Please remove this and rerun the command \n'
)

process.exit(1)
}
}
const frameworkPackagesPath = path.join(frameworkPath, 'packages/')
const projectPackagesPath = path.join(getPaths().base, 'redwood')

console.log(
`Linking your local Redwood build from ${c.info(frameworkPackagesPath)} \n`
`Copying your local Redwood build from ${c.info(frameworkPackagesPath)} \n`
)

fs.symlinkSync(frameworkPackagesPath, symLinkPath)
updateProjectWithResolutions(frameworkPath)

// Unlink framework repo, when process cancelled
process.on('SIGINT', rwtUnlink)

// Let workspaces do the link
await execa('yarn install', {
shell: true,
stdio: 'inherit',
cleanup: true,
cwd: getPaths().base,
})
if (!fs.existsSync(projectPackagesPath)) {
fs.mkdirSync(projectPackagesPath)
}

fixBinaryPermissions(getPaths().base)
updateProjectWithResolutions(frameworkPackagesPath)

const message = `
${c.bold('🚀 Go Forth and Contribute!')}\n
🏗 Building your local redwood repo..\n
Contributing doc: ${c.underline('https://redwoodjs.com/docs/contributing')}
`
console.log(
boxen(message, {
padding: { top: 0, bottom: 0, right: 1, left: 1 },
margin: 1,
borderColour: 'gray',
})
)
addRedwoodFolderToGitIgnore()

if (clean) {
await execa('yarn build:clean', {
Expand All @@ -218,22 +177,45 @@ const rwtLink = async (yargs) => {
})
}

const buildCommand = watch ? 'yarn build:watch' : 'yarn build'
// Unlink framework repo, when process cancelled
process.on('SIGINT', () => {
const message = `
🙏 Thanks for contributing..\n
Please run ${c.green('yarn rwt unlink')} to restore your project
`
console.log(
boxen(message, {
padding: { top: 0, bottom: 0, right: 1, left: 1 },
margin: 1,
borderColour: 'gray',
})
)
})

// Delete existing redwood folders in node_modules
rimraf.sync(path.join(getPaths().base, 'node_modules/@redwoodjs/'))

execa(buildCommand, {
await execa('yarn build:link', ['-- --', '--dest', projectPackagesPath], {
shell: true,
stdio: 'inherit',
cleanup: true,
cwd: frameworkPath,
})
}

// This should be synchronous
const rwtUnlink = () => {
// Let workspaces do the link
await execa('yarn install', ['--pure-lockfile'], {
shell: true,
stdio: 'inherit',
cleanup: true,
cwd: getPaths().base,
})

fixBinaryPermissions(getPaths().base)

const message = `
🙏 Thanks for contributing..\n
Unlinking framework. \n
${c.green('Re-run yarn install if this fails')}
${c.bold('🚀 Go Forth and Contribute!')}\n
🔗 Your project is linked!\n
Contributing doc: ${c.underline('https://redwoodjs.com/docs/contributing')}
`
console.log(
boxen(message, {
Expand All @@ -243,21 +225,34 @@ const rwtUnlink = () => {
})
)

const symLinkPath = path.join(getPaths().base, 'redwood')
if (
fs.existsSync(symLinkPath) &&
fs.lstatSync(symLinkPath).isSymbolicLink()
) {
// remove resolutions we added in link
updateProjectWithResolutions(
path.join(fs.readlinkSync(symLinkPath), '../'),
true
if (watch) {
// Restart build:link scripts in watchmode
execa(
'yarn build:link',
['-- --', '--dest', projectPackagesPath, '--watch'],
{
shell: true,
stdio: 'inherit',
cleanup: true,
cwd: frameworkPath,
}
)
}
}

rimraf.sync(symLinkPath)
// This should be synchronous
const rwtUnlink = () => {
const linkedPackagesPath = path.join(getPaths().base, 'redwood')
if (fs.existsSync(linkedPackagesPath)) {
// remove resolutions we added in link
updateProjectWithResolutions(linkedPackagesPath, true)

rimraf.sync(path.join(getPaths().base, 'node_modules/@redwoodjs'))

rimraf.sync(linkedPackagesPath)
}

execa.sync('yarn install', {
execa.sync('yarn install', ['--check-files'], {
shell: true,
stdio: 'inherit',
cleanup: true,
Expand Down Expand Up @@ -299,27 +294,46 @@ const rwtInstall = ({ packageName }) => {
)
}

const getRwPackageResolutions = (frameworkPath) => {
const frameworkVersion = require(path.join(
frameworkPath,
'packages/cli/package.json'
)).version
const packageFolders = fs.readdirSync(path.join(frameworkPath, 'packages'))
const addRedwoodFolderToGitIgnore = () => {
const gitIgnore = fs.readFileSync(
path.join(getPaths().base, '.gitignore'),
'utf-8'
)

if (gitIgnore.includes('redwood/*')) {
console.log('Redwood folder already in gitignore')
} else {
console.log('Adding `redwood/*` to .gitignore...')
fs.appendFileSync(path.join(getPaths().base, '.gitignore'), 'redwood/*')
}
}

const getRwPackagesToLink = (packagesPath) => {
const packageFolders = fs.readdirSync(packagesPath)

const rwResolutions = {}
packageFolders
return packageFolders
.filter((folderName) => folderName !== 'create-redwood-app')
.forEach((packageFolder) => {
rwResolutions[`@redwoodjs/${packageFolder}`] = frameworkVersion
.filter((item) => !/(^|\/)\.[^\/\.]/g.test(item)) // filter hidden files
.map((packageFolder) => {
return `@redwoodjs/${packageFolder}`
})

return rwResolutions
}

const updateProjectWithResolutions = (frameworkPath, remove) => {
const updateProjectWithResolutions = (redwoodPackagesPath, remove) => {
const pkgJSONPath = path.join(getPaths().base, 'package.json')
const packageJSON = require(pkgJSONPath)
const frameworkRepoResolutions = getRwPackageResolutions(frameworkPath)

const frameworkVersion = require(path.join(
redwoodPackagesPath,
'cli/package.json'
)).version

const frameworkRepoResolutions = getRwPackagesToLink(
redwoodPackagesPath
).reduce((resolutions, packageName) => {
resolutions[packageName] = frameworkVersion
return resolutions
}, {})

let resolutions = packageJSON.resolutions
let packages = packageJSON.workspaces.packages
Expand All @@ -334,7 +348,9 @@ const updateProjectWithResolutions = (frameworkPath, remove) => {
...resolutions,
...frameworkRepoResolutions,
}
packages.push('redwood/*')
if (!packages.includes('redwood/*')) {
packages.push('redwood/*')
}
}

const updatedPackageJSON = {
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"build": "yarn build:js && yarn build:types",
"prepublishOnly": "yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:link": "../../tasks/build-link",
"build:types": "ttsc --build --verbose",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx,template\" --ignore dist --exec \"yarn build\"",
"test": "jest",
Expand Down
1 change: 1 addition & 0 deletions packages/dev-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"build": "yarn build:js",
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:link": "../../tasks/build-link",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build && yarn fix:permissions\"",
"fix:permissions": "chmod +x dist/main.js"
},
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
"eslint-plugin-react-hooks": "^4.1.0",
"prettier": "^2.1.1"
},
"scripts": {
"build:link": "../../tasks/build-link",
"build": "echo 'Nothing to build..'"
},
"gitHead": "c235e7d7186e5e258764699c0e0e1d5cc0fdd0b5"
}
1 change: 1 addition & 0 deletions packages/eslint-plugin-redwood/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"build": "yarn build:js",
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist",
"build:link": "../../tasks/build-link",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx,template\" --ignore dist --exec \"yarn build\""
}
}
1 change: 1 addition & 0 deletions packages/forms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"build": "yarn build:js",
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:link": "../../tasks/build-link",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",
"test": "jest",
"test:watch": "yarn test --watch"
Expand Down
1 change: 1 addition & 0 deletions packages/internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"build": "yarn build:js && yarn build:types",
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:link": "../../tasks/build-link",
"build:types": "ttsc --build --verbose",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",
"test": "jest",
Expand Down
1 change: 1 addition & 0 deletions packages/prerender/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"build": "yarn build:js && yarn build:types",
"prepublishOnly": "yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx,.jsx\"",
"build:link": "../../tasks/build-link",
"build:types": "ttsc --build --verbose",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx,template\" --ignore dist --exec \"yarn build\"",
"test": "jest",
Expand Down
1 change: 1 addition & 0 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"build": "yarn build:js && yarn build:types",
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:link": "../../tasks/build-link",
"build:types": "ttsc --build --verbose",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",
"test": "jest",
Expand Down
1 change: 1 addition & 0 deletions packages/structure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"build": "yarn build:js && yarn build:types",
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:link": "../../tasks/build-link",
"build:types": "ttsc --build --verbose",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",
"test": "jest",
Expand Down
1 change: 1 addition & 0 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"build": "yarn build:js && yarn build:types",
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:link": "../../tasks/build-link",
"build:types": "ttsc --build --verbose",
"build:watch": "nodemon --watch src --ext 'js,ts,tsx' --ignore dist --exec 'yarn build'",
"test": "jest",
Expand Down
1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"build": "yarn build:js && yarn build:types",
"prepublishOnly": "yarn cross-env NODE_ENV=production yarn build",
"build:js": "babel src -d dist --extensions \".js,.ts,.tsx\"",
"build:link": "../../tasks/build-link",
"build:types": "ttsc --build --verbose",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",
"test": "jest",
Expand Down
Loading