-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
130 additions
and
116 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,5 +1,6 @@ | ||
// added for Jest inline snapshots to not use default Prettier config | ||
module.exports = { | ||
singleQuote: true, | ||
trailingComma: 'all', | ||
bracketSpacing: false, | ||
trailingComma: "all" | ||
} | ||
jsxBracketSameLine: 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 |
---|---|---|
|
@@ -4,13 +4,10 @@ import {fetch} from '../../../tools/fetch'; | |
|
||
jest.mock('../../../tools/fetch', () => ({fetch: jest.fn()})); | ||
|
||
const VERSION = '0.58.0'; | ||
const RN_WITH_VERSION = '[email protected]'; | ||
const RN_NPM_PACKAGE = 'react-native'; | ||
const ABS_RN_PATH = '/path/to/react-native'; | ||
const ABS_RN_TARBALL_PATH = '/path/to/react-native/react-native-1.2.3-rc.0.tgz'; | ||
const PACKAGE_NAME = 'react-native'; | ||
|
||
test('should support file protocol with absolute path', async () => { | ||
test('supports file protocol with absolute path', async () => { | ||
jest.mock( | ||
`${ABS_RN_PATH}/package.json`, | ||
() => ({ | ||
|
@@ -20,18 +17,11 @@ test('should support file protocol with absolute path', async () => { | |
); | ||
expect(await processTemplateName(`file://${ABS_RN_PATH}`)).toEqual({ | ||
uri: ABS_RN_PATH, | ||
name: PACKAGE_NAME, | ||
name: RN_NPM_PACKAGE, | ||
}); | ||
}); | ||
|
||
test('should get default package if none protocols were handled', async () => { | ||
expect(await processTemplateName(VERSION)).toEqual({ | ||
uri: VERSION, | ||
name: VERSION, | ||
}); | ||
}); | ||
|
||
test('should support shorthand templates', async () => { | ||
test('supports shorthand templates', async () => { | ||
const templateName = 'typescript'; | ||
(fetch: any).mockImplementationOnce(() => { | ||
return Promise.resolve(`{"name": "react-native-template-${templateName}"}`); | ||
|
@@ -42,7 +32,7 @@ test('should support shorthand templates', async () => { | |
}); | ||
}); | ||
|
||
test('should support not-found shorthand templates', async () => { | ||
test('supports not-found shorthand templates', async () => { | ||
const templateName = 'typescriptz'; | ||
(fetch: any).mockImplementationOnce(() => { | ||
return Promise.resolve('Not found'); | ||
|
@@ -53,14 +43,29 @@ test('should support not-found shorthand templates', async () => { | |
}); | ||
}); | ||
|
||
test('should get package if none protocols were handled', async () => { | ||
expect(await processTemplateName(RN_WITH_VERSION)).toEqual({ | ||
uri: RN_WITH_VERSION, | ||
name: RN_WITH_VERSION, | ||
test('supports npm packages as template names', async () => { | ||
expect(await processTemplateName(RN_NPM_PACKAGE)).toEqual({ | ||
uri: RN_NPM_PACKAGE, | ||
name: RN_NPM_PACKAGE, | ||
}); | ||
}); | ||
|
||
test('should support path to tgz archives', async () => { | ||
test.each` | ||
templateName | uri | name | ||
${'[email protected]'} | ${'[email protected]'} | ${'react-native'} | ||
${'some-name@latest'} | ${'some-name@latest'} | ${'some-name'} | ||
${'@scoped/[email protected]'} | ${'@scoped/[email protected]'} | ${'@scoped/name'} | ||
${'@scoped/name@tag'} | ${'@scoped/name@tag'} | ${'@scoped/name'} | ||
`( | ||
'supports versioned npm package "$templateName" as template name', | ||
async ({templateName, uri, name}) => { | ||
expect(await processTemplateName(templateName)).toEqual({uri, name}); | ||
}, | ||
); | ||
|
||
test('supports path to tgz archives', async () => { | ||
const ABS_RN_TARBALL_PATH = | ||
'/path/to/react-native/react-native-1.2.3-rc.0.tgz'; | ||
expect(await processTemplateName(`file://${ABS_RN_TARBALL_PATH}`)).toEqual({ | ||
uri: `file://${ABS_RN_TARBALL_PATH}`, | ||
name: 'react-native', | ||
|
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ const FILE_PROTOCOL = /file:/; | |
const HTTP_PROTOCOL = /https?:/; | ||
const TARBALL = /\.tgz$/; | ||
const VERSION_POSTFIX = /(.*)(-\d+\.\d+\.\d+)/; | ||
const VERSIONED_PACKAGE = /(@?.*)(@)(.*)/; | ||
|
||
function handleFileProtocol(filePath: string) { | ||
const uri = new URL(filePath).pathname; | ||
|
@@ -32,13 +33,29 @@ function handleTarball(filePath: string) { | |
}; | ||
} | ||
|
||
function handleVersionedPackage(versionedPackage: string) { | ||
const versionedPackageMatch = versionedPackage.match(VERSIONED_PACKAGE); | ||
if (!versionedPackageMatch) { | ||
throw new Error( | ||
`Failed to retrieve package name. We expect the package to include name and version, e.g.: "[email protected]", but received: "${versionedPackage}".`, | ||
); | ||
} | ||
return { | ||
uri: versionedPackage, | ||
name: versionedPackageMatch[1], | ||
}; | ||
} | ||
|
||
export async function processTemplateName(templateName: string) { | ||
if (templateName.match(TARBALL)) { | ||
return handleTarball(templateName); | ||
} | ||
if (templateName.match(FILE_PROTOCOL)) { | ||
return handleFileProtocol(templateName); | ||
} | ||
if (templateName.match(VERSIONED_PACKAGE)) { | ||
return handleVersionedPackage(templateName); | ||
} | ||
|
||
const name = await tryTemplateShorthand(templateName); | ||
|
||
|
12 changes: 12 additions & 0 deletions
12
packages/cli/src/tools/config/__mocks__/resolveNodeModuleDir.js
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,12 @@ | ||
/** | ||
* @flow | ||
*/ | ||
|
||
const path = require('path'); | ||
|
||
export default function resolveNodeModuleDir( | ||
root: string, | ||
packageName: string, | ||
): string { | ||
return path.join(root, 'node_modules', packageName); | ||
} |
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,19 @@ | ||
/** | ||
* @flow | ||
*/ | ||
import path from 'path'; | ||
|
||
/** | ||
* Finds a path inside `node_modules` | ||
*/ | ||
export default function resolveNodeModuleDir( | ||
root: string, | ||
packageName: string, | ||
): string { | ||
return path.dirname( | ||
// $FlowIssue: Wrong `require.resolve` type definition | ||
require.resolve(path.join(packageName, 'package.json'), { | ||
paths: [root], | ||
}), | ||
); | ||
} |
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
Oops, something went wrong.