Skip to content

Commit

Permalink
Merge pull request #9536 from keymanapp/chore/npm-pack-bundling
Browse files Browse the repository at this point in the history
chore: workaround npm/cli#3466 when bundling internal deps
  • Loading branch information
mcdurdin authored Sep 26, 2023
2 parents efd92af + d7f73f3 commit cc82a3c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
10 changes: 10 additions & 0 deletions developer/src/kmc/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ fi
if builder_start_action publish; then
. "$KEYMAN_ROOT/resources/build/build-utils-ci.inc.sh"

# To ensure that we cache the top-level package.json, we must call this before
# the global publish
builder_publish_cleanup

# For now, kmc will have responsibility for publishing keyman-version and
# common-types, as well as all the other dependent modules. In the future, we
# should probably have a top-level npm publish script that publishes all
Expand All @@ -143,14 +147,20 @@ if builder_start_action publish; then

# Finally, publish kmc
builder_publish_to_npm
builder_publish_cleanup
builder_finish_action success publish
elif builder_start_action pack; then
. "$KEYMAN_ROOT/resources/build/build-utils-ci.inc.sh"

# To ensure that we cache the top-level package.json, we must call this before
# the global pack
builder_publish_cleanup

for package in "${PACKAGES[@]}"; do
"$KEYMAN_ROOT/$package/build.sh" pack $DRY_RUN
done

builder_publish_to_pack
builder_publish_cleanup
builder_finish_action success pack
fi
63 changes: 62 additions & 1 deletion resources/build/build-utils-ci.inc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ function _builder_publish_npm_package() {
dry_run=--dry-run
fi

_builder_publish_cache_package_json
_builder_write_npm_version
_builder_prepublish

# Note: In either case, npm publish MUST be given --access public to publish a
# package in the @keymanapp scope on the public npm package index.
Expand Down Expand Up @@ -141,9 +143,68 @@ function _builder_write_npm_version() {
. +
(try { dependencies: (.dependencies | to_entries | . + map(select(.key | match("@keymanapp/.*")) .value |= $VERSION_WITH_TAG) | from_entries) } catch {}) +
(try { devDependencies: (.devDependencies | to_entries | . + map(select(.key | match("@keymanapp/.*")) .value |= $VERSION_WITH_TAG) | from_entries) } catch {}) +
(try { bundleDependencies: (.bundleDependencies | to_entries | . + map(select(.key | match("@keymanapp/.*")) .value |= $VERSION_WITH_TAG) | from_entries) } catch {}) +
(try { optionalDependencies: (.optionalDependencies | to_entries | . + map(select(.key | match("@keymanapp/.*")) .value |= $VERSION_WITH_TAG) | from_entries) } catch {})
' > "${line}_"
mv -f "${line}_" "$line"
done
}

#
# Due to https://github.com/npm/cli/issues/3466, we manually create all
# bundleDependencies (__NOT__ bundledDependencies, beware typos) from
# the target's package.json in its node_modules folder. Must run from
# the target's folder.
#
function _builder_prepublish() {
mkdir -p node_modules/@keymanapp
local packages=($(cat package.json | "$JQ" --raw-output '.bundleDependencies | join(" ")'))
local package

# For each @keymanapp/ package, we'll do a local symlink, note that Windows
# mklink is internal to cmd!
for package in "${packages[@]}"; do
if [[ $package =~ ^@keymanapp/ ]]; then
# Creating local symlink under node_modules
local link_source=node_modules/$package

# lookup the link_target from top-level package.json/dependencies
local link_target="$(cat "$KEYMAN_ROOT/builder_package_publish.json" | jq -r .dependencies.\"$package\")"

if [[ $link_target =~ ^file: ]]; then
link_target="$KEYMAN_ROOT"/${link_target#file:}

builder_echo "Manually linking $link_source -> $link_target (see https://github.com/npm/cli/issues/3466)"
rm -rf $link_source
if [[ $BUILDER_OS == win ]]; then
link_source="$(cygpath -w "$link_source")"
link_target="$(cygpath -w "$link_target")"
cmd //c mklink //j "$link_source" "$link_target"
else
ln -sr "$link_target" "$link_source"
fi
fi
fi
done
}

#
# We need to cache /package.json before npm version gets its sticky fingers on
# it, because afterwards, we lose the file: paths that help us to resolve
# dependencies easily. Part of the https://github.com/npm/cli/issues/3466
# workaround.
#
function _builder_publish_cache_package_json() {
if [[ -f "$KEYMAN_ROOT/builder_package_publish.json" ]]; then
return 0
fi

if "$JQ" -e '.version' "$KEYMAN_ROOT/developer/src/kmc/package.json" > /dev/null; then
builder_die "npm version has already been run. Revert the version changes to all package.json files before re-running"
fi

cp "$KEYMAN_ROOT/package.json" "$KEYMAN_ROOT/builder_package_publish.json"
}

function builder_publish_cleanup() {
rm -f "$KEYMAN_ROOT/builder_package_publish.json"
}

0 comments on commit cc82a3c

Please sign in to comment.