diff --git a/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.js b/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts similarity index 89% rename from packages/cli/src/tools/generator/copyProjectTemplateAndReplace.js rename to packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts index 285cce5d3..1646d18ec 100644 --- a/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.js +++ b/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts @@ -9,13 +9,22 @@ import chalk from 'chalk'; import path from 'path'; +// @ts-ignore FIXME: copyAndReplace should be ts import copyAndReplace from '../copyAndReplace'; import promptInitializer from './promptSync'; +// @ts-ignore FIXME: walk should be ts import walk from '../walk'; import {logger} from '@react-native-community/cli-tools'; const prompt = promptInitializer(); +type Options = { + upgrade?: boolean; + force?: boolean; + displayName?: string; + ignorePaths?: string[]; +}; + /** * Util for creating a new React Native project. * Copy the project from a template and use the correct project name in @@ -31,10 +40,10 @@ const prompt = promptInitializer(); * } */ function copyProjectTemplateAndReplace( - srcPath, - destPath, - newProjectName, - options = {}, + srcPath: string, + destPath: string, + newProjectName: string, + options: Options = {}, ) { if (!srcPath) { throw new Error('Need a path to copy from'); @@ -46,7 +55,7 @@ function copyProjectTemplateAndReplace( throw new Error('Need a project name'); } - walk(srcPath).forEach(absoluteSrcFilePath => { + walk(srcPath).forEach((absoluteSrcFilePath: string) => { // 'react-native upgrade' if (options.upgrade) { // Don't upgrade these files @@ -90,7 +99,7 @@ function copyProjectTemplateAndReplace( let contentChangedCallback = null; if (options.upgrade && !options.force) { - contentChangedCallback = (_, contentChanged) => + contentChangedCallback = (_destPath: string, contentChanged: string) => upgradeFileContentChangedCallback( absoluteSrcFilePath, relativeFilePath, @@ -118,7 +127,7 @@ function copyProjectTemplateAndReplace( * This is especially important for .gitignore because npm has some special * behavior of automatically renaming .gitignore to .npmignore. */ -function translateFilePath(filePath) { +function translateFilePath(filePath: string) { if (!filePath) { return filePath; } @@ -135,9 +144,9 @@ function translateFilePath(filePath) { } function upgradeFileContentChangedCallback( - absoluteSrcFilePath, - relativeDestPath, - contentChanged, + absoluteSrcFilePath: string, + relativeDestPath: string, + contentChanged: string, ) { if (contentChanged === 'new') { logger.info(`${chalk.bold('new')} ${relativeDestPath}`); diff --git a/packages/cli/src/tools/generator/promptSync.js b/packages/cli/src/tools/generator/promptSync.ts similarity index 82% rename from packages/cli/src/tools/generator/promptSync.js rename to packages/cli/src/tools/generator/promptSync.ts index 8c73a5a2c..bef753e5a 100644 --- a/packages/cli/src/tools/generator/promptSync.js +++ b/packages/cli/src/tools/generator/promptSync.ts @@ -12,19 +12,30 @@ import fs from 'fs'; +type Options = { + echo?: string; + ask?: string; + value?: string; + autocomplete?: string[] | Function; +}; + const term = 13; // carriage return function create() { return prompt; - function prompt(ask, value, opts) { + function prompt( + ask?: string | Options, + value?: string | Options, + opts?: Options, + ) { let insert = 0; opts = opts || {}; - if (Object(ask) === ask) { + if (typeof ask === 'object') { opts = ask; ask = opts.ask; - } else if (Object(value) === value) { + } else if (typeof value === 'object') { opts = value; value = opts.value; } @@ -32,13 +43,16 @@ function create() { const echo = opts.echo; const masked = 'echo' in opts; + /*eslint-disable prettier/prettier*/ const fd = process.platform === 'win32' + // @ts-ignore ? process.stdin.fd : fs.openSync('/dev/tty', 'rs'); + /*eslint-enable prettier/prettier*/ const wasRaw = process.stdin.isRaw; - if (!wasRaw) { + if (!wasRaw && process.stdin.setRawMode) { process.stdin.setRawMode(true); } @@ -54,7 +68,7 @@ function create() { } while (true) { - read = fs.readSync(fd, buf, 0, 3); + read = fs.readSync(fd, buf, 0, 3, null); if (read > 1) { // received a control sequence if (buf.toString()) { @@ -76,7 +90,9 @@ function create() { process.stdout.write('^C\n'); fs.closeSync(fd); process.exit(130); - process.stdin.setRawMode(wasRaw); + if (process.stdin.setRawMode) { + process.stdin.setRawMode(!!wasRaw); + } return null; } @@ -130,7 +146,9 @@ function create() { process.stdout.write('\n'); - process.stdin.setRawMode(wasRaw); + if (process.stdin.setRawMode) { + process.stdin.setRawMode(!!wasRaw); + } return str || value || ''; } diff --git a/packages/cli/src/tools/generator/templates.js b/packages/cli/src/tools/generator/templates.ts similarity index 90% rename from packages/cli/src/tools/generator/templates.js rename to packages/cli/src/tools/generator/templates.ts index 76b43632f..4ff6370af 100644 --- a/packages/cli/src/tools/generator/templates.js +++ b/packages/cli/src/tools/generator/templates.ts @@ -13,7 +13,6 @@ import fs from 'fs'; import path from 'path'; import copyProjectTemplateAndReplace from './copyProjectTemplateAndReplace'; import {logger} from '@react-native-community/cli-tools'; -// $FlowFixMe - converted to TS import * as PackageManager from '../packageManager'; /** @@ -27,7 +26,6 @@ async function createProjectFromTemplate( destPath: string, newProjectName: string, template: string, - destinationRoot: string, ) { const templatePath = path.dirname(require.resolve('react-native/template')); copyProjectTemplateAndReplace(templatePath, destPath, newProjectName); @@ -45,12 +43,7 @@ async function createProjectFromTemplate( // This way we don't have to duplicate the native files in every template. // If we duplicated them we'd make RN larger and risk that people would // forget to maintain all the copies so they would go out of sync. - await createFromRemoteTemplate( - template, - destPath, - newProjectName, - destinationRoot, - ); + await createFromRemoteTemplate(template, destPath, newProjectName); } /** @@ -62,7 +55,6 @@ async function createFromRemoteTemplate( template: string, destPath: string, newProjectName: string, - destinationRoot: string, ) { let installPackage; let templateName; @@ -92,8 +84,8 @@ async function createFromRemoteTemplate( 'devDependencies.json', ], }); - await installTemplateDependencies(templatePath, destinationRoot); - await installTemplateDevDependencies(templatePath, destinationRoot); + await installTemplateDependencies(templatePath); + await installTemplateDevDependencies(templatePath); } finally { // Clean up the temp files try { @@ -109,7 +101,7 @@ async function createFromRemoteTemplate( } } -async function installTemplateDependencies(templatePath, destinationRoot) { +async function installTemplateDependencies(templatePath: string) { // dependencies.json is a special file that lists additional dependencies // that are required by this template const dependenciesJsonPath = path.resolve(templatePath, 'dependencies.json'); @@ -119,7 +111,7 @@ async function installTemplateDependencies(templatePath, destinationRoot) { return; } - let dependencies; + let dependencies: any; try { dependencies = require(dependenciesJsonPath); } catch (err) { @@ -135,7 +127,7 @@ async function installTemplateDependencies(templatePath, destinationRoot) { execSync('react-native link', {stdio: 'inherit'}); } -async function installTemplateDevDependencies(templatePath, destinationRoot) { +async function installTemplateDevDependencies(templatePath: string) { // devDependencies.json is a special file that lists additional develop dependencies // that are required by this template const devDependenciesJsonPath = path.resolve( @@ -148,7 +140,7 @@ async function installTemplateDevDependencies(templatePath, destinationRoot) { return; } - let dependencies; + let dependencies: any; try { dependencies = require(devDependenciesJsonPath); } catch (err) {