Skip to content

Commit 5db8180

Browse files
committed
Add npmrc with legacy peer deps
1 parent b7413cb commit 5db8180

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/commands/__tests__/createApp.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { confirm, input } from '@inquirer/prompts';
22
import { fs, vol } from 'memfs';
3-
import { Mock, afterEach, describe, expect, test, vi } from 'vitest';
3+
import {
4+
Mock,
5+
afterEach,
6+
beforeEach,
7+
describe,
8+
expect,
9+
test,
10+
vi,
11+
} from 'vitest';
412
import exec from '../../util/exec';
513
import print from '../../util/print';
614
import { createApp } from '../createApp';
@@ -12,6 +20,10 @@ vi.mock('@inquirer/prompts', () => ({
1220
vi.mock('../../util/addDependency');
1321
vi.mock('../../util/print', () => ({ default: vi.fn() }));
1422

23+
beforeEach(() => {
24+
process.env.npm_config_user_agent = 'yarn/1.22.0';
25+
});
26+
1527
afterEach(() => {
1628
vol.reset();
1729
(print as Mock).mockReset();
@@ -84,18 +96,28 @@ describe('package manager options', () => {
8496
(confirm as Mock).mockResolvedValueOnce(true);
8597
await createApp('MyApp', { npm: true });
8698
expect(exec).toHaveBeenCalledWith('npm install');
99+
expectFileContents('MyApp/.npmrc', 'legacy-peer-deps=true');
87100
});
88101

89102
test('creates with bun', async () => {
90103
(confirm as Mock).mockResolvedValueOnce(true);
91104
await createApp('MyApp', { bun: true });
92105
expect(exec).toHaveBeenCalledWith('bun install');
106+
expect(() => fs.readFileSync('MyApp/.npmrc', 'utf8')).toThrow();
93107
});
94108

95109
test('creates with pnpm', async () => {
96110
(confirm as Mock).mockResolvedValueOnce(true);
97111
await createApp('MyApp', { pnpm: true });
98112
expect(exec).toHaveBeenCalledWith('pnpm install');
113+
expect(() => fs.readFileSync('MyApp/.npmrc', 'utf8')).toThrow();
114+
});
115+
116+
test('creates with yarn (does not add .npmrc)', async () => {
117+
(confirm as Mock).mockResolvedValueOnce(true);
118+
await createApp('MyApp', { yarn: true });
119+
expect(exec).toHaveBeenCalledWith('yarn install');
120+
expect(() => fs.readFileSync('MyApp/.npmrc', 'utf8')).toThrow();
99121
});
100122
});
101123

src/commands/createApp.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import _ from 'lodash';
55
import ora from 'ora';
66
import path from 'path';
77
import { PACKAGE_ROOT, globals } from '../constants';
8+
import addNpmrc from '../util/addNpmrc';
89
import commit from '../util/commit';
910
import copyTemplateDirectory from '../util/copyTemplateDirectory';
1011
import exec from '../util/exec';
@@ -54,10 +55,14 @@ export async function createApp(
5455

5556
spinner.succeed('Created new Belt app with Expo');
5657

58+
const packageManager = getPackageManager(options);
59+
if (packageManager === 'npm') {
60+
await addNpmrc(appName);
61+
}
62+
5763
process.chdir(`./${appName}`);
5864

5965
spinner.start('Installing dependencies');
60-
const packageManager = getPackageManager(options);
6166
await exec(`${packageManager} install`);
6267
await exec('git init');
6368
await commit('Initial commit');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { fs, vol } from 'memfs';
2+
import { afterEach, describe, expect, test } from 'vitest';
3+
import addNpmrc from '../addNpmrc';
4+
5+
afterEach(() => {
6+
vol.reset();
7+
});
8+
9+
describe('addNpmrc', () => {
10+
test('creates .npmrc file in current directory', async () => {
11+
await addNpmrc();
12+
13+
const npmrcContent = fs.readFileSync('.npmrc', 'utf8');
14+
expect(npmrcContent).toContain('legacy-peer-deps=true');
15+
});
16+
});

src/util/addNpmrc.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import copyTemplateDirectory from './copyTemplateDirectory';
2+
3+
export default async function addNpmrc(destinationDir = '.') {
4+
await copyTemplateDirectory({
5+
templateDir: 'npmrc',
6+
destinationDir,
7+
});
8+
}

templates/npmrc/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
legacy-peer-deps=true

0 commit comments

Comments
 (0)