-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #503 from connectdotz/yarn-lookup-fix
fixed using wrong package manager in yarn workspace projects
- Loading branch information
Showing
10 changed files
with
2,564 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"format": "prettier --single-quote --trailing-comma=es5 --print-width=100 --write '{create-react-native-app,react-native-scripts}/{src,template}/**/*.js'" | ||
"format": "prettier --single-quote --trailing-comma=es5 --print-width=100 --write '{create-react-native-app,react-native-scripts}/{src,template}/**/*.js'", | ||
"test": "jest" | ||
}, | ||
"devDependencies": { | ||
"jest": "^21.2.1", | ||
"prettier": "^0.21.0" | ||
}, | ||
"jest": { | ||
"testPathIgnorePatterns": [ | ||
"node_modules", | ||
"template" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// based on jest official doc: https://facebook.github.io/jest/docs/en/manual-mocks.html | ||
// __mocks__/fs.js | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
const fs = jest.genMockFromModule('fs'); | ||
|
||
// This is a custom function that our tests can use during setup to specify | ||
// what the files on the "mock" filesystem should look like when any of the | ||
// `fs` APIs are used. | ||
let mockFiles = Object.create(null); | ||
function __setMockFiles(newMockFiles) { | ||
mockFiles = Object.create(null); | ||
for (const file in newMockFiles) { | ||
const dir = path.dirname(file); | ||
if (!mockFiles[dir]) { | ||
mockFiles[dir] = []; | ||
} | ||
mockFiles[dir].push(path.basename(file)); | ||
} | ||
} | ||
|
||
// A custom version of `readdirSync` that reads from the special mocked out | ||
// file list set via __setMockFiles | ||
function readdirSync(directoryPath) { | ||
return mockFiles[directoryPath] || []; | ||
} | ||
|
||
function accessSync(directoryPath) { | ||
const dir = path.dirname(directoryPath); | ||
const children = mockFiles[dir]; | ||
|
||
if (!children || children.indexOf(path.basename(directoryPath)) < 0) { | ||
throw new TypeError(`no such file/dir: ${directoryPath}`); | ||
} | ||
} | ||
|
||
fs.__setMockFiles = __setMockFiles; | ||
fs.readdirSync = readdirSync; | ||
fs.accessSync = accessSync; | ||
|
||
module.exports = fs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
jest.mock('fs'); | ||
|
||
import { hasYarn } from '../pm'; | ||
|
||
describe('hasYarn', () => { | ||
const MOCK_FILE_INFO = { | ||
'/a/b/yarn.lock': 'fake yarn.lock', | ||
}; | ||
|
||
beforeEach(() => { | ||
// Set up some mocked out file info before each test | ||
require('fs').__setMockFiles(MOCK_FILE_INFO); | ||
}); | ||
test('undefined path will throw exception', () => { | ||
expect(() => hasYarn(undefined, false)).toThrow(); | ||
}); | ||
test('empty path is ok', () => { | ||
expect(hasYarn('', false)).toEqual(false); | ||
}); | ||
test('can find yarn in the given path', () => { | ||
expect(hasYarn('/a/b', false)).toEqual(true); | ||
}); | ||
test('can find yarn in the parent path', () => { | ||
expect(hasYarn('/a/b/c/d/e', false)).toEqual(true); | ||
}); | ||
test('can NOT find yarn in the children path', () => { | ||
expect(hasYarn('/a', false)).toEqual(false); | ||
}); | ||
test('can use cached value 2nd time around', () => { | ||
expect(hasYarn('/a/b/c/d/e', false)).toEqual(true); | ||
expect(hasYarn(undefined, true)).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// @flow | ||
|
||
'use strict'; | ||
|
||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
/** | ||
* check if the current path uses yarn, i.e. looking for yarn.lock in | ||
* the current path and up | ||
* | ||
* @param {*} startingPath a path where we will look for yarn.lock file. | ||
* Will traverse up the filesystem until we either find the file or reach the root | ||
* | ||
* @param {boolean} useCached if true and we have a cached hasYarn result, it will be returned, otherwise go through the | ||
* normal lookup logic described above. mainly for optimization purpose, default is true. | ||
*/ | ||
let _hasYarn: ?boolean; | ||
export function hasYarn(startingPath: string, useCached: boolean = true): boolean { | ||
if (_hasYarn != null && useCached) { | ||
return _hasYarn; | ||
} | ||
|
||
_hasYarn = false; | ||
let p = path.normalize(startingPath); | ||
while (p.length > 0) { | ||
const yarnLock = path.resolve(p, 'yarn.lock'); | ||
try { | ||
const file = path.join(p, 'yarn.lock'); | ||
fs.accessSync(file); | ||
_hasYarn = true; | ||
break; | ||
} catch (e) { | ||
const parsed = path.parse(p); | ||
if (parsed.root === parsed.dir) { | ||
break; | ||
} | ||
p = parsed.dir; | ||
} | ||
} | ||
return _hasYarn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.