-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
transpile ts to js to handle ts types #4133
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,11 +336,29 @@ class CliRunner { | |
return path.resolve(CliRunner.CONFIG_FILE_JS); | ||
} | ||
|
||
transpileTStoJS(tsFile, filename) { | ||
const ts = require('typescript'); | ||
const fs = require('fs'); | ||
const tsFileContent = fs.readFileSync(tsFile, 'utf8'); | ||
|
||
// Define TypeScript compiler options | ||
const compilerOptions = { | ||
target: ts.ScriptTarget.ES5, | ||
module: ts.ModuleKind.CommonJS | ||
}; | ||
const transpiledCode = ts.transpileModule(tsFileContent, {compilerOptions}); | ||
const jsCode = transpiledCode.outputText; | ||
var Module = module.constructor; | ||
var m = new Module(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use |
||
m._compile(jsCode, filename); | ||
|
||
return m.exports; | ||
} | ||
|
||
loadConfig() { | ||
if (!this.argv.config) { | ||
return null; | ||
} | ||
|
||
const localJsOrTsValue = this.getLocalConfigFileName(); | ||
|
||
// use default nightwatch.json file if we haven't received another value | ||
|
@@ -354,7 +372,11 @@ class CliRunner { | |
newConfigCreated = CliRunner.createDefaultConfig(localJsOrTsValue); | ||
} | ||
|
||
if (hasJsOrTsConfig || newConfigCreated) { | ||
if (Utils.fileExistsSync(CliRunner.CONFIG_FILE_TS)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should ideally use This could be solved by changing this statement to: And for the issue you were facing with tests, that could be solved by changing the
|
||
this.argv.config = localJsOrTsValue; | ||
|
||
return this.transpileTStoJS(localJsOrTsValue, ''); | ||
} else if (hasJsOrTsConfig || newConfigCreated) { | ||
this.argv.config = localJsOrTsValue; | ||
} else if (hasJsonConfig) { | ||
this.argv.config = path.join(path.resolve('./'), this.argv.config); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this, we can also put a
const Module = require('module');
statement at the top of the function definition (where other requires are done).