Skip to content

Commit cb170f3

Browse files
authored
Merge pull request #139 from section/add-script-deploy-protection
improved prevalidation
2 parents ab27d3f + b932002 commit cb170f3

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

commands/deploy.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *DeployCmd) Run(ctx *kong.Context, logWriters *LogWriters) (err error) {
6161
}
6262
}
6363

64-
log.Info().Msg(Green("Deploying your node.js package to Account ID: %d, App ID: %d, Environment %s",c.AccountID, c.AppID, c.Environment))
64+
log.Info().Msg(Green("Deploying your node.js package to Account ID: %d, App ID: %d, Environment %s", c.AccountID, c.AppID, c.Environment))
6565
if !c.SkipValidation {
6666
errs := IsValidNodeApp(dir)
6767
if len(errs) > 0 {
@@ -86,7 +86,7 @@ func (c *DeployCmd) Run(ctx *kong.Context, logWriters *LogWriters) (err error) {
8686
s.Stop()
8787
log.Debug().Msg("Archiving files:")
8888
for _, file := range files {
89-
log.Debug().Str("file",file)
89+
log.Debug().Str("file", file)
9090
}
9191

9292
tempFile, err := ioutil.TempFile("", "sectionctl-deploy.*.tar.gz")
@@ -130,11 +130,11 @@ func (c *DeployCmd) Run(ctx *kong.Context, logWriters *LogWriters) (err error) {
130130

131131
req.Header.Add("section-token", api.Token)
132132

133-
log.Debug().Str("URL",req.URL.String())
133+
log.Debug().Str("URL", req.URL.String())
134134

135135
artifactSizeMB := stat.Size() / 1024 / 1024
136136
log.Debug().Msg(fmt.Sprintf("Upload artifact is %dMB (%d bytes) large", artifactSizeMB, stat.Size()))
137-
s = NewSpinner(fmt.Sprintf("Uploading app (%dMB)...", artifactSizeMB),logWriters)
137+
s = NewSpinner(fmt.Sprintf("Uploading app (%dMB)...", artifactSizeMB), logWriters)
138138
s.Start()
139139
client := &http.Client{
140140
Timeout: c.Timeout,
@@ -171,9 +171,24 @@ func (c *DeployCmd) Run(ctx *kong.Context, logWriters *LogWriters) (err error) {
171171
// IsValidNodeApp detects if a Node.js app is present in a given directory
172172
func IsValidNodeApp(dir string) (errs []error) {
173173
packageJSONPath := filepath.Join(dir, "package.json")
174-
if _, err := os.Stat(packageJSONPath); os.IsNotExist(err) {
175-
errs = append(errs, fmt.Errorf("%s is not a file", packageJSONPath))
174+
if packageJSONContents, err := ioutil.ReadFile(packageJSONPath); err != nil {
175+
if os.IsNotExist(err) {
176+
log.Debug().Msg(fmt.Sprintf("[WARN] %s is not a file", packageJSONPath))
177+
} else {
178+
log.Info().Err(err).Msg("Error reading your package.json")
179+
}
180+
} else {
181+
packageJSON, err := ParsePackageJSON(string(packageJSONContents))
182+
if err != nil {
183+
log.Info().Err(err).Msg("Error parsing your package.json")
184+
}
185+
if len(packageJSON.Section.StartScript) == 0 && packageJSON.Scripts["start"] == "" {
186+
errs = append(errs, fmt.Errorf("package.json does not include a start script. please add one"))
187+
} else if len(packageJSON.Section.StartScript) > 0 && len(packageJSON.Scripts[packageJSON.Section.StartScript]) == 0 {
188+
errs = append(errs, fmt.Errorf("package.json does not include the script: %s", packageJSON.Section.StartScript))
189+
}
176190
}
191+
177192
nodeModulesPath := filepath.Join(dir, "node_modules")
178193
fi, err := os.Stat(nodeModulesPath)
179194
if os.IsNotExist(err) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"description": "This is just an example package for the go tests.",
5+
"main": "index.js",
6+
"scripts": {
7+
"predeploy": "npm install",
8+
"deploy": "sectionctl deploy",
9+
"start": "npx serve node_modules -l 8080"
10+
},
11+
"author": "section.io",
12+
"license": "ISC"
13+
}

commands/testdata/deploy/valid-nodejs-app/server.conf

Whitespace-only changes.

0 commit comments

Comments
 (0)