Skip to content
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

chore: convert generator files into TS #697

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -90,7 +99,7 @@ function copyProjectTemplateAndReplace(

let contentChangedCallback = null;
if (options.upgrade && !options.force) {
contentChangedCallback = (_, contentChanged) =>
contentChangedCallback = (_destPath: string, contentChanged: string) =>
upgradeFileContentChangedCallback(
absoluteSrcFilePath,
relativeFilePath,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,47 @@

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') {
thymikee marked this conversation as resolved.
Show resolved Hide resolved
opts = ask;
ask = opts.ask;
} else if (Object(value) === value) {
} else if (typeof value === 'object') {
thymikee marked this conversation as resolved.
Show resolved Hide resolved
opts = value;
value = opts.value;
}
ask = ask || '';
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);
}

Expand All @@ -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()) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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 || '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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);
Expand All @@ -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);
}

/**
Expand All @@ -62,7 +55,6 @@ async function createFromRemoteTemplate(
template: string,
destPath: string,
newProjectName: string,
destinationRoot: string,
) {
let installPackage;
let templateName;
Expand Down Expand Up @@ -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 {
Expand All @@ -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');
Expand All @@ -119,7 +111,7 @@ async function installTemplateDependencies(templatePath, destinationRoot) {
return;
}

let dependencies;
let dependencies: any;
try {
dependencies = require(dependenciesJsonPath);
} catch (err) {
Expand All @@ -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(
Expand All @@ -148,7 +140,7 @@ async function installTemplateDevDependencies(templatePath, destinationRoot) {
return;
}

let dependencies;
let dependencies: any;
try {
dependencies = require(devDependenciesJsonPath);
} catch (err) {
Expand Down