Review round 1 for the Node build module: registry for other @novox packages and refused bundles (1489), toolchain.reported stream (1483, 1488), re-pin to mesh-sdk 36d6f774; delta part 4 of 4
This commit is contained in:
@@ -60,6 +60,9 @@ func (m *Module) esbuild() string {
|
||||
|
||||
// provide copies the toolchain's packages to <root>/node_modules unless they are there.
|
||||
func (m *Module) provide(root string) error {
|
||||
if err := m.writeBin(filepath.Join(root, "bin")); err != nil {
|
||||
return err
|
||||
}
|
||||
dest := filepath.Join(root, "node_modules")
|
||||
if _, err := os.Stat(dest); err == nil {
|
||||
return nil
|
||||
@@ -67,6 +70,52 @@ func (m *Module) provide(root string) error {
|
||||
return copyPackages(filepath.Join(m.toolchainDir(), "node_modules"), dest)
|
||||
}
|
||||
|
||||
// binDir is the workspace's bin directory: small shell scripts, files and not links, that call the toolchain's tsc and
|
||||
// esbuild, so a package's scripts (`tsc`, `esbuild` by name) find them. npm ci runs with --no-bin-links, which leaves
|
||||
// the toolchain no .bin directory of its own.
|
||||
func binDir(root string) string { return filepath.Join(root, "bin") }
|
||||
|
||||
func (m *Module) writeBin(dir string) error {
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
for name, body := range map[string]string{
|
||||
"tsc": "exec node '" + m.tsc() + "' \"$@\"\n",
|
||||
"esbuild": "exec '" + m.esbuild() + "' \"$@\"\n",
|
||||
} {
|
||||
path := filepath.Join(dir, name)
|
||||
if err := os.WriteFile(path, []byte("#!/bin/sh\n"+body), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Chmod(path, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// copyTree copies a directory of regular files and directories, keeping permission bits, except a build's output and
|
||||
// the repository's own .git. Anything else in it (a link) is an error: the mesh makes no symlink.
|
||||
func copyTree(from, to string) error {
|
||||
return filepath.WalkDir(from, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel, _ := filepath.Rel(from, path)
|
||||
if d.IsDir() && (rel == ".mesh-build" || rel == ".git") {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
switch {
|
||||
case d.IsDir():
|
||||
return os.MkdirAll(filepath.Join(to, rel), 0o755)
|
||||
case d.Type().IsRegular():
|
||||
return copyFile(path, filepath.Join(to, rel))
|
||||
default:
|
||||
return fmt.Errorf("%s is neither a file nor a directory, and the build copies only those", path)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// copyPackages copies the packages of a node_modules directory except the toolchain's own tools. It copies regular
|
||||
// files and directories only and keeps their permission bits; anything else in the source is an error.
|
||||
func copyPackages(from, to string) error {
|
||||
|
||||
Reference in New Issue
Block a user