Created
April 22, 2017 17:23
-
-
Save bc-jasond/5e4c487e85a89836c228619c7189e043 to your computer and use it in GitHub Desktop.
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
function getImportsFromFile(fileName) { | |
const readline = require('readline'); | |
const fs = require('fs'); | |
const imports = []; | |
return new Promise((resolve, reject) => { | |
const readStream = fs.createReadStream(fileName); | |
readStream.on('error', (err) => reject(err)); | |
const rl = readline.createInterface({input: readStream}); | |
rl.on('line', (line) => { | |
if (line.indexOf('import') > -1) { | |
const importFileName = line.match(/'(.*?)'/); | |
if (importFileName) { | |
imports.push(importFileName[1]); | |
} | |
} | |
}); | |
rl.on('close', () => { | |
resolve(imports); | |
}) | |
}); | |
} | |
async function getAllDependencies(initialFileName) { | |
const orderedDependencies = []; | |
const workerQueue = [initialFileName]; | |
try { | |
while (workerQueue.length) { | |
const currentFile = workerQueue.shift(); | |
if (orderedDependencies.indexOf(currentFile) === -1) { | |
orderedDependencies.unshift(currentFile); | |
} | |
workerQueue.push(...await getImportsFromFile(currentFile)); | |
} | |
} catch (err) { | |
throw err; | |
} | |
return orderedDependencies; | |
} | |
const cliFileName = process.argv[2]; | |
getAllDependencies(cliFileName) | |
.then((result) => console.log(result)) | |
.catch((err) => console.log(`Derp${err}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment