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 copyAndReplace to TS #723

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import path from 'path';
// Binary files, don't process these (avoid decoding as utf8)
const binaryExtensions = ['.png', '.jar', '.keystore'];

type ContentChangedCallbackOption = 'identical' | 'changed' | 'new' | null;

/**
* Copy a file to given destination, replacing parts of its contents.
* @param srcPath Path to a file to be copied.
Expand All @@ -26,10 +28,15 @@ const binaryExtensions = ['.png', '.jar', '.keystore'];
* Function(path, 'identical' | 'changed' | 'new') => 'keep' | 'overwrite'
*/
function copyAndReplace(
srcPath,
destPath,
replacements,
contentChangedCallback,
srcPath: string,
destPath: string,
replacements: Record<string, string>,
contentChangedCallback:
| ((
path: string,
option: ContentChangedCallbackOption,
) => 'keep' | 'overwrite')
| null,
) {
if (fs.lstatSync(srcPath).isDirectory()) {
if (!fs.existsSync(destPath)) {
Expand All @@ -45,7 +52,7 @@ function copyAndReplace(
let shouldOverwrite = 'overwrite';
if (contentChangedCallback) {
const newContentBuffer = fs.readFileSync(srcPath);
let contentChanged = 'identical';
let contentChanged: ContentChangedCallbackOption = 'identical';
try {
const origContentBuffer = fs.readFileSync(destPath);
if (Buffer.compare(origContentBuffer, newContentBuffer) !== 0) {
Expand Down Expand Up @@ -78,7 +85,7 @@ function copyAndReplace(
let shouldOverwrite = 'overwrite';
if (contentChangedCallback) {
// Check if contents changed and ask to overwrite
let contentChanged = 'identical';
let contentChanged: ContentChangedCallbackOption = 'identical';
try {
const origContent = fs.readFileSync(destPath, 'utf8');
if (content !== origContent) {
Expand Down Expand Up @@ -106,7 +113,11 @@ function copyAndReplace(
/**
* Same as 'cp' on Unix. Don't do any replacements.
*/
function copyBinaryFile(srcPath, destPath, cb) {
function copyBinaryFile(
srcPath: string,
destPath: string,
cb: (err?: Error) => void,
) {
let cbCalled = false;
const srcPermissions = fs.statSync(srcPath).mode;
const readStream = fs.createReadStream(srcPath);
Expand All @@ -123,7 +134,7 @@ function copyBinaryFile(srcPath, destPath, cb) {
done();
});
readStream.pipe(writeStream);
function done(err) {
function done(err?: Error) {
if (!cbCalled) {
cb(err);
cbCalled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

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
Expand All @@ -18,6 +17,8 @@ import {logger} from '@react-native-community/cli-tools';

const prompt = promptInitializer();

type ContentChangedCallbackOption = 'identical' | 'changed' | 'new' | null;

type Options = {
upgrade?: boolean;
force?: boolean;
Expand Down Expand Up @@ -99,7 +100,10 @@ function copyProjectTemplateAndReplace(

let contentChangedCallback = null;
if (options.upgrade && !options.force) {
contentChangedCallback = (_destPath: string, contentChanged: string) =>
contentChangedCallback = (
_destPath: string,
contentChanged: ContentChangedCallbackOption,
) =>
upgradeFileContentChangedCallback(
absoluteSrcFilePath,
relativeFilePath,
Expand Down Expand Up @@ -146,7 +150,7 @@ function translateFilePath(filePath: string) {
function upgradeFileContentChangedCallback(
absoluteSrcFilePath: string,
relativeDestPath: string,
contentChanged: string,
contentChanged: ContentChangedCallbackOption,
) {
if (contentChanged === 'new') {
logger.info(`${chalk.bold('new')} ${relativeDestPath}`);
Expand Down