@@ -100,7 +100,9 @@ function _builder_publish_npm_package() {
100100 dry_run=--dry-run
101101 fi
102102
103+ _builder_publish_cache_package_json
103104 _builder_write_npm_version
105+ _builder_prepublish
104106
105107 # Note: In either case, npm publish MUST be given --access public to publish a
106108 # package in the @keymanapp scope on the public npm package index.
@@ -141,9 +143,68 @@ function _builder_write_npm_version() {
141143 . +
142144 (try { dependencies: (.dependencies | to_entries | . + map(select(.key | match("@keymanapp/.*")) .value |= $VERSION_WITH_TAG) | from_entries) } catch {}) +
143145 (try { devDependencies: (.devDependencies | to_entries | . + map(select(.key | match("@keymanapp/.*")) .value |= $VERSION_WITH_TAG) | from_entries) } catch {}) +
144- (try { bundleDependencies: (.bundleDependencies | to_entries | . + map(select(.key | match("@keymanapp/.*")) .value |= $VERSION_WITH_TAG) | from_entries) } catch {}) +
145146 (try { optionalDependencies: (.optionalDependencies | to_entries | . + map(select(.key | match("@keymanapp/.*")) .value |= $VERSION_WITH_TAG) | from_entries) } catch {})
146147 ' > " ${line} _"
147148 mv -f " ${line} _" " $line "
148149 done
150+ }
151+
152+ #
153+ # Due to https://github.com/npm/cli/issues/3466, we manually create all
154+ # bundleDependencies (__NOT__ bundledDependencies, beware typos) from
155+ # the target's package.json in its node_modules folder. Must run from
156+ # the target's folder.
157+ #
158+ function _builder_prepublish() {
159+ mkdir -p node_modules/@keymanapp
160+ local packages=($( cat package.json | " $JQ " --raw-output ' .bundleDependencies | join(" ")' ) )
161+ local package
162+
163+ # For each @keymanapp/ package, we'll do a local symlink, note that Windows
164+ # mklink is internal to cmd!
165+ for package in " ${packages[@]} " ; do
166+ if [[ $package =~ ^@keymanapp/ ]]; then
167+ # Creating local symlink under node_modules
168+ local link_source=node_modules/$package
169+
170+ # lookup the link_target from top-level package.json/dependencies
171+ local link_target=" $( cat " $KEYMAN_ROOT /builder_package_publish.json" | jq -r .dependencies.\" $package \" ) "
172+
173+ if [[ $link_target =~ ^file: ]]; then
174+ link_target=" $KEYMAN_ROOT " /${link_target# file: }
175+
176+ builder_echo " Manually linking $link_source -> $link_target (see https://github.com/npm/cli/issues/3466)"
177+ rm -rf $link_source
178+ if [[ $BUILDER_OS == win ]]; then
179+ link_source=" $( cygpath -w " $link_source " ) "
180+ link_target=" $( cygpath -w " $link_target " ) "
181+ cmd //c mklink //j " $link_source " " $link_target "
182+ else
183+ ln -sr " $link_target " " $link_source "
184+ fi
185+ fi
186+ fi
187+ done
188+ }
189+
190+ #
191+ # We need to cache /package.json before npm version gets its sticky fingers on
192+ # it, because afterwards, we lose the file: paths that help us to resolve
193+ # dependencies easily. Part of the https://github.com/npm/cli/issues/3466
194+ # workaround.
195+ #
196+ function _builder_publish_cache_package_json() {
197+ if [[ -f " $KEYMAN_ROOT /builder_package_publish.json" ]]; then
198+ return 0
199+ fi
200+
201+ if " $JQ " -e ' .version' " $KEYMAN_ROOT /developer/src/kmc/package.json" > /dev/null; then
202+ builder_die " npm version has already been run. Revert the version changes to all package.json files before re-running"
203+ fi
204+
205+ cp " $KEYMAN_ROOT /package.json" " $KEYMAN_ROOT /builder_package_publish.json"
206+ }
207+
208+ function builder_publish_cleanup() {
209+ rm -f " $KEYMAN_ROOT /builder_package_publish.json"
149210}
0 commit comments