Skip to content

Commit

Permalink
feat: use ts and for now allow js
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed May 30, 2019
1 parent 3cfe0e4 commit c7f7af8
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ To release a major you need to add `BREAKING CHANGE: ` to the start of the body

## Code standards

Ensure that your code adheres to the included `.eslintrc` config by running `npm run lint`.
Ensure that your code adheres to the included `tslint.json` config by running `npm run lint`.

## Sending pull requests

Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package-lock.json
dist/
/node_modules/
test/fixtures/testproj*/project/project/
test/fixtures/testproj*/project/target/
test/fixtures/testproj*/target/
test/fixtures/testproj*/target/
1 change: 1 addition & 0 deletions .java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.8
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ node_js:
cache:
directories:
- node_modules
install:
before_install:
# Install a later version of sbt-extras to support SBT >=1.0
- curl -Ls https://git.io/sbt > ~/bin/sbt
- chmod a+x ~/bin/sbt
Expand Down
51 changes: 33 additions & 18 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
# https://www.appveyor.com/docs/appveyor-yml

# to disable automatic builds
build: off
branches:
only:
- master

init:
- git config --global core.autocrlf true
- git config --global core.autocrlf input

shallow_clone: true
clone_depth: 1
install:
- cinst jdk8 -params 'installdir=C:\\jdk8'
- cinst jdk11 -params 'installdir=C:\\jdk11'
- SET JAVA_HOME=C:\jdk8
- SET PATH=C:\jdk8\bin;%PATH%

- ps: |
Add-Type -AssemblyName System.IO.Compression.FileSystem
if (!(Test-Path -Path "C:\sbt" )) {
(new-object System.Net.WebClient).DownloadFile(
'https://piccolo.link/sbt-0.13.18.zip',
'C:\sbt-bin.zip'
)
[System.IO.Compression.ZipFile]::ExtractToDirectory("C:\sbt-bin.zip", "C:\sbt")
}
- SET PATH=C:\sbt\sbt\bin;%PATH%
- SET SBT_OPTS=-Xmx4g -Dfile.encoding=UTF8
- ps: Install-Product node $env:nodejs_version
- node --version
- npm --version
- npm install

cache:
- '%USERPROFILE%\.ivy2\cache'
- '%USERPROFILE%\.sbt'
- node_modules -> package.json


branches:
only:
- master
shallow_clone: true
clone_depth: 1

environment:
matrix:
- nodejs_version: "10"
- nodejs_version: "8"
- nodejs_version: "6"

matrix:
fast_finish: true

install:
- ps: Install-Product node $env:nodejs_version
- node --version
- npm --version
- npm install

