@netlify/zip-it-and-ship-it 4.23.4 → 4.23.5-setup-typescript-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/dist/archive.d.ts +8 -0
  2. package/dist/archive.js +39 -0
  3. package/dist/bin.d.ts +2 -0
  4. package/dist/bin.js +67 -0
  5. package/dist/config.d.ts +4 -0
  6. package/dist/config.js +26 -0
  7. package/dist/feature_flags.d.ts +12 -0
  8. package/dist/feature_flags.js +10 -0
  9. package/dist/main.d.ts +15 -0
  10. package/dist/main.js +80 -0
  11. package/dist/manifest.d.ts +4 -0
  12. package/dist/manifest.js +25 -0
  13. package/dist/node_dependencies/index.d.ts +15 -0
  14. package/dist/node_dependencies/index.js +132 -0
  15. package/dist/node_dependencies/module.d.ts +1 -0
  16. package/dist/node_dependencies/module.js +10 -0
  17. package/dist/node_dependencies/nested.d.ts +10 -0
  18. package/{src → dist}/node_dependencies/nested.js +39 -60
  19. package/dist/node_dependencies/package_json.d.ts +1 -0
  20. package/dist/node_dependencies/package_json.js +29 -0
  21. package/dist/node_dependencies/published.d.ts +1 -0
  22. package/dist/node_dependencies/published.js +42 -0
  23. package/dist/node_dependencies/resolve.d.ts +2 -0
  24. package/dist/node_dependencies/resolve.js +114 -0
  25. package/dist/node_dependencies/side_files.d.ts +1 -0
  26. package/dist/node_dependencies/side_files.js +14 -0
  27. package/dist/node_dependencies/special_cases.d.ts +6 -0
  28. package/dist/node_dependencies/special_cases.js +44 -0
  29. package/dist/node_dependencies/traverse.d.ts +39 -0
  30. package/dist/node_dependencies/traverse.js +120 -0
  31. package/dist/node_dependencies/tree_files.d.ts +3 -0
  32. package/dist/node_dependencies/tree_files.js +32 -0
  33. package/dist/node_dependencies/tree_shake.d.ts +1 -0
  34. package/{src → dist}/node_dependencies/tree_shake.js +8 -12
  35. package/dist/runtimes/detect_runtime.d.ts +4 -0
  36. package/dist/runtimes/detect_runtime.js +27 -0
  37. package/dist/runtimes/go/builder.d.ts +12 -0
  38. package/dist/runtimes/go/builder.js +39 -0
  39. package/dist/runtimes/go/index.d.ts +18 -0
  40. package/dist/runtimes/go/index.js +78 -0
  41. package/dist/runtimes/index.d.ts +9 -0
  42. package/dist/runtimes/index.js +87 -0
  43. package/dist/runtimes/node/base_path.d.ts +1 -0
  44. package/dist/runtimes/node/base_path.js +8 -0
  45. package/dist/runtimes/node/bundler.d.ts +20 -0
  46. package/dist/runtimes/node/bundler.js +135 -0
  47. package/dist/runtimes/node/bundler_target.d.ts +1 -0
  48. package/dist/runtimes/node/bundler_target.js +16 -0
  49. package/dist/runtimes/node/detect_es_module.d.ts +3 -0
  50. package/dist/runtimes/node/detect_es_module.js +28 -0
  51. package/dist/runtimes/node/dynamic_imports/parser.d.ts +8 -0
  52. package/dist/runtimes/node/dynamic_imports/parser.js +135 -0
  53. package/dist/runtimes/node/dynamic_imports/plugin.d.ts +10 -0
  54. package/dist/runtimes/node/dynamic_imports/plugin.js +102 -0
  55. package/dist/runtimes/node/finder.d.ts +12 -0
  56. package/dist/runtimes/node/finder.js +82 -0
  57. package/dist/runtimes/node/index.d.ts +16 -0
  58. package/dist/runtimes/node/index.js +118 -0
  59. package/dist/runtimes/node/list_imports.d.ts +4 -0
  60. package/dist/runtimes/node/list_imports.js +68 -0
  61. package/dist/runtimes/node/native_modules/detector.d.ts +7 -0
  62. package/dist/runtimes/node/native_modules/detector.js +14 -0
  63. package/dist/runtimes/node/native_modules/plugin.d.ts +4 -0
  64. package/dist/runtimes/node/native_modules/plugin.js +65 -0
  65. package/dist/runtimes/node/src_files.d.ts +20 -0
  66. package/dist/runtimes/node/src_files.js +84 -0
  67. package/dist/runtimes/node/zip_esbuild.d.ts +22 -0
  68. package/dist/runtimes/node/zip_esbuild.js +95 -0
  69. package/dist/runtimes/node/zip_zisi.d.ts +20 -0
  70. package/dist/runtimes/node/zip_zisi.js +42 -0
  71. package/dist/runtimes/rust/builder.d.ts +9 -0
  72. package/dist/runtimes/rust/builder.js +100 -0
  73. package/dist/runtimes/rust/constants.d.ts +2 -0
  74. package/dist/runtimes/rust/constants.js +3 -0
  75. package/dist/runtimes/rust/index.d.ts +20 -0
  76. package/dist/runtimes/rust/index.js +101 -0
  77. package/dist/utils/consts.d.ts +8 -0
  78. package/dist/utils/consts.js +10 -0
  79. package/dist/utils/fs.d.ts +14 -0
  80. package/dist/utils/fs.js +86 -0
  81. package/dist/utils/polyfills.d.ts +1 -0
  82. package/{src → dist}/utils/polyfills.js +1 -1
  83. package/dist/utils/remove_falsy.d.ts +1 -0
  84. package/dist/utils/remove_falsy.js +9 -0
  85. package/dist/utils/shell.d.ts +2 -0
  86. package/dist/utils/shell.js +2 -0
  87. package/dist/zip.d.ts +15 -0
  88. package/dist/zip.js +116 -0
  89. package/dist/zip_binary.d.ts +7 -0
  90. package/dist/zip_binary.js +20 -0
  91. package/dist/zip_node.d.ts +4 -0
  92. package/dist/zip_node.js +143 -0
  93. package/package.json +18 -13
  94. package/src/archive.js +0 -36
  95. package/src/bin.js +0 -53
  96. package/src/config.js +0 -32
  97. package/src/feature_flags.js +0 -20
  98. package/src/main.js +0 -91
  99. package/src/manifest.js +0 -22
  100. package/src/node_dependencies/index.js +0 -161
  101. package/src/node_dependencies/module.js +0 -13
  102. package/src/node_dependencies/package_json.js +0 -21
  103. package/src/node_dependencies/published.js +0 -37
  104. package/src/node_dependencies/resolve.js +0 -108
  105. package/src/node_dependencies/side_files.js +0 -18
  106. package/src/node_dependencies/special_cases.js +0 -42
  107. package/src/node_dependencies/traverse.js +0 -147
  108. package/src/node_dependencies/tree_files.js +0 -20
  109. package/src/runtimes/detect_runtime.js +0 -20
  110. package/src/runtimes/go/builder.js +0 -37
  111. package/src/runtimes/go/index.js +0 -89
  112. package/src/runtimes/index.js +0 -96
  113. package/src/runtimes/node/base_path.js +0 -11
  114. package/src/runtimes/node/bundler.js +0 -151
  115. package/src/runtimes/node/bundler_target.js +0 -22
  116. package/src/runtimes/node/detect_es_module.js +0 -24
  117. package/src/runtimes/node/dynamic_imports/parser.js +0 -162
  118. package/src/runtimes/node/dynamic_imports/plugin.js +0 -116
  119. package/src/runtimes/node/finder.js +0 -93
  120. package/src/runtimes/node/index.js +0 -122
  121. package/src/runtimes/node/list_imports.js +0 -59
  122. package/src/runtimes/node/native_modules/detector.js +0 -21
  123. package/src/runtimes/node/native_modules/plugin.js +0 -77
  124. package/src/runtimes/node/src_files.js +0 -99
  125. package/src/runtimes/node/zip_esbuild.js +0 -119
  126. package/src/runtimes/node/zip_zisi.js +0 -50
  127. package/src/runtimes/rust/builder.js +0 -114
  128. package/src/runtimes/rust/constants.js +0 -4
  129. package/src/runtimes/rust/index.js +0 -112
  130. package/src/utils/consts.js +0 -10
  131. package/src/utils/fs.js +0 -88
  132. package/src/utils/remove_falsy.js +0 -12
  133. package/src/utils/shell.js +0 -3
  134. package/src/zip.js +0 -161
  135. package/src/zip_binary.js +0 -11
  136. package/src/zip_node.js +0 -179
@@ -1,37 +0,0 @@
1
- const { promisify } = require('util')
2
-
3
- const glob = require('glob')
4
-
5
- const pGlob = promisify(glob)
6
-
7
- // We use all the files published by the Node.js except some that are not needed
8
- const getPublishedFiles = async function (modulePath) {
9
- const ignore = getIgnoredFiles(modulePath)
10
- const publishedFiles = await pGlob(`${modulePath}/**`, {
11
- ignore,
12
- nodir: true,
13
- absolute: true,
14
- dot: true,
15
- })
16
- return publishedFiles
17
- }
18
-
19
- const getIgnoredFiles = function (modulePath) {
20
- return IGNORED_FILES.map((ignoreFile) => `${modulePath}/${ignoreFile}`)
21
- }
22
-
23
- // To make the zip archive smaller, we remove those.
24
- const IGNORED_FILES = [
25
- 'node_modules/**',
26
- '.npmignore',
27
- 'package-lock.json',
28
- 'yarn.lock',
29
- '*.log',
30
- '*.lock',
31
- '*~',
32
- '*.map',
33
- '*.ts',
34
- '*.patch',
35
- ]
36
-
37
- module.exports = { getPublishedFiles }
@@ -1,108 +0,0 @@
1
- const { version: nodeVersion } = require('process')
2
-
3
- const findUp = require('find-up')
4
- const pathExists = require('path-exists')
5
- const resolveLib = require('resolve')
6
- const { lt: ltVersion } = require('semver')
7
-
8
- const BACKSLASH_REGEXP = /\\/g
9
-
10
- // Find the path to a module's `package.json`
11
- // We need to use `resolve` instead of `require.resolve()` because:
12
- // - it is async
13
- // - it preserves symlinks:
14
- // - this is important because if a file does a `require('./symlink')`, we
15
- // need to bundle the symlink and its target, not only the target
16
- // - `path.resolve()` cannot be used for relative|absolute file paths
17
- // because it does not resolve ommitted file extension,
18
- // e.g. `require('./file')` instead of `require('./file.js')`
19
- // - the CLI flag `--preserve-symlinks` can be used with Node.js, but it
20
- // cannot be set runtime
21
- // However it does not give helpful error messages.
22
- // https://github.com/browserify/resolve/issues/223
23
- // So, on errors, we fallback to `require.resolve()`
24
- const resolvePackage = async function (moduleName, baseDirs) {
25
- try {
26
- return await resolvePathPreserveSymlinks(`${moduleName}/package.json`, baseDirs)
27
- } catch (error) {
28
- if (ltVersion(nodeVersion, REQUEST_RESOLVE_MIN_VERSION)) {
29
- throw error
30
- }
31
-
32
- try {
33
- return resolvePathFollowSymlinks(`${moduleName}/package.json`, baseDirs)
34
- } catch (error_) {
35
- return await resolvePackageFallback(moduleName, baseDirs, error_)
36
- }
37
- }
38
- }
39
-
40
- // TODO: remove after dropping support for Node <8.9.0
41
- // `require.resolve()` option `paths` was introduced in Node 8.9.0
42
- const REQUEST_RESOLVE_MIN_VERSION = '8.9.0'
43
-
44
- // We need to use `new Promise()` due to a bug with `utils.promisify()` on
45
- // `resolve`:
46
- // https://github.com/browserify/resolve/issues/151#issuecomment-368210310
47
- const resolvePathPreserveSymlinksForDir = function (path, basedir) {
48
- return new Promise((resolve, reject) => {
49
- // eslint-disable-next-line promise/prefer-await-to-callbacks
50
- resolveLib(path, { basedir, preserveSymlinks: true }, (error, resolvedLocation) => {
51
- if (error) {
52
- return reject(error)
53
- }
54
-
55
- resolve(resolvedLocation)
56
- })
57
- })
58
- }
59
-
60
- // the resolve library has a `paths` option but it's not the same as multiple basedirs
61
- // see https://github.com/browserify/resolve/issues/188#issuecomment-679010477
62
- // we return the first resolved location or the first error if all failed
63
- const resolvePathPreserveSymlinks = async function (path, baseDirs) {
64
- let firstError
65
- for (const basedir of baseDirs) {
66
- try {
67
- // eslint-disable-next-line no-await-in-loop
68
- return await resolvePathPreserveSymlinksForDir(path, basedir)
69
- } catch (error) {
70
- firstError = firstError || error
71
- }
72
- }
73
-
74
- throw firstError
75
- }
76
-
77
- const resolvePathFollowSymlinks = function (path, baseDirs) {
78
- return require.resolve(path, { paths: baseDirs })
79
- }
80
-
81
- // `require.resolve()` on a module's specific file (like `package.json`)
82
- // can be forbidden by the package author by using an `exports` field in
83
- // their `package.json`. We need this fallback.
84
- // It looks for the first directory up from a package's `main` file that:
85
- // - is named like the package
86
- // - has a `package.json`
87
- // Theoretically, this might not the root `package.json`, but this is very
88
- // unlikely, and we don't have any better alternative.
89
- const resolvePackageFallback = async function (moduleName, baseDirs, error) {
90
- const mainFilePath = resolvePathFollowSymlinks(moduleName, baseDirs)
91
- const packagePath = await findUp(isPackageDir.bind(null, moduleName), { cwd: mainFilePath, type: 'directory' })
92
- if (packagePath === undefined) {
93
- throw error
94
- }
95
- return packagePath
96
- }
97
-
98
- const isPackageDir = async function (moduleName, dir) {
99
- // Need to use `endsWith()` to take into account `@scope/package`.
100
- // Backslashes need to be converted for Windows.
101
- if (!dir.replace(BACKSLASH_REGEXP, '/').endsWith(moduleName) || !(await pathExists(`${dir}/package.json`))) {
102
- return
103
- }
104
-
105
- return dir
106
- }
107
-
108
- module.exports = { resolvePackage, resolvePathPreserveSymlinks }
@@ -1,18 +0,0 @@
1
- const { getPublishedFiles } = require('./published')
2
-
3
- // Some modules generate source files on `postinstall` that are not located
4
- // inside the module's directory itself.
5
- const getSideFiles = function (modulePath, moduleName) {
6
- const sideFiles = SIDE_FILES[moduleName]
7
- if (sideFiles === undefined) {
8
- return []
9
- }
10
-
11
- return getPublishedFiles(`${modulePath}/${sideFiles}`)
12
- }
13
-
14
- const SIDE_FILES = {
15
- '@prisma/client': '../../.prisma',
16
- }
17
-
18
- module.exports = { getSideFiles }
@@ -1,42 +0,0 @@
1
- const { getPackageJson } = require('./package_json')
2
-
3
- const EXTERNAL_MODULES = ['@prisma/client']
4
- const IGNORED_MODULES = ['aws-sdk']
5
-
6
- const getPackageJsonIfAvailable = async (srcDir) => {
7
- try {
8
- const packageJson = await getPackageJson(srcDir)
9
-
10
- return packageJson
11
- } catch (_) {
12
- return {}
13
- }
14
- }
15
-
16
- const getModulesForNextJs = ({ dependencies, devDependencies }) => {
17
- const allDependencies = { ...dependencies, ...devDependencies }
18
- const externalModules = allDependencies.next ? ['critters', 'nanoid'] : []
19
- const ignoredModules = []
20
-
21
- return {
22
- externalModules,
23
- ignoredModules,
24
- }
25
- }
26
-
27
- const getExternalAndIgnoredModulesFromSpecialCases = async ({ srcDir }) => {
28
- const { dependencies = {}, devDependencies = {} } = await getPackageJsonIfAvailable(srcDir)
29
- const { externalModules: nextJsExternalModules, ignoredModules: nextJsIgnoredModules } = getModulesForNextJs({
30
- dependencies,
31
- devDependencies,
32
- })
33
- const externalModules = [...EXTERNAL_MODULES, ...nextJsExternalModules]
34
- const ignoredModules = [...IGNORED_MODULES, ...nextJsIgnoredModules]
35
-
36
- return {
37
- externalModules,
38
- ignoredModules,
39
- }
40
- }
41
-
42
- module.exports = { getExternalAndIgnoredModulesFromSpecialCases }
@@ -1,147 +0,0 @@
1
- const { dirname } = require('path')
2
-
3
- const { getModuleName } = require('./module')
4
- const { getNestedDependencies, handleModuleNotFound } = require('./nested')
5
- const { getPackageJson } = require('./package_json')
6
- const { getPublishedFiles } = require('./published')
7
- const { resolvePackage } = require('./resolve')
8
- const { getSideFiles } = require('./side_files')
9
-
10
- const EXCLUDED_MODULES = new Set(['aws-sdk'])
11
-
12
- // Local cache used for optimizing the traversal of module dependencies.
13
- const getNewCache = () => ({ localFiles: new Set(), moduleNames: new Set(), modulePaths: new Set() })
14
-
15
- // When a file requires a module, we find its path inside `node_modules` and
16
- // use all its published files. We also recurse on the module's dependencies.
17
- const getDependencyPathsForDependency = async function ({
18
- dependency,
19
- basedir,
20
- state,
21
- packageJson,
22
- pluginsModulesPath,
23
- }) {
24
- const moduleName = getModuleName(dependency)
25
-
26
- // Happens when doing require("@scope") (not "@scope/name") or other oddities
27
- // Ignore those.
28
- if (moduleName === null) {
29
- return []
30
- }
31
-
32
- try {
33
- return await getDependenciesForModuleName({ moduleName, basedir, state, pluginsModulesPath })
34
- } catch (error) {
35
- return handleModuleNotFound({ error, moduleName, packageJson })
36
- }
37
- }
38
-
39
- const getDependencyNamesAndPathsForDependencies = async function ({
40
- dependencies: dependencyNames,
41
- basedir,
42
- state = getNewCache(),
43
- pluginsModulesPath,
44
- }) {
45
- const packageJson = await getPackageJson(basedir)
46
- const dependencies = await Promise.all(
47
- dependencyNames.map((dependencyName) =>
48
- getDependencyNamesAndPathsForDependency({
49
- dependency: dependencyName,
50
- basedir,
51
- state,
52
- packageJson,
53
- pluginsModulesPath,
54
- }),
55
- ),
56
- )
57
- const moduleNames = new Set(dependencies.flatMap((dependency) => [...dependency.moduleNames]))
58
- const paths = new Set(dependencies.flatMap((dependency) => [...dependency.paths]))
59
-
60
- return {
61
- moduleNames: [...moduleNames],
62
- paths: [...paths],
63
- }
64
- }
65
-
66
- const getDependencyNamesAndPathsForDependency = async function ({
67
- dependency,
68
- basedir,
69
- state = getNewCache(),
70
- packageJson,
71
- pluginsModulesPath,
72
- }) {
73
- try {
74
- const paths = await getDependencyPathsForDependency({ dependency, basedir, state, packageJson, pluginsModulesPath })
75
-
76
- return {
77
- moduleNames: [...state.moduleNames],
78
- paths,
79
- }
80
- } catch (error) {
81
- if (error.code === 'MODULE_NOT_FOUND') {
82
- return {
83
- moduleNames: [],
84
- paths: [],
85
- }
86
- }
87
-
88
- throw error
89
- }
90
- }
91
-
92
- const getDependenciesForModuleName = async function ({ moduleName, basedir, state, pluginsModulesPath }) {
93
- if (isExcludedModule(moduleName)) {
94
- return []
95
- }
96
-
97
- // Find the Node.js module directory path
98
- const packagePath = await resolvePackage(moduleName, [basedir, pluginsModulesPath].filter(Boolean))
99
-
100
- if (packagePath === undefined) {
101
- return []
102
- }
103
-
104
- const modulePath = dirname(packagePath)
105
-
106
- if (state.modulePaths.has(modulePath)) {
107
- return []
108
- }
109
-
110
- state.moduleNames.add(moduleName)
111
- state.modulePaths.add(modulePath)
112
-
113
- // The path depends on the user's build, i.e. must be dynamic
114
- // eslint-disable-next-line import/no-dynamic-require, node/global-require
115
- const packageJson = require(packagePath)
116
-
117
- const [publishedFiles, sideFiles, depsPaths] = await Promise.all([
118
- getPublishedFiles(modulePath),
119
- getSideFiles(modulePath, moduleName),
120
- getNestedModules({ modulePath, state, packageJson, pluginsModulesPath }),
121
- ])
122
- return [...publishedFiles, ...sideFiles, ...depsPaths]
123
- }
124
-
125
- const isExcludedModule = function (moduleName) {
126
- return EXCLUDED_MODULES.has(moduleName) || moduleName.startsWith('@types/')
127
- }
128
-
129
- const getNestedModules = async function ({ modulePath, state, packageJson, pluginsModulesPath }) {
130
- const dependencies = getNestedDependencies(packageJson)
131
-
132
- const depsPaths = await Promise.all(
133
- dependencies.map((dependency) =>
134
- getDependencyPathsForDependency({ dependency, basedir: modulePath, state, packageJson, pluginsModulesPath }),
135
- ),
136
- )
137
- // TODO: switch to Array.flat() once we drop support for Node.js < 11.0.0
138
- // eslint-disable-next-line unicorn/prefer-spread
139
- return [].concat(...depsPaths)
140
- }
141
-
142
- module.exports = {
143
- getDependencyPathsForDependency,
144
- getDependencyNamesAndPathsForDependencies,
145
- getDependencyNamesAndPathsForDependency,
146
- getNewCache,
147
- }
@@ -1,20 +0,0 @@
1
- const { promisify } = require('util')
2
-
3
- const glob = require('glob')
4
-
5
- const pGlob = promisify(glob)
6
-
7
- // When using a directory, we include all its descendants except `node_modules`
8
- const getTreeFiles = function (srcPath, stat) {
9
- if (!stat.isDirectory()) {
10
- return [srcPath]
11
- }
12
-
13
- return pGlob(`${srcPath}/**`, {
14
- ignore: `${srcPath}/**/node_modules/**`,
15
- nodir: true,
16
- absolute: true,
17
- })
18
- }
19
-
20
- module.exports = { getTreeFiles }
@@ -1,20 +0,0 @@
1
- const { detect, Runtime } = require('elf-cam')
2
-
3
- const { RUNTIME_GO, RUNTIME_RUST } = require('../utils/consts')
4
- const { cachedReadFile } = require('../utils/fs')
5
-
6
- // Try to guess the runtime by inspecting the binary file.
7
- const detectBinaryRuntime = async function ({ fsCache, path }) {
8
- try {
9
- const buffer = await cachedReadFile(fsCache, path)
10
-
11
- return RUNTIMES[detect(buffer)]
12
- } catch (error) {}
13
- }
14
-
15
- const RUNTIMES = {
16
- [Runtime.Go]: RUNTIME_GO,
17
- [Runtime.Rust]: RUNTIME_RUST,
18
- }
19
-
20
- module.exports = { detectBinaryRuntime }
@@ -1,37 +0,0 @@
1
- const { basename } = require('path')
2
-
3
- const { RUNTIME_GO } = require('../../utils/consts')
4
- const { lstat } = require('../../utils/fs')
5
- const { runCommand } = require('../../utils/shell')
6
-
7
- const build = async ({ destPath, mainFile, srcDir }) => {
8
- const functionName = basename(srcDir)
9
-
10
- try {
11
- await runCommand('go', ['build', '-o', destPath, '-ldflags', '-s -w'], {
12
- cwd: srcDir,
13
- env: {
14
- CGO_ENABLED: '0',
15
- GOOS: 'linux',
16
- },
17
- })
18
- } catch (error) {
19
- error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: RUNTIME_GO } }
20
-
21
- console.error(`Could not compile Go function ${functionName}:\n`)
22
-
23
- throw error
24
- }
25
-
26
- const stat = await lstat(destPath)
27
-
28
- return {
29
- mainFile,
30
- name: functionName,
31
- srcDir,
32
- srcPath: destPath,
33
- stat,
34
- }
35
- }
36
-
37
- module.exports = { build }
@@ -1,89 +0,0 @@
1
- const { basename, dirname, extname, join } = require('path')
2
-
3
- const cpFile = require('cp-file')
4
-
5
- const { RUNTIME_GO } = require('../../utils/consts')
6
- const { cachedLstat, cachedReaddir } = require('../../utils/fs')
7
- const { detectBinaryRuntime } = require('../detect_runtime')
8
-
9
- const { build } = require('./builder')
10
-
11
- const detectGoFunction = async ({ fsCache, path }) => {
12
- const stat = await cachedLstat(fsCache, path)
13
-
14
- if (!stat.isDirectory()) {
15
- return
16
- }
17
-
18
- const directoryName = basename(path)
19
- const files = await cachedReaddir(fsCache, path)
20
- const mainFileName = [`${directoryName}.go`, 'main.go'].find((name) => files.includes(name))
21
-
22
- if (mainFileName === undefined) {
23
- return
24
- }
25
-
26
- return mainFileName
27
- }
28
-
29
- const findFunctionsInPaths = async function ({ featureFlags, fsCache, paths }) {
30
- const functions = await Promise.all(
31
- paths.map(async (path) => {
32
- const runtime = await detectBinaryRuntime({ fsCache, path })
33
-
34
- if (runtime === RUNTIME_GO) {
35
- return processBinary({ fsCache, path })
36
- }
37
-
38
- if (featureFlags.buildGoSource !== true) {
39
- return
40
- }
41
-
42
- const goSourceFile = await detectGoFunction({ fsCache, path })
43
-
44
- if (goSourceFile) {
45
- return processSource({ fsCache, mainFile: goSourceFile, path })
46
- }
47
- }),
48
- )
49
-
50
- return functions.filter(Boolean)
51
- }
52
-
53
- const processBinary = async ({ fsCache, path }) => {
54
- const stat = await cachedLstat(fsCache, path)
55
- const name = basename(path, extname(path))
56
-
57
- return {
58
- mainFile: path,
59
- name,
60
- srcDir: dirname(path),
61
- srcPath: path,
62
- stat,
63
- }
64
- }
65
-
66
- const processSource = ({ mainFile, path }) => {
67
- const functionName = basename(path)
68
-
69
- return {
70
- mainFile: join(path, mainFile),
71
- name: functionName,
72
- srcDir: path,
73
- srcPath: path,
74
- }
75
- }
76
-
77
- const zipFunction = async function ({ config, destFolder, filename, mainFile, srcDir, srcPath }) {
78
- const destPath = join(destFolder, filename)
79
- const isSource = extname(mainFile) === '.go'
80
-
81
- // If we're building a Go function from source, we call the build method and
82
- // it'll take care of placing the binary in the right location. If not, we
83
- // need to copy the existing binary file to the destination directory.
84
- await (isSource ? build({ destPath, mainFile, srcDir }) : cpFile(srcPath, destPath))
85
-
86
- return { config, path: destPath }
87
- }
88
-
89
- module.exports = { findFunctionsInPaths, name: RUNTIME_GO, zipFunction }
@@ -1,96 +0,0 @@
1
- const { extname, basename } = require('path')
2
-
3
- const { getConfigForFunction } = require('../config')
4
-
5
- const goRuntime = require('./go')
6
- const jsRuntime = require('./node')
7
- const rustRuntime = require('./rust')
8
-
9
- /**
10
- * Finds functions for a list of paths using a specific runtime. The return
11
- * value is an object containing an array of the functions found (`functions`)
12
- * and an array with the paths that haven't been recognized by the runtime
13
- * (`remainingPaths`).
14
- *
15
- * @param {Boolean} dedupe
16
- * @param {Object} fsCache
17
- * @param {Array<String>} paths
18
- * @param {Object} runtime
19
- * @returns {Promise<Object>}
20
- */
21
- const findFunctionsInRuntime = async function ({ dedupe = false, featureFlags, fsCache, paths, runtime }) {
22
- const functions = await runtime.findFunctionsInPaths({ featureFlags, fsCache, paths })
23
-
24
- // If `dedupe` is true, we use the function name (`filename`) as the map key,
25
- // so that `function-1.js` will overwrite `function-1.go`. Otherwise, we use
26
- // `srcPath`, so that both functions are returned.
27
- const key = dedupe ? 'name' : 'srcPath'
28
-
29
- // Augmenting the function objects with additional information.
30
- const augmentedFunctions = functions.map((func) => [
31
- func[key],
32
- {
33
- ...func,
34
- extension: extname(func.mainFile),
35
- filename: basename(func.srcPath),
36
- runtime,
37
- },
38
- ])
39
- const usedPaths = new Set(augmentedFunctions.map(([path]) => path))
40
- const remainingPaths = paths.filter((path) => !usedPaths.has(path))
41
-
42
- return { functions: augmentedFunctions, remainingPaths }
43
- }
44
-
45
- /**
46
- * Gets a list of functions found in a list of paths.
47
- *
48
- * @param {Object} config
49
- * @param {Boolean} dedupe
50
- * @param {String} path
51
- * @returns {Promise<Map>}
52
- */
53
- const getFunctionsFromPaths = async (paths, { config, dedupe = false, featureFlags } = {}) => {
54
- // An object to cache filesystem operations. This allows different functions
55
- // to perform IO operations on the same file (i.e. getting its stats or its
56
- // contents) without duplicating work.
57
- const fsCache = {}
58
-
59
- // The order of this array determines the priority of the runtimes. If a path
60
- // is used by the first time, it won't be made available to the subsequent
61
- // runtimes.
62
- const runtimes = [jsRuntime, goRuntime, rustRuntime]
63
-
64
- // We cycle through the ordered array of runtimes, passing each one of them
65
- // through `findFunctionsInRuntime`. For each iteration, we collect all the
66
- // functions found plus the list of paths that still need to be evaluated,
67
- // using them as the input for the next iteration until the last runtime.
68
- const { functions } = await runtimes.reduce(
69
- async (aggregate, runtime) => {
70
- const { functions: aggregateFunctions, remainingPaths: aggregatePaths } = await aggregate
71
- const { functions: runtimeFunctions, remainingPaths: runtimePaths } = await findFunctionsInRuntime({
72
- dedupe,
73
- featureFlags,
74
- fsCache,
75
- paths: aggregatePaths,
76
- runtime,
77
- })
78
-
79
- return {
80
- functions: [...aggregateFunctions, ...runtimeFunctions],
81
- remainingPaths: runtimePaths,
82
- }
83
- },
84
- { functions: [], remainingPaths: paths },
85
- )
86
- const functionsWithConfig = functions.map(([name, func]) => [
87
- name,
88
- { ...func, config: getConfigForFunction({ config, func }) },
89
- ])
90
-
91
- return new Map(functionsWithConfig)
92
- }
93
-
94
- module.exports = {
95
- getFunctionsFromPaths,
96
- }
@@ -1,11 +0,0 @@
1
- const commonPathPrefix = require('common-path-prefix')
2
-
3
- const getBasePath = (dirnames) => {
4
- if (dirnames.length === 1) {
5
- return dirnames[0]
6
- }
7
-
8
- return commonPathPrefix(dirnames)
9
- }
10
-
11
- module.exports = { getBasePath }