From 92c80fc68ed52015a434786e12eabd45d180ed5d Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Sat, 16 May 2015 18:20:49 -0400 Subject: [PATCH] Add a simple tool for printing the dependencies This adds a simple command line tool for printing the dependencies of a module. $ ./tval tools/print-dependencies.js src/syntax/Parser.js --- src/node/get-dependencies.js | 47 +++++++++++++++++++++++++++++ test/unit/tools/get-dependencies.js | 41 +++++++++++++++++++++++++ tools/print-dependencies.js | 29 ++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 src/node/get-dependencies.js create mode 100644 test/unit/tools/get-dependencies.js create mode 100644 tools/print-dependencies.js diff --git a/src/node/get-dependencies.js b/src/node/get-dependencies.js new file mode 100644 index 000000000..600ec400a --- /dev/null +++ b/src/node/get-dependencies.js @@ -0,0 +1,47 @@ +// Copyright 2015 Traceur Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import {ModuleSpecifierVisitor} from '../codegeneration/module/ModuleSpecifierVisitor.js'; +import {Parser} from '../syntax/Parser.js'; +import {SourceFile} from '../syntax/SourceFile.js'; + +const {normalize, resolve, dirname} = require('path'); +const {readFileSync} = require('fs'); + +function addDependencies(deps, path) { + path = resolve(path); + if (deps.has(path)) return; + + let content = readFileSync(path, 'utf-8'); + let sourceFile = new SourceFile(path, content); + let parser = new Parser(sourceFile); + let tree = parser.parseModule(); + let options = {}; + let visitor = new ModuleSpecifierVisitor(options); + visitor.visitAny(tree); + + deps.add(path); + for (let spec of visitor.moduleSpecifiers) { + let resolved = resolve(dirname(path), spec); + addDependencies(deps, resolved); + } +} + +export default function getDependencies(...paths) { + let deps = new Set(); + for (let path of paths) { + addDependencies(deps, path); + } + return deps; +} diff --git a/test/unit/tools/get-dependencies.js b/test/unit/tools/get-dependencies.js new file mode 100644 index 000000000..086a78cf2 --- /dev/null +++ b/test/unit/tools/get-dependencies.js @@ -0,0 +1,41 @@ +// Copyright 2015 Traceur Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import {suite, test, assert} from '../../unit/unitTestRunner.js'; +import getDependencies from '../../../src/node/get-dependencies.js'; + +suite('get-dependencies.js', () => { + test('Empty', () => { + let deps = getDependencies(); + assert.equal(deps.size, 0); + }); + + test('Basic', () => { + let path = './test/unit/runtime/resources/test_module.js'; + let deps = getDependencies(path); + assert.equal(deps.size, 4); + let a = [...deps].map((d) => { + d = d.replace(/\\/g, '/'); + let i = d.indexOf('/test/'); + return d.slice(i); + }); + assert.deepEqual(a, [ + '/test/unit/runtime/resources/test_module.js', + '/test/unit/runtime/resources/test_a.js', + '/test/unit/runtime/resources/test_b.js', + '/test/unit/runtime/resources/test_c.js' + ]); + }); + +}); diff --git a/tools/print-dependencies.js b/tools/print-dependencies.js new file mode 100644 index 000000000..2cf8ac5d1 --- /dev/null +++ b/tools/print-dependencies.js @@ -0,0 +1,29 @@ +// Copyright 2015 Traceur Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import getDependencies from '../src/node/get-dependencies.js'; + +function main() { + if (process.argv.length < 3) { + console.error('Usage: ./tval print-deps.js paths...') + return; + } + + let deps = getDependencies(...process.argv.slice(2)); + for (let name of deps) { + console.log(name); + } +} + +main();