1111 */
1212public class ScriptTsProjectGenerator {
1313
14+ /**
15+ * JavaScript language.
16+ */
17+ public static final String JAVASCRIPT = "javascript" ;
18+ /**
19+ * TypeScript language.
20+ */
21+ public static final String TYPESCRIPT = "typescript" ;
22+
1423 /**
1524 * Script header comment.
1625 */
@@ -45,6 +54,16 @@ public ScriptTsProjectGenerator(Script<?> script) {
4554 * @param path Path of generated project
4655 */
4756 public void generate (Path path ) {
57+ this .generate (path , JAVASCRIPT );
58+ }
59+
60+ /**
61+ * Generates TS projects with tsconfig with declarations support.
62+ *s
63+ * @param path Path of generated project
64+ * @param language Scripting language (JavaScript and TypeScript supported)
65+ */
66+ public void generate (Path path , String language ) {
4867 Path src = path .resolve ("src" );
4968
5069 if (!path .toFile ().exists ()) {
@@ -54,10 +73,16 @@ public void generate(Path path) {
5473 }
5574 }
5675
76+ String extension = switch (language .toLowerCase ()) {
77+ case JAVASCRIPT -> "js" ;
78+ case TYPESCRIPT -> "ts" ;
79+ default -> throw new IllegalArgumentException ("Unknown language " + language );
80+ };
81+
5782 String declaration = generator .generate ();
5883 try {
5984 Files .writeString (src .resolve ("types.d.ts" ), declaration );
60- Files .writeString (src .resolve ("script.js" ), SCRIPT_HEADER_COMMENT );
85+ Files .writeString (src .resolve ("script." + extension ), SCRIPT_HEADER_COMMENT );
6186 Files .writeString (path .resolve ("tsconfig.json" ), TYPE_SCRIPT_CONFIG );
6287 } catch (IOException e ) {
6388 throw new RuntimeException (e );
0 commit comments