Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't delete dist directory in between builds on local dev #512

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions node/formats/javascript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdir, rm, writeFile } from 'fs/promises'
import { mkdir, writeFile } from 'fs/promises'
import { join } from 'path'
import { pathToFileURL } from 'url'

Expand Down Expand Up @@ -27,7 +27,6 @@ const generateStage2 = async ({
formatImportError,
functions,
}: GenerateStage2Options) => {
await rm(distDirectory, { force: true, recursive: true, maxRetries: 3 })
await mkdir(distDirectory, { recursive: true })

const entryPoint = getLocalEntryPoint(functions, { bootstrapURL, formatExportTypeError, formatImportError })
Expand Down
1 change: 1 addition & 0 deletions node/npm_dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,5 +325,6 @@ export const vendorNPMSpecifiers = async ({
directory: temporaryDirectory.path,
importMap: newImportMap,
npmSpecifiersWithExtraneousFiles,
outputFiles: outputFiles.map((file) => file.path),
}
}
20 changes: 20 additions & 0 deletions node/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { readdir, unlink } from 'fs/promises'
import { join } from 'path'

import { DenoBridge, OnAfterDownloadHook, OnBeforeDownloadHook, ProcessRef } from '../bridge.js'
import { getFunctionConfig, FunctionConfig } from '../config.js'
import type { EdgeFunction } from '../edge_function.js'
Expand Down Expand Up @@ -32,6 +35,17 @@ interface StartServerOptions {
getFunctionsConfig?: boolean
}

/**
* Cleans up a directory, except for the files specified in the `except` array.
* Both should be given as absolute paths.
* Assumes the directory doesn't contain any nested directories.
*/
const cleanDirectory = async (directory: string, except: string[]) => {
const files = await readdir(directory)
const toBeDeleted = files.filter((file) => !except.includes(join(directory, file)))
await Promise.all(toBeDeleted.map((file) => unlink(join(directory, file))))
}

const prepareServer = ({
basePath,
bootstrapURL,
Expand Down Expand Up @@ -71,6 +85,9 @@ const prepareServer = ({
const importMap = baseImportMap.clone()
const npmSpecifiersWithExtraneousFiles: string[] = []

// we keep track of the files that are relevant to the user's code, so we can clean up leftovers from past executions later
const relevantFiles = [stage2Path]

if (featureFlags?.edge_functions_npm_modules) {
const vendor = await vendorNPMSpecifiers({
basePath,
Expand All @@ -85,9 +102,12 @@ const prepareServer = ({
features.npmModules = true
importMap.add(vendor.importMap)
npmSpecifiersWithExtraneousFiles.push(...vendor.npmSpecifiersWithExtraneousFiles)
relevantFiles.push(...vendor.outputFiles)
}
}

await cleanDirectory(distDirectory, relevantFiles)

try {
// This command will print a JSON object with all the modules found in
// the `stage2Path` file as well as all of their dependencies.
Expand Down