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

Migrating to input/output facade #120

Merged
merged 2 commits into from
Dec 10, 2019
Merged
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
54 changes: 26 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'use strict';

const fs = require('fs-extra');
const path = require('path-posix');
const walkSync = require('walk-sync');
const Minimatch = require('minimatch').Minimatch;
const arrayEqual = require('array-equal');
const Plugin = require('broccoli-plugin');
const symlinkOrCopy = require('symlink-or-copy');
const debug = require('debug');
const FSTree = require('fs-tree-diff');
const BlankObject = require('blank-object');
Expand Down Expand Up @@ -65,7 +62,7 @@ class Funnel extends Plugin {
this[key] = options[key];
}

this.destDir = this.destDir || '/';
this.destDir = this.destDir || './';
this.count = 0;

if (this.files && typeof this.files === 'function') {
Expand Down Expand Up @@ -189,7 +186,7 @@ class Funnel extends Plugin {
}
}

let inputPathExists = fs.existsSync(inputPath);
let inputPathExists = this.input.existsSync(this.srcDir || './');

let linkedRoots = false;
if (this.shouldLinkRoots()) {
Expand All @@ -210,7 +207,7 @@ class Funnel extends Plugin {
*/

// This is specifically looking for broken symlinks.
let outputPathExists = fs.existsSync(this.outputPath);
let outputPathExists = this.output.existsSync('./');

// Doesn't count as a rebuild if there's not an existing outputPath.
this._isRebuild = this._isRebuild && outputPathExists;
Expand All @@ -221,24 +218,24 @@ class Funnel extends Plugin {
// Already works because of symlinks. Do nothing.
} else if (!inputPathExists && this.allowEmpty) {
// Make sure we're safely using a new outputPath since we were previously symlinked:
fs.removeSync(this.outputPath);
this.output.rmdirSync('./', { recursive: true });
// Create a new empty folder:
fs.mkdirSync(this.destPath, { recursive: true });
this.output.mkdirSync(this.destDir, { recursive: true });
} else { // this._isRebuild && !inputPathExists && !this.allowEmpty
// Need to remove it on the rebuild.
// Can blindly remove a symlink if path exists.
fs.removeSync(this.outputPath);
this.output.rmdirSync('./', { recursive: true });
}
} else { // Not a rebuild.
if (inputPathExists) {
// We don't want to use the generated-for-us folder.
// Instead let's remove it:
fs.removeSync(this.outputPath);
this.output.rmdirSync('./', { recursive: true });
// And then symlinkOrCopy over top of it:
this._copy(inputPath, this.destPath);
this._copy(inputPath, this.destPath, this.destDir);
} else if (!inputPathExists && this.allowEmpty) {
// Can't symlink nothing, so make an empty folder at `destPath`:
fs.mkdirSync(this.destPath, { recursive: true });
this.output.mkdirSync(this.destDir, { recursive: true });
} else { // !this._isRebuild && !inputPathExists && !this.allowEmpty
throw new Error(`You specified a \`"srcDir": ${this.srcDir}\` which does not exist and did not specify \`"allowEmpty": true\`.`);
}
Expand All @@ -253,7 +250,7 @@ class Funnel extends Plugin {
} else { // !inputPathExists && this.allowEmpty
// Just make an empty folder so that any downstream consumers who don't know
// to ignore this on `allowEmpty` don't get trolled.
fs.mkdirSync(this.destPath, { recursive: true });
this.output.mkdirSync(this.destDir, { recursive: true });
}

this._debug('build, %o', {
Expand Down Expand Up @@ -308,9 +305,9 @@ class Funnel extends Plugin {
} else {

if (this._matchedWalk) {
entries = walkSync.entries(inputPath, { globs: this.include, ignore: this.exclude });
entries = this.input.entries('.', { globs: this.include, ignore: this.exclude });
} else {
entries = walkSync.entries(inputPath);
entries = this.input.entries('.');
}

entries = this._processEntries(entries);
Expand Down Expand Up @@ -355,15 +352,15 @@ class Funnel extends Plugin {
case 'unlink' :
stats.unlink++;

fs.unlinkSync(outputPath);
this.output.unlinkSync(outputRelative);
break;
case 'rmdir' :
stats.rmdir++;
fs.rmdirSync(outputPath);
this.output.rmdirSync(outputRelative);
break;
case 'mkdir' :
stats.mkdir++;
fs.mkdirSync(outputPath);
this.output.mkdirSync(outputRelative);
break;
case 'change':
stats.change++;
Expand Down Expand Up @@ -462,25 +459,26 @@ class Funnel extends Plugin {
throw new Error(`Pattern \`${pattern}\` was not a RegExp, Glob, or Function.`);
}

processFile(sourcePath, destPath /*, relativePath */) {
this._copy(sourcePath, destPath);
processFile(sourcePath, destPath, relativePath) {
this._copy(sourcePath, destPath, relativePath);
}

_copy(sourcePath, destPath) {
let destDir = path.dirname(destPath);
_copy(sourcePath, destPath, relativePath) {
if (this.getDestinationPath) {
relativePath = this.lookupDestinationPath(relativePath);
}
let destDir = path.dirname(relativePath);

try {
symlinkOrCopy.sync(sourcePath, destPath);
this.output.symlinkOrCopySync(sourcePath, relativePath);
} catch (e) {
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
this.output.mkdirSync(destDir, { recursive: true });
try {
fs.unlinkSync(destPath);
this.output.unlinkSync(relativePath);
} catch (e) {
// swallow the error
}
symlinkOrCopy.sync(sourcePath, destPath);
this.output.symlinkOrCopySync(sourcePath, relativePath);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"array-equal": "^1.0.0",
"blank-object": "^1.0.1",
"broccoli-plugin": "^3.0.0",
"broccoli-plugin": "^4.0.0",
"debug": "^4.1.1",
"fast-ordered-set": "^1.0.0",
"fs-tree-diff": "^2.0.1",
Expand Down
38 changes: 23 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,22 @@ broccoli-node-info@^2.1.0:
resolved "https://registry.yarnpkg.com/broccoli-node-info/-/broccoli-node-info-2.1.0.tgz#ca84560e8570ff78565bea1699866ddbf58ad644"
integrity sha512-l6qDuboJThHfRVVWQVaTs++bFdrFTP0gJXgsWenczc1PavRVUmL1Eyb2swTAXXMpDOnr2zhNOBLx4w9AxkqbPQ==

broccoli-output-wrapper@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/broccoli-output-wrapper/-/broccoli-output-wrapper-2.0.0.tgz#f1e0b9b2f259a67fd41a380141c3c20b096828e6"
integrity sha512-V/ozejo+snzNf75i/a6iTmp71k+rlvqjE3+jYfimuMwR1tjNNRdtfno+NGNQB2An9bIAeqZnKhMDurAznHAdtA==
broccoli-output-wrapper@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/broccoli-output-wrapper/-/broccoli-output-wrapper-3.1.1.tgz#d5f2c04b1259fd1df72e4d9951df2d23f202d301"
integrity sha512-iIA5EJ7jmG7rmAFerJ1Fi57L7NICN6ErVhw8ClLDVBrLQYXK0uWicwXL2/2HWlGIL0oFFrIiw+/W0BNVib6DSA==
dependencies:
fs-extra "^8.1.0"
heimdalljs-logger "^0.1.10"
symlink-or-copy "^1.2.0"

broccoli-plugin@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/broccoli-plugin/-/broccoli-plugin-3.1.0.tgz#54ba6dd90a42ec3db5624063292610e326b1e542"
integrity sha512-7w7FP8WJYjLvb0eaw27LO678TGGaom++49O1VYIuzjhXjK5kn2+AMlDm7CaUFw4F7CLGoVQeZ84d8gICMJa4lA==
broccoli-plugin@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/broccoli-plugin/-/broccoli-plugin-4.0.0.tgz#79e6912b01b6d48c1f384013c04b7cd37e0c874a"
integrity sha512-Zi21TkITZKqVB/w1ksVyOqDK5WwK3wbCOyXcF8y2EH+Smfd/oW7deqeeFRfDpdz4cTeRGfLM9lLeyys+YuL9hA==
dependencies:
broccoli-node-api "^1.6.0"
broccoli-output-wrapper "^2.0.0"
broccoli-output-wrapper "^3.1.0"
fs-merger "^3.0.1"
promise-map-series "^0.2.1"
quick-temp "^0.1.3"
Expand Down Expand Up @@ -800,7 +802,7 @@ fast-json-stable-stringify@^2.0.0:
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
integrity sha1-1RQsDK7msRifh9OnYREGT4bIu/I=

fast-levenshtein@~2.0.4:
fast-levenshtein@~2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=
Expand Down Expand Up @@ -2000,6 +2002,7 @@ require-directory@^2.1.1:
integrity sha1-jGStX9MNqxyXbiNE/+f3kqam30I=

require-main-filename@^2.0.0, restore-cursor@^2.0.0:
name require-main-filename
version "2.0.0"
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf"
integrity sha1-n37ih/gv0ybU/RYpI9YhKe7g368=
Expand Down Expand Up @@ -2335,6 +2338,11 @@ symlink-or-copy@^1.0.0, symlink-or-copy@^1.1.8:
resolved "https://registry.yarnpkg.com/symlink-or-copy/-/symlink-or-copy-1.2.0.tgz#5d49108e2ab824a34069b68974486c290020b393"
integrity sha512-W31+GLiBmU/ZR02Ii0mVZICuNEN9daZ63xZMPDsYgPgNjMtg+atqLEGI7PPI936jYSQZxoLb/63xos8Adrx4Eg==

symlink-or-copy@^1.2.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/symlink-or-copy/-/symlink-or-copy-1.3.0.tgz#a034398f11408ef083f51e0f7433040b6123ebf0"
integrity sha512-czheMn4hgCWK/nxqtm9On5JgUrl5KBPCdXYWZjvwiRQ4NA7XhdHY2GSASxQoVh+mbXPMoGcR7/MnngLtUZij4Q==

table@^5.2.3:
version "5.4.6"
resolved "https://registry.yarnpkg.com/table/-/table-5.4.6.tgz#1292d19500ce3f86053b05f0e8e7e4a3bb21079e"
Expand Down Expand Up @@ -2545,16 +2553,16 @@ [email protected]:
dependencies:
string-width "^1.0.2 || 2"

word-wrap@~1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"
integrity sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==

wordwrap@~0.0.2:
version "0.0.3"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"
integrity sha1-o9XabNXAvAAI03I0u68b7WMFkQc=

wordwrap@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus=

wrap-ansi@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-5.1.0.tgz#1fd1f67235d5b6d0fee781056001bfb694c03b09"
Expand Down