Skip to content

Commit

Permalink
feat: refactor template name logic (react-native-community#1325)
Browse files Browse the repository at this point in the history
* feat: refactor template name lgoic

* chore: remove console log

* chore: update docs

* chore: fix lint errors
  • Loading branch information
grabbou authored Nov 25, 2020
1 parent ecbfbd2 commit ef98aee
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 188 deletions.
16 changes: 11 additions & 5 deletions docs/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ npx react-native init ProjectName --version ${VERSION}
npx react-native@${VERSION} init ProjectName
```

#### Initializing project with custom template.
#### Initializing project with custom template

In following examples `TEMPLATE_NAME` can be either:
It is possible to initialise a new application with a custom template with
a `--template` option.

It should point to a valid package that can be installed with `yarn` or `npm` (if you're using `--npm` option).

The most common options are:
- Full package name, eg. `react-native-template-typescript`.
- Absolute path to directory containing template, eg. `file:///Users/username/project/some-template`.
- Absolute path to a tarball created using `npm pack`.
- Link to a GitHub repository (supports `username/repo` format).

For all available options, please check [Yarn documentation](https://classic.yarnpkg.com/en/docs/cli/add/#toc-adding-dependencies) and [Npm](https://docs.npmjs.com/cli/v6/commands/npm-install#synopsis).


```sh
# This will initialize new project using template from TEMPLATE_NAME package
# This will initialize new project using template from `react-native-template-typescript` package
npx react-native init ProjectName --template ${TEMPLATE_NAME}

# This will initialize new project using init command from react-native@VERSION but will use TEMPLATE_NAME custom template
# This will initialize new project using init command from react-native@VERSION but will use a custom template
npx react-native@${VERSION} init ProjectName --template ${TEMPLATE_NAME}
```

Expand Down
56 changes: 0 additions & 56 deletions packages/cli/src/commands/init/__tests__/templateName.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
{
name: '--template [string]',
description:
'Uses a custom template. Valid arguments are: npm package, absolute directory prefixed with `file://`, Git repository or a tarball',
'Uses a custom template. Valid arguments are the ones supported by `yarn add [package]` or `npm install [package]`, if you are using `--npm` option',
},
{
name: '--npm',
Expand Down
40 changes: 22 additions & 18 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import {changePlaceholderInTemplate} from './editTemplate';
import * as PackageManager from '../../tools/packageManager';
import installPods from '../../tools/installPods';
import {processTemplateName} from './templateName';
import banner from './banner';
import {getLoader} from '../../tools/loader';

Expand All @@ -34,7 +33,7 @@ type Options = {

interface TemplateOptions {
projectName: string;
templateName: string;
templateUri: string;
npm?: boolean;
directory: string;
projectTitle?: string;
Expand Down Expand Up @@ -63,22 +62,20 @@ async function setProjectDirectory(directory: string) {
return process.cwd();
}

function adjustNameIfUrl(name: string, cwd: string) {
function getTemplateName(cwd: string) {
// We use package manager to infer the name of the template module for us.
// That's why we get it from temporary package.json, where the name is the
// first and only dependency (hence 0).
if (name.match(/https?:/)) {
name = Object.keys(
JSON.parse(fs.readFileSync(path.join(cwd, './package.json'), 'utf8'))
.dependencies,
)[0];
}
const name = Object.keys(
JSON.parse(fs.readFileSync(path.join(cwd, './package.json'), 'utf8'))
.dependencies,
)[0];
return name;
}

async function createFromTemplate({
projectName,
templateName,
templateUri,
npm,
directory,
projectTitle,
Expand All @@ -97,16 +94,19 @@ async function createFromTemplate({

try {
loader.start();
let {uri, name} = await processTemplateName(templateName);

await installTemplatePackage(uri, templateSourceDir, npm);
await installTemplatePackage(templateUri, templateSourceDir, npm);

loader.succeed();
loader.start('Copying template');

name = adjustNameIfUrl(name, templateSourceDir);
const templateConfig = getTemplateConfig(name, templateSourceDir);
await copyTemplate(name, templateConfig.templateDir, templateSourceDir);
const templateName = getTemplateName(templateSourceDir);
const templateConfig = getTemplateConfig(templateName, templateSourceDir);
await copyTemplate(
templateName,
templateConfig.templateDir,
templateSourceDir,
);

loader.succeed();
loader.start('Processing template');
Expand All @@ -123,7 +123,11 @@ async function createFromTemplate({
if (postInitScript) {
// Leaving trailing space because there may be stdout from the script
loader.start('Executing post init script ');
await executePostInitScript(name, postInitScript, templateSourceDir);
await executePostInitScript(
templateName,
postInitScript,
templateSourceDir,
);
loader.succeed();
}

Expand Down Expand Up @@ -177,11 +181,11 @@ async function createProject(
version: string,
options: Options,
) {
const templateName = options.template || `react-native@${version}`;
const templateUri = options.template || `react-native@${version}`;

return createFromTemplate({
projectName,
templateName,
templateUri,
npm: options.npm,
directory,
projectTitle: options.title,
Expand Down
108 changes: 0 additions & 108 deletions packages/cli/src/commands/init/templateName.ts

This file was deleted.

0 comments on commit ef98aee

Please sign in to comment.