-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move js dep walking into a module, add tests
- Loading branch information
Showing
12 changed files
with
114 additions
and
37 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
test/mocks/normal | ||
test/mocks/ | ||
test/tmp |
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,34 @@ | ||
let { join } = require('path') | ||
let { sync: rm } = require('rimraf') | ||
let { sync: glob } = require('glob') | ||
let { ignoreDeps } = require('../../lib') | ||
let getRequires = require('./get-requires') | ||
|
||
module.exports = function getDirDeps ({ dir, update }) { | ||
// Clean everything out bebefore we get going jic | ||
rm(join(dir, 'node_modules')) | ||
|
||
// Collection of all dependencies from all files in this directory | ||
let deps = [] | ||
|
||
// Userland files that could not be parsed | ||
let failures = [] | ||
|
||
// Gather ye business logic while ye may | ||
let files = glob('**/*.js', { cwd: dir }).filter(ignoreDeps) | ||
files.forEach(f => { | ||
try { | ||
let requires = getRequires({ dir, file: join(dir, f), update }) | ||
if (requires) deps = deps.concat(requires) | ||
} | ||
catch (error) { | ||
failures.push({ file: join(dir, f), error }) | ||
} | ||
}) | ||
|
||
// Tidy up the dependencies | ||
deps = [ ...new Set(deps.sort()) ] // Dedupe | ||
deps = deps.filter(d => d !== 'aws-sdk') // Already present at runtime | ||
|
||
return { deps, failures, files } | ||
} |
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
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,9 @@ | ||
// Built-ins should be ignored | ||
let path = require('path') | ||
|
||
// AWS-SDK should also be ignored | ||
let aws = require('aws-sdk') | ||
|
||
// Real deps should not be ignored! | ||
// Let's keep it in here with the ignored ones to prove this file was read | ||
let a = require('a') |
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,4 @@ | ||
let options = { ok: true } | ||
let d = require('d')(options) | ||
|
||
let something = [ d, require('e') ] |
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 @@ | ||
// Goose egg |
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 @@ | ||
// Ignore me! |
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,5 @@ | ||
let something = process.env.SOMETHING | ||
require(something) | ||
|
||
// Also require 'c' to test de-duping | ||
require('c') |
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,3 @@ | ||
let something = { | ||
c: require('c') | ||
} |
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,3 @@ | ||
function idk () { | ||
let b = require('b') | ||
} |
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,35 @@ | ||
let { join } = require('path') | ||
let test = require('tape') | ||
let sut = join(process.cwd(), 'src', 'actions', 'autoinstall', 'get-dir-deps') | ||
let getDirDeps = require(sut) | ||
let mock = join(process.cwd(), 'test', 'mocks', 'deps') | ||
let { updater } = require('@architect/utils') | ||
let update = updater('Hydrate') | ||
|
||
test('Set up env', t => { | ||
t.plan(1) | ||
t.ok(getDirDeps, 'Dependency getter module is present') | ||
}) | ||
|
||
test(`Walk a folder's deps`, t => { | ||
t.plan(4) | ||
let stdout = process.stdout.write | ||
let data = '' | ||
process.stdout.write = write => { | ||
data += write | ||
} | ||
let { deps, failures, files } = getDirDeps({ dir: mock, update }) | ||
process.stdout.write = stdout | ||
let correct = [ 'a', 'b', 'c', 'd', 'e' ] | ||
|
||
t.deepEqual(deps.sort(), correct, `Got correct deps`) | ||
console.log(correct) | ||
|
||
t.notOk(failures.length, 'Got no failures') | ||
|
||
t.equal(files.length, 6, 'Scanned walked six js files') | ||
console.log(files.sort()) | ||
|
||
t.ok(data.includes(`'something'`), 'Warned about dynamic require') | ||
console.log(data) | ||
}) |
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