Browse Source

Refactor NPM generator for CLI and development support

Improve the NPM generator to handle both CLI and development scenarios. Added conditional logic for CLI package deployments and development environment package generation. Simplified error handling and adjusted file-saving logic for better maintainability.

NP-637
pull/21/head
Jelle Spijker 1 year ago
parent
commit
f46170f765
  1. 43
      extensions/generators/npm.py

43
extensions/generators/npm.py

@ -1,8 +1,8 @@
import os
import json import json
from conan import ConanFile from conan import ConanFile
from conan.errors import ConanException from conan.tools.files import copy, mkdir, save
from conan.tools.files import copy, save
from pathlib import Path from pathlib import Path
@ -13,18 +13,27 @@ class npm:
def generate(self): def generate(self):
if self._conanfile.settings.os != "Emscripten": if self._conanfile.settings.os != "Emscripten":
self._conanfile.output.error("Can only deploy to NPM when build for Emscripten") self._conanfile.output.error("Can only deploy to NPM when build for Emscripten")
raise ConanException("Can only deploy to NPM when build for Emscripten") return
root_package = [dep for dep in self._conanfile.dependencies.direct_host.values()][0] if self._conanfile.display_name == "cli": # We're installing a CLI package from the conan local cache
root_package = [dep for dep in self._conanfile.dependencies.direct_host.values()][0]
# Copy the *.js and *.d.ts src_folder = root_package.package_folder
copy(self._conanfile, "*.js", src=root_package.package_folder, dst=self._conanfile.generators_folder) conf_info = root_package.conf_info
copy(self._conanfile, "*.d.ts", src=root_package.package_folder, dst=self._conanfile.generators_folder) name = root_package.ref.name
# Create the package.json # Copy the *.js and *.d.ts
save(self._conanfile, str(Path(self._conanfile.generators_folder, "package.json")), copy(self._conanfile, "*.js", src=src_folder, dst=self._conanfile.generators_folder)
json.dumps(root_package.conf_info.get(f"user.{root_package.ref.name.lower()}:package_json"))) copy(self._conanfile, "*.d.ts", src=src_folder, dst=self._conanfile.generators_folder)
# Create the .npmrc file # Create the package.json
save(self._conanfile, str(Path(self._conanfile.generators_folder, ".npmrc")), save(self._conanfile, str(Path(self._conanfile.generators_folder, "package.json")),
"//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}\n@ultimaker:registry=https://npm.pkg.github.com\nalways-auth=true") json.dumps(conf_info.get(f"user.{name.lower()}:package_json")))
# Create the .npmrc file
save(self._conanfile, str(Path(self._conanfile.generators_folder, ".npmrc")),
"//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}\n@ultimaker:registry=https://npm.pkg.github.com\nalways-auth=true")
else: # We're generating a package for a development environment
package_json = self._conanfile.python_requires["npmpackage"].module.generate_package_json(self._conanfile,
os.path.join(self._conanfile.cpp.build.bindirs[0], self._conanfile.cpp.build.bin[0]))
save(self._conanfile, str(Path(self._conanfile.build_folder, "package.json")),
json.dumps(package_json))

Loading…
Cancel
Save