Skip to content
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

Support ts-node app driver #822

Closed
ccurrie-amzn opened this issue Oct 1, 2018 · 20 comments
Closed

Support ts-node app driver #822

ccurrie-amzn opened this issue Oct 1, 2018 · 20 comments

Comments

@ccurrie-amzn
Copy link
Contributor

Instead of requiring a build step to convert the Typescript code to javascript, it would help development cycle time if cdk.json could say something like:

{
  "app": "ts-node bin/deploy.ts"
}
@rix0rrr
Copy link
Contributor

rix0rrr commented Oct 2, 2018

Why does this not work? Or is this issue about making it the default via init templates?

@rix0rrr
Copy link
Contributor

rix0rrr commented Oct 2, 2018

(My question is: why is this a ticket?)

@eladb
Copy link
Contributor

eladb commented Oct 2, 2018

I think it's a proposed improvement on our current template/guidelines to use "node" and require users to have a separate compile step...

@ccurrie-amzn
Copy link
Contributor Author

If I take a brand new project from cdk init, run npm i -D ts-node, and change cdk.json to:

{
   "app": "npx ts-node bin/test_cdk.ts"
}

When I run cdk synth I get Cannot parse request 'Projects/testCdk/bin/test_cdk.ts': Unexpected token / in JSON at position 0.

@eladb
Copy link
Contributor

eladb commented Oct 3, 2018

I can see what’s going on here. Since you are using ts-node, this code doesn’t take effect.

The good news is that I am working on getting rid of the argv protocol (#216) as we speak so hopefully soon this won’t be an issue.

@rix0rrr
Copy link
Contributor

rix0rrr commented Oct 16, 2018

Does it work now in 0.12.0?

@ccurrie-amzn
Copy link
Contributor Author

Yes it does! Thanks.

@mikecann
Copy link

mikecann commented Dec 7, 2018

ts-node doesnt work for me (as of CDK 0.19.0) has something changed?

The error I get is:

spawn npx ENOENT
Error: spawn npx ENOENT
    at _errnoException (util.js:992:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
    at onErrorNT (internal/child_process.js:372:16)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)

@eladb
Copy link
Contributor

eladb commented Dec 9, 2018

Seems like the toolkit can't find npx on your system. What node.js version are you using?

I verified the following configuration works with 0.19.0 and node.js v10.13.0:

cdk.json

{
  "app": "npx ts-node bin/hello-cdk.ts"
}

package.json:

{
  "devDependencies": {
    "@types/node": "^8.9.4",
    "aws-cdk": "^0.19.0",
    "ts-node": "^7.0.1",
    "typescript": "^3.1.2"
  },
  "dependencies": {
    "@aws-cdk/cdk": "^0.19.0"
  }
}

@mikecann
Copy link

mikecann commented Dec 9, 2018

hmm wierd..

npx -v

npx: installed 1 in 1.624s
Path must be a string. Received undefined
9.7.1

node -v

v8.11.3

package.json:

{
  "name": "my_widget_service",
  "version": "0.1.0",
  "bin": {
    "my_widget_service": "bin/my_widget_service.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "cdk": "cdk",
    "ts-node": "ts-node"
  },
  "devDependencies": {
    "@types/node": "^8.9.4",
    "aws-cdk": "^0.19.0",
    "ts-node": "^7.0.1",
    "typescript": "^3.1.2"
  },
  "dependencies": {
    "@aws-cdk/aws-apigateway": "^0.19.0",
    "@aws-cdk/aws-lambda": "^0.19.0",
    "@aws-cdk/aws-s3": "^0.19.0",
    "@aws-cdk/cdk": "^0.19.0"
  }
}

cdk.json

{
  "app": "npx ts-node bin/my_widget_service.ts"
}

then

npx cdk deploy

npx: installed 1 in 1.568s
Path must be a string. Received undefined
C:\dev\bam\experiments\MyWidgetService\node_modules\aws-cdk\bin\cdk
spawn ts-node ENOENT

I usually use yarn not npx, so lets give that a try:

I changed cdk.json to

{
  "app": "yarn ts-node bin/my_widget_service.ts"
}

then

yarn cdk deploy

$ cdk deploy
spawn yarn ENOENT
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

trying

yarn cdk deploy -v

$ yarn cdk deploy -vyarn run v1.9.4
warning package.json: No license field
$ cdk deploy -vCDK toolkit version: 0.19.0 (build 2625a05)Command line arguments: { _: [ 'deploy' ], trace: false,
strict: false,
'ignore-errors': false,
ignoreErrors: false,
json: false,
j: false,
verbose: true,
v: true,
ec2creds: undefined,
i: undefined,
'version-reporting': undefined,
versionReporting: undefined,
'path-metadata': true,
pathMetadata: true,
version: false,
help: false,
h: false,
'role-arn': undefined,
r: undefined,
roleArn: undefined,
'$0': 'node_modules\aws-cdk\bin\cdk',
app: undefined,
context: undefined,
plugin: undefined,
rename: undefined,
profile: undefined,
proxy: undefined,
'toolkit-stack-name': undefined,
STACKS: [] }
Determining whether we're on an EC2 instance.
cdk.json: {
"app": "yarn ts-node bin/my_widget_service.ts"
}
Setting "aws:cdk:toolkit:default-region" context to ap-southeast-2
Resolving default credentials
Does not look like EC2 instance.
Looking up default account ID from STS
Default account ID: 385936341354
Setting "aws:cdk:toolkit:default-account" context to 385936341354
context: { 'aws:cdk:toolkit:default-region': 'ap-southeast-2',
'aws:cdk:toolkit:default-account': '385936341354',
'aws:cdk:enable-path-metadata': true }
outdir: C:\Users\mikec\AppData\Local\Temp\cdkqD4uPl
Removing outdir C:\Users\mikec\AppData\Local\Temp\cdkqD4uPl
spawn yarn ENOENT
Error: spawn yarn ENOENT
at _errnoException (util.js:992:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
at onErrorNT (internal/child_process.js:372:16)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

If I try running the TS file directly it runs okay:

yarn ts-node bin/my_widget_service.ts

ERROR: The environment variable "CDK_OUTDIR" is not defined
AWS CDK Toolkit (>= 0.11.0) is required in order to interact with this program.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

So im out of ideas now...

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 10, 2018

I think the missing piece of information here was "Windows".

Your npx executable probably ends up being called npx.bat or npx.cmd. If you just type npx on the command-line, cmd.exe is going to try all the possible extensions, but our launcher won't. Try updating your cdk.json to:

{
  "app": "npx.bat ts-node bin/my_widget_service.ts"
}

(Or npx.cmd... or maybe it's even npx.js?)

@mikecann
Copy link

@rix0rrr wow ye! I am running in the git bash shell in windows so I am surprised that there is a difference but indeed running npx.cmd ts-node bin/my_widget_service.ts or yarn.cmd ts-node bin/my_widget_service.ts works!

Thanks for all you assistance guys, I hope this helps other future googlers too 😉

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 10, 2018

Oh, that confuses me too then. But it might be that the bash shell for Windows has the same behavior as cmd built-in? Anyway, glad it works for you.

@mikecann
Copy link

@rix0rrr sorry to keep going on about this mate, but although the "add .cmd" works on windows it doesnt work on osx or linux. On those platforms you can just do "npx" or "yarn" without the ".cmd" which is annoying as I want a consistent build command on all platforms. Any idea if there is a way around this?

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 12, 2018

Offhand, making an executable yarn.cmd file that you only put on the $PATH in Linux will work. Potentially in everyone's home directory? Obviously, not a fantastic solution but it would work.

Let me think about other solutions.

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 12, 2018

An alternative is we run the command through the shell on Windows... but that opens up a host of potential shell quoting issues that I really don't want to be responsible for. Will crop up easily in a directory with spaces, not to mention that quoted argument parsing in Windows is unpredictable and depends on a complicated dance between OS and userspace code.

@mikecann
Copy link

I am experimenting around with run-script-os lets me do:

    "cdk": "run-script-os",
    "cdk:win32": "cdk --app \"npm.cmd run ts-node -P tsconfig-cdk.json cdk/index.ts\"",
    "cdk:darwin:linux": "cdk --app \"npm run ts-node -P tsconfig-cdk.json cdk/index.ts\"",

It works, but it feels like it should be part of cdk somehow..

@mikecann
Copy link

@rix0rrr what do you think? Should we reopen this issue because it would be nice to have a cross-platform solution as the extra CDK compilation step with TS really shouldnt be needed..

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 13, 2018

Sure, let's reopen

@rix0rrr rix0rrr changed the title Support ts-node as app driver Toolkit: support NPM scripts on Windows Dec 13, 2018
@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 13, 2018

Okay, too much detritus in this ticket. I'll create a new one

@rix0rrr rix0rrr changed the title Toolkit: support NPM scripts on Windows Support ts-node app driver Dec 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants