-
Notifications
You must be signed in to change notification settings - Fork 170
/
framework_service_npm.go
171 lines (146 loc) · 5 KB
/
framework_service_npm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package project
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/azure/azure-dev/cli/azd/pkg/async"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/tools"
"github.com/azure/azure-dev/cli/azd/pkg/tools/npm"
)
type npmProject struct {
env *environment.Environment
cli npm.NpmCli
}
// NewNpmProject creates a new instance of a NPM project
func NewNpmProject(cli npm.NpmCli, env *environment.Environment) FrameworkService {
return &npmProject{
env: env,
cli: cli,
}
}
func (np *npmProject) Requirements() FrameworkRequirements {
return FrameworkRequirements{
Package: FrameworkPackageRequirements{
// NPM requires a restore before running any NPM scripts
RequireRestore: true,
RequireBuild: false,
},
}
}
// Gets the required external tools for the project
func (np *npmProject) RequiredExternalTools(context.Context) []tools.ExternalTool {
return []tools.ExternalTool{np.cli}
}
// Initializes the NPM project
func (np *npmProject) Initialize(ctx context.Context, serviceConfig *ServiceConfig) error {
return nil
}
// Restores dependencies for the NPM project using npm install command
func (np *npmProject) Restore(
ctx context.Context,
serviceConfig *ServiceConfig,
) *async.TaskWithProgress[*ServiceRestoreResult, ServiceProgress] {
return async.RunTaskWithProgress(
func(task *async.TaskContextWithProgress[*ServiceRestoreResult, ServiceProgress]) {
task.SetProgress(NewServiceProgress("Installing NPM dependencies"))
if err := np.cli.Install(ctx, serviceConfig.Path()); err != nil {
task.SetError(err)
return
}
task.SetResult(&ServiceRestoreResult{})
},
)
}
// Builds the project executing the npm `build` script defined within the project package.json
func (np *npmProject) Build(
ctx context.Context,
serviceConfig *ServiceConfig,
restoreOutput *ServiceRestoreResult,
) *async.TaskWithProgress[*ServiceBuildResult, ServiceProgress] {
return async.RunTaskWithProgress(
func(task *async.TaskContextWithProgress[*ServiceBuildResult, ServiceProgress]) {
// Exec custom `build` script if available
// If `build`` script is not defined in the package.json the NPM script will NOT fail
task.SetProgress(NewServiceProgress("Running NPM build script"))
if err := np.cli.RunScript(ctx, serviceConfig.Path(), "build"); err != nil {
task.SetError(err)
return
}
buildSource := serviceConfig.Path()
if serviceConfig.OutputPath != "" {
buildSource = filepath.Join(buildSource, serviceConfig.OutputPath)
}
task.SetResult(&ServiceBuildResult{
Restore: restoreOutput,
BuildOutputPath: buildSource,
})
},
)
}
func (np *npmProject) Package(
ctx context.Context,
serviceConfig *ServiceConfig,
buildOutput *ServiceBuildResult,
) *async.TaskWithProgress[*ServicePackageResult, ServiceProgress] {
return async.RunTaskWithProgress(
func(task *async.TaskContextWithProgress[*ServicePackageResult, ServiceProgress]) {
packageDest, err := os.MkdirTemp("", "azd")
if err != nil {
task.SetError(fmt.Errorf("creating package directory for %s: %w", serviceConfig.Name, err))
return
}
task.SetProgress(NewServiceProgress("Running NPM package script"))
// Long term this script we call should better align with our inner-loop scenarios
// Keeping this defaulted to `build` will create confusion for users when we start to support
// both local dev / debug builds and production bundled builds
if err := np.cli.RunScript(ctx, serviceConfig.Path(), "build"); err != nil {
task.SetError(err)
return
}
// Copy directory rooted by dist to package root.
packageSource := buildOutput.BuildOutputPath
if packageSource == "" {
packageSource = filepath.Join(serviceConfig.Path(), serviceConfig.OutputPath)
}
if entries, err := os.ReadDir(packageSource); err != nil || len(entries) == 0 {
task.SetError(
fmt.Errorf(
//nolint:lll
"package source '%s' is empty or does not exist. If your service has custom packaging requirements create an NPM script named 'build' within your package.json and ensure your package artifacts are written to the '%s' directory",
packageSource,
packageSource,
),
)
return
}
task.SetProgress(NewServiceProgress("Copying deployment package"))
if err := buildForZip(
packageSource,
packageDest,
buildForZipOptions{
excludeConditions: []excludeDirEntryCondition{
excludeNodeModules,
},
}); err != nil {
task.SetError(fmt.Errorf("packaging for %s: %w", serviceConfig.Name, err))
return
}
if err := validatePackageOutput(packageDest); err != nil {
task.SetError(err)
return
}
task.SetResult(&ServicePackageResult{
Build: buildOutput,
PackagePath: packageDest,
})
},
)
}
const cNodeModulesName = "node_modules"
func excludeNodeModules(path string, file os.FileInfo) bool {
return file.IsDir() && file.Name() == cNodeModulesName
}