From 6a80df24a7e82dc80e73756f5b35c97c328ca43c Mon Sep 17 00:00:00 2001 From: kang yoosam Date: Tue, 10 Sep 2019 17:19:02 +0900 Subject: [PATCH 1/8] :+1: convert copyProjectTemplateAndReplace into ts --- ...ce.js => copyProjectTemplateAndReplace.ts} | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) rename packages/cli/src/tools/generator/{copyProjectTemplateAndReplace.js => copyProjectTemplateAndReplace.ts} (91%) diff --git a/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.js b/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts similarity index 91% rename from packages/cli/src/tools/generator/copyProjectTemplateAndReplace.js rename to packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts index 285cce5d3..2ec5e6357 100644 --- a/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.js +++ b/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts @@ -16,6 +16,13 @@ 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 +38,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 +53,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 +97,7 @@ function copyProjectTemplateAndReplace( let contentChangedCallback = null; if (options.upgrade && !options.force) { - contentChangedCallback = (_, contentChanged) => + contentChangedCallback = (_destPath: string, contentChanged: string) => upgradeFileContentChangedCallback( absoluteSrcFilePath, relativeFilePath, @@ -118,7 +125,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 +142,9 @@ function translateFilePath(filePath) { } function upgradeFileContentChangedCallback( - absoluteSrcFilePath, - relativeDestPath, - contentChanged, + absoluteSrcFilePath: string, + relativeDestPath: string, + contentChanged: string, ) { if (contentChanged === 'new') { logger.info(`${chalk.bold('new')} ${relativeDestPath}`); From ded8910a359ac570fe0b7843ba9e477639393947 Mon Sep 17 00:00:00 2001 From: kang yoosam Date: Tue, 10 Sep 2019 19:48:28 +0900 Subject: [PATCH 2/8] :+1: convert template.js into ts --- .../generator/{templates.js => templates.ts} | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) rename packages/cli/src/tools/generator/{templates.js => templates.ts} (94%) diff --git a/packages/cli/src/tools/generator/templates.js b/packages/cli/src/tools/generator/templates.ts similarity index 94% rename from packages/cli/src/tools/generator/templates.js rename to packages/cli/src/tools/generator/templates.ts index 1520c0645..4905cf3ca 100644 --- a/packages/cli/src/tools/generator/templates.js +++ b/packages/cli/src/tools/generator/templates.ts @@ -13,6 +13,8 @@ import fs from 'fs'; import path from 'path'; import copyProjectTemplateAndReplace from './copyProjectTemplateAndReplace'; import {logger} from '@react-native-community/cli-tools'; +// @ts-ignore FIXME: after converting to ts +// eslint-disable-next-line import/namespace import * as PackageManager from '../packageManager'; /** @@ -108,7 +110,10 @@ async function createFromRemoteTemplate( } } -async function installTemplateDependencies(templatePath, destinationRoot) { +async function installTemplateDependencies( + templatePath: string, + _destinationRoot: string, +) { // dependencies.json is a special file that lists additional dependencies // that are required by this template const dependenciesJsonPath = path.resolve(templatePath, 'dependencies.json'); @@ -118,7 +123,7 @@ async function installTemplateDependencies(templatePath, destinationRoot) { return; } - let dependencies; + let dependencies: any; try { dependencies = require(dependenciesJsonPath); } catch (err) { @@ -134,7 +139,10 @@ async function installTemplateDependencies(templatePath, destinationRoot) { execSync('react-native link', {stdio: 'inherit'}); } -async function installTemplateDevDependencies(templatePath, destinationRoot) { +async function installTemplateDevDependencies( + templatePath: string, + _destinationRoot: string, +) { // devDependencies.json is a special file that lists additional develop dependencies // that are required by this template const devDependenciesJsonPath = path.resolve( @@ -147,7 +155,7 @@ async function installTemplateDevDependencies(templatePath, destinationRoot) { return; } - let dependencies; + let dependencies: any; try { dependencies = require(devDependenciesJsonPath); } catch (err) { From 333099b5726376fde2fe8778c92803d3df1ec7b7 Mon Sep 17 00:00:00 2001 From: kang yoosam Date: Tue, 10 Sep 2019 20:18:48 +0900 Subject: [PATCH 3/8] :+1: convert promptSync.js into ts --- .../{promptSync.js => promptSync.ts} | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) rename packages/cli/src/tools/generator/{promptSync.js => promptSync.ts} (82%) 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..8aa7dbcdd 100644 --- a/packages/cli/src/tools/generator/promptSync.js +++ b/packages/cli/src/tools/generator/promptSync.ts @@ -12,19 +12,26 @@ 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, 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 +39,14 @@ function create() { const echo = opts.echo; const masked = 'echo' in opts; - const fd = - process.platform === 'win32' - ? process.stdin.fd - : fs.openSync('/dev/tty', 'rs'); + let fd = fs.openSync('/dev/tty', 'rs'); + if (process.platform === 'win32') { + // @ts-ignore MEMO: fd exists when win32 + fd = process.stdin.fd; + } const wasRaw = process.stdin.isRaw; - if (!wasRaw) { + if (!wasRaw && process.stdin.setRawMode) { process.stdin.setRawMode(true); } @@ -54,7 +62,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 +84,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 +140,9 @@ function create() { process.stdout.write('\n'); - process.stdin.setRawMode(wasRaw); + if (process.stdin.setRawMode) { + process.stdin.setRawMode(!!wasRaw); + } return str || value || ''; } From 2a9bb58bb69328e0b7d47c25f74163fdf0216cd6 Mon Sep 17 00:00:00 2001 From: kang yoosam Date: Tue, 10 Sep 2019 20:38:11 +0900 Subject: [PATCH 4/8] :green_heart: update type --- .../generator/copyProjectTemplateAndReplace.ts | 2 ++ packages/cli/src/tools/generator/promptSync.ts | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts b/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts index 2ec5e6357..1646d18ec 100644 --- a/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts +++ b/packages/cli/src/tools/generator/copyProjectTemplateAndReplace.ts @@ -9,8 +9,10 @@ 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'; diff --git a/packages/cli/src/tools/generator/promptSync.ts b/packages/cli/src/tools/generator/promptSync.ts index 8aa7dbcdd..a7fa9dece 100644 --- a/packages/cli/src/tools/generator/promptSync.ts +++ b/packages/cli/src/tools/generator/promptSync.ts @@ -13,10 +13,10 @@ import fs from 'fs'; type Options = { - echo: string; - ask: string; - value: string; - autocomplete: string[] | Function; + echo?: string; + ask?: string; + value?: string; + autocomplete?: string[] | Function; }; const term = 13; // carriage return @@ -24,7 +24,11 @@ const term = 13; // carriage return function create() { return prompt; - function prompt(ask: string | Options, value: string, opts: Options) { + function prompt( + ask?: string | Options, + value?: string | Options, + opts?: Options, + ) { let insert = 0; opts = opts || {}; From f817839cba4bfcb1ba5df24839a174ae46163d32 Mon Sep 17 00:00:00 2001 From: kang yoosam Date: Tue, 10 Sep 2019 21:40:29 +0900 Subject: [PATCH 5/8] :+1: remove unused argument --- packages/cli/src/tools/generator/templates.ts | 23 ++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/packages/cli/src/tools/generator/templates.ts b/packages/cli/src/tools/generator/templates.ts index 4d38a807b..4ff6370af 100644 --- a/packages/cli/src/tools/generator/templates.ts +++ b/packages/cli/src/tools/generator/templates.ts @@ -26,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); @@ -44,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); } /** @@ -61,7 +55,6 @@ async function createFromRemoteTemplate( template: string, destPath: string, newProjectName: string, - destinationRoot: string, ) { let installPackage; let templateName; @@ -91,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 { @@ -108,10 +101,7 @@ async function createFromRemoteTemplate( } } -async function installTemplateDependencies( - templatePath: string, - _destinationRoot: string, -) { +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'); @@ -137,10 +127,7 @@ async function installTemplateDependencies( execSync('react-native link', {stdio: 'inherit'}); } -async function installTemplateDevDependencies( - templatePath: string, - _destinationRoot: string, -) { +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( From efda26999c9d49c1d893b7f194370a36da119275 Mon Sep 17 00:00:00 2001 From: kang yoosam Date: Tue, 10 Sep 2019 21:48:52 +0900 Subject: [PATCH 6/8] :+1: use conditional assignment --- packages/cli/src/tools/generator/promptSync.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/tools/generator/promptSync.ts b/packages/cli/src/tools/generator/promptSync.ts index a7fa9dece..ab03fc7cd 100644 --- a/packages/cli/src/tools/generator/promptSync.ts +++ b/packages/cli/src/tools/generator/promptSync.ts @@ -43,11 +43,10 @@ function create() { const echo = opts.echo; const masked = 'echo' in opts; - let fd = fs.openSync('/dev/tty', 'rs'); - if (process.platform === 'win32') { - // @ts-ignore MEMO: fd exists when win32 - fd = process.stdin.fd; - } + const fd = + process.platform === 'win32' + ? process.stdin.fd + : fs.openSync('/dev/tty', 'rs'); const wasRaw = process.stdin.isRaw; if (!wasRaw && process.stdin.setRawMode) { From 077aebe5ea4890ceef0d21caef5adc312a112964 Mon Sep 17 00:00:00 2001 From: kang yoosam Date: Tue, 10 Sep 2019 21:55:50 +0900 Subject: [PATCH 7/8] Revert ":+1: use conditional assignment" This reverts commit efda26999c9d49c1d893b7f194370a36da119275. --- packages/cli/src/tools/generator/promptSync.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/tools/generator/promptSync.ts b/packages/cli/src/tools/generator/promptSync.ts index ab03fc7cd..a7fa9dece 100644 --- a/packages/cli/src/tools/generator/promptSync.ts +++ b/packages/cli/src/tools/generator/promptSync.ts @@ -43,10 +43,11 @@ function create() { const echo = opts.echo; const masked = 'echo' in opts; - const fd = - process.platform === 'win32' - ? process.stdin.fd - : fs.openSync('/dev/tty', 'rs'); + let fd = fs.openSync('/dev/tty', 'rs'); + if (process.platform === 'win32') { + // @ts-ignore MEMO: fd exists when win32 + fd = process.stdin.fd; + } const wasRaw = process.stdin.isRaw; if (!wasRaw && process.stdin.setRawMode) { From 8f83905c05e714cf0c64c03953c6a0a4005deeb1 Mon Sep 17 00:00:00 2001 From: kang yoosam Date: Tue, 10 Sep 2019 22:23:55 +0900 Subject: [PATCH 8/8] :+1: use previous code with eslint-disable --- packages/cli/src/tools/generator/promptSync.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/tools/generator/promptSync.ts b/packages/cli/src/tools/generator/promptSync.ts index a7fa9dece..bef753e5a 100644 --- a/packages/cli/src/tools/generator/promptSync.ts +++ b/packages/cli/src/tools/generator/promptSync.ts @@ -43,11 +43,13 @@ function create() { const echo = opts.echo; const masked = 'echo' in opts; - let fd = fs.openSync('/dev/tty', 'rs'); - if (process.platform === 'win32') { - // @ts-ignore MEMO: fd exists when win32 - fd = process.stdin.fd; - } + /*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 && process.stdin.setRawMode) {