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

Added file watcher to now dev #2153

Merged
merged 22 commits into from Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
1 change: 1 addition & 0 deletions .circleci/config.yml
Expand Up @@ -12,6 +12,7 @@ jobs:
- v1-dependencies-{{ checksum "yarn.lock" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
leo marked this conversation as resolved.
Show resolved Hide resolved
- run:
name: Installing Dependencies
command: yarn
Expand Down
52 changes: 52 additions & 0 deletions @types/zeit__nsfw.d.ts
@@ -0,0 +1,52 @@
declare function nsfw(
dir: string,
callback: nsfw.EventsCallback,
options?: nsfw.WatcherOptions,
errorCallback?: (errors: Error[]) => void,
modulePath?: string,
): Promise<nsfw.Watcher>;

declare namespace nsfw {
export class Watcher {
start(): Promise<void>;
stop(): Promise<void>;
}
export interface BaseEvent {
action: nsfw.actions;
directory: string;
};
export interface CreatedEvent extends BaseEvent {
action: nsfw.actions.CREATED;
file: string;
}
export interface DeletedEvent extends BaseEvent {
action: nsfw.actions.DELETED;
file: string;
}
export interface ModifiedEvent extends BaseEvent {
action: nsfw.actions.MODIFIED;
file: string;
}
export interface RenamedEvent extends BaseEvent {
action: nsfw.actions.RENAMED;
oldFile: string;
newDirectory: string;
newFile: string;
}
export type Event = CreatedEvent | DeletedEvent | ModifiedEvent | RenamedEvent;
export type EventsCallback = (events: Event[]) => void;
export interface WatcherOptions {
debouceMS?: number;
errorCallback?(errors: Error[]);
};
export enum actions {
CREATED,
DELETED,
MODIFIED,
RENAMED
};
}

declare module '@zeit/nsfw' {
export = nsfw;
}
9 changes: 5 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": "1.2.6",
"@zeit/source-map-support": "0.6.2",
"alpha-sort": "2.0.1",
"ansi-escapes": "3.0.0",
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;
Copy link
Member

Choose a reason for hiding this comment

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

So this why returning a version would be good because then you wouldn't need to cast. Oh well 🤷‍♂️

} 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