Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
feat: An ability to create context in Sparky (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchanged committed Nov 30, 2017
1 parent efa3ffe commit afca694
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "fuse-box",
"version": "3.0.0-next.26",
"version": "3.0.0-next.27",
"description": "Fuse-Box a bundler that does it right",
"typings": "index.d.ts",
"main": "index.js",
Expand Down
11 changes: 11 additions & 0 deletions src/Utils.ts
Expand Up @@ -189,6 +189,17 @@ export function hashString(text: string) {
let data = hash >>> 0;
return data.toString(16);
}
export function isClass(obj) {
const isCtorClass = obj.constructor
&& obj.constructor.toString().substring(0, 5) === 'class'
if (obj.prototype === undefined) {
return isCtorClass
}
const isPrototypeCtorClass = obj.prototype.constructor
&& obj.prototype.constructor.toString
&& obj.prototype.constructor.toString().substring(0, 5) === 'class'
return isCtorClass || isPrototypeCtorClass
}

export function fastHash(text: string) {
let hash = 0;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -43,6 +43,7 @@ export { OptimizeJSPlugin } from "./plugins/OptimizeJSPlugin";
export { Fluent } from "./arithmetic/Fluent";
export { FuseBox } from "./core/FuseBox";
export { Sparky } from "./sparky/Sparky";
export { SparkyContext } from './sparky/SparkyContext';
export { CLI } from "./cli/Cli";
export { CSSModules } from "./plugins/stylesheet/CSSModules";
export { CopyPlugin } from "./plugins/CopyPlugin";
Expand Down
10 changes: 9 additions & 1 deletion src/sparky/Sparky.ts
Expand Up @@ -4,6 +4,7 @@ import { SparkyFilePatternOptions } from "./SparkyFilePattern";
import { each } from "realm-utils";
import { WorkFlowContext } from "../core/WorkflowContext";
import { Log } from "../Log";
import { SparkyContext, SparkyContextClass, SparkyCurrentContext } from './SparkyContext';

const context = new WorkFlowContext();
context.doLog = process.env.SPARKY_LOG !== 'false';
Expand Down Expand Up @@ -39,6 +40,13 @@ export class Sparky {
return this;
}

public static context(target:
() => { [key: string]: any } |
(new () => any) |
{ [key: string]: any }): SparkyContextClass {
return SparkyContext(target);
}

public static src(glob: string | string[], opts?: SparkyFilePatternOptions): SparkFlow {
const flow = new SparkFlow();
let globs = Array.isArray(glob) ? glob : [glob]
Expand Down Expand Up @@ -66,7 +74,7 @@ export class Sparky {
// resolve waterfal dependencies
each(task.waterfallDependencies, name => this.resolve(name))
]).then(() => {
return this.execute(task.fn());
return this.execute(task.fn(SparkyCurrentContext));
});
}

Expand Down
23 changes: 23 additions & 0 deletions src/sparky/SparkyContext.ts
@@ -0,0 +1,23 @@
import { utils } from "realm-utils";
import { isClass } from '../Utils';
export let SparkyCurrentContext;

export class SparkyContextClass {
constructor(public target: any) {}
}
export function SparkyContext(
target:
() => { [key: string]: any } |
(new () => any) |
{ [key: string]: any }
) {
if (utils.isPlainObject(target)) {
SparkyCurrentContext = target;
} else if (isClass(target)) {
const Class: any = target;
SparkyCurrentContext = new Class();
} else if (utils.isFunction(target)) {
SparkyCurrentContext = target();
}
return new SparkyContextClass(SparkyCurrentContext);
}
7 changes: 7 additions & 0 deletions src/sparky/index.ts
@@ -0,0 +1,7 @@
import { Sparky } from './Sparky';
import { SparkyContext } from './SparkyContext';

export const src = Sparky.src;
export const watch = Sparky.watch;
export const task = Sparky.task;
export const context = SparkyContext;

0 comments on commit afca694

Please sign in to comment.