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

Add Typescript Support #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true


[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.hbs]
insert_final_newline = false

[*.{diff,md}]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
tmp
dist
14 changes: 0 additions & 14 deletions .eslintrc

This file was deleted.

39 changes: 39 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

module.exports = {
root: true,
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2018,
sourceType: "module",
},
plugins: ["@typescript-eslint"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
],
env: {
node: true,
},
rules: {
"quotes": "off", // note you must disable the base rule as it can report incorrect errors
"no-unused-vars": "off", // note you must disable the base rule as it can report incorrect errors
"@typescript-eslint/quotes": ["error", "double"],
"@typescript-eslint/no-use-before-define": "off",
},
overrides: [
{
files: ["test/**/*.js"],
plugins: ["mocha"],
env: {
mocha: true,
},
rules: {
"mocha/no-exclusive-tests": "error",
"mocha/handle-done-callback": "error",
"@typescript-eslint/no-var-requires": "off"
},
},
],
};
12 changes: 9 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
node_modules
test/tmp
tmp
# jetbrains
/.idea/
/*.iml

# other
/node_modules
/test/tmp
/tmp
/dist
51 changes: 0 additions & 51 deletions index.js

This file was deleted.

33 changes: 23 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "Broccoli plugin to merge multiple trees into one",
"version": "4.2.0",
"author": "Jo Liss <[email protected]>",
"main": "index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -14,27 +15,39 @@
"merge",
"copy"
],
"files": [
"index.js"
],
"engines": {
"node": "10.* || >= 12.*"
"scripts": {
"build": "tsc",
"clean": "rm -rf dist",
"lint": "eslint src test",
"lint:fix": "eslint --fix src test",
"test": "yarn build && mocha",
"test:debug": "yarn build && mocha --inspect-brk",
"watch": "tsc --watch"
},
"dependencies": {
"broccoli-plugin": "^4.0.2",
"merge-trees": "^2.0.0"
},
"devDependencies": {
"@types/node": "12.12.6",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
"broccoli-fixture": "^1.0.0",
"broccoli-node-api": "^1.7.0",
"broccoli-test-helper": "^2.0.0",
"chai": "^4.1.2",
"eslint": "^6.8.0",
"eslint-plugin-mocha": "^6.3.0",
"mocha": "^7.1.1",
"mocha-eslint": "^6.0.0"
"mocha-eslint": "^6.0.0",
"typescript": "^3.8.3"
},
"scripts": {
"test": "mocha",
"test:debug": "mocha debug"
"engines": {
"node": "10.* || >= 12.*"
},
"files": [
"src/index.ts"
],
"volta": {
"node": "12.16.1",
"yarn": "1.22.4"
Expand Down
74 changes: 74 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Plugin from "broccoli-plugin";
import {default as NodeMergeTrees} from "merge-trees";
import {InputNode} from "broccoli-node-api";

export interface Options {
/**
* By default, broccoli-merge-trees throws an error when a file exists in multiple nodes.
* If you pass { overwrite: true }, the output will contain the version of the file as it exists in
* the last input node that contains it.
*/
overwrite?: boolean;
/**
* A note to help tell multiple plugin instances apart.
*/
annotation?: string;
/**
* A string representing the destination path that merged files will be copied to.
*/
destDir?: string;
}

/**
* Copy multiple trees of files on top of each other, resulting in a single merged tree.
*/
export class BroccoliMergeTrees extends Plugin {

private readonly inputNodes: InputNode[];
private readonly options: Options;
private mergeTrees: NodeMergeTrees | null;

constructor(inputNodes: InputNode[], options: Options = {}) {
if (!Array.isArray(inputNodes)) {
const name = "broccoli-merge-trees:" + (options.annotation ?? "");
throw new TypeError(`${name}: Expected array, got: [${inputNodes}]`);
}
super(inputNodes, {
persistentOutput: true,
needsCache: false,
annotation: options.annotation
});

this.inputNodes = inputNodes;
this.options = options;
this.mergeTrees = null;
}

build(): void {
if (this.mergeTrees === null) {
// Defer instantiation until the first build because we only
// have this.inputPaths and this.outputPath once we build.
const outputPath = `${this.outputPath}${this.options.destDir ? "/" + this.options.destDir : ""}`;
this.mergeTrees = new NodeMergeTrees(this.inputPaths, outputPath, {
overwrite: this.options.overwrite ?? false,
annotation: this.options.annotation
});
}

try {
this.mergeTrees.merge();
} catch (err) {
if (err !== null && typeof err === "object") {
const nodesList = this.inputNodes.map((node, i) => ` ${i + 1}. ${node.toString()}`).join("\n");
const message = `${this.toString()} error while merging the following:\n${nodesList}`;

err.message = `${message}\n${err.message}`;
}
throw err;
}
}
}

export default function mergeTrees(inputNodes: InputNode[], options: Options = {}): BroccoliMergeTrees {
return new BroccoliMergeTrees(inputNodes, options);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading