Skip to content

Commit

Permalink
set hash in cache when initializing new project
Browse files Browse the repository at this point in the history
  • Loading branch information
TMisiukiewicz committed Sep 19, 2023
1 parent cdcd2fa commit d35c42a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 8 additions & 1 deletion __e2e__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ test('init fails if the directory already exists', () => {

test('init should prompt for the project name', () => {
createCustomTemplateFiles();
const {stdout} = runCLI(DIR, ['init', 'test', '--template', templatePath]);
const {stdout} = runCLI(DIR, [
'init',
'test',
'--template',
templatePath,
'--install-pods',
'false',
]);

(prompts as jest.MockedFunction<typeof prompts>).mockReturnValue(
Promise.resolve({
Expand Down
16 changes: 14 additions & 2 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
logger,
getLoader,
Loader,
cacheManager,
} from '@react-native-community/cli-tools';
import {installPods} from '@react-native-community/cli-platform-ios';
import {
Expand All @@ -26,7 +27,7 @@ import TemplateAndVersionError from './errors/TemplateAndVersionError';
import {getBunVersionIfAvailable} from '../../tools/bun';
import {getNpmVersionIfAvailable} from '../../tools/npm';
import {getYarnVersionIfAvailable} from '../../tools/yarn';
import prompts from 'prompts';
import {createHash} from 'crypto';

const DEFAULT_VERSION = 'latest';

Expand Down Expand Up @@ -88,6 +89,15 @@ function getTemplateName(cwd: string) {
return name;
}

//set cache to empty string to prevent installing cocoapods on freshly created project
function setEmptyHashForCachedDependencies(projectName: string) {
cacheManager.set(
projectName,
'dependencies',
createHash('md5').update('').digest('hex'),
);
}

async function createFromTemplate({
projectName,
templateUri,
Expand Down Expand Up @@ -182,6 +192,7 @@ async function createFromTemplate({
if (installPodsValue === 'true') {
await installPods(loader);
loader.succeed();
setEmptyHashForCachedDependencies(projectName);
} else if (installPodsValue === 'undefined') {
const {installCocoapods} = await prompt({
type: 'confirm',
Expand All @@ -194,6 +205,7 @@ async function createFromTemplate({
if (installCocoapods) {
await installPods(loader);
loader.succeed();
setEmptyHashForCachedDependencies(projectName);
}
}
}
Expand Down Expand Up @@ -300,7 +312,7 @@ export default (async function initialize(
options: Options,
) {
if (!projectName) {
const {projName} = await prompts({
const {projName} = await prompt({
type: 'text',
name: 'projName',
message: 'How would you like to name the app?',
Expand Down

0 comments on commit d35c42a

Please sign in to comment.