-
Notifications
You must be signed in to change notification settings - Fork 931
chore: convert init command to TS #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,13 @@ | ||
| // @flow | ||
| jest.mock('execa', () => jest.fn()); | ||
| import execa from 'execa'; | ||
| import path from 'path'; | ||
| // $FlowFixMe - converted to TS | ||
| import * as PackageManger from '../../../tools/packageManager'; | ||
| import { | ||
| installTemplatePackage, | ||
| getTemplateConfig, | ||
| copyTemplate, | ||
| executePostInitScript, | ||
| } from '../template'; | ||
| // $FlowFixMe - converted to TS | ||
| import * as copyFiles from '../../../tools/copyFiles'; | ||
|
|
||
| const TEMPLATE_NAME = 'templateName'; | ||
|
|
@@ -22,7 +19,7 @@ afterEach(() => { | |
| }); | ||
|
|
||
| test('installTemplatePackage', async () => { | ||
| jest.spyOn(PackageManger, 'install').mockImplementationOnce(() => {}); | ||
| jest.spyOn(PackageManger, 'install').mockImplementationOnce(() => null); | ||
|
|
||
| await installTemplatePackage(TEMPLATE_NAME, TEMPLATE_SOURCE_DIR, true); | ||
|
|
||
|
|
@@ -63,7 +60,7 @@ test('copyTemplate', async () => { | |
| const CWD = '.'; | ||
|
|
||
| jest.spyOn(path, 'resolve').mockImplementationOnce((...e) => e.join('/')); | ||
| jest.spyOn(copyFiles, 'default').mockImplementationOnce(() => {}); | ||
| jest.spyOn(copyFiles, 'default').mockImplementationOnce(() => null); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the comment above. |
||
| jest.spyOn(process, 'cwd').mockImplementationOnce(() => CWD); | ||
|
|
||
| await copyTemplate(TEMPLATE_NAME, TEMPLATE_DIR, TEMPLATE_SOURCE_DIR); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| import {processTemplateName} from '../templateName'; | ||
|
|
||
| const RN_NPM_PACKAGE = 'react-native'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| import {validateProjectName} from '../validate'; | ||
| import InvalidNameError from '../errors/InvalidNameError'; | ||
| import ReservedNameError from '../errors/ReservedNameError'; | ||
|
|
@@ -28,6 +27,8 @@ test.each([ | |
| name: 'helloworld_test', | ||
| error: HelloWorldError, | ||
| }, | ||
| ])("'%s' is invalid name", ({name, error}: {name: string, error: Error}) => { | ||
| // @ts-ignore-next-line FIXME extending the Error class causes weird TS validation errors | ||
| // https://stackoverflow.com/questions/41102060/typescript-extending-error-class | ||
| ])("'%s' is invalid name", ({name, error}: {name: string; error: Error}) => { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only unsolved case from the conversion. |
||
| expect(() => validateProjectName(name)).toThrowError(error); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| import chalk from 'chalk'; | ||
|
|
||
| const reactLogoArray = [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| export default class HelloWorldError extends Error { | ||
| constructor() { | ||
| super( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| export default class InvalidNameError extends Error { | ||
| constructor(name: string) { | ||
| super( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| export default class ReservedNameError extends Error { | ||
| constructor() { | ||
| super( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| import init from './init'; | ||
|
|
||
| export default { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,63 @@ | ||
| // @flow | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
| import fs from 'fs-extra'; | ||
| import Ora from 'ora'; | ||
| import minimist from 'minimist'; | ||
| import ora from 'ora'; | ||
thymikee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import semver from 'semver'; | ||
| // @ts-ignore untyped | ||
| import inquirer from 'inquirer'; | ||
| import mkdirp from 'mkdirp'; | ||
| import type {ConfigT} from 'types'; | ||
| import {validateProjectName} from './validate'; | ||
| import DirectoryAlreadyExistsError from './errors/DirectoryAlreadyExistsError'; | ||
| import printRunInstructions from './printRunInstructions'; | ||
| import {logger} from '@react-native-community/cli-tools'; | ||
| import {CLIError, logger} from '@react-native-community/cli-tools'; | ||
| import {Config} from '@react-native-community/cli-types'; | ||
| import { | ||
| installTemplatePackage, | ||
| getTemplateConfig, | ||
| copyTemplate, | ||
| executePostInitScript, | ||
| } from './template'; | ||
| import {changePlaceholderInTemplate} from './editTemplate'; | ||
| // $FlowFixMe - converted to TS | ||
| import * as PackageManager from '../../tools/packageManager'; | ||
| // $FlowFixMe - converted to TS | ||
| import installPods from '../../tools/installPods'; | ||
| import {processTemplateName} from './templateName'; | ||
| import banner from './banner'; | ||
| // $FlowFixMe - converted to TS | ||
| import {getLoader} from '../../tools/loader'; | ||
| import {CLIError} from '@react-native-community/cli-tools'; | ||
|
|
||
| const DEFAULT_VERSION = 'latest'; | ||
|
|
||
| type Options = {| | ||
| template?: string, | ||
| npm?: boolean, | ||
| directory?: string, | ||
| displayName?: string, | ||
| title?: string, | ||
| |}; | ||
| type Options = { | ||
| template?: string; | ||
| npm?: boolean; | ||
| directory?: string; | ||
| displayName?: string; | ||
| title?: string; | ||
| }; | ||
|
|
||
| interface TemplateOptions { | ||
| projectName: string; | ||
| templateName: string; | ||
| npm?: boolean; | ||
| directory: string; | ||
| projectTitle?: string; | ||
| } | ||
|
|
||
| function doesDirectoryExist(dir: string) { | ||
| return fs.existsSync(dir); | ||
| } | ||
|
|
||
| function getProjectDirectory({projectName, directory}): string { | ||
| function getProjectDirectory({ | ||
| projectName, | ||
| directory, | ||
| }: { | ||
| projectName: string; | ||
| directory: string; | ||
| }): string { | ||
| return path.relative(process.cwd(), directory || projectName); | ||
| } | ||
|
|
||
| async function setProjectDirectory(directory) { | ||
| async function setProjectDirectory(directory: string) { | ||
| const directoryExists = doesDirectoryExist(directory); | ||
| if (directoryExists) { | ||
| const {shouldReplaceprojectDirectory} = await inquirer.prompt([ | ||
|
|
@@ -79,7 +89,7 @@ async function setProjectDirectory(directory) { | |
| } | ||
| } | ||
|
|
||
| function adjustNameIfUrl(name, cwd) { | ||
| function adjustNameIfUrl(name: string, 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). | ||
|
|
@@ -98,13 +108,7 @@ async function createFromTemplate({ | |
| npm, | ||
| directory, | ||
| projectTitle, | ||
| }: { | ||
| projectName: string, | ||
| templateName: string, | ||
| npm?: boolean, | ||
| directory: string, | ||
| projectTitle?: string, | ||
| }) { | ||
| }: TemplateOptions) { | ||
| logger.debug('Initializing new project'); | ||
| logger.log(banner); | ||
|
|
||
|
|
@@ -136,7 +140,7 @@ async function createFromTemplate({ | |
| projectName, | ||
| projectTitle, | ||
| placeholderName: templateConfig.placeholderName, | ||
| titlePlaceholder: templateConfig.titlePlaceholder, | ||
| placeholderTitle: templateConfig.titlePlaceholder, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. cc @Esemesek – 0.61 is right behind the corner, so I guess we'll need to keep this name for a while? |
||
| }); | ||
|
|
||
| loader.succeed(); | ||
|
|
@@ -162,9 +166,9 @@ async function installDependencies({ | |
| npm, | ||
| loader, | ||
| }: { | ||
| projectName: string, | ||
| npm?: boolean, | ||
| loader: typeof Ora, | ||
| projectName: string; | ||
| npm?: boolean; | ||
| loader: ora.Ora; | ||
| }) { | ||
| loader.start('Installing dependencies'); | ||
|
|
||
|
|
@@ -209,7 +213,7 @@ async function createProject( | |
|
|
||
| export default (async function initialize( | ||
| [projectName]: Array<string>, | ||
| context: ConfigT, | ||
| context: Config, | ||
| options: Options, | ||
| ) { | ||
| const rootFolder = context.root; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| import path from 'path'; | ||
| import {URL} from 'url'; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the mock implementation to avoid type conflicts. There was no change in the overall outcome from the unit tests since we're not asserting the result from the mock.