Skip to content

Commit

Permalink
feat: remove then-fs package
Browse files Browse the repository at this point in the history
  • Loading branch information
JackuB committed Jun 9, 2020
1 parent dcf06f0 commit 99da9c2
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 96 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -95,7 +95,6 @@
"source-map-support": "^0.5.11",
"strip-ansi": "^5.2.0",
"tempfile": "^2.0.0",
"then-fs": "^2.0.0",
"update-notifier": "^4.1.0",
"uuid": "^3.3.2",
"wrap-ansi": "^5.1.0"
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/help.ts
@@ -1,4 +1,4 @@
import * as fs from 'then-fs';
import * as fs from 'fs';
import * as path from 'path';
import * as Debug from 'debug';
const debug = Debug('snyk');
Expand All @@ -14,7 +14,7 @@ export = async function help(item: string | boolean) {

const filename = path.resolve(__dirname, '../../../help', item + '.txt');
try {
return await fs.readFile(filename, 'utf8');
return fs.readFileSync(filename, 'utf8');
} catch (error) {
debug(error);
return `'${item}' help can't be found at location: ${filename}`;
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/monitor/index.ts
@@ -1,7 +1,7 @@
export = monitor;

import chalk from 'chalk';
import * as fs from 'then-fs';
import * as fs from 'fs';
import * as Debug from 'debug';
import * as pathUtil from 'path';
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
Expand Down Expand Up @@ -274,7 +274,7 @@ function generateMonitorMeta(options, packageManager?): MonitorMeta {
}

async function validateMonitorPath(path, isDocker) {
const exists = await fs.exists(path);
const exists = fs.existsSync(path);
if (!exists && !isDocker) {
throw new Error('"' + path + '" is not a valid path for "snyk monitor"');
}
Expand Down
15 changes: 6 additions & 9 deletions src/cli/commands/protect/wizard.ts
Expand Up @@ -11,7 +11,7 @@ const debug = debugModule('snyk');

import * as path from 'path';
import * as inquirer from '@snyk/inquirer';
import * as fs from 'then-fs';
import * as fs from 'fs';
import * as tryRequire from 'snyk-try-require';
import chalk from 'chalk';
import * as url from 'url';
Expand Down Expand Up @@ -68,7 +68,7 @@ async function processPackageManager(options: Options) {
);
}

const nodeModulesExist = await fs.exists(path.join('.', 'node_modules'));
const nodeModulesExist = fs.existsSync(path.join('.', 'node_modules'));
if (!nodeModulesExist) {
// throw a custom error
throw new Error(
Expand Down Expand Up @@ -126,8 +126,8 @@ async function processWizardFlow(options) {
debug('ignore disabled');
}
const intro = __dirname + '/../../../../help/wizard.txt';
return fs
.readFile(intro, 'utf8')

return Promise.resolve(fs.readFileSync(intro, 'utf8'))
.then((str) => {
if (!isCI()) {
console.log(str);
Expand Down Expand Up @@ -402,8 +402,7 @@ function processAnswers(answers, policy, options) {
.then(() => {
// re-read the package.json - because the generatePolicy can apply
// an `npm install` which will change the deps
return fs
.readFile(packageFile, 'utf8')
return Promise.resolve(fs.readFileSync(packageFile, 'utf8'))
.then((packageFileString) => {
pkgIndentation = calculatePkgFileIndentation(packageFileString);
return packageFileString;
Expand Down Expand Up @@ -525,9 +524,7 @@ function processAnswers(answers, policy, options) {
return (
spinner(lbl)
.then(() => {
return fs.writeFile(packageFile, packageString);
})
.then(() => {
fs.writeFileSync(packageFile, packageString);
if (isLockFileBased) {
// we need to trigger a lockfile update after adding snyk
// as a dep
Expand Down
2 changes: 1 addition & 1 deletion src/lib/detect.ts
@@ -1,4 +1,4 @@
import * as fs from 'then-fs';
import * as fs from 'fs';
import * as pathLib from 'path';
import * as debugLib from 'debug';
import * as _ from '@snyk/lodash';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/plugins/nodejs-plugin/npm-modules-parser.ts
@@ -1,5 +1,5 @@
import * as path from 'path';
import * as fs from 'then-fs';
import * as fs from 'fs';
import * as resolveNodeDeps from 'snyk-resolve-deps';
import { PkgTree } from 'snyk-nodejs-lockfile-parser';

Expand Down
6 changes: 3 additions & 3 deletions src/lib/plugins/rubygems/inspectors/try-get-spec.ts
@@ -1,5 +1,5 @@
import * as path from 'path';
import * as fs from 'then-fs';
import * as fs from 'fs';

interface File {
name: string;
Expand All @@ -18,10 +18,10 @@ export async function tryGetSpec(
): Promise<File | null> {
const filePath = path.resolve(dir, name);

if (await fs.exists(filePath)) {
if (fs.existsSync(filePath)) {
return {
name,
contents: Buffer.from(await fs.readFile(filePath)).toString('base64'),
contents: Buffer.from(fs.readFileSync(filePath)).toString('base64'),
};
}
return null;
Expand Down
7 changes: 3 additions & 4 deletions src/lib/protect/patch.js
Expand Up @@ -6,7 +6,7 @@ const debug = require('debug')('snyk');
const chalk = require('chalk');
const glob = require('glob');
const tempfile = require('tempfile');
const fs = require('then-fs');
const fs = require('fs');
const path = require('path');
const _ = require('@snyk/lodash');
const applyPatch = require('./apply-patch');
Expand Down Expand Up @@ -64,12 +64,11 @@ function patch(vulns, live) {
return getPatchFile(url, filename)
.then((patch) => {
// check whether there's a trace of us having patched before
return fs
.exists(flag)
return Promise.resolve(fs.existsSync(flag))
.then((exists) => {
// if the file doesn't exist, look for the old style filename
// in case and for backwards compatability
return exists || fs.exists(oldFlag);
return exists || fs.existsSync(oldFlag);
})
.then((exists) => {
if (!exists) {
Expand Down
15 changes: 11 additions & 4 deletions src/lib/protect/write-patch-flag.js
@@ -1,7 +1,7 @@
module.exports = writePatchFlag;

const debug = require('debug')('snyk');
const fs = require('then-fs');
const fs = require('fs');
const path = require('path');

function writePatchFlag(now, vuln) {
Expand All @@ -17,17 +17,24 @@ function writePatchFlag(now, vuln) {
const flag = path.resolve(vuln.source, '.snyk-' + fileSafeId + '.flag');
if (vuln.grouped && vuln.grouped.includes) {
debug('found addition vulns to write flag files for');
const writePromises = [fs.writeFile(flag, now.toJSON(), 'utf8')];
const writePromises = [];
fs.writeFileSync(flag, now.toJSON(), 'utf8');
vuln.grouped.includes.forEach(() => {
const fileSafeId = vuln.id.replace(/:/g, '-');
const flag = path.resolve(vuln.source, '.snyk-' + fileSafeId + '.flag');
debug('Writing flag for grouped vulns', flag);
writePromises.push(fs.writeFile(flag, now.toJSON(), 'utf8'));
writePromises.push();
fs.writeFileSync(flag, now.toJSON(), 'utf8');
});
promise = Promise.all(writePromises);
} else {
debug('Writing flag for single vuln', flag);
promise = fs.writeFile(flag, now.toJSON(), 'utf8');
/* TODO:
This piece is actually swallowing fs.writeFile errors!
See the `promise.then` construct below.
This should be refactored and tests should be updated.
*/
promise = new Promise((r) => fs.writeFile(flag, now.toJSON(), 'utf8', r));
}
return promise.then(() => {
return vuln;
Expand Down
4 changes: 2 additions & 2 deletions test/missing-node-modules.test.ts
@@ -1,6 +1,6 @@
import * as tap from 'tap';
const { test } = tap;
import * as fs from 'then-fs';
import * as fs from 'fs';

const apiKey = '123456789';
const notAuthorizedApiKey = 'notAuthorized';
Expand Down Expand Up @@ -42,7 +42,7 @@ test('throws when missing node_modules', async (t) => {
const dir = baseDir + 'npm/npm-3-no-node-modules';
// ensure node_modules does not exist
try {
fs.rmdir(dir + '/node_modules');
fs.rmdirSync(dir + '/node_modules');
} catch (err) {
// ignore
}
Expand Down
4 changes: 2 additions & 2 deletions test/policy-display.test.ts
@@ -1,6 +1,6 @@
import * as policy from 'snyk-policy';
import { test } from 'tap';
import * as fs from 'then-fs';
import * as fs from 'fs';
import { display } from '../src/lib/display-policy';
import stripAnsi from 'strip-ansi';
import { URL } from 'url';
Expand All @@ -11,7 +11,7 @@ const { hostname } = new URL(SNYK_API);
test('test sensibly bails if gets an old .snyk format', async (t) => {
const filename = __dirname + '/fixtures/snyk-config-no-version';
const loadedPolicy = await policy.load(filename);
const expectedFile = await fs.readFile(filename + '/expected', 'utf8');
const expectedFile = await fs.readFileSync(filename + '/expected', 'utf8');

try {
const [displayPolicy, expectedFileString] = await Promise.all([
Expand Down
6 changes: 3 additions & 3 deletions test/protect-apply-same-patch-again.test.js
@@ -1,7 +1,7 @@
const test = require('tap').test;
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const fs = require('then-fs');
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');

Expand Down Expand Up @@ -84,7 +84,7 @@ test('Same patch is applied multiple times without issue', (t) => {
fixturesBaseFolderFiles.forEach((file) => {
const flagMatch = file.match(/\.snyk.*\.flag/);
if (flagMatch) {
fs.unlink(fixturesBaseFolder + file);
fs.unlinkSync(path.join(fixturesBaseFolder, file));
}
});
});
Expand All @@ -98,7 +98,7 @@ test('Same patch is applied multiple times without issue', (t) => {

fixturesModuleFolderFiles.forEach((file) => {
if (file !== debugNodeFileFixture) {
fs.unlink(fixturesModuleFolder + file);
fs.unlinkSync(path.join(fixturesModuleFolder, file));
}
});
});
Expand Down
45 changes: 21 additions & 24 deletions test/protect-fail.test.js
@@ -1,18 +1,17 @@
var applyPatch = require('../src/lib/protect/apply-patch');
var path = require('path');
var fs = require('fs');
var thenfs = require('then-fs');
var test = require('tap').test;
var snyk = require('../src/lib');
const applyPatch = require('../src/lib/protect/apply-patch');
const path = require('path');
const fs = require('fs');
const test = require('tap').test;
const snyk = require('../src/lib');

test('bad patch file does not apply', function(t) {
// check the target file first
var root = path.resolve(__dirname, './fixtures/semver-patch-fail/');
var dir = path.resolve(root, './node_modules/semver');
var semver = fs.readFileSync(dir + '/semver.js', 'utf8');
const root = path.resolve(__dirname, './fixtures/semver-patch-fail/');
const dir = path.resolve(root, './node_modules/semver');
const semver = fs.readFileSync(dir + '/semver.js', 'utf8');
t.ok('original semver loaded');

var old = snyk.config.get('disable-analytics');
const old = snyk.config.get('disable-analytics');
snyk.config.set('disable-analytics', '1');

applyPatch(
Expand All @@ -32,25 +31,23 @@ test('bad patch file does not apply', function(t) {
fs.writeFileSync(dir + '/semver.js', semver);
})
.catch(function(error) {
var semver2 = fs.readFileSync(dir + '/semver.js', 'utf8');
const semver2 = fs.readFileSync(dir + '/semver.js', 'utf8');
t.equal(semver, semver2, 'target was untouched');
t.equal(error.code, 'FAIL_PATCH', 'patch failed, task exited correctly');
})
.then(function() {
// clean up
var noop = function() {};
var promises = [];
promises.push(thenfs.unlink(dir + '/semver.js.orig').catch(noop));
promises.push(thenfs.unlink(dir + '/semver.js.rej').catch(noop));
promises.push(
thenfs.unlink(dir + '/test/big-numbers.js.orig').catch(noop),
);
promises.push(
thenfs.unlink(dir + '/test/big-numbers.js.rej').catch(noop),
);
fs.writeFileSync(dir + '/semver.js', semver);

return Promise.all(promises);
/* TODO:
These promises might throw, but you still want to run all of them
Might need rethinking of this test
*/
return Promise.all([
new Promise((r) => fs.unlink(dir + '/semver.js.orig', r)),
new Promise((r) => fs.unlink(dir + '/semver.js.rej', r)),
new Promise((r) => fs.unlink(dir + '/test/big-numbers.js.orig', r)),
new Promise((r) => fs.unlink(dir + '/test/big-numbers.js.rej', r)),
new Promise((r) => fs.writeFile(dir + '/semver.js', semver, r)),
]);
})
.then(function() {
t.ok('clean up done');
Expand Down
8 changes: 3 additions & 5 deletions test/protect-patch-same-pkg.test.js
Expand Up @@ -21,14 +21,12 @@ var patch = proxyquire('../src/lib/protect/patch', {
'./write-patch-flag': function(now, vuln) {
return Promise.resolve(vuln);
},
'then-fs': {
rename: function(filename) {
fs: {
renameSync: function(filename) {
renameSpy(filename);
return Promise.resolve();
},
writeFile: function(filename, body) {
writeFileSync: function(filename, body) {
writeSpy(filename, body);
return Promise.resolve();
},
createWriteStream: function() {
// fake event emitter (sort of)
Expand Down
9 changes: 4 additions & 5 deletions test/wizard-package-changes.test.ts
Expand Up @@ -17,15 +17,14 @@ tap.tearDown(() => {
let mockPackage;

const wizard = proxyquire('../src/cli/commands/protect/wizard', {
'then-fs': {
writeFile(filename, content) {
fs: {
writeFileSync(filename, content) {
if (filename.includes('package.json')) {
writeSpy(JSON.parse(content));
}
return Promise.resolve();
},
readFile(filename) {
return Promise.resolve(JSON.stringify(mockPackage));
readFileSync() {
return JSON.stringify(mockPackage);
},
'../../../lib/npm': {
getVersion() {
Expand Down

0 comments on commit 99da9c2

Please sign in to comment.