From 8068adb4944df35a4f0b91d34cda72e2c19cfa7b Mon Sep 17 00:00:00 2001 From: Luca Greco Date: Thu, 4 Aug 2016 15:28:33 +0200 Subject: [PATCH] test: added more flow type checks --- src/cmd/build.js | 51 +++++++++++++++++++++++++++++++++++++------- src/util/manifest.js | 9 ++++++++ src/watcher.js | 36 ++++++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 11 deletions(-) diff --git a/src/cmd/build.js b/src/cmd/build.js index a608b822ce..362047132d 100644 --- a/src/cmd/build.js +++ b/src/cmd/build.js @@ -12,10 +12,43 @@ import {createLogger} from '../util/logger'; const log = createLogger(__filename); +// Flow Types -async function defaultPackageCreator( - {manifestData, sourceDir, fileFilter, artifactsDir}) { +import type {OnSourceChangeFn} from '../watcher'; +import type {ExtensionManifest} from '../util/manifest'; + +export type PackageCreatorFn = + (params: PackageCreatorParams) => Promise; + +export type ExtensionBuildResult = { + extensionPath: string, +}; + +export type PackageCreatorParams = { + manifestData?: ExtensionManifest, + sourceDir: string, + fileFilter: FileFilter, + artifactsDir: string, +}; + +export type BuildCmdParams = { + sourceDir: string, + artifactsDir: string, + asNeeded?: boolean, +}; +export type BuildCmdExtraParams = { + manifestData?: ExtensionManifest, + fileFilter?: FileFilter, + onSourceChange?: OnSourceChangeFn, + packageCreator?: PackageCreatorFn, +}; + +// Private Helpers + +async function defaultPackageCreator( + {manifestData, sourceDir, fileFilter, artifactsDir}: PackageCreatorParams +): Promise { manifestData = await new Promise( (resolve) => { if (manifestData) { @@ -44,14 +77,16 @@ async function defaultPackageCreator( return {extensionPath}; } +// Exports export default async function build( - {sourceDir, artifactsDir, asNeeded=false}: Object, - {manifestData, fileFilter=new FileFilter(), - onSourceChange=defaultSourceWatcher, - packageCreator=defaultPackageCreator} - : Object = {}): Promise { - + {sourceDir, artifactsDir, asNeeded=false}: BuildCmdParams, + { + manifestData, fileFilter=new FileFilter(), + onSourceChange=defaultSourceWatcher, + packageCreator=defaultPackageCreator, + }: BuildCmdExtraParams = {} +): Promise { const rebuildAsNeeded = asNeeded; // alias for `build --as-needed` log.info(`Building web extension from ${sourceDir}`); diff --git a/src/util/manifest.js b/src/util/manifest.js index 2180810cad..3e6649e281 100644 --- a/src/util/manifest.js +++ b/src/util/manifest.js @@ -8,6 +8,15 @@ import {createLogger} from './logger'; const log = createLogger(__filename); +// Flow Types + +export type ExtensionManifest = { + name: string, + version: string, +}; + +// Exports + export default async function getValidatedManifest(sourceDir: string): Promise { let manifestFile = path.join(sourceDir, 'manifest.json'); log.debug(`Validating manifest at ${manifestFile}`); diff --git a/src/watcher.js b/src/watcher.js index bfe5048564..d897ef6481 100644 --- a/src/watcher.js +++ b/src/watcher.js @@ -8,8 +8,38 @@ import {FileFilter} from './cmd/build'; const log = createLogger(__filename); +// Flow Types + +export type ShouldWatchFn = (filePath: string) => boolean; + +export type OnChangeFn = () => any; + +export type OnSourceChangeParams = { + sourceDir: string, + artifactsDir: string, + onChange: OnChangeFn, + shouldWatchFile?: ShouldWatchFn, +}; + +export type ProxyFileChangesParams = { + artifactsDir: string, + onChange: OnChangeFn, + filePath: string, + shouldWatchFile?: ShouldWatchFn, +}; + +export type OnSourceChangeFn = (params: OnSourceChangeParams) => Watchpack; + +// NOTE: this fix an issue with flow and default exports (which currently +// lose their type signatures) by explicitly declare the default export +// signature. Reference: https://github.com/facebook/flow/issues/449 +declare function exports(params: OnSourceChangeParams): Watchpack; // eslint-disable-line no-unused-vars + +// Exports + export default function onSourceChange( - {sourceDir, artifactsDir, onChange, shouldWatchFile}: Object): Watchpack { + {sourceDir, artifactsDir, onChange, shouldWatchFile}: OnSourceChangeParams +): Watchpack { // TODO: For network disks, we would need to add {poll: true}. const watcher = new Watchpack(); @@ -29,9 +59,9 @@ export default function onSourceChange( return watcher; } - export function proxyFileChanges( - {artifactsDir, onChange, filePath, shouldWatchFile}: Object) { + {artifactsDir, onChange, filePath, shouldWatchFile}: ProxyFileChangesParams +) { if (!shouldWatchFile) { const fileFilter = new FileFilter(); shouldWatchFile = (...args) => fileFilter.wantFile(...args);