Skip to content

Commit

Permalink
rewrite copy as flow (#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
didi0613 authored and jchip committed Jul 28, 2018
1 parent 35ab702 commit 6012a2a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use strict";

/**
* Support outputting Flow annotated files in node_modules wiht flow declaration
* files as documented in http://flowtype.org/blog/2015/12/01/Version-0.19.0.html
Expand All @@ -7,15 +8,16 @@
* will copy the original contents out to `lib/{file}.js.flow` accordingly.
*/

const IS_VERBOSE = process.argv.some((arg) => arg === "--verbose" || arg === "-v");
const IS_VERBOSE = process.argv.some(arg => arg === "--verbose" || arg === "-v");
const fs = require("fs");
const path = require("path");
const cwd = process.cwd();
const srcRootDir = path.join(cwd, "src");
const destRootDir = path.join(cwd, "lib");
const Bluebird = require("bluebird");
const isMain = require.main === module;
Bluebird.promisifyAll(fs);

/* eslint-disable */
// disable eslint because:
// * it is configured to prefer (...args) over apply, but that is not supported
// in node v4
// * `console` is disallowed, but we're logging.
Expand All @@ -24,40 +26,59 @@ function log() {
console.log.apply(console, arguments);
}
}
/* eslint-enable */

const copyFile = (dir, filename) => {
const destDir = dir.replace(srcRootDir, destRootDir);
const stream = fs.createReadStream(path.join(dir, filename));
const destPath = path.join(destDir, filename.replace(/x$/, ""));
const dest = `${destPath}.flow`;
log("Copying %s to %s", dir.replace(cwd, ".") + path.sep + filename, dest.replace(cwd, "."));
stream.pipe(fs.createWriteStream(dest));
};

const handleFile = (dir) => (filename) => {
const file = path.join(dir, filename);
fs.stat(file, (err, stats) => {
if (err) {
throw err;
}

if (stats.isDirectory()) {
// handleFile recusively calls copyDir
copyDir(path.join(dir, filename)); // eslint-disable-line no-use-before-define
} else if (stats.isFile() && /\.jsx?$/.test(filename)) {
copyFile(dir, filename);
}
const copyFile = filename => {
const dest = `${filename.replace(srcRootDir, destRootDir).replace(/x$/, "")}.flow`;
const readStream = fs.createReadStream(filename);
const writeStream = fs.createWriteStream(dest);

return new Promise((resolve, reject) => {
readStream.on("error", reject);
writeStream.on("error", reject);
writeStream.on("finish", resolve);
log(`Copying ${filename} to ${dest}`);
readStream.pipe(writeStream);
});
};

const copyDir = (dir) => {
fs.readdir(dir, (err, data) => {
if (err) {
throw err;
}
data.forEach(handleFile(dir));
});
const handleFile = filename => {
return fs
.statAsync(filename)
.then(stat => {
if (stat.isFile() && /\.jsx?$/.test(filename)) {
return copyFile(filename);
} else if (stat.isDirectory()) {
return copyDir(filename);
}
})
.catch(err => {
console.error(`Error catched: ${err.message}`);
process.exit(1);
});
};

copyDir(srcRootDir);
const copyDir = (dir = srcRootDir) => {
return fs
.readdirAsync(dir)
.then(items => {
return Bluebird.map(
items,
item => {
item = path.resolve(dir, item);
return handleFile(item);
},
{ concurrency: 10 }
);
})
.then(() => {})
.catch(err => {
console.error(`Error catched: ${err.message}`);
process.exit(1);
});
};

if (isMain) {
copyDir(srcRootDir);
}

module.exports = copyDir;
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Fs = require("fs");
const glob = devRequire("glob");

const flattenMessagesL10n = require(`${archetype.devPath}/scripts/l10n/flatten-messages.js`);
const copyAsFlowDeclaration = require(`${archetype.devPath}/scripts/copy-as-flow-declaration.js`);

if (process.argv[1].indexOf("gulp") >= 0) {
const cmd = chalk.magenta(`clap ${process.argv.slice(2).join(" ")}`);
Expand Down Expand Up @@ -125,15 +126,13 @@ function makeTasks(hostDir) {
"clean-lib",
".tmp-to-lib",
"build-lib:flatten-l10n",
// TODO: fix the badly written and messy copy-as-flow-declaration.js so it can
// deterministically await async complete (and exit if isMain).
// "build-lib:copy-flow",
"build-lib:copy-flow",
"build-lib:clean-tmp"
]
},
"babel-src-step": `babel -D src -d .tmplib`,
"build-lib:clean-tmp": () => $$.rm("-rf", "./tmp"),
"build-lib:copy-flow": `node -r ${archetype.devPath}/scripts/copy-as-flow-declaration.js`,
"build-lib:copy-flow": copyAsFlowDeclaration,
"build-lib:flatten-l10n": flattenMessagesL10n,

"archetype:check": [
Expand Down

0 comments on commit 6012a2a

Please sign in to comment.