Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: recursively merge package.json scripts field from build configuration #1372

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion private/smithy-rpcv2-cbor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build:types": "tsc -p tsconfig.types.json",
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo || exit 0",
"prepack": "yarn run clean && yarn run build"
"prepack": "yarn run clean && yarn run build",
"merged": "echo \"this is merged from user configuration.\""
},
"main": "./dist-cjs/index.js",
"types": "./dist-types/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,19 @@ static void writePackageJson(
) {
// Write the package.json file.
InputStream resource = PackageJsonGenerator.class.getResourceAsStream("base-package.json");
ObjectNode node = Node.parse(IoUtils.toUtf8String(resource))
.expectObjectNode()
.merge(settings.getPackageJson());

ObjectNode userSuppliedPackageJson = settings.getPackageJson();
ObjectNode defaultPackageJson = Node.parse(IoUtils.toUtf8String(resource))
.expectObjectNode();

ObjectNode mergedScripts = defaultPackageJson.expectObjectMember("scripts")
.merge(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the merge on line 57 not sufficient for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a shallow merge, so this does the following:

Default:

{
  "name": "A",
  "scripts": {
    "build": "..."  
  },
  "metadata": "A"
}

User supplied:

{
  "name": "B",
  "scripts": {
    "test": "..."  
  }
}
  • create the merged pkg.scripts object.
{
  "build": "...",
  "test": "..."
}
  • do the normal shallow merge
{
  "name": "B",
  "scripts": {
    "test": "..."  
  },
  "metadata": "A"
}
  • write the merged pkg.scripts onto the shallow merge
{
  "name": "B",
  "scripts": {
    "build": "...",
    "test": "..."  
  },
  "metadata": "A"
}

userSuppliedPackageJson.getObjectMember("scripts")
.orElse(ObjectNode.builder().build())
);

ObjectNode node = defaultPackageJson.merge(userSuppliedPackageJson)
.withMember("scripts", mergedScripts);

// Merge TypeScript dependencies into the package.json file.
for (Map.Entry<String, Map<String, SymbolDependency>> depEntry : dependencies.entrySet()) {
Expand Down
3 changes: 3 additions & 0 deletions smithy-typescript-protocol-test-codegen/smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"name": "Smithy team",
"url": "https://smithy.io/"
},
"scripts": {
"merged": "echo \"this is merged from user configuration.\""
},
"license": "Apache-2.0"
},
"private": true,
Expand Down
Loading