Skip to content

Commit

Permalink
chore: remove unstable init --template shorthand support (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored Jul 9, 2019
1 parent 9e2be6c commit a3d468e
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 53 deletions.
1 change: 0 additions & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ Uses a custom directory instead of `<projectName>`.
Uses a custom template. Accepts following template sources:

- an npm package name
- a shorthand name for packages prefixed with `react-native-template-`
- an absolute path to a local directory
- an absolute path to a tarball created using `npm pack`

Expand Down
1 change: 0 additions & 1 deletion docs/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ npx react-native@${VERSION} init ProjectName
In following examples `TEMPLATE_NAME` can be either:

- Full package name, eg. `react-native-template-typescript`.
- Shorthand name of template, eg. `typescript`.
- Absolute path to directory containing template, eg. `file:///Users/username/project/some-template`.
- Absolute path to a tarball created using `npm pack`.

Expand Down
22 changes: 0 additions & 22 deletions packages/cli/src/commands/init/__tests__/templateName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,6 @@ test('supports file protocol with absolute path', async () => {
});
});

test('supports shorthand templates', async () => {
const templateName = 'typescript';
(fetch: any).mockImplementationOnce(() => {
return Promise.resolve(`{"name": "react-native-template-${templateName}"}`);
});
expect(await processTemplateName(templateName)).toEqual({
uri: `react-native-template-${templateName}`,
name: `react-native-template-${templateName}`,
});
});

test('supports not-found shorthand templates', async () => {
const templateName = 'typescriptz';
(fetch: any).mockImplementationOnce(() => {
return Promise.resolve('Not found');
});
expect(await processTemplateName(templateName)).toEqual({
uri: templateName,
name: templateName,
});
});

test('supports npm packages as template names', async () => {
expect(await processTemplateName(RN_NPM_PACKAGE)).toEqual({
uri: RN_NPM_PACKAGE,
Expand Down
31 changes: 2 additions & 29 deletions packages/cli/src/commands/init/templateName.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,8 @@ export async function processTemplateName(templateName: string) {
return handleVersionedPackage(templateName);
}

const name = await tryTemplateShorthand(templateName);

return {
uri: name,
name,
uri: templateName,
name: templateName,
};
}

/**
* `init` may be invoked with a shorthand like `--template typescript`
* which should resolve to `react-native-template-typescript` package.
* To support that, we query npm registry if a package like this exists, if not
* we return the original name without a change.
*/
async function tryTemplateShorthand(templateName: string) {
if (templateName.match(FILE_PROTOCOL) || templateName.match(HTTP_PROTOCOL)) {
return templateName;
}
try {
const reactNativeTemplatePackage = `react-native-template-${templateName}`;
const response = await fetch(
`https://registry.yarnpkg.com/${reactNativeTemplatePackage}/latest`,
);

if (JSON.parse(response).name) {
return reactNativeTemplatePackage;
}
} catch (e) {
// we expect this to fail when `file://` protocol or regular module is passed
}
return templateName;
}

0 comments on commit a3d468e

Please sign in to comment.