Skip to content

Commit 8f088d2

Browse files
authored
Merge pull request #212 from scratchfoundation/build-script-improvements
Build script improvements
2 parents 22f0e57 + 55288a9 commit 8f088d2

File tree

10 files changed

+128
-63
lines changed

10 files changed

+128
-63
lines changed

.github/workflows/publish.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
GIT_TAG="${{github.event.release.tag_name}}"
3030
NEW_VERSION="${GIT_TAG/v/}"
3131
32-
bash ./scripts/update-dependencies-with-tag-versions.sh "$NEW_VERSION"
32+
npm version "$NEW_VERSION" --no-git-tag-version
3333
git add package* && git commit -m "Release $NEW_VERSION"
3434
3535
- uses: ./.github/actions/install-dependencies

package-lock.json

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,27 @@
1111
},
1212
"scripts": {
1313
"refresh-gh-workflow": "ts-node scripts/build-gha-workflows.ts",
14-
"build-monorepo": "bash ./scripts/build-monorepo.sh",
14+
"build-monorepo": "cross-env-shell ./scripts/build-monorepo.sh",
1515
"prepare": "husky install",
16-
"build": "bash ./scripts/build-packages.sh"
16+
"build": "cross-env NODE_ENV=production npm run --workspaces build",
17+
"clean": "npm run --workspaces clean",
18+
"version": "cross-env-shell ./scripts/npm-version.sh"
1719
},
1820
"config": {
1921
"commitizen": {
2022
"path": "cz-conventional-changelog"
2123
}
2224
},
2325
"workspaces": [
24-
"packages/*"
26+
"packages/scratch-svg-renderer",
27+
"packages/scratch-render",
28+
"packages/scratch-vm",
29+
"packages/scratch-gui"
2530
],
2631
"devDependencies": {
2732
"@commitlint/cli": "17.8.1",
2833
"@commitlint/config-conventional": "17.8.1",
34+
"cross-env": "7.0.3",
2935
"husky": "8.0.3",
3036
"npm": "10.9.2",
3137
"ts-node": "10.9.2"

packages/scratch-gui/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
],
2828
"scripts": {
2929
"build": "npm run clean && BUILD_TYPE=dev webpack && BUILD_TYPE=dist webpack && BUILD_TYPE=dist-standalone webpack",
30-
"clean": "rimraf ./build ./dist",
30+
"clean": "rimraf build dist",
3131
"deploy": "touch build/.nojekyll && gh-pages -t -d build -m \"[skip ci] Build for $(git log --pretty=format:%H -n1)\"",
3232
"prepare": "node scripts/prepare.mjs",
3333
"prune": "./prune-gh-pages.sh",

packages/scratch-render/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
],
2323
"scripts": {
2424
"build": "webpack --progress && npm run docs",
25+
"clean": "rimraf dist playground",
2526
"docs": "jsdoc -c .jsdoc.json",
2627
"lint": "eslint .",
2728
"prepublish": "npm run build",

packages/scratch-svg-renderer/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"scripts": {
1818
"build": "npm run clean && webpack",
19-
"clean": "rimraf ./dist",
19+
"clean": "rimraf dist playground",
2020
"start": "webpack-dev-server",
2121
"test": "npm run test:lint && npm run test:unit",
2222
"test:lint": "eslint . --ext .js",

packages/scratch-vm/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
],
2424
"scripts": {
2525
"build": "npm run docs && webpack --progress",
26+
"clean": "rimraf dist playground",
2627
"coverage": "tap ./test/{unit,integration}/*.js --coverage --coverage-report=lcov",
2728
"docs": "jsdoc -c .jsdoc.json",
2829
"i18n:src": "mkdirp translations/core && format-message extract --out-file translations/core/en.json src/extensions/**/index.js",

scripts/build-packages.sh

-11
This file was deleted.

scripts/npm-version.sh

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
3+
# `npm version 1.2.3` does the following:
4+
# 1. Update `/package.json` and `/package-lock.json` with the new version
5+
# 2. `git add package.json package-lock.json`
6+
# 3. `git commit -m 1.2.3`
7+
# 4. `git --no-replace-objects tag -m 1.2.3 v1.2.3`
8+
9+
# `npm --workspaces version 1.2.3` changes step 1 to update the version of each workspace but NOT the root.
10+
# The other steps are executed as-is: step 2 will say there's nothing to commit and the process will stop.
11+
# The version numbers in `/packages/*/package.json` will be updated but not committed.
12+
13+
# `npm --workspaces --include-workspace-root version 1.2.3` changes step 1 to update the version of each workspace AND
14+
# the root. However, the remaining steps are still unaltered. You'll end up with a commit that only updates the
15+
# workspace root, and the version numbers in `/packages/*/package.json` will be updated but not committed.
16+
17+
# Fortunately, npm runs the `version` script as part of this process. Unfortunately, the script runs after
18+
# `/package.json` and `/package-lock.json` are updated, but before `/packages/*/package.json` are updated. That
19+
# means the `--workspaces` flag isn't helping, and we need to update workspace versions ourselves. We can do that with
20+
# `npm --workspaces version --no-git-tag-version ${npm_package_version}` and then stage the files.
21+
22+
# (Props to this post: <https://blog.chlod.net/technical/easy-npm-version-synchronization-for-monorepos/>)
23+
24+
# BUT WAIT!
25+
26+
# What about `--no-git-tag-version`? The normal behavior for `npm version` in that case is to adjust version numbers
27+
# but not stage or commit anything. If this script stages `/packages/*/package.json` in that case, that's no good.
28+
29+
# The only way I've found to detect `--no-git-tag-version` is to check for the existence of an environment variable
30+
# called `npm_config_git_tag_version`. Unfortunately, it's considered "true" when the value is an empty string, so we
31+
# need to check for that as well. Big thanks to this StackOverflow answer: <https://serverfault.com/a/382740>
32+
# TL;DR: `[ -z "${npm_config_git_tag_version+set}" ]` is test we want.
33+
34+
# So, the plan is:
35+
# 1. The user should run `npm version 1.2.3`
36+
# 2. npm will update `/package.json` and `/package-lock.json` for the workspace root
37+
# 3. npm will run this script (it should be set as the `version` script in `/package.json`)
38+
# 4. This script will stage `/packages/*/package.json` if `npm_config_git_tag_version` exists
39+
# 5. npm will choose whether to commit / tag afterward on its own
40+
41+
me="$(basename "$0")"
42+
43+
if [ -z "${npm_package_version}" ]; then
44+
echo "This script should be run by npm as part of the versioning process."
45+
echo "To test it, run: npm version --no-git-tag-version 1.2.3"
46+
echo "If you really must run it outside of npm, try: npm_package_version=1.2.3 npm_config_git_tag_version='' ${me}"
47+
echo "Be aware that running it directly will not update the version in /package.json"
48+
exit 1
49+
fi
50+
51+
update_dependency_in_workspace () {
52+
workspace="$1"
53+
dependency="$2"
54+
55+
jq_filter="
56+
if .dependencies.\"$dependency\" then
57+
.dependencies.\"$dependency\" = \"$npm_package_version\"
58+
else
59+
.
60+
end |
61+
if .devDependencies.\"$dependency\" then
62+
.devDependencies.\"$dependency\" = \"$npm_package_version\"
63+
else
64+
.
65+
end |
66+
if .optionalDependencies.\"$dependency\" then
67+
.optionalDependencies.\"$dependency\" = \"$npm_package_version\"
68+
else
69+
.
70+
end |
71+
if .peerDependencies.\"$dependency\" then
72+
.peerDependencies.\"$dependency\" = \"$npm_package_version\"
73+
else
74+
.
75+
end
76+
"
77+
78+
jq "$jq_filter" "$workspace/package.json" > "$workspace/package.json.tmp"
79+
mv "$workspace/package.json.tmp" "$workspace/package.json"
80+
}
81+
82+
echo "${me}: Setting workspace versions..." >&2
83+
npm --workspaces version --no-git-tag-version "${npm_package_version}"
84+
85+
echo "${me}: Reading workspaces..." >&2
86+
readarray -t workspace_locations < <( npm query .workspace | jq -r '.[].location' )
87+
readarray -t workspace_names < <( npm query .workspace | jq -r '.[].name' )
88+
89+
echo "${me}: Updating internal dependency versions..." >&2
90+
for workspace in "." "${workspace_locations[@]}"; do
91+
for dependency in "${workspace_names[@]}"; do
92+
update_dependency_in_workspace "$workspace" "$dependency"
93+
done
94+
done
95+
96+
echo "${me}: Asking npm to clean up the lock file..." >&2
97+
npm install --offline --no-audit --no-fund --ignore-scripts --package-lock-only
98+
# Sometimes it makes further changes the second time
99+
npm install --offline --no-audit --no-fund --ignore-scripts --package-lock-only
100+
101+
if [ -z "${npm_config_git_tag_version+set}" ]; then
102+
echo "${me}: Staging workspace package.json files..." >&2
103+
git add package.json package-lock.json
104+
for workspace in "${workspace_locations[@]}"; do
105+
git add "$workspace/package.json"
106+
done
107+
fi
108+
109+
echo "${me}: Done!" >&2

scripts/update-dependencies-with-tag-versions.sh

-45
This file was deleted.

0 commit comments

Comments
 (0)