Skip to content

Commit b59f7e4

Browse files
authored
chore: copy theme distribution from NPM theme packages (#8077)
* chore: copy theme distribution from NPM theme packages * add comment about purpose of the script
1 parent ad97a78 commit b59f7e4

4 files changed

Lines changed: 90 additions & 184 deletions

File tree

scripts/copy-npm-theme-files.js

Lines changed: 0 additions & 175 deletions
This file was deleted.

scripts/copy-theme-distribution.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Downloads the given NPM theme package with the version specified in the given Java theme class,
5+
* and copies its distribution files to the target directory to expose them as static resources in
6+
* a web application. The script is automatically run in the Maven build for the Aura and Lumo
7+
* theme modules.
8+
*/
9+
10+
const fs = require('fs');
11+
const path = require('path');
12+
const { execSync } = require('child_process');
13+
14+
// Check command line arguments
15+
if (process.argv.length !== 4) {
16+
console.error('Usage: node copy-theme-distribution.js <path-to-theme-class> <npm-theme-package>');
17+
process.exit(1);
18+
}
19+
20+
const javaThemeClass = process.argv[2];
21+
const npmThemePackage = process.argv[3];
22+
23+
// Read Java file and extract NPM package info
24+
if (!fs.existsSync(javaThemeClass)) {
25+
console.error(`Theme class not found: ${javaThemeClass}`);
26+
process.exit(1);
27+
}
28+
29+
const javaContent = fs.readFileSync(javaThemeClass, 'utf8');
30+
31+
// Find version from NpmPackage annotation for the specific package
32+
const packageRegex = new RegExp(`@NpmPackage\\s*\\(\\s*value\\s*=\\s*"${npmThemePackage.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"\\s*,\\s*version\\s*=\\s*"([^"]+)"`);
33+
const match = javaContent.match(packageRegex);
34+
35+
if (!match) {
36+
console.error(`Could not extract NPM package version for ${npmThemePackage} from ${javaThemeClass}`);
37+
process.exit(1);
38+
}
39+
40+
const npmThemePackageVersion = match[1];
41+
42+
console.log(`Extracting theme distribution for ${npmThemePackage}@${npmThemePackageVersion}`);
43+
44+
// Create temporary directory for npm install
45+
const tempDir = path.join('target', 'npm-workspace');
46+
fs.mkdirSync(tempDir, { recursive: true });
47+
48+
// Install the theme package using npm
49+
const packageJson = {
50+
name: 'theme-extractor',
51+
version: '1.0.0',
52+
dependencies: {
53+
[npmThemePackage]: npmThemePackageVersion
54+
}
55+
};
56+
57+
fs.writeFileSync(
58+
path.join(tempDir, 'package.json'),
59+
JSON.stringify(packageJson, null, 2)
60+
);
61+
62+
execSync('npm install', {
63+
cwd: tempDir,
64+
stdio: 'inherit'
65+
});
66+
67+
// Create target directory
68+
const targetDir = path.join('target/classes/META-INF/resources', npmThemePackage);
69+
fs.mkdirSync(targetDir, { recursive: true });
70+
71+
// Copy the distribution files
72+
const sourceDir = path.join(tempDir, 'node_modules', npmThemePackage, 'dist');
73+
if (!fs.existsSync(sourceDir)) {
74+
console.error(`Distribution directory not found: ${sourceDir}`);
75+
process.exit(1);
76+
}
77+
78+
fs.cpSync(sourceDir, targetDir, { recursive: true });
79+
80+
console.log(`Copied theme distribution to ${targetDir}`);

vaadin-aura-theme-flow-parent/vaadin-aura-theme-flow/pom.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@
2525
<version>3.1.0</version>
2626
<executions>
2727
<execution>
28-
<id>copy-npm-package</id>
28+
<id>copy-theme-distribution</id>
2929
<goals>
3030
<goal>exec</goal>
3131
</goals>
3232
<phase>process-resources</phase>
3333
<configuration>
3434
<executable>node</executable>
35+
<workingDirectory>${project.basedir}</workingDirectory>
3536
<arguments>
36-
<argument>${project.basedir}/../../scripts/copy-npm-theme-files.js</argument>
37-
<argument>${project.basedir}/src/main/java/com/vaadin/flow/theme/aura/Aura.java</argument>
38-
<argument>aura.css</argument>
37+
<argument>../../scripts/copy-theme-distribution.js</argument>
38+
<argument>src/main/java/com/vaadin/flow/theme/aura/Aura.java</argument>
39+
<argument>@vaadin/aura</argument>
3940
</arguments>
4041
</configuration>
4142
</execution>

vaadin-lumo-theme-flow-parent/vaadin-lumo-theme-flow/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@
3333
<version>3.1.0</version>
3434
<executions>
3535
<execution>
36-
<id>copy-npm-package</id>
36+
<id>copy-theme-distribution</id>
3737
<goals>
3838
<goal>exec</goal>
3939
</goals>
4040
<phase>process-resources</phase>
4141
<configuration>
4242
<executable>node</executable>
43+
<workingDirectory>${project.basedir}</workingDirectory>
4344
<arguments>
44-
<argument>${project.basedir}/../../scripts/copy-npm-theme-files.js</argument>
45-
<argument>${project.basedir}/src/main/java/com/vaadin/flow/theme/lumo/Lumo.java</argument>
46-
<argument>lumo.css</argument>
47-
<argument>utility.css</argument>
45+
<argument>../../scripts/copy-theme-distribution.js</argument>
46+
<argument>src/main/java/com/vaadin/flow/theme/lumo/Lumo.java</argument>
47+
<argument>@vaadin/vaadin-lumo-styles</argument>
4848
</arguments>
4949
</configuration>
5050
</execution>

0 commit comments

Comments
 (0)