Skip to content

Commit

Permalink
fix: delete com. prefix in package name (#1915)
Browse files Browse the repository at this point in the history
* fix: delete default prefix in package name

* fix: code review improvements

* fix: tests

* test: add missing tests for `replaceNameInUTF8File`
  • Loading branch information
szymonrybczak committed Apr 21, 2023
1 parent 6e9a441 commit 69c033d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 45 deletions.
55 changes: 52 additions & 3 deletions packages/cli/src/commands/init/__tests__/editTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
changePlaceholderInTemplate,
replacePlaceholderWithPackageName,
validatePackageName,
replaceNameInUTF8File,
} from '../editTemplate';

const FIXTURE_DIR = path.resolve(
Expand Down Expand Up @@ -210,14 +211,13 @@ describe('replacePlaceholderWithPackageName', () => {
placeholderTitle: 'Test',
packageName: PACKAGE_NAME,
});
const [prefix, ...segments] = PACKAGE_NAME.split('.');

const mainActivityFile = fs.readFileSync(
path.resolve(
testPath,
'android',
prefix,
segments.join('.'),
'com',
PACKAGE_NAME,
'MainActivity.java',
),
'utf8',
Expand All @@ -240,3 +240,52 @@ describe('validatePackageName', () => {
);
});
});

describe('replaceNameInUTF8File', () => {
test('should replace string in utf8 file', async () => {
const pathToUtf8File = path.join(
testPath,
'ios',
PLACEHOLDER_NAME,
'project.pbxproj',
);

const textToReplace = `PRODUCT_BUNDLE_IDENTIFIER = "${PACKAGE_NAME}"`;

const beforeReplacement = await fs.readFile(pathToUtf8File, 'utf8');

await replaceNameInUTF8File(
pathToUtf8File,
textToReplace,
'PRODUCT_BUNDLE_IDENTIFIER = "(.*)"',
);

const afterReplacement = await fs.readFile(pathToUtf8File, 'utf8');

expect(beforeReplacement).not.toBe(afterReplacement);
expect(afterReplacement).toContain(textToReplace);
});

test('should not replace string in utf8 file', async () => {
const fsWriteFileSpy = jest.spyOn(fs, 'writeFile');
const pathToUtf8File = path.join(
testPath,
'ios',
PLACEHOLDER_NAME,
'project.pbxproj',
);

const beforeReplacement = await fs.readFile(pathToUtf8File, 'utf8');

await replaceNameInUTF8File(
pathToUtf8File,
'random-string',
'random-string',
);

const afterReplacement = await fs.readFile(pathToUtf8File, 'utf8');

expect(beforeReplacement).toEqual(afterReplacement);
expect(fsWriteFileSpy).toHaveBeenCalledTimes(0);
});
});
57 changes: 15 additions & 42 deletions packages/cli/src/commands/init/editTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function validatePackageName(packageName: string) {
}
}

async function replaceNameInUTF8File(
export async function replaceNameInUTF8File(
filePath: string,
projectName: string,
templateName: string,
Expand Down Expand Up @@ -101,49 +101,33 @@ async function processDotfiles(filePath: string) {
await renameFile(filePath, `_${dotfile}`, `.${dotfile}`);
}

function getPackageNameDetails(packageName: string) {
const cleanPackageName = packageName.replace(/[^\p{L}\p{N}.]+/gu, '');
const packageNameArray = cleanPackageName.split('.');
const [prefix, ...segments] = packageNameArray;
const startsWithCom = prefix === 'com';

return {
cleanPackageName,
packageNameArray,
prefix,
segments,
startsWithCom,
};
}

async function createAndroidPackagePaths(
filePath: string,
packageName: string,
) {
const {startsWithCom, segments, packageNameArray} = getPackageNameDetails(
packageName,
);
const pathFolders = filePath.split('/').slice(-2);
if (pathFolders[0] === 'java' && pathFolders[1] === 'com') {
const segmentsList = startsWithCom ? segments : packageNameArray;
const pathParts = filePath.split('/').slice(-2);

if (pathParts[0] === 'java' && pathParts[1] === 'com') {
const pathToFolders = filePath.split('/').slice(0, -2).join('/');
const segmentsList = packageName.split('.');

if (segmentsList.length > 1) {
const initialDir = process.cwd();
process.chdir(filePath);
process.chdir(filePath.split('/').slice(0, -1).join('/'));

try {
await fs.rename(
`${filePath}/${segmentsList.join('.')}`,
`${filePath}/${segmentsList[segmentsList.length - 1]}`,
`${pathToFolders}/${segmentsList[segmentsList.length - 1]}`,
);
await fs.rmdir(filePath);

for (const segment of segmentsList) {
fs.mkdirSync(segment);
process.chdir(segment);
}

await fs.rename(
`${filePath}/${segmentsList[segmentsList.length - 1]}`,
`${pathToFolders}/${segmentsList[segmentsList.length - 1]}`,
process.cwd(),
);
} catch {
Expand All @@ -162,10 +146,7 @@ export async function replacePlaceholderWithPackageName({
packageName,
}: Omit<Required<PlaceholderConfig>, 'projectTitle'>) {
validatePackageName(packageName);

const {cleanPackageName, segments, startsWithCom} = getPackageNameDetails(
packageName,
);
const cleanPackageName = packageName.replace(/[^\p{L}\p{N}.]+/gu, '');

for (const filePath of walk(process.cwd()).reverse()) {
if (shouldIgnoreFile(filePath)) {
Expand All @@ -175,20 +156,12 @@ export async function replacePlaceholderWithPackageName({
const iosFile = isIosFile(filePath);

if (!(await fs.stat(filePath)).isDirectory()) {
let newName = startsWithCom
? cleanPackageName
: `com.${cleanPackageName}`;

if (iosFile) {
newName = projectName;
}
let newName = iosFile ? projectName : cleanPackageName;

//replace bundleID for iOS
await replaceNameInUTF8File(
filePath,
`PRODUCT_BUNDLE_IDENTIFIER = "${
startsWithCom ? cleanPackageName : `com.${cleanPackageName}`
}"`,
`PRODUCT_BUNDLE_IDENTIFIER = "${cleanPackageName}"`,
'PRODUCT_BUNDLE_IDENTIFIER = "(.*)"',
);

Expand Down Expand Up @@ -217,7 +190,7 @@ export async function replacePlaceholderWithPackageName({
}
}

let fileName = startsWithCom ? segments.join('.') : cleanPackageName;
let fileName = cleanPackageName;

if (shouldRenameFile(filePath, placeholderName)) {
if (iosFile) {
Expand All @@ -233,7 +206,7 @@ export async function replacePlaceholderWithPackageName({
);
}
try {
await createAndroidPackagePaths(filePath, packageName);
await createAndroidPackagePaths(filePath, cleanPackageName);
} catch (error) {
throw new CLIError('Failed to create correct paths for Android.');
}
Expand Down

0 comments on commit 69c033d

Please sign in to comment.