Skip to content

Commit 343b476

Browse files
authored
feat: make project name lowercased in package.json (#1370)
* fix: make project name lowercased in package.json * refactor: address comments from review * refactor: update snapshot
1 parent e3b938e commit 343b476

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "PlaceholderName",
3+
"version": "0.0.1"
4+
}

packages/cli/src/commands/init/__tests__/__snapshots__/editTemplate.test.ts.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`changePlaceholderInTemplate should produce a lowercased version of "ProjectName" in package.json "name" field 1`] = `
4+
"Snapshot Diff:
5+
- First value
6+
+ Second value
7+
8+
@@ -1,3 +1,3 @@
9+
{
10+
- \\"name\\": \\"PlaceholderName\\",
11+
+ \\"name\\": \\"projectname\\",
12+
\\"version\\": \\"0.0.1\\""
13+
`;
14+
315
exports[`should edit template 1`] = `
416
"Snapshot Diff:
517
- First value

packages/cli/src/commands/init/__tests__/editTemplate.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,33 @@ test('should edit template with custom title', () => {
124124
snapshotDiff(oldJavaFile, newJavaFile, {contextLines: 1}),
125125
).toMatchSnapshot();
126126
});
127+
128+
describe('changePlaceholderInTemplate', () => {
129+
beforeEach(() => {
130+
jest.spyOn(process, 'cwd').mockImplementation(() => testPath);
131+
});
132+
133+
afterEach(() => {
134+
jest.resetAllMocks();
135+
});
136+
137+
test(`should produce a lowercased version of "${PROJECT_NAME}" in package.json "name" field`, () => {
138+
changePlaceholderInTemplate({
139+
projectName: PROJECT_NAME,
140+
placeholderName: PLACEHOLDER_NAME,
141+
});
142+
143+
const oldPackageJsonFile = fs.readFileSync(
144+
path.resolve(FIXTURE_DIR, 'package.json'),
145+
'utf8',
146+
);
147+
const newPackageJsonFile = fs.readFileSync(
148+
path.resolve(testPath, 'package.json'),
149+
'utf8',
150+
);
151+
152+
expect(
153+
snapshotDiff(oldPackageJsonFile, newPackageJsonFile, {contextLines: 1}),
154+
).toMatchSnapshot();
155+
});
156+
});

packages/cli/src/commands/init/editTemplate.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function replaceNameInUTF8File(
2222
templateName: string,
2323
) {
2424
logger.debug(`Replacing in ${filePath}`);
25+
const isPackageJson = path.basename(filePath) === 'package.json';
2526
const fileContent = fs.readFileSync(filePath, 'utf8');
2627
const replacedFileContent = fileContent
2728
.replace(new RegExp(templateName, 'g'), projectName)
@@ -33,6 +34,14 @@ function replaceNameInUTF8File(
3334
if (fileContent !== replacedFileContent) {
3435
fs.writeFileSync(filePath, replacedFileContent, 'utf8');
3536
}
37+
38+
if (isPackageJson) {
39+
fs.writeFileSync(
40+
filePath,
41+
fileContent.replace(templateName, projectName.toLowerCase()),
42+
'utf8',
43+
);
44+
}
3645
}
3746

3847
function renameFile(filePath: string, oldName: string, newName: string) {

0 commit comments

Comments
 (0)