Last active
November 10, 2017 17:06
-
-
Save NervosaX/eadc44f0f7159c43fdb2da35a2d56002 to your computer and use it in GitHub Desktop.
Create flowconfig mapping from jspm
This file contains hidden or 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
#!/usr/bin/env node | |
"use strict"; | |
const _ = require("lodash"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const jspm = require("jspm"); | |
const build = new jspm.Builder(); | |
const System = build.loader; | |
// Generate the path to the root of the project... We can use this to | |
// remove it from paths later. | |
const appRootPath = System.normalizeSync("").replace("file://", ""); | |
const dependencyPaths = Object.keys(System.map) | |
.reduce((deps, importName) => { | |
const dep = System.normalizeSync(importName); | |
if (dep.indexOf("@") === -1) { | |
return deps; | |
} | |
let mappedFiles = []; | |
if (System.packages[dep]) { | |
mappedFiles = Object.keys(System.packages[dep].map) | |
.reduce((mapPackages, mapPackage) => { | |
return mapPackages.concat([ | |
[mapPackage, System.normalizeSync(mapPackage, dep).replace("file://", "")] | |
]); | |
}, []); | |
} | |
return deps.concat([ | |
[importName, dep.replace("file://", "")] | |
]).concat(mappedFiles) | |
}, []); | |
let output = []; | |
for (let i = 0; i < dependencyPaths.length; i++) { | |
const dependency = dependencyPaths[i][0]; | |
const fullPath = dependencyPaths[i][1]; | |
const pkg = require(`${fullPath}.json`); | |
// Generate the root directory for the dependency (minus the filesystem path) | |
const basePath = fullPath.slice(appRootPath.length - 1); | |
// If there is a main path, generate it here (using the json file each jspm package comes with) | |
let mainPath = pkg.main ? path.resolve(fullPath, pkg.main).slice(appRootPath.length - 1) : null; | |
// Sometimes the path does not include the .js extension... so make sure it does. | |
if (mainPath && mainPath.indexOf(".js") === -1) { | |
mainPath += ".js"; | |
} | |
output.push(`module.name_mapper='^${dependency}\\/\\(.*\\)$' -> '<PROJECT_ROOT>${basePath}/\\1'`); | |
if (mainPath) { | |
output.push(`module.name_mapper='^${dependency}$' -> '<PROJECT_ROOT>${mainPath}'`); | |
} | |
} | |
// HACK: It's possible to get lots of projects with the same third party dependency, so | |
// let's remove it here. | |
output = _.uniq(output); | |
const warningMessage = "## AUTOMATICALLY GENERATED -- DO NOT MODIFY"; | |
const flowConfig = fs.readFileSync(path.resolve(".flowconfig"), "utf8"); | |
const flowConfigLines = flowConfig.split("\n"); | |
const warningLine = flowConfigLines.indexOf(warningMessage); | |
let newConfig = flowConfigLines; | |
if (warningLine > -1) { | |
newConfig = flowConfigLines.slice(0, warningLine + 1).concat(output); | |
} else { | |
newConfig = flowConfigLines.concat([warningMessage]).concat(output); | |
} | |
fs.writeFileSync(".flowconfig", newConfig.join("\n")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment