Skip to content

Commit 52373b3

Browse files
Always create package.json based on the default file when merging (#23)
This will solve the problem of managing dependencies and, for example, removing them after they have previously been added in the plugin.
2 parents 6325397 + 8fe121f commit 52373b3

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

package.json.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"license": "MIT",
3+
"scripts": {
4+
"preinstall": "node scripts/merge-plugin-dependencies.js",
5+
"build": "encore dev",
6+
"build:prod": "encore production",
7+
"watch": "encore dev --watch"
8+
},
9+
"dependencies": {
10+
"@sylius-ui/admin": "file:../sylius/src/Sylius/Bundle/AdminBundle",
11+
"@sylius-ui/shop": "file:../sylius/src/Sylius/Bundle/ShopBundle",
12+
"@symfony/ux-autocomplete": "file:../../../vendor/symfony/ux-autocomplete/assets",
13+
"@symfony/ux-live-component": "file:../../../vendor/symfony/ux-live-component/assets"
14+
},
15+
"devDependencies": {
16+
"@hotwired/stimulus": "^3.0.0",
17+
"@symfony/stimulus-bridge": "^3.2.0",
18+
"@symfony/webpack-encore": "^5.0.1",
19+
"tom-select": "^2.2.2"
20+
}
21+
}

scripts/merge-plugin-dependencies.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ const fs = require('fs');
22
const path = require('path');
33

44
const root = path.resolve(__dirname, '..');
5+
const packageDistPath = path.join(root, 'package.json.dist');
56
const packagePath = path.join(root, 'package.json');
67
const pluginRoot = path.resolve(root, '../../..');
78
const pluginPackagePath = path.join(pluginRoot, 'tests', 'TestApplication', 'package.json');
89

9-
if (!fs.existsSync(pluginPackagePath)) {
10-
process.exit(0);
11-
}
10+
const pluginPackageExists = fs.existsSync(pluginPackagePath);
11+
const pluginPackage = pluginPackageExists
12+
? JSON.parse(fs.readFileSync(pluginPackagePath, 'utf-8'))
13+
: { dependencies: {}, devDependencies: {} }
14+
;
1215

13-
const pluginPackage = JSON.parse(fs.readFileSync(pluginPackagePath, 'utf-8'));
14-
const rootPackage = JSON.parse(fs.readFileSync(packagePath, 'utf-8'));
16+
const basePackage = JSON.parse(fs.readFileSync(packageDistPath, 'utf-8'));
1517

1618
function mergeDependencies(target = {}, source = {}) {
1719
const result = { ...target };
@@ -22,7 +24,10 @@ function mergeDependencies(target = {}, source = {}) {
2224
return result;
2325
}
2426

25-
rootPackage.dependencies = mergeDependencies(rootPackage.dependencies, pluginPackage.dependencies);
26-
rootPackage.devDependencies = mergeDependencies(rootPackage.devDependencies, pluginPackage.devDependencies);
27+
const finalPackage = {
28+
...basePackage,
29+
dependencies: mergeDependencies(basePackage.dependencies, pluginPackage.dependencies),
30+
devDependencies: mergeDependencies(basePackage.devDependencies, pluginPackage.devDependencies),
31+
};
2732

28-
fs.writeFileSync(packagePath, JSON.stringify(rootPackage, null, 4));
33+
fs.writeFileSync(packagePath, JSON.stringify(finalPackage, null, 4));

0 commit comments

Comments
 (0)