Skip to content

Commit

Permalink
chore: convert core code to Typescript (no tests yes)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Yegupov authored and kyegupov committed May 22, 2019
1 parent bbbfc55 commit fab75b0
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 115 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ npm-debug.log
/.tox/
*.egg-info/
.venvs/
dist/
package-lock.json
130 changes: 61 additions & 69 deletions lib/index.js → lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
var path = require('path');
var subProcess = require('./sub-process');
var fs = require('fs');
var tmp = require('tmp');

module.exports = {
inspect: inspect,
};

module.exports.__tests = {
buildArgs: buildArgs,
import * as path from 'path';
import * as subProcess from './sub-process';
import * as fs from 'fs';
import * as tmp from 'tmp';

// tslint:disable-next-line
export const __tests = {
buildArgs,
};

function inspect(root, targetFile, options) {
export function inspect(root, targetFile, options) {
if (!options) {
options = {};
}
var command = options.command || 'python';
var includeDevDeps = !!(options.dev || false);
var baseargs = [];
let command = options.command || 'python';
const includeDevDeps = !!(options.dev || false);
let baseargs: string[] = [];

if (path.basename(targetFile) === 'Pipfile') {
// Check that pipenv is available by running it.
var pipenvCheckProc = subProcess.executeSync('pipenv', ['--version']);
const pipenvCheckProc = subProcess.executeSync('pipenv', ['--version']);
if (pipenvCheckProc.status !== 0) {
throw new Error(
'Failed to run `pipenv`; please make sure it is installed.');
Expand All @@ -39,10 +36,10 @@ function inspect(root, targetFile, options) {
targetFile,
options.allowMissing,
includeDevDeps,
options.args
options.args,
),
])
.then(function (result) {
.then((result) => {
return {
plugin: result[0],
package: result[1],
Expand All @@ -53,10 +50,10 @@ function inspect(root, targetFile, options) {
function getMetaData(command, baseargs, root, targetFile) {
return subProcess.execute(
command,
[].concat(baseargs, ['--version']),
{cwd: root}
[...baseargs, '--version'],
{cwd: root},
)
.then(function (output) {
.then((output) => {
return {
name: 'snyk-python-plugin',
runtime: output.replace('\n', ''),
Expand All @@ -67,46 +64,40 @@ function getMetaData(command, baseargs, root, targetFile) {
});
}

// Hack:
// We're using Zeit assets feature in order to support Python and Go testing
// within a binary release. By doing "path.join(__dirname, 'PATH'), Zeit adds
// PATH file auto to the assets. Sadly, Zeit doesn't support (as far as I
// understand) adding a full folder as an asset, and this is why we're adding
// the required files this way. In addition, Zeit doesn't support
// path.resolve(), and this is why I'm using path.join()
function createAssets(){
var assets = [];
assets.push(path.join(__dirname, '../plug/pip_resolve.py'));
assets.push(path.join(__dirname, '../plug/distPackage.py'));
assets.push(path.join(__dirname, '../plug/package.py'));
assets.push(path.join(__dirname, '../plug/pipfile.py'));
assets.push(path.join(__dirname, '../plug/reqPackage.py'));
assets.push(path.join(__dirname, '../plug/utils.py'));

assets.push(path.join(__dirname, '../plug/requirements/fragment.py'));
assets.push(path.join(__dirname, '../plug/requirements/parser.py'));
assets.push(path.join(__dirname, '../plug/requirements/requirement.py'));
assets.push(path.join(__dirname, '../plug/requirements/vcs.py'));
assets.push(path.join(__dirname, '../plug/requirements/__init__.py'));

assets.push(path.join(__dirname, '../plug/pytoml/__init__.py'));
assets.push(path.join(__dirname, '../plug/pytoml/core.py'));
assets.push(path.join(__dirname, '../plug/pytoml/parser.py'));
assets.push(path.join(__dirname, '../plug/pytoml/writer.py'));

return assets;
// path.join calls have to be exactly in this format, needed by "pkg" to build a standalone Snyk CLI binary:
// https://www.npmjs.com/package/pkg#detecting-assets-in-source-code
function createAssets() {
return [
path.join(__dirname, '../plug/pip_resolve.py'),
path.join(__dirname, '../plug/distPackage.py'),
path.join(__dirname, '../plug/package.py'),
path.join(__dirname, '../plug/pipfile.py'),
path.join(__dirname, '../plug/reqPackage.py'),
path.join(__dirname, '../plug/utils.py'),

path.join(__dirname, '../plug/requirements/fragment.py'),
path.join(__dirname, '../plug/requirements/parser.py'),
path.join(__dirname, '../plug/requirements/requirement.py'),
path.join(__dirname, '../plug/requirements/vcs.py'),
path.join(__dirname, '../plug/requirements/__init__.py'),

path.join(__dirname, '../plug/pytoml/__init__.py'),
path.join(__dirname, '../plug/pytoml/core.py'),
path.join(__dirname, '../plug/pytoml/parser.py'),
path.join(__dirname, '../plug/pytoml/writer.py'),
];
}

function writeFile(writeFilePath, contents) {
var dirPath = path.dirname(writeFilePath);
const dirPath = path.dirname(writeFilePath);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
fs.writeFileSync(writeFilePath, contents);
}

function getFilePathRelativeToDumpDir(filePath) {
var pathParts = filePath.split('\\plug\\');
let pathParts = filePath.split('\\plug\\');

// Windows
if (pathParts.length > 1) {
Expand All @@ -119,17 +110,17 @@ function getFilePathRelativeToDumpDir(filePath) {
}

function dumpAllFilesInTempDir(tempDirName) {
createAssets().forEach(function(currentReadFilePath) {
createAssets().forEach((currentReadFilePath) => {
if (!fs.existsSync(currentReadFilePath)) {
throw new Error('The file `' + currentReadFilePath + '` is missing');
}

var relFilePathToDumpDir =
const relFilePathToDumpDir =
getFilePathRelativeToDumpDir(currentReadFilePath);

var writeFilePath = path.join(tempDirName, relFilePathToDumpDir);
const writeFilePath = path.join(tempDirName, relFilePathToDumpDir);

var contents = fs.readFileSync(currentReadFilePath);
const contents = fs.readFileSync(currentReadFilePath);
writeFile(writeFilePath, contents);
});
}
Expand All @@ -141,36 +132,37 @@ function getDependencies(
targetFile,
allowMissing,
includeDevDeps,
args
args,
) {
var tempDirObj = tmp.dirSync({
const tempDirObj = tmp.dirSync({
unsafeCleanup: true,
});

dumpAllFilesInTempDir(tempDirObj.name);

return subProcess.execute(
command,
[].concat(baseargs,
buildArgs(
[
...baseargs,
...buildArgs(
targetFile,
allowMissing,
tempDirObj.name,
includeDevDeps,
args
)
),
{cwd: root}
args,
),
],
{cwd: root},
)
.then(function (output) {
.then((output) => {
tempDirObj.removeCallback();
return JSON.parse(output);
})
.catch(function (error) {
.catch((error) => {
tempDirObj.removeCallback();
if (typeof error === 'string') {
if (error.indexOf('Required package missing') !== -1) {
var errMsg = 'Please run `pip install -r ' + targetFile + '`';
let errMsg = 'Please run `pip install -r ' + targetFile + '`';
if (path.basename(targetFile) === 'Pipfile') {
errMsg = 'Please run `pipenv update`';
}
Expand All @@ -187,10 +179,10 @@ function buildArgs(
allowMissing,
tempDirPath,
includeDevDeps,
extraArgs
extraArgs: string[],
) {
var pathToRun = path.join(tempDirPath, 'pip_resolve.py');
var args = [pathToRun];
const pathToRun = path.join(tempDirPath, 'pip_resolve.py');
let args = [pathToRun];
if (targetFile) {
args.push(targetFile);
}
Expand Down
42 changes: 0 additions & 42 deletions lib/sub-process.js

This file was deleted.

42 changes: 42 additions & 0 deletions lib/sub-process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as childProcess from 'child_process';

function makeSpawnOptions(options) {
const spawnOptions: childProcess.SpawnOptions = {shell: true};
if (options && options.cwd) {
spawnOptions.cwd = options.cwd;
}
if (options && options.env) {
spawnOptions.env = options.env;
}
return spawnOptions;
}

export function execute(command, args, options?): Promise<string> {
const spawnOptions = makeSpawnOptions(options);

return new Promise((resolve, reject) => {
let stdout = '';
let stderr = '';

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

proc.on('close', (code) => {
if (code !== 0) {
return reject(stdout || stderr);
}
resolve(stdout || stderr);
});
});
}

export function executeSync(command, args, options?) {
const spawnOptions = makeSpawnOptions(options);

return childProcess.spawnSync(command, args, spawnOptions);
}
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"type": "git",
"url": "https://github.com/snyk/snyk-python-plugin"
},
"main": "lib/index.js",
"main": "dist/index.js",
"scripts": {
"test": "tap ./test/*.test.js -R spec --timeout=900",
"lint": "eslint -c .eslintrc lib test",
"build": "tsc",
"prepare": "npm run build",
"test": "tap --node-arg=-r --node-arg=ts-node/register ./test/*.test.js -R spec --timeout=900",
"lint": "tslint --project tsconfig.json --format stylish && eslint -c .eslintrc lib test",
"semantic-release": "semantic-release"
},
"author": "snyk.io",
Expand All @@ -18,9 +20,14 @@
"tmp": "0.0.33"
},
"devDependencies": {
"@types/node": "^6.14.6",
"@types/tmp": "^0.1.0",
"eslint": "^4.11.0",
"semantic-release": "^15",
"sinon": "^2.3.2",
"tap": "^12.0.1"
"tap": "^12.6.1",
"ts-node": "^8.1.0",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
}
}
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"outDir": "./dist",
"pretty": true,
"target": "es2015",
"lib": [
"es2015",
],
"module": "commonjs",
"allowJs": false,
"sourceMap": true,
"strict": true,
"declaration": true,
"importHelpers": true,
"noImplicitAny": false // Needed to compile tap and ansi-escapes
},
"include": [
"./lib/**/*"
]
}
19 changes: 19 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "tslint:recommended",
"rules": {
"max-line-length": {
"options": [120]
},
"trailing-comma": [true, {"multiline": "always"}],
"triple-equals": true,
"curly": true,
"indent": [true, "spaces", 2],
"interface-name": false,
"no-use-before-declare": true,
"object-literal-sort-keys": false,
"ordered-imports": false,
"quotemark": [true, "single", "avoid-escape", "avoid-template"],
"semicolon": [true, "always"],
"no-console": [false]
}
}

0 comments on commit fab75b0

Please sign in to comment.