test_script:
- npm test
# TODO: fix setu and re-enable
# - npm run test-system
18 changes: 9 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ function inspect(root, targetFile, options) {
const sbtArgs = buildArgs(options.args, useCoursier);

return subProcess.execute('sbt', sbtArgs, {cwd: targetFilePath})
.catch(function (error) {
.catch((error) => {
if (detectedCoursier) {
// if we've tried coursier already, we'll fallback to dependency-graph
// in case we've failed to parse the files correctly #paranoid
useCoursier = false;
const sbtArgs = buildArgs(options.args, useCoursier);
return subProcess.execute('sbt', sbtArgs, {cwd: targetFilePath});
const sbtArgsNoCoursier = buildArgs(options.args, useCoursier);
return subProcess.execute('sbt', sbtArgsNoCoursier, {cwd: targetFilePath});
}
// otherwise cascade the reject
return new Promise((resolve, reject) => {
reject(error);
});
})
.then(function (result) {
.then((result) => {
const packageName = path.basename(root);
const packageVersion = '0.0.0';
const depTree = parser
Expand All @@ -52,8 +52,8 @@ function inspect(root, targetFile, options) {
package: depTree,
};
})
.catch(function (error) {
const dgArgs = '`sbt '+ buildArgs(options.args, false).join(' ') + '`';
.catch((error) => {
const dgArgs = '`sbt ' + buildArgs(options.args, false).join(' ') + '`';
const csArgs = '`sbt ' + buildArgs(options.args, true).join(' ') + '`';
error.message = error.message + '\n\n' +
'Please make sure that the `sbt-dependency-graph` plugin ' +
Expand All @@ -77,7 +77,7 @@ function inspect(root, targetFile, options) {
function coursierPluginInProject(basePath) {
const sbtFileList = sbtFiles(path.join(basePath, 'project'))
.concat(sbtFiles(path.join(basePath, 'project', 'project')));
const searchResults = sbtFileList.map ( function (file) {
const searchResults = sbtFileList.map ((file) => {
return searchWithFs(file);
});
return searchResults.includes(true);
Expand All @@ -86,9 +86,9 @@ function coursierPluginInProject(basePath) {
// provide a list of .sbt files in the specified directory
function sbtFiles(basePath) {
if (fs.existsSync(basePath) && fs.lstatSync(basePath).isDirectory()) {
return fs.readdirSync(basePath).filter(function (fileName) {
return fs.readdirSync(basePath).filter((fileName) => {
return path.extname(fileName) === '.sbt';
}).map(function (file) {
}).map((file) => {
return path.join(basePath, file);
});
}
Expand Down
20 changes: 11 additions & 9 deletions lib/parse-sbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ var tabdown = require('./tabdown');
function convertStrToTree(dependenciesTextTree) {
var lines = dependenciesTextTree.toString().split('\n') || [];
var newLines = lines
.map(function (line) {
.map(function(line) {
return line.replace(/\u001b\[0m/g, '');
})
.filter(function (line) {
.filter(function(line) {
if (line.indexOf('[info] ') === 0 && line.indexOf('+-') > -1) {
return true;
}
Expand All @@ -20,7 +20,7 @@ function convertStrToTree(dependenciesTextTree) {
}
return false;
})
.map(function (line) {
.map(function(line) {
return line
.slice(7, line.length) // slice off '[info] '
.replace(' [S]', '')
Expand All @@ -35,10 +35,10 @@ function convertStrToTree(dependenciesTextTree) {
function convertCoursierStrToTree(dependenciesTextTree) {
const lines = dependenciesTextTree.toString().split('\n') || [];
const newLines = lines
.map(function (line) {
.map((line) => {
return line.replace(/\u001b\[0m/g, '');
})
.filter(function (line) {
.filter((line) => {
if (line.match(/[│├└].*/)) {
return true;
}
Expand All @@ -47,7 +47,7 @@ function convertCoursierStrToTree(dependenciesTextTree) {
}
return false;
})
.map(function (line) {
.map((line) => {
return line
.replace(/\│/g, ' ')
.replace('├─ ', ' ')
Expand Down Expand Up @@ -80,7 +80,9 @@ function walkInTree(toNode, fromNode) {
}

function getPackageNameAndVersion(packageDependency) {
var splited, version, app;
var splited;
var version;
var app;
if (packageDependency.indexOf('(evicted by:') > -1) {
return null;
}
Expand All @@ -100,7 +102,7 @@ function convertDepArrayToObject(depsArr) {
if (!depsArr) {
return null;
}
return depsArr.reduce(function (acc, dep) {
return depsArr.reduce((acc, dep) => {
dep.dependencies = convertDepArrayToObject(dep.dependencies);
acc[dep.name] = dep;
return acc;
Expand Down Expand Up @@ -153,7 +155,7 @@ function createCoursierSnykTree(rootTree, name, version) {
// multi build configuration
// - use provided package name and version
// - use complete tree as root
const dependencies = rootTree.root.map(function(appTree){
const dependencies = rootTree.root.map((appTree) => {
const subTree = getProjectName(getKeys(appTree).pop());
subTree.dependencies = [];
walkInTree(subTree, appTree);
Expand Down
10 changes: 5 additions & 5 deletions lib/sub-process.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
var childProcess = require('child_process');

module.exports.execute = function (command, args, options) {
module.exports.execute = function(command, args, options) {
var spawnOptions = {shell: true};
if (options && options.cwd) {
spawnOptions.cwd = options.cwd;
}

return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
var stdout = '';
var stderr = '';

var proc = childProcess.spawn(command, args, spawnOptions);
proc.stdout.on('data', function (data) {
proc.stdout.on('data', function(data) {
stdout = stdout + data;
});
proc.stderr.on('data', function (data) {
proc.stderr.on('data', function(data) {
stderr = stderr + data;
});

proc.on('close', function (code) {
proc.on('close', function(code) {
if (code !== 0) {
return reject(new Error(stdout || stderr));
}
Expand Down
39 changes: 20 additions & 19 deletions lib/tabdown.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
exports.parse = function (lines) {
exports.parse = function(lines) {
function addHiddenProperties(scope, props) {
for (var p in props) {
for (let p of Object.keys(props)) {
Object.defineProperty(scope, p, {enumerable: false, value: props[p]});
}
}
var TreeNode = function (data, depth) {
var TreeNode = function(data, depth) {
this.parent = null;
addHiddenProperties(this, {
data: data,
depth: depth,
data,
depth,
parent: null,
children: [],
});
this[data || 'root'] = this.children;
};

TreeNode.prototype.toString = function () {
TreeNode.prototype.toString = function() {
return JSON.stringify(this.children);
};

Expand All @@ -36,8 +36,8 @@ exports.parse = function (lines) {
return -1; // no content
}

for (var i = 0; i < lines.length; i++) {
var line = lines[i];
for (let i = 0; i < lines.length; i++) {
let line = lines[i];

var tabcount = countTabs(line);

Expand All @@ -46,7 +46,7 @@ exports.parse = function (lines) {
while (tabcount - levels[levels.length - 1].depth <= 0) {
levels.pop();
}
var depth = levels.length - 1;
let depth = levels.length - 1;
var node = new TreeNode(line.substring(tabcount), depth);
node.parent = levels[levels.length - 1];
node.parent.children.push(node);
Expand All @@ -56,29 +56,30 @@ exports.parse = function (lines) {
return tree;
};

exports.traverse = function (tree, cb) {
exports.traverse = function(tree, cb) {
function _traverse(node) {
cb(node);
for (var i = 0; i < node.children.length; i++) {
for (let i = 0; i < node.children.length; i++) {
_traverse(node.children[i]);
}
}
for (var i = 0; i < tree.children.length; i++) {
for (let i = 0; i < tree.children.length; i++) {
_traverse(tree.children[i]);
}
};

exports.print = function (tree) {
exports.traverse(tree, function (node) {
var string = '';
exports.print = function(tree) {
exports.traverse(tree, function(node) {
var str = '';
for (var i = 0; i < node.depth; i++) {
string += '\t';
str += '\t';
}
string += node.data;
console.log(string);
str += node.data;
// tslint:disable
console.log(str);
});
};

exports.toJSON = function (tree) {
exports.toJSON = function(tree) {
return JSON.stringify(tree.children);
};
27 changes: 20 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,32 @@
"type": "git",
"url": "https://github.com/snyk/snyk-sbt-plugin"
},
"main": "lib/index.js",
"files": [
"dist"
],
"directories": {
"test": "test"
},
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"lint": "tslint --project tsconfig.json --format stylish",
"prepare": "npm run build",
"test": "npm run lint && npm run test-functional",
"lint": "eslint -c .eslintrc lib test",
"test-functional": "tap -R spec ./test/functional/*.test.js",
"test-system": "tap -R spec --timeout=300 ./test/system/*.test.js"
"test-functional": "tap -R spec ./test/functional/*.test.[tj]s",
"test-system": "tap -R spec --timeout=300 ./test/system/*.test.[tj]s"
},
"author": "snyk.io",
"license": "Apache-2.0",
"devDependencies": {
"eslint": "^4.11.0",
"sinon": "^2.4.1",
"tap": "^12.0.1",
"tap-only": "0.0.5"
"tap": "12.6.1",
"tap-only": "0.0.5",
"typescript": "3.4.5",
"tslint": "5.16.0"
},
"dependencies": {
"@types/sinon": "7.0.11",
"tslib": "1.9.3"
}
}

0 comments on commit c7f7af8

Please sign in to comment.