Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Merge branch '#45/common_node_modules'
Browse files Browse the repository at this point in the history
* #45/common_node_modules:
  Docs: updates to build commands
  Feat: Improved build both locally and in included .gradle files: now more consistent across projects.
  Feat: included .gradle files parse and react to CLI overrides -Pdev(elopment) and -Pprod(uction) and -Pverbose, making those omittable from react4xp.properties and lowering risk of releasing dev code to prod.
  Chore: code whitespace
  Chore: code whitespace
  Chore: code whitespace
  #45: NPM updates. Tested this build, did not work. Unclear error, this update might not even be the problem.
  Dependabot updates. Passes local unit tests.
  • Loading branch information
espen42 committed Apr 19, 2021
2 parents 75b50dd + 3640c8b commit 5902c02
Show file tree
Hide file tree
Showing 15 changed files with 1,747 additions and 1,109 deletions.
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ These packages don't need separate installation, they are bundled as dependencie

You'll need Gradle 5+ (a 6.2.1 gradle wrapper is included), Java JDK 11, Enonic XP 7+, and Node.

Use **Node 10** for development in this project _and in all projects when using `npm link` for linking to these packages locally_. Usually, that's when developing lib-react4xp.

NOTE: The commands `npm i` is meant to never be run in this project, should be avoided if possibl. The same goes for manually deleting _node_modules_ folders. Instead, package.json and build.gradle define scripts and tasks that handle NPM setup and interlinking with [CLI commands](#terminal-commands) that should always be **run from root**.

### Internal package dependencies

All the sub-packages mentioned above are dependencies of this main react4xp package. In addition, _react4xp-regions_ and _react4xp-buildconstants_ are dependencies of _react4xp-build-components_, and _react4xp-buildconstants_ is a dependency of _react4xp-runtime-nashornpolyfills_:
Expand All @@ -39,21 +43,43 @@ When committing to git, please follow the [conventional commits](https://www.con

### Terminal commands

From the project root, handles the entire file structure: triggers the same tasks in subprojects under /packages/, where needed.
#### NPM setup

- `npm run setup`: initial install, run this first.
**From the project root**, handles the entire file structure: triggers the same tasks in subprojects under /packages/, where needed. Only sets up NPM basis, ready for [actual project building](#project-building).

- `gradlew build`: main build command
- `npm run setup`: initial NPM install, run this first. When this is done, _node_modules_ have been installed across all subprojects, and interlinked.

- `gradlew test`: main test command
- `npm run npmInstall` (or `gradlew npmInstall`): When `npm run setup` has been run once and _node_modules_ exists in the project root, using the `npmInstall` script updates and re-links the NPM packages but skips initial boilerplate setup to save a little time.

- `npm run clean`: NPM cleanup. Removes links and node_modules across all subprojects, including the project root. Does not clean up any files from the actual [project build](#project-building).

<a name="npm-structure"></a>
#### NPM structure

After the NPM script `npmInstall` (and `setup`, which runs it after installing boilerplate), the state is ready for local development of this project _and_ for local development of lib-react4xp which uses all these projects - and usually downloads them from NPM but can now use `npm link react4xp` etc for using these packages without having to publish them to NPM before seeing them in action.

Thins ready state should be:

- `gradlew npmInstall`: install NPM dependencies. Note: in linked mode (see `npmLink` below), NPM links will be preserved (by this command and the build commands).
- All NPM packages for both main project setup _and all subprojects_ are found in _(project root)/node_modules_.
- _node_modules_ in all subpackages ar symlinked to _(project root)/node_modules_ (using [symlink-dir](https://www.npmjs.com/package/symlink-dir) for cross-platform symlinking).
- Each of the subprojects have their own symlink under _(project root)/node_modules_ - taking care of cross-dependencies.
- A marker file, _npmLinked.marker_, is created under _packages/react4xp_. This only serves to speed up the NPM istall: gradle skips this step if this marker exists.
- NOTE: this creates a **circular symlink graph**. So far this has not been a problem as long as these CLI commands are used. But be aware of it.


<a name="project-building"></a>
#### Building

Again, these commands are only used **from the project root**:

- `gradlew build`: main build command: builds file structure ready for testing and publishing to NPM

- `gradlew test`: main test command

- `gradlew cleanNpm`: delete node_modules/ folders in all packages. Cancels the linked mode (see `npmLink` below).
- `gradlew clean`: deletes everything built by gradle (but leaves the [NPM structure](#npm-structure) alone).

- `gradlew clean`: delete build/ folders in all packages

- `gradlew npmLink`: enter development linked mode: same as running [npm link](https://docs.npmjs.com/cli/link.html) in each package, but also linking up the internal dependencies between the packages. Use the cleanNpm gradle task (above) to exit linked mode. A corresponding script, `getlinks.sh`, can be run by relative path from a parent project's root folder (e.g. `sh ../../react4xp-npm/getlinks.sh`) to hook up the same links below that project's node_modules/. Sorry, no windows script yet (but it should be easy to reverse-engineer).
#### Publishing

- `gradlew versionAndPublish [ -Pdry ] [ -Pmessage='...' ]`: Auto-versions all changed packages, and publishes to NPM, after updating internal cross-dependency references. After committing your changes, run this to let [lerna](https://github.com/lerna/lerna) handle independent versioning in the packages, by tracking changes across them (use **[conventional-commit](https://www.conventionalcommits.org/en/v1.0.0/) flags** from your commit messages to track major:minor:patch versions), tagging the commit and auto-updating version tags everywhere. IMPORTANT: before running `version`, you should have run the `test` task. And after `versionAndPublish`, verify that the react4xp-* references in all packages/*/package-lock.json files are up-to-date (i.e. don't still refer to the previous versions for their dependencies). Further description in comments in [versionAndPublish.gradle](https://github.com/enonic/react4xp-npm/blob/master/versionAndPublish.gradle). Optional parameters:
- `-Pdry`: dry-run
Expand Down
65 changes: 53 additions & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,48 @@ configure(subprojects.findAll {!project.ext.SUBPROJS_TO_IGNORE.contains(it.name)


def slash = File.separator
def markerName = ".${slash}node_modules${slash}react4xp${slash}npmInstalled.txt"
def markerName = ".${slash}packages${slash}react4xp${slash}npmLinked.marker"


npmInstall.inputs.files('package.json', 'package-lock.json', 'build.gradle')
npmInstall.outputs.file('package-lock.json')
npmInstall.outputs.file file(markerName)

// Replace npmInstall with npmDoInstall in order to ensure than npmLink is run afterwards!
task npmDoInstall(type: NpmTask) {
args=['install']

inputs.files('package.json', 'package-lock.json', 'build.gradle')
outputs.file('package-lock.json')
inputs.files('package.json', 'build.gradle')
outputs.file file(markerName)
}

task clean(type: Delete) {
delete 'build'
followSymlinks = false

doLast {
def marker = new File(markerName)
marker.delete()
}
}

task npmUnlink(type: NpmTask) {
args=['run', 'npmUnlink']

npmInstall.inputs.files('package.json', 'package-lock.json', 'build.gradle')
outputs.file file(markerName)

doLast {
def marker = new File(markerName)
marker.delete()
}
}
task npmLink(type: NpmTask) {
args=['run', 'npmLink']

npmInstall.inputs.files('package.json', 'package-lock.json', 'build.gradle')
inputs.files('package.json', 'build.gradle')
outputs.file file(markerName)

doLast {
Expand All @@ -72,22 +107,28 @@ task npmDoInstall(type: NpmTask) {
marker.text = """
Marker file, indicating that the npmInstall gradle task has been run in the root project - faster than traversing the entire node_modules tree for changes.
"""
println "\nNPM marker installed --> $markerName"
}
}

task npmPreInstall(type: NpmTask) {
args=['run', 'npmUnlink']
}
task npmPostInstall(type: NpmTask) {
args=['run', 'npmLink']
}

npmInstall.enabled = false;
npmInstall.dependsOn npmPostInstall
npmPostInstall.dependsOn npmDoInstall
npmDoInstall.dependsOn npmPreInstall
npmInstall.dependsOn npmDoInstall
npmInstall.dependsOn npmLink
npmLink.mustRunAfter npmDoInstall
npmDoInstall.dependsOn npmUnlink


task setup(type: NpmTask) {
doFirst {
def marker = new File(markerName)
marker.delete()
}

args=['run', 'setup']
}

///////////////////////////////////////////////////

apply from: 'versionAndPublish.gradle'


Loading

0 comments on commit 5902c02

Please sign in to comment.