Skip to content

Commit

Permalink
test: added more flow type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rpl committed Aug 4, 2016
1 parent 246f60f commit 8068adb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
51 changes: 43 additions & 8 deletions src/cmd/build.js
Expand Up @@ -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<ExtensionBuildResult>;

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<ExtensionBuildResult> {
manifestData = await new Promise(
(resolve) => {
if (manifestData) {
Expand Down Expand Up @@ -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<ExtensionBuildResult> {
const rebuildAsNeeded = asNeeded; // alias for `build --as-needed`
log.info(`Building web extension from ${sourceDir}`);

Expand Down
9 changes: 9 additions & 0 deletions src/util/manifest.js
Expand Up @@ -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}`);
Expand Down
36 changes: 33 additions & 3 deletions src/watcher.js
Expand Up @@ -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();

Expand All @@ -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);
Expand Down

0 comments on commit 8068adb

Please sign in to comment.