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

fix/supportedPlatforms_detection #1801

Merged
merged 2 commits into from
Nov 27, 2024
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
3 changes: 3 additions & 0 deletions packages/core/src/projects/__tests__/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('checkAndUpdateProjectIfRequired', () => {
const c = getContext();
c.platform = 'android';
c.buildConfig.isMonorepo = false;
c.buildConfig.platforms = { ios: {}, android: {}, web: {} };
c.files.project.config = { defaults: { supportedPlatforms: ['ios'] } };
c.paths.template.configTemplate = '/path/to/template';
jest.mocked(readObjectSync).mockReturnValue({
Expand All @@ -81,6 +82,7 @@ describe('checkAndUpdateProjectIfRequired', () => {
const c = getContext();
c.platform = 'android';
c.buildConfig.isMonorepo = false;
c.buildConfig.platforms = { ios: {}, android: {}, web: {} };
c.files.project.config = { defaults: { supportedPlatforms: ['ios'] } };
c.paths.project.dir = '/project/dir';
c.paths.project.config = '/project/dir/renative.json';
Expand Down Expand Up @@ -115,6 +117,7 @@ describe('checkAndUpdateProjectIfRequired', () => {
const c = getContext();
c.platform = 'android';
c.buildConfig.isMonorepo = false;
c.buildConfig.platforms = { ios: {}, android: {}, web: {} };
c.files.project.config = { defaults: { supportedPlatforms: ['ios', 'android'] } };
c.paths.project.dir = '/project/dir';
c.paths.project.config = '/project/dir/renative.json';
Expand Down
96 changes: 43 additions & 53 deletions packages/core/src/projects/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,75 +25,65 @@ export const checkAndUpdateProjectIfRequired = async () => {
if (isMonorepo) return true;
await applyTemplate();

const allPlatforms = Object.keys(c.buildConfig?.platforms || {});
if (!allPlatforms?.includes(platform)) {
logError(`Platform ${platform} is not supported!`);
return Promise.reject(`Platform ${platform} is not supported!`);
}
const templateConfigFile = readObjectSync<ConfigFileTemplate>(c.paths.template.configTemplate);

if (templateConfigFile) {
const availablePlatforms = _getAllAvailablePlatforms(templateConfigFile);
if (!availablePlatforms.includes(platform)) {
logError(`Platform ${platform} is not supported!`);
return Promise.reject(`Platform ${platform} is not supported!`);
} else {
const missingFiles = _getMisFilesForPlatform({
templateConfigFile,
platform,
projectPath: c.paths.project.dir,
templatePath: c.paths.template.dir,
const missingFiles = _getMisFilesForPlatform({
templateConfigFile,
platform,
projectPath: c.paths.project.dir,
templatePath: c.paths.template.dir,
});

if (missingFiles.length || !supportedPlatforms?.includes(platform)) {
const { confirm } = await inquirerPrompt({
type: 'confirm',
message: `You are trying to run platform ${chalk().bold.magenta(
platform
)} which is not configured. Do you want to configure it now?`,
});
if (missingFiles.length || !supportedPlatforms?.includes(platform)) {
const { confirm } = await inquirerPrompt({
type: 'confirm',
message: `You are trying to run platform ${chalk().bold.magenta(
platform
)} which is not configured. Do you want to configure it now?`,
});
if (!confirm) {
return Promise.reject('Cancelled by user');
}
if (!confirm) {
return Promise.reject('Cancelled by user');
}

if (supportedPlatforms) {
if (!supportedPlatforms.includes(platform)) {
supportedPlatforms.push(platform);
if (c.files.project.config) {
writeFileSync(c.paths.project.config, c.files.project.config);
}
if (supportedPlatforms) {
if (!supportedPlatforms.includes(platform)) {
supportedPlatforms.push(platform);
if (c.files.project.config) {
writeFileSync(c.paths.project.config, c.files.project.config);
}
}
}

missingFiles.forEach((mf) => {
const destPath = path.join(c.paths.project.dir, mf);
const sourcePath = path.join(c.paths.template.dir, mf);
missingFiles.forEach((mf) => {
const destPath = path.join(c.paths.project.dir, mf);
const sourcePath = path.join(c.paths.template.dir, mf);

if (!fsExistsSync(destPath) && fsExistsSync(sourcePath)) {
try {
if (fsLstatSync(sourcePath).isDirectory()) {
logInfo(
`Missing directory ${chalk().bold.white(destPath)}. COPYING from TEMPLATE...DONE`
);
copyFolderContentsRecursiveSync(sourcePath, destPath);
} else {
logInfo(`Missing file ${chalk().bold.white(destPath)}. COPYING from TEMPLATE...DONE`);
copyFileSync(sourcePath, destPath);
}
} catch (e) {
console.log(e);
if (!fsExistsSync(destPath) && fsExistsSync(sourcePath)) {
try {
if (fsLstatSync(sourcePath).isDirectory()) {
logInfo(`Missing directory ${chalk().bold.white(destPath)}. COPYING from TEMPLATE...DONE`);
copyFolderContentsRecursiveSync(sourcePath, destPath);
} else {
logInfo(`Missing file ${chalk().bold.white(destPath)}. COPYING from TEMPLATE...DONE`);
copyFileSync(sourcePath, destPath);
}
} catch (e) {
console.log(e);
}
});
}
}
});
}
}

return true;
};
const _getAllAvailablePlatforms = (templateConfigFile: ConfigFileTemplate): string[] => {
const includedPaths = templateConfigFile.templateConfig?.includedPaths || [];
return includedPaths.reduce((acc, item) => {
if (typeof item !== 'string' && item.platforms) {
acc.push(...item.platforms);
}
return acc;
}, [] as string[]);
};

const _getMisFilesForPlatform = (opts: {
templateConfigFile: ConfigFileTemplate;
platform: RnvPlatform;
Expand Down
Loading