Skip to content

Commit 94cd825

Browse files
authored
Merge pull request datreeio#20 from datreeio/remove-fs-extra
Remove fs extra
2 parents f73cd76 + ca01449 commit 94cd825

File tree

4 files changed

+80
-46
lines changed

4 files changed

+80
-46
lines changed

lib/node.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs-extra')
1+
const fs = require('fs')
22
const path = require('path')
33

44
const NODEJS_PACKAGE_FILENAME = 'package.json'
@@ -35,15 +35,17 @@ function getPackages(packageJson) {
3535
}
3636

3737
function packageJsonExists(repoDir) {
38-
if (!fs.pathExistsSync(`${repoDir}/${NODEJS_PACKAGE_FILENAME}`)) {
38+
if (!fs.existsSync(`${repoDir}/${NODEJS_PACKAGE_FILENAME}`)) {
3939
throw new Error(
4040
`Package file: ${NODEJS_PACKAGE_FILENAME} not found in: ${repoDir}`
4141
)
4242
}
4343
}
4444

4545
function readPackageJson(repoDir) {
46-
return fs.readJSONSync(`${repoDir}/${NODEJS_PACKAGE_FILENAME}`)
46+
return JSON.parse(
47+
fs.readFileSync(`${repoDir}/${NODEJS_PACKAGE_FILENAME}`).toString('utf8')
48+
)
4749
}
4850

4951
function extractPackageInfo(repoDir) {

package-lock.json

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"cli-table": "^0.3.1",
4040
"commander": "^2.12.2",
4141
"figlet": "^1.2.0",
42-
"fs-extra": "^5.0.0",
4342
"ini": "^1.3.4",
4443
"os-name": "^2.0.1",
4544
"request": "^2.81.0",

test/node.test.js

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* global describe it */
22

33
require('babel-polyfill')
4+
const fs = require('fs')
45
const node = require('../lib/node')
56
const sinon = require('sinon')
6-
const fs = require('fs-extra')
77
const assert = require('assert')
88

99
let packageJsonStub = `{
@@ -49,11 +49,17 @@ let packageJsonStub = `{
4949
}
5050
}`
5151

52-
describe('Node.js module', async function () {
53-
describe('Get packages', async function () {
54-
it('Should extract packages from package.json', async function () {
52+
describe('Node.js module', async function() {
53+
describe('Get packages', async function() {
54+
it('Should extract packages from package.json', async function() {
5555
try {
56-
let deps = ['dependencies', 'devDependencies', 'bundledDependencies', 'peerDependencies', 'optionalDependencies']
56+
let deps = [
57+
'dependencies',
58+
'devDependencies',
59+
'bundledDependencies',
60+
'peerDependencies',
61+
'optionalDependencies'
62+
]
5763
let packages = await node.getPackages(JSON.parse(packageJsonStub))
5864

5965
for (let dep of deps) {
@@ -65,8 +71,8 @@ describe('Node.js module', async function () {
6571
})
6672
})
6773

68-
it('Should check if packageJson exists', function () {
69-
let stub = sinon.stub(fs, 'pathExistsSync').returns(false)
74+
it('Should check if packageJson exists', function() {
75+
let stub = sinon.stub(fs, 'existsSync').returns(false)
7076
let errmsg = 'should throw an error, package.json does not exist!'
7177

7278
try {
@@ -83,9 +89,11 @@ describe('Node.js module', async function () {
8389
}
8490
})
8591

86-
it('Should read the package.json according to package and module name', function () {
92+
it('Should read the package.json according to package and module name', function() {
8793
let packagename = node.NODEJS_PACKAGE_FILENAME
88-
let stub = sinon.stub(fs, 'readJSONSync').returns({package: 'json'})
94+
let stub = sinon
95+
.stub(fs, 'readFileSync')
96+
.returns(JSON.stringify({ package: 'json' }))
8997
let path = '/fake/path'
9098
try {
9199
node.readPackageJson(path)
@@ -97,9 +105,11 @@ describe('Node.js module', async function () {
97105
}
98106
})
99107

100-
describe('extractPackageInfo', async function () {
101-
it('should return an empty object of package json is not found', async function () {
102-
let stub = sinon.stub(node, 'packageJsonExists').throws('no package.json found')
108+
describe('extractPackageInfo', async function() {
109+
it('should return an empty object of package json is not found', async function() {
110+
let stub = sinon
111+
.stub(node, 'packageJsonExists')
112+
.throws('no package.json found')
103113

104114
try {
105115
let res = await node.extractPackageInfo('/fake/path')
@@ -111,33 +121,40 @@ describe('Node.js module', async function () {
111121
}
112122
})
113123

114-
it('Should extract data from package.json', async function () {
124+
it('Should extract data from package.json', async function() {
115125
let packages = {
116-
devDependencies:
117-
{ 'babel-cli': '^6.24.1',
126+
devDependencies: {
127+
'babel-cli': '^6.24.1',
118128
'babel-core': '^6.25.0',
119129
'babel-preset-env': '^1.6.0',
120130
'babel-polyfill': '^6.23.0',
121131
mocha: '^3.4.2',
122132
nyc: '^11.0.3',
123133
sinon: '^2.3.8',
124134
snazzy: '^7.0.0',
125-
standard: '^10.0.2' },
126-
dependencies:
127-
{ 'fs-extra': '^4.0.0',
135+
standard: '^10.0.2'
136+
},
137+
dependencies: {
138+
'fs-extra': '^4.0.0',
128139
request: '^2.81.0',
129-
'request-promise-native': '^1.0.2' }
140+
'request-promise-native': '^1.0.2'
141+
}
130142
}
131143

132-
let stubPackageJsonExists = sinon.stub(fs, 'pathExistsSync').returns(true)
133-
let stubReadPackageJson = sinon.stub(fs, 'readJSONSync').returns(JSON.parse(packageJsonStub))
144+
let stubPackageJsonExists = sinon.stub(fs, 'existsSync').returns(true)
145+
let stubReadPackageJson = sinon
146+
.stub(fs, 'readFileSync')
147+
.returns(packageJsonStub)
134148
let stubGetPackages = sinon.stub(node, 'getPackages').returns(packages)
135149

136150
try {
137151
let res = await node.extractPackageInfo('/fake/path')
138152
assert.ok('packages' in res, 'packages not in package info')
139153
assert.ok('project_name' in res, 'project_name not in package info')
140-
assert.ok('node_package_index' in res, 'node_package_index not in package info')
154+
assert.ok(
155+
'node_package_index' in res,
156+
'node_package_index not in package info'
157+
)
141158
} catch (err) {
142159
assert.ifError(err)
143160
} finally {
@@ -147,11 +164,13 @@ describe('Node.js module', async function () {
147164
}
148165
})
149166

150-
it('extracts repository name from a shorthand name', async function () {
151-
const stubPackageJsonExists = sinon.stub(fs, 'pathExistsSync').returns(true)
167+
it('extracts repository name from a shorthand name', async function() {
168+
const stubPackageJsonExists = sinon.stub(fs, 'existsSync').returns(true)
152169
const alteredPackageJsonStub = JSON.parse(packageJsonStub)
153170
alteredPackageJsonStub.repository = 'bla:org/repo'
154-
const stubReadPackageJson = sinon.stub(fs, 'readJSONSync').returns(alteredPackageJsonStub)
171+
const stubReadPackageJson = sinon
172+
.stub(fs, 'readFileSync')
173+
.returns(JSON.stringify(alteredPackageJsonStub))
155174
const stubGetPackages = sinon.stub(node, 'getPackages').returns({})
156175
try {
157176
const res = await node.extractPackageInfo('/fake/path')
@@ -165,11 +184,13 @@ describe('Node.js module', async function () {
165184
}
166185
})
167186

168-
it('extracts repository name from a shorthand name with a branch or tag (#)', async function () {
169-
const stubPackageJsonExists = sinon.stub(fs, 'pathExistsSync').returns(true)
187+
it('extracts repository name from a shorthand name with a branch or tag (#)', async function() {
188+
const stubPackageJsonExists = sinon.stub(fs, 'existsSync').returns(true)
170189
const alteredPackageJsonStub = JSON.parse(packageJsonStub)
171190
alteredPackageJsonStub.repository = 'bla:org/repo#branch'
172-
const stubReadPackageJson = sinon.stub(fs, 'readJSONSync').returns(alteredPackageJsonStub)
191+
const stubReadPackageJson = sinon
192+
.stub(fs, 'readFileSync')
193+
.returns(JSON.stringify(alteredPackageJsonStub))
173194
const stubGetPackages = sinon.stub(node, 'getPackages').returns({})
174195
try {
175196
const res = await node.extractPackageInfo('/fake/path')
@@ -183,11 +204,13 @@ describe('Node.js module', async function () {
183204
}
184205
})
185206

186-
it('extracts repository name from a shorthand simple name', function () {
187-
const stubPackageJsonExists = sinon.stub(fs, 'pathExistsSync').returns(true)
207+
it('extracts repository name from a shorthand simple name', function() {
208+
const stubPackageJsonExists = sinon.stub(fs, 'existsSync').returns(true)
188209
const alteredPackageJsonStub = JSON.parse(packageJsonStub)
189210
alteredPackageJsonStub.repository = 'repo'
190-
const stubReadPackageJson = sinon.stub(fs, 'readJSONSync').returns(alteredPackageJsonStub)
211+
const stubReadPackageJson = sinon
212+
.stub(fs, 'readFileSync')
213+
.returns(JSON.stringify(alteredPackageJsonStub))
191214
const stubGetPackages = sinon.stub(node, 'getPackages').returns({})
192215
try {
193216
const res = node.extractPackageInfo('/fake/path')
@@ -201,14 +224,16 @@ describe('Node.js module', async function () {
201224
}
202225
})
203226

204-
it('extracts repository name from git url', function () {
205-
const stubPackageJsonExists = sinon.stub(fs, 'pathExistsSync').returns(true)
227+
it('extracts repository name from git url', function() {
228+
const stubPackageJsonExists = sinon.stub(fs, 'existsSync').returns(true)
206229
const alteredPackageJsonStub = JSON.parse(packageJsonStub)
207230
alteredPackageJsonStub.repository = {
208231
type: 'git',
209232
url: 'https://github.com/npm/repo.git'
210233
}
211-
const stubReadPackageJson = sinon.stub(fs, 'readJSONSync').returns(alteredPackageJsonStub)
234+
const stubReadPackageJson = sinon
235+
.stub(fs, 'readFileSync')
236+
.returns(JSON.stringify(alteredPackageJsonStub))
212237
const stubGetPackages = sinon.stub(node, 'getPackages').returns({})
213238
try {
214239
const res = node.extractPackageInfo('/fake/path')
@@ -222,14 +247,16 @@ describe('Node.js module', async function () {
222247
}
223248
})
224249

225-
it('extracts repository name from svn url', function () {
226-
const stubPackageJsonExists = sinon.stub(fs, 'pathExistsSync').returns(true)
250+
it('extracts repository name from svn url', function() {
251+
const stubPackageJsonExists = sinon.stub(fs, 'existsSync').returns(true)
227252
const alteredPackageJsonStub = JSON.parse(packageJsonStub)
228253
alteredPackageJsonStub.repository = {
229254
type: 'svn',
230255
url: 'https://svn.com/repo'
231256
}
232-
const stubReadPackageJson = sinon.stub(fs, 'readJSONSync').returns(alteredPackageJsonStub)
257+
const stubReadPackageJson = sinon
258+
.stub(fs, 'readFileSync')
259+
.returns(JSON.stringify(alteredPackageJsonStub))
233260
const stubGetPackages = sinon.stub(node, 'getPackages').returns({})
234261
try {
235262
const res = node.extractPackageInfo('/fake/path')
@@ -243,14 +270,16 @@ describe('Node.js module', async function () {
243270
}
244271
})
245272

246-
it('returns an empty string for an unknown url type', function () {
247-
const stubPackageJsonExists = sinon.stub(fs, 'pathExistsSync').returns(true)
273+
it('returns an empty string for an unknown url type', function() {
274+
const stubPackageJsonExists = sinon.stub(fs, 'existsSync').returns(true)
248275
const alteredPackageJsonStub = JSON.parse(packageJsonStub)
249276
alteredPackageJsonStub.repository = {
250277
type: 'mercurial',
251278
url: 'https://svn.com/repo'
252279
}
253-
const stubReadPackageJson = sinon.stub(fs, 'readJSONSync').returns(alteredPackageJsonStub)
280+
const stubReadPackageJson = sinon
281+
.stub(fs, 'readFileSync')
282+
.returns(JSON.stringify(alteredPackageJsonStub))
254283
const stubGetPackages = sinon.stub(node, 'getPackages').returns({})
255284
try {
256285
const res = node.extractPackageInfo('/fake/path')

0 commit comments

Comments
 (0)