Skip to content

Commit 8d59aa6

Browse files
authored
[planner] Add typescript support to nodejs build (#153)
## Summary Add typescript support to nodejs build. I don't think we need a separate typescript planner, as most typescript projects need nodejs and `package.json` to function properly. ## How was it tested? `go test ./...`
1 parent 0df4214 commit 8d59aa6

File tree

9 files changed

+152
-50
lines changed

9 files changed

+152
-50
lines changed

planner/languages/javascript/nodejs_planner.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (p *Planner) GetPlan(srcDir string) *plansdk.Plan {
4444
BuildStage: &plansdk.Stage{
4545
// Copy the rest of the directory over, since at install stage we only copied package.json and its lock file.
4646
InputFiles: []string{"."},
47-
Command: p.buildCommand(pkgManager, project),
47+
Command: p.buildCommand(srcDir, pkgManager, project),
4848
},
4949

5050
StartStage: &plansdk.Stage{
@@ -64,22 +64,25 @@ type nodeProject struct {
6464
} `json:"engines,omitempty"`
6565
}
6666

67+
var versionMap = map[string]string{
68+
// Map node versions to the corresponding nixpkgs:
69+
"10": "nodejs-10_x",
70+
"12": "nodejs-12_x",
71+
"16": "nodejs-16_x",
72+
"18": "nodejs-18_x",
73+
}
74+
var defaultNodeJSPkg = "nodejs"
75+
6776
func (p *Planner) nodePackage(project *nodeProject) string {
6877
v := p.nodeVersion(project)
6978
if v != nil {
70-
switch v.Major() {
71-
case "10":
72-
return "nodejs-10_x"
73-
case "12":
74-
return "nodejs-12_x"
75-
case "16":
76-
return "nodejs-16_x"
77-
case "18":
78-
return "nodejs-18_x"
79+
pkg, ok := versionMap[v.Major()]
80+
if ok {
81+
return pkg
7982
}
8083
}
8184

82-
return "nodejs"
85+
return defaultNodeJSPkg
8386
}
8487

8588
func (p *Planner) nodeVersion(project *nodeProject) *plansdk.Version {
@@ -128,20 +131,29 @@ func (p *Planner) inputFiles(srcDir string) []string {
128131
return inputFiles
129132
}
130133

131-
func (p *Planner) buildCommand(pkgManager string, project *nodeProject) string {
132-
buildScript := project.Scripts.Build
133-
defaultBuildCmd := "npm run build"
134-
postBuildCmdHook := "npm prune --production"
134+
var buildCmdMap = map[string]string{
135+
// Map package manager to build command:
136+
"npm": "npm run build",
137+
"yarn": "yarn build",
138+
}
139+
var postBuildCmdHookMap = map[string]string{
140+
// Map package manager to post build hook command:
141+
"npm": "npm prune --production",
142+
"yarn": "yarn install --production --ignore-scripts --prefer-offline",
143+
}
135144

136-
if pkgManager == "yarn" {
137-
defaultBuildCmd = "yarn build"
138-
postBuildCmdHook = "yarn install --production --ignore-scripts --prefer-offline"
139-
}
140-
if buildScript == "" {
141-
return postBuildCmdHook
145+
func (p *Planner) buildCommand(srcDir string, pkgManager string, project *nodeProject) string {
146+
buildScript := project.Scripts.Build
147+
if buildScript != "" {
148+
return fmt.Sprintf("%s && %s", buildCmdMap[pkgManager], postBuildCmdHookMap[pkgManager])
149+
} else {
150+
if p.hasTypescriptConfig(srcDir) {
151+
return fmt.Sprintf("%s && %s", "npx tsc", postBuildCmdHookMap[pkgManager])
152+
} else {
153+
// Still runs the post build command hook to clean up dev packages.
154+
return postBuildCmdHookMap[pkgManager]
155+
}
142156
}
143-
144-
return fmt.Sprintf("%s && %s", defaultBuildCmd, postBuildCmdHook)
145157
}
146158

147159
func (p *Planner) startCommand(pkgManager string, project *nodeProject) string {
@@ -162,3 +174,8 @@ func (p *Planner) nodeProject(srcDir string) *nodeProject {
162174

163175
return project
164176
}
177+
178+
func (p *Planner) hasTypescriptConfig(srcDir string) bool {
179+
tsPath := filepath.Join(srcDir, "tsconfig.json")
180+
return plansdk.FileExists(tsPath)
181+
}

planner/languages/typescript/typescript_planner.go

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

planner/planner.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"go.jetpack.io/devbox/planner/languages/rust"
3333
"go.jetpack.io/devbox/planner/languages/scala"
3434
"go.jetpack.io/devbox/planner/languages/swift"
35-
"go.jetpack.io/devbox/planner/languages/typescript"
3635
"go.jetpack.io/devbox/planner/languages/zig"
3736
"go.jetpack.io/devbox/planner/plansdk"
3837
)
@@ -65,7 +64,6 @@ var PLANNERS = []plansdk.Planner{
6564
&rust.Planner{},
6665
&scala.Planner{},
6766
&swift.Planner{},
68-
&typescript.Planner{},
6967
&zig.Planner{},
7068
}
7169

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"packages": []
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const NODE_MAJOR_VERSION = process.versions.node.split('.')[0];
2+
if (NODE_MAJOR_VERSION !== "18") {
3+
throw new Error('Node version is not 18');
4+
}

testdata/nodejs/nodejs-typescript/package-lock.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "nodejs-18",
3+
"main": "index.js",
4+
"devDependencies": {
5+
"@types/node": "^18.7.18",
6+
"typescript": "^4.6.4"
7+
},
8+
"engines": {
9+
"node": ">=18"
10+
}
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"install_stage": {
3+
"command": "npm install",
4+
"input_files": [
5+
"package.json",
6+
"package-lock.json"
7+
]
8+
},
9+
"build_stage": {
10+
"command": "npx tsc && npm prune --production",
11+
"input_files": [
12+
"."
13+
]
14+
},
15+
"start_stage": {
16+
"command": "node index.js",
17+
"input_files": [
18+
"."
19+
]
20+
},
21+
"dev_packages": [
22+
"nodejs-18_x"
23+
],
24+
"runtime_packages": [
25+
"nodejs-18_x"
26+
]
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"include": ["**/*.ts", "**/*.tsx"],
3+
"exclude": [],
4+
"compilerOptions": {
5+
/* Visit https://aka.ms/tsconfig to read more about this file */
6+
/* Language and Environment */
7+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
8+
/* Modules */
9+
"module": "commonjs", /* Specify what module code is generated. */
10+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
11+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
12+
/* Type Checking */
13+
"strict": true, /* Enable all strict type-checking options. */
14+
/* Completeness */
15+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
16+
}
17+
}

0 commit comments

Comments
 (0)