@@ -42,20 +42,20 @@ export async function genExtension(opts: GenerationOptions, install: Installatio
4242 const writer = new CodeBlockWriter ( ) ;
4343
4444 // imports
45- genImport ( writer , "requireService" , opts . corePackage . name , opts . language ) ;
45+ genImport ( writer , "requireService" , opts . corePackage . name , opts . language , false ) ;
4646
4747 if ( opts . language === "typescript" ) {
48- genImport ( writer , "NodeCG" , `${ opts . nodeCGTypingsPackage } /types/server` , opts . language ) ;
48+ generateNodeCGImport ( writer , opts , install ) ;
49+
4950 // Service import statements
5051 services . forEach ( ( svc ) => {
51- genImport ( writer , svc . clientName , svc . packageName , opts . language ) ;
52+ genImport ( writer , svc . clientName , svc . packageName , opts . language , false ) ;
5253 } ) ;
5354 }
5455
5556 // global nodecg function
5657 writer . blankLine ( ) ;
57- const nodecgVariableType = opts . language === "typescript" ? ": NodeCG" : "" ;
58- writer . write ( `module.exports = function (nodecg${ nodecgVariableType } ) ` ) . block ( ( ) => {
58+ writer . write ( `module.exports = function (nodecg${ getNodeCGType ( opts , install ) } ) ` ) . block ( ( ) => {
5959 genLog ( writer , `${ opts . bundleName } bundle started.` ) ;
6060 writer . blankLine ( ) ;
6161
@@ -76,18 +76,68 @@ export async function genExtension(opts: GenerationOptions, install: Installatio
7676 await writeBundleFile ( writer . toString ( ) , opts . bundlePath , "extension" , `index.${ fileExtension } ` ) ;
7777}
7878
79- function genImport ( writer : CodeBlockWriter , symbolToImport : string , packageName : string , lang : CodeLanguage ) {
79+ function genImport (
80+ writer : CodeBlockWriter ,
81+ symbolToImport : string ,
82+ packageName : string ,
83+ lang : CodeLanguage ,
84+ isDefaultImport : boolean ,
85+ ) {
8086 if ( lang === "typescript" ) {
81- writer . write ( `import { ${ symbolToImport } } from ` ) . quote ( packageName ) . write ( ";" ) ;
87+ writer . write ( "import " ) ;
88+
89+ if ( ! isDefaultImport ) {
90+ writer . write ( "{ " ) ;
91+ }
92+ writer . write ( symbolToImport ) ;
93+ if ( ! isDefaultImport ) {
94+ writer . write ( " }" ) ;
95+ }
96+
97+ writer . write ( ` from ` ) . quote ( packageName ) . write ( ";" ) ;
8298 } else if ( lang === "javascript" ) {
83- writer . write ( `const ${ symbolToImport } = require(` ) . quote ( packageName ) . write ( `).${ symbolToImport } ;` ) ;
99+ writer . write ( `const ${ symbolToImport } = require(` ) . quote ( packageName ) . write ( ")" ) ;
100+
101+ if ( ! isDefaultImport ) {
102+ writer . write ( `.${ symbolToImport } ` ) ;
103+ }
104+
105+ writer . write ( ";" ) ;
84106 } else {
85107 throw new Error ( "unsupported language: " + lang ) ;
86108 }
87109
88110 writer . write ( "\n" ) ;
89111}
90112
113+ export function determineNodeCGImportPath ( opts : GenerationOptions , install : Installation ) : string {
114+ if ( install . version === "0.1" ) {
115+ // nodecg-io 0.1 is only compatible with the NodeCG typings bundled inside the full nodecg package
116+ return "nodecg/types/server" ;
117+ } else if ( install . version === "0.2" || opts . nodeCGVersion . major === 1 ) {
118+ // nodecg-io 0.2 is only compatible with nodecg-types.
119+ // Newer versions are compatible with both: nodecg-types (NodeCG v1) and @nodecg/types (NodeCG v2)
120+ // There we check the current nodecg version to determine which import to use.
121+ return "nodecg-types/types/server" ;
122+ } else if ( opts . nodeCGVersion . major === 2 ) {
123+ // All versions from 0.3 and upwards support the official @nodecg/types package for NodeCG v2
124+ return "@nodecg/types" ;
125+ } else {
126+ throw new Error (
127+ "unable to determine nodecg typings import for nodecg " +
128+ opts . nodeCGVersion +
129+ " and nodecg-io " +
130+ install . version ,
131+ ) ;
132+ }
133+ }
134+
135+ function generateNodeCGImport ( writer : CodeBlockWriter , opts : GenerationOptions , install : Installation ) {
136+ const importPath = determineNodeCGImportPath ( opts , install ) ;
137+ const isDefaultImport = opts . nodeCGVersion . major === 2 && install . version !== "0.1" && install . version !== "0.2" ;
138+ genImport ( writer , "NodeCG" , importPath , opts . language , isDefaultImport ) ;
139+ }
140+
91141function genLog ( writer : CodeBlockWriter , logMessage : string ) {
92142 writer . write ( "nodecg.log.info(" ) . quote ( logMessage ) . write ( ");" ) ;
93143}
@@ -121,3 +171,15 @@ function genOnUnavailableCall(writer: CodeBlockWriter, svc: ServiceNames) {
121171 } )
122172 . write ( ");" ) ;
123173}
174+
175+ function getNodeCGType ( opts : GenerationOptions , install : Installation ) : string {
176+ if ( opts . language !== "typescript" ) {
177+ return "" ;
178+ }
179+
180+ if ( install . version === "0.1" || install . version === "0.2" || opts . nodeCGVersion . major === 1 ) {
181+ return ": NodeCG" ;
182+ } else {
183+ return ": NodeCG.ServerAPI" ;
184+ }
185+ }
0 commit comments