forked from electrode-io/electrode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request electrode-io#154 from electrode/flow
RFC: output flow declaration files.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
packages/electrode-archetype-react-component/scripts/copy-as-flow-declaration.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"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 | ||
* | ||
* Searches all .js|.jsx files in `src` and if it contains a `@flow` pragma, | ||
* will copy the original contents out to `lib/{file}.js.flow` accordingly. | ||
*/ | ||
|
||
const IS_VERBOSE = process.argv.some((arg) => arg === "--verbose" || arg === "-v"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const cwd = process.cwd(); | ||
const input = path.join(cwd, "src"); | ||
|
||
/* 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. | ||
function log() { | ||
if (IS_VERBOSE) { | ||
console.log.apply(console, arguments); | ||
} | ||
} | ||
/* eslint-enable */ | ||
|
||
const copyFile = (dir, filename) => { | ||
const stream = fs.createReadStream(path.join(dir, filename)); | ||
const destPath = path.join(dir.replace("src", "lib"), 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 copyDir = (dir) => { | ||
fs.readdir(dir, (err, data) => { | ||
if (err) { | ||
throw err; | ||
} | ||
data.forEach(handleFile(dir)); | ||
}); | ||
}; | ||
|
||
copyDir(input); | ||
|