Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
refactor: migrate Node runtime utils to TypeScript (#747)
Browse files Browse the repository at this point in the history
* refactor: convert base_path util to TypeScript

* refactor: convert detect_es_module util to TypeScript

* refactor: convert plugin_modules_path util to TypeScript

* refactor: convert included_files util to TypeScript

* refactor: convert archive util to TypeScript

* refactor: convert zip util to TypeScript

* refactor: remove internal interface
  • Loading branch information
eduardoboucas committed Oct 15, 2021
1 parent c4b41ab commit b0c7d12
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 94 deletions.
51 changes: 51 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@
},
"devDependencies": {
"@netlify/eslint-config-node": "^3.3.3",
"@types/archiver": "^5.1.1",
"@types/end-of-stream": "^1.4.1",
"@types/semver": "^7.3.8",
"@types/unixify": "^1.0.0",
"adm-zip": "0.5.5",
"ava": "^3.0.0",
"cpy": "^8.0.0",
Expand Down
36 changes: 0 additions & 36 deletions src/archive.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/archive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Buffer } from 'buffer'
import { createWriteStream, Stats } from 'fs'
import { Writable } from 'stream'
import { promisify } from 'util'

import archiver, { Archiver } from 'archiver'
import endOfStream from 'end-of-stream'

const pEndOfStream = promisify(endOfStream)

// Start zipping files
const startZip = function (destPath: string): { archive: Archiver; output: Writable } {
const output = createWriteStream(destPath)
const archive = archiver('zip')
archive.pipe(output)
return { archive, output }
}

// Add new file to zip
const addZipFile = function (archive: Archiver, file: string, name: string, stat: Stats): void {
// Ensure sha256 stability regardless of mtime
archive.file(file, { name, mode: stat.mode, date: new Date(0), stats: stat })
}

// Add new file content to zip
const addZipContent = function (archive: Archiver, content: Buffer, name: string): void {
archive.append(content, { name, date: new Date(0) })
}

// End zipping files
const endZip = async function (archive: Archiver, output: Writable): Promise<void> {
archive.finalize()
await pEndOfStream(output)
}

export { startZip, addZipFile, addZipContent, endZip }
export type { Archiver as ZipArchive }
11 changes: 0 additions & 11 deletions src/runtimes/node/utils/base_path.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/runtimes/node/utils/base_path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import commonPathPrefix from 'common-path-prefix'

const getBasePath = (dirnames: string[]): string => {
if (dirnames.length === 1) {
return dirnames[0]
}

return commonPathPrefix(dirnames)
}

export { getBasePath }
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { readFile } = require('fs')
const { promisify } = require('util')
import { readFile } from 'fs'
import { promisify } from 'util'

const pReadFile = promisify(readFile)
import { init, parse } from 'es-module-lexer'

const { init, parse } = require('es-module-lexer')
const pReadFile = promisify(readFile)

const detectEsModule = async ({ mainFile }) => {
const detectEsModule = async ({ mainFile }: { mainFile: string }): Promise<boolean> => {
if (!mainFile) {
return false
}
Expand All @@ -21,4 +21,4 @@ const detectEsModule = async ({ mainFile }) => {
}
}

module.exports = { detectEsModule }
export { detectEsModule }
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { normalize, resolve } = require('path')
const { promisify } = require('util')
import { normalize, resolve } from 'path'
import { promisify } from 'util'

const glob = require('glob')
const minimatch = require('minimatch')
import glob from 'glob'
import minimatch from 'minimatch'

const pGlob = promisify(glob)

// Returns the subset of `paths` that don't match any of the glob expressions
// from `exclude`.
const filterExcludedPaths = (paths, exclude = []) => {
const filterExcludedPaths = (paths: string[], exclude: string[] = []) => {
if (exclude.length === 0) {
return paths
}
Expand All @@ -18,25 +18,28 @@ const filterExcludedPaths = (paths, exclude = []) => {
return excludedPaths
}

const getPathsOfIncludedFiles = async (includedFiles, basePath) => {
const getPathsOfIncludedFiles = async (
includedFiles: string[],
basePath: string,
): Promise<{ exclude: string[]; paths: string[] }> => {
// Some of the globs in `includedFiles` might be exclusion patterns, which
// means paths that should NOT be included in the bundle. We need to treat
// these differently, so we iterate on the array and put those paths in a
// `exclude` array and the rest of the paths in an `include` array.
const { include, exclude } = includedFiles.reduce(
const { include, exclude } = includedFiles.reduce<{ include: string[]; exclude: string[] }>(
(acc, path) => {
if (path.startsWith('!')) {
const excludePath = resolve(basePath, path.slice(1))

return {
...acc,
include: acc.include,
exclude: [...acc.exclude, excludePath],
}
}

return {
...acc,
include: [...acc.include, path],
exclude: acc.exclude,
}
},
{ include: [], exclude: [] },
Expand All @@ -53,4 +56,4 @@ const getPathsOfIncludedFiles = async (includedFiles, basePath) => {
return { exclude, paths: [...new Set(normalizedPaths)] }
}

module.exports = { filterExcludedPaths, getPathsOfIncludedFiles }
export { filterExcludedPaths, getPathsOfIncludedFiles }
7 changes: 0 additions & 7 deletions src/runtimes/node/utils/plugin_modules_path.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/runtimes/node/utils/plugin_modules_path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import findUp from 'find-up'

const AUTO_PLUGINS_DIR = '.netlify/plugins/'

const getPluginsModulesPath = (srcDir: string): Promise<string | undefined> =>
findUp(`${AUTO_PLUGINS_DIR}node_modules`, { cwd: srcDir, type: 'directory' })

export { getPluginsModulesPath }

1 comment on commit b0c7d12

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⏱ Benchmark results

largeDepsEsbuild: 7.8s

largeDepsNft: 50.4s

largeDepsZisi: 1m 5.5s

Please sign in to comment.