Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 44 additions & 23 deletions pkg/commands/compute/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,36 +141,16 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {

dest := filepath.Join("pkg", fmt.Sprintf("%s.tar.gz", packageName))

// NOTE: The minimum package requirement is `fastly.toml` and `main.wasm`.
files := []string{
manifest.Filename,
"bin/main.wasm",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default location that @fastly/js-compute will write to but it can be configured to write to any location. Should we provide a configuration option in the fastly compute build command to specify the wasm location?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently document the expectation that the main.wasm needs to exist inside the bin directory:

Screenshot 2023-02-21 at 14 50 12

So although it might be possible to change this, for this PR I think we're OK 👍🏻

}

ignoreFiles, err := GetIgnoredFiles(IgnoreFilePath)
files, err = c.includeSourceCode(files, language.SourceDirectory)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

binFiles, err := GetNonIgnoredFiles("bin", ignoreFiles)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Ignore files": ignoreFiles,
})
return err
}
files = append(files, binFiles...)

if c.Flags.IncludeSrc {
srcFiles, err := GetNonIgnoredFiles(language.SourceDirectory, ignoreFiles)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Source directory": language.SourceDirectory,
"Ignore files": ignoreFiles,
})
return err
}
files = append(files, srcFiles...)
}

err = CreatePackageArchive(files, dest)
if err != nil {
Expand All @@ -188,6 +168,47 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
return nil
}

// includeSourceCode calculates what source code files to include in the final
// package.tar.gz that is uploaded to the Fastly API.
//
// TODO: Investigate possible change to --include-source flag.
// The following implementation presumes source code is stored in a constant
// location, which might not be true for all users. We should look at whether
// we should change the --include-source flag to not be a boolean but to
// accept a 'source code' path instead.
func (c *BuildCommand) includeSourceCode(files []string, srcDir string) ([]string, error) {
empty := make([]string, 0)

if c.Flags.IncludeSrc {
ignoreFiles, err := GetIgnoredFiles(IgnoreFilePath)
if err != nil {
c.Globals.ErrLog.Add(err)
return empty, err
}

binFiles, err := GetNonIgnoredFiles("bin", ignoreFiles)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Ignore files": ignoreFiles,
})
return empty, err
}
files = append(files, binFiles...)

srcFiles, err := GetNonIgnoredFiles(srcDir, ignoreFiles)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Source directory": srcDir,
"Ignore files": ignoreFiles,
})
return empty, err
}
files = append(files, srcFiles...)
}

return files, nil
}

// packageName acquires the package name from either a flag or manifest.
// Additionally it will sanitize the name.
func packageName(c *BuildCommand) (string, error) {
Expand Down