Skip to content

Commit

Permalink
Added file watcher to now dev (#2153)
Browse files Browse the repository at this point in the history
* [now dev] Add `nsfw` file watcher

WIP for triggering background rebuilds. So far the `files` mapping is
now kept in sync via the events produced by `nsfw`.

* Add `now.json` caching and invalidation from nsfw events

* Add initial rebuilding logic from filesystem watching

* Remove previously built assets when a rebuild occurs

* Make `@now/static` not require a hard refresh

* Shut down the `nsfw` instance when stopping the DevServer

* Prettier and some minor tweaks

* Ship module

* Support private deps

* Fixed tests

* Fixed integration tests

* Revert "Support private deps"

This reverts commit 0b4991b.

* Support multiple platforms

* Typed

* Consider Alpine

* Bumped @zeit/nsfw to latest version

* Update src/commands/dev/lib/nsfw-module.ts

Co-Authored-By: leo <mindrun@icloud.com>

* Pass module path differently

* Bumped package

* Make binary executable

* Wait until piping is complete

* Added debug statements
  • Loading branch information
TooTallNate authored and leo committed Apr 25, 2019
1 parent de560ea commit defb2f1
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 176 deletions.
8 changes: 8 additions & 0 deletions @types/promisepipe/index.d.ts
@@ -0,0 +1,8 @@
declare module 'promisepipe' {
import { ReadableStream, WritableStream } from 'stream'

export default function (
stream: ReadableStream,
anotherStream: WritableStream,
): Promise<void>
}
11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -62,10 +62,10 @@
"dist/runtimes"
],
"targets": [
"node10-alpine-x64",
"node10-linux-x64",
"node10-macos-x64",
"node10-win-x64"
"node10.4.1-alpine-x64",
"node10.4.1-linux-x64",
"node10.4.1-macos-x64",
"node10.4.1-win-x64"
]
},
"ava": {
Expand Down Expand Up @@ -115,6 +115,7 @@
"@zeit/fun": "0.6.0",
"@zeit/git-hooks": "0.1.4",
"@zeit/ncc": "0.17.4",
"@zeit/nsfw": "2.0.1",
"@zeit/source-map-support": "0.6.2",
"alpha-sort": "2.0.1",
"ansi-escapes": "3.0.0",
Expand Down Expand Up @@ -178,10 +179,12 @@
"prettier": "1.16.2",
"printf": "0.2.5",
"progress": "2.0.3",
"promisepipe": "3.0.0",
"psl": "1.1.31",
"qr-image": "3.2.0",
"raw-body": "2.3.3",
"read-pkg": "2.0.0",
"rx-lite-aggregates": "4.0.8",
"semver": "5.5.0",
"serve-handler": "5.0.7",
"sinon": "4.4.2",
Expand Down
5 changes: 4 additions & 1 deletion src/commands/dev/lib/builder-cache.ts
Expand Up @@ -85,7 +85,10 @@ export async function installBuilders(
packages: string[],
update: boolean = false
): Promise<void> {
if (packages.length === 1 && Object.hasOwnProperty.call(localBuilders, packages[0])) {
if (
packages.length === 1 &&
Object.hasOwnProperty.call(localBuilders, packages[0])
) {
// Static deployment, no builders to install
return;
}
Expand Down
28 changes: 18 additions & 10 deletions src/commands/dev/lib/dev-builder.ts
Expand Up @@ -11,7 +11,6 @@ import { FileFsRef, download } from '@now/build-utils';

import { globBuilderInputs } from './glob';
import DevServer from './dev-server';
import wait from '../../../util/output/wait';
import IGNORED from '../../../util/ignored';
import { LambdaSizeExceededError } from '../../../util/errors-ts';
import { installBuilders, getBuilder } from './builder-cache';
Expand Down Expand Up @@ -74,9 +73,6 @@ export async function executeBuild(
match: BuildMatch,
requestPath: string | null = null
): Promise<void> {
if (!match.buildOutput) {
match.buildOutput = {};
}
const {
builderWithPkg: { builder, package: pkg }
} = match;
Expand Down Expand Up @@ -106,18 +102,20 @@ export async function executeBuild(
let result: BuildResult;
try {
devServer.applyBuildEnv(nowJson);
let result = await builder.build({
const r = await builder.build({
files,
entrypoint,
workPath,
config,
meta: { isDev: true, requestPath }
});
if (!result.output) {
// `BuilderOutputs` map was returned
result = { output: result as BuilderOutputs };
if (r.output) {
result = r as BuildResult;
} else {
// `BuilderOutputs` map was returned (Now Builder v1 behavior)
result = { output: r as BuilderOutputs };
}
outputs = result.output as BuilderOutputs;
outputs = result.output;

if (typeof builder.prepareCache === 'function') {
const cachePath = getWorkPath();
Expand Down Expand Up @@ -185,6 +183,7 @@ export async function executeBuild(
})
);

match.buildResults.set(requestPath, result);
Object.assign(match.buildOutput, outputs);
}

Expand All @@ -202,12 +201,21 @@ export async function getBuildMatches(
// of Now deployments.
src = src.substring(1);
}

// TODO: use the `files` map from DevServer instead of hitting the filesystem
const entries = Object.values(await collectProjectFiles(src, cwd));

for (const fileRef of entries) {
src = relative(cwd, fileRef.fsPath);
const builderWithPkg = await getBuilder(use);
matches.push({ ...buildConfig, src, builderWithPkg });
matches.push({
...buildConfig,
src,
builderWithPkg,
buildOutput: {},
buildResults: new Map(),
buildTimestamp: 0
});
}
}
return matches;
Expand Down
7 changes: 3 additions & 4 deletions src/commands/dev/lib/dev-router.ts
Expand Up @@ -28,8 +28,7 @@ export function resolveRouteParameters(
export default async function(
reqPath = '',
routes?: RouteConfig[],
devServer?: DevServer,
files?: BuilderInputs
devServer?: DevServer
): Promise<RouteResult> {
let found: RouteResult | undefined;
const { pathname: reqPathname = '/', query } = url.parse(reqPath, true);
Expand All @@ -41,8 +40,8 @@ export default async function(
idx++;
let { src, headers, handle } = routeConfig;
if (handle) {
if (handle === 'filesystem' && devServer && files) {
if (await devServer.hasFilesystem(files, reqPathname)) {
if (handle === 'filesystem' && devServer) {
if (await devServer.hasFilesystem(reqPathname)) {
break;
}
}
Expand Down

0 comments on commit defb2f1

Please sign in to comment.