Skip to content

Commit

Permalink
Release 1.2.1
Browse files Browse the repository at this point in the history
Cleaned up project structure.
  • Loading branch information
thomas-darling committed Feb 1, 2017
1 parent c358d45 commit 7d12ee0
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 87 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-dependents",
"version": "1.2.0",
"version": "1.2.1",
"description": "Gulp plugin that tracks dependencies between files and adds any files that depend on the files currently in the stream, thus enabling incremental build of pcss, less, scss, sass, with extensibility points to support other file types.",
"keywords": [
"gulp",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/**
* Represents a configuration for a DependencyParser, specifying how it should parse the file type with which it is associated.
*/
export default class DependencyParserConfig
export interface IDependencyParserConfig
{
/**
* The sequence of RegExps and/or functions to use when parsing dependency paths from a source file.
* Each RegExp must have the "gm" modifier and at least one capture group. Each function must accept a string and return
* an array of captured strings. The strings captured by each RegExp or function will be passed to the next, thus iteratively
* reducing the file content to an array of dependency file paths.
*/
public parserSteps: (RegExp|((text: string) => string[]))[];
parserSteps: (RegExp|((text: string) => string[]))[];

/**
* The file name prefixes to try when looking for dependency files, if the syntax does not require them to be specified
* in dependency statements. This could be e.g. "_", which is often used as a naming convention for mixin files.
*/
public prefixes: string[];
prefixes: string[];

/**
* The file name postfixes to try when looking for dependency files, if the syntax does not require it to be specified
* in dependency statements. This could be e.g. the file name extension, which is often allowed to be omitted.
*/
public postfixes: string[];
postfixes: string[];

/**
* The additional base paths to try when looking for dependency files referenced using relative paths, if the compiler
* allows alternative base paths to be specified.
*/
public basePaths: string[];
}
basePaths: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as util from "gulp-util";
/**
* Represents a parser that extracts dependency file paths from a file.
*/
interface IDependencyParser
export interface IDependencyParser
{
/**
* Parses the specified file, returning the set dependency file paths on which it depends.
Expand All @@ -13,6 +13,3 @@ interface IDependencyParser
*/
getDependencyFilePaths(file: util.File, encoding: string): string[];
}

// TODO: Workaround for TypeScript limitation. See: https://github.com/Microsoft/TypeScript/issues/3914
export default IDependencyParser;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as util from "gulp-util";
import * as path from "path";
import * as fs from "fs";
import IDependencyParser from "./IDependencyParser";
import DependencyParserConfig from "./DependencyParserConfig";
import {IDependencyParser} from "../dependency-parser";
import {IDependencyParserConfig} from "../dependency-parser-config";

/**
* Represents the default configuration for the dependency tracker, specifying
Expand Down Expand Up @@ -63,7 +63,7 @@ export const defaultConfig =
/**
* Represents a parser that extracts dependency file paths from a file.
*/
export default class DependencyParser implements IDependencyParser
export class DependencyParser implements IDependencyParser
{
/**
* The configuration describing how files should be parsed.
Expand Down Expand Up @@ -92,7 +92,7 @@ export default class DependencyParser implements IDependencyParser
public getDependencyFilePaths(file: util.File, encoding: string): string[]
{
// Get the configuration for the file type.
const config = this.config[path.extname(file.path).toLowerCase()] as DependencyParserConfig;
const config = this.config[path.extname(file.path).toLowerCase()] as IDependencyParserConfig;

// Ignore file types for which we have no config.
if (!config)
Expand Down Expand Up @@ -147,7 +147,7 @@ export default class DependencyParser implements IDependencyParser
* @param config The parser config for the file type being parsed.
* @return The set of paths specified in the files dependency statements.
*/
private parseFile(file: util.File, config: DependencyParserConfig): string[]
private parseFile(file: util.File, config: IDependencyParserConfig): string[]
{
// Read the file contents as a string.
const fileContents = file.contents.toString();
Expand Down Expand Up @@ -215,7 +215,7 @@ export default class DependencyParser implements IDependencyParser
* @param config The parser config for the file type being parsed.
* @return A list of prefixed path variants.
*/
private getPrefixedPathVariants(dependencyPaths: string[], config: DependencyParserConfig): string[]
private getPrefixedPathVariants(dependencyPaths: string[], config: IDependencyParserConfig): string[]
{
let variants: string[] = [];

Expand All @@ -237,7 +237,7 @@ export default class DependencyParser implements IDependencyParser
* @param config The parser config for the file type being parsed.
* @return A list of path variants, with default file names appended to each folder path.
*/
private getPostfixedPathVariants(dependencyPaths: string[], config: DependencyParserConfig): string[]
private getPostfixedPathVariants(dependencyPaths: string[], config: IDependencyParserConfig): string[]
{
let variants: string[] = [];

Expand All @@ -259,7 +259,7 @@ export default class DependencyParser implements IDependencyParser
* @param config The parser config for the file type being parsed.
* @return A list of postfixed path variants.
*/
private getBasePathVariants(dependencyPaths: string[], config: DependencyParserConfig): string[]
private getBasePathVariants(dependencyPaths: string[], config: IDependencyParserConfig): string[]
{
let variants: string[] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as util from "gulp-util";
/**
* Represents a dependency tracker, which tracks the files that pass through and the dependencies between them.
*/
interface IDependencyTracker
export interface IDependencyTracker
{
/**
* Updates the dependency map, returning the set of files that depend on the specified file.
Expand All @@ -22,11 +22,8 @@ interface IDependencyTracker

/**
* Logs the state of the dependency map to the console.
* Note that this lists only dependencies and their dependents; files without dependencies
* Note that this lists only dependencies and their dependents; files without dependencies
* are not listed, except as dependents, even though they are in fact tracked.
*/
logDependencyMap(): void;
}

// TODO: Workaround for TypeScript limitation. See: https://github.com/Microsoft/TypeScript/issues/3914
export default IDependencyTracker;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as util from "gulp-util";
import * as path from "path";
import * as fs from "fs";
import IDependencyTracker from "./IDependencyTracker";
import IDependencyParser from "./IDependencyParser";
import {IDependencyTracker} from "../dependency-tracker";
import {IDependencyParser} from "../../dependency-parser/dependency-parser";

/**
* Represents a dependency tracker, which tracks the files that pass through and the dependencies between them.
Expand All @@ -17,7 +17,7 @@ import IDependencyParser from "./IDependencyParser";
* in the cache or which have changed compared to the cached version. Alternatively, you may use the 'gulp-watch'
* plugin, which creates an infinite stream that initially pass through all files, and after that, only changes.
*/
export default class DependencyTracker implements IDependencyTracker
export class DependencyTracker implements IDependencyTracker
{
/**
* The parser used to extract dependency file paths from files.
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Plugin from "./plugin/Plugin";
import {plugin} from "./plugin/Plugin";

// Export the plugin function that will be used in the gulp pipeline.
export = Plugin.create;
export = plugin;
102 changes: 47 additions & 55 deletions src/plugin/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,67 @@
import * as util from "gulp-util";
import * as path from "path";
import * as through from "through2";
import PluginConfig from "./PluginConfig";
import DependencyParser from "./DependencyParser";
import DependencyTracker from "./DependencyTracker";
import {IPluginConfig} from "./plugin-config";
import {DependencyParser} from "../core/dependency-parser/implementation/dependency-parser";
import {DependencyTracker} from "../core/dependency-tracker/implementation/dependency-tracker";

// The static dependency tracker instance.
let dependencyTracker: DependencyTracker;

/**
* Represents the plugin.
* Creates a new instance of the plugin.
* @param parserConfig The parser configuration use, null, undefined or empty string to use the default configuration, or an instance of a custom IDependencyParser.
* @param pluginConfig The debug configuration use, or null or undefined to disable all debug options.
*/
export default class Plugin
export function plugin(parserConfig?: {}, pluginConfig?: IPluginConfig): NodeJS.ReadWriteStream
{
/**
* The static dependency tracker instance.
*/
private static dependencyTracker: DependencyTracker;
// Get or create the debug options.
if (!pluginConfig)
{
pluginConfig = {};
}

/**
* Creates a new instance of the plugin.
* @param parserConfig The parser configuration use, null, undefined or empty string to use the default configuration, or an instance of a custom IDependencyParser.
* @param pluginConfig The debug configuration use, or null or undefined to disable all debug options.
*/
public static create(parserConfig?: {}, pluginConfig?: PluginConfig): NodeJS.ReadWriteStream
// Get or create the dependency parser and tracker.

if (dependencyTracker == null)
{
// Get or create the debug options.
if (!pluginConfig)
{
pluginConfig = new PluginConfig();
}
dependencyTracker = new DependencyTracker(new DependencyParser(parserConfig));
}

// Get or create the dependency parser and tracker.
// Return the stream transform.
return through.obj(function (file: util.File, encoding: string, callback: (err?: any, data?: any) => void)
{
// Get the files that depend on the current file.
let dependentFiles = dependencyTracker.updateAndGetDependents(file, encoding);

if (Plugin.dependencyTracker == null)
// Should we log the dependents to the console?
if (dependentFiles != null && pluginConfig.logDependents)
{
Plugin.dependencyTracker = new DependencyTracker(new DependencyParser(parserConfig));
dependencyTracker.logDependents(path.normalize(file.path), true, process.cwd());
}

// Return the stream transform.
return through.obj(function (file: util.File, encoding: string, callback: (err?: any, data?: any) => void)
{
// Get the files that depend on the current file.
let dependentFiles = Plugin.dependencyTracker.updateAndGetDependents(file, encoding);

// Should we log the dependents to the console?
if (dependentFiles != null && pluginConfig.logDependents)
{
Plugin.dependencyTracker.logDependents(path.normalize(file.path), true, process.cwd());
}

// Push the current file to the stream.
this.push(file);
// Push the current file to the stream.
this.push(file);

// If the current file is tracked, add its dependents to the stream.
if (dependentFiles != null)
// If the current file is tracked, add its dependents to the stream.
if (dependentFiles != null)
{
for (let dependentFile of dependentFiles)
{
for (let dependentFile of dependentFiles)
{
this.push(dependentFile);
}
this.push(dependentFile);
}
}

callback();
},
function (callback)
callback();
},
function (callback)
{
// Should we log the dependency map to the console?
if (pluginConfig.logDependencyMap)
{
// Should we log the dependency map to the console?
if (pluginConfig.logDependencyMap)
{
Plugin.dependencyTracker.logDependencyMap(process.cwd());
}
dependencyTracker.logDependencyMap(process.cwd());
}

callback();
});
}
}
callback();
});
}
6 changes: 3 additions & 3 deletions src/plugin/PluginConfig.ts → src/plugin/plugin-config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* Represents the plugin configuration.
*/
export default class PluginConfig
export interface IPluginConfig
{
/**
* True to log the dependents of each file to the console; otherwise false.
*/
public logDependents: boolean;
logDependents?: boolean;

/**
* True to log the state of the dependency map to the console at the end of the stream; otherwise false.
*/
public logDependencyMap: boolean;
logDependencyMap?: boolean;
}

0 comments on commit 7d12ee0

Please sign in to comment.