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

Feat/override plugins on install #1723

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 8 additions & 1 deletion packages/core/jsonSchema/renative-1.0.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,14 @@
"version": {
"type": "string"
},
"browserslist": {}
"browserslist": {},
"scripts": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"description": "Defines scripts you want to include in template"
}
},
"additionalProperties": false
}
Expand Down
9 changes: 8 additions & 1 deletion packages/core/jsonSchema/rnv.project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4407,7 +4407,14 @@
"version": {
"type": "string"
},
"browserslist": {}
"browserslist": {},
"scripts": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"description": "Defines scripts you want to include in template"
}
},
"additionalProperties": false
}
Expand Down
9 changes: 8 additions & 1 deletion packages/core/jsonSchema/rnv.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@
"version": {
"type": "string"
},
"browserslist": {}
"browserslist": {},
"scripts": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"description": "Defines scripts you want to include in template"
}
},
"additionalProperties": false
}
Expand Down
82 changes: 48 additions & 34 deletions packages/core/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,15 +463,23 @@ export const parsePlugins = (
const c = getContext();
const { platform } = c;
logDefault('parsePlugins');
if (c.buildConfig && platform) {
if (c.buildConfig) {
const includedPluginsConfig = getConfigProp('includedPlugins');
// default to all plugins if it's not defined (null allowed for overrides)
const includedPlugins = includedPluginsConfig === undefined ? ['*'] : includedPluginsConfig;

const excludedPlugins = getConfigProp('excludedPlugins') || [];
const supportedPlatforms = c.files.project.config?.defaults?.supportedPlatforms || [];
const platformsToCheck = platform ? [platform] : supportedPlatforms;
const parsedPlugins: string[] = [];

const handleActivePlugin = (plugin: RnvPlugin, pluginPlat: ConfigPluginPlatformSchema, key: string) => {
// log deprecated if present
if (plugin._id) {
if (!parsedPlugins.includes(plugin._id)) {
parsedPlugins.push(plugin._id);
} else return;
}
if (plugin.deprecated) {
logWarning(plugin.deprecated);
}
Expand All @@ -491,45 +499,51 @@ export const parsePlugins = (
Object.keys(plugins).forEach((key) => {
const plugin = getMergedPlugin(c, key);
if (!plugin) return;

if (
(includedPlugins.includes('*') || includedPlugins.includes(key)) &&
!excludedPlugins.includes(key)
) {
const pluginPlat: ConfigPluginPlatformSchema = plugin[platform] || {};

// NOTE: we do not want to disable plugin just because object is missing. instead we will let people to do it explicitly
// {
// skipLinking: true,
// disabled: true,
// };
//TODO: consider supportedPlatforms for plugins
const isPluginPlatDisabled = pluginPlat.disabled === true;
const isPluginDisabled = plugin.disabled === true;
const isPluginPlatSupported = plugin.supportedPlatforms
? plugin.supportedPlatforms.includes(platform)
: true;

if (ignorePlatformObjectCheck || includeDisabledOrExcludedPlugins) {
if (isPluginDisabled) {
logDefault(`Plugin ${key} is marked disabled. skipping.`);
} else if (isPluginPlatDisabled) {
logDefault(`Plugin ${key} is marked disabled for platform ${platform}. skipping.`);
} else if (!isPluginPlatSupported) {
logDefault(
`Plugin ${key}'s supportedPlatforms does not include ${platform}. skipping.`
);
platformsToCheck.forEach((platformToCheck) => {
const pluginPlat: ConfigPluginPlatformSchema = plugin[platformToCheck] || {};

// NOTE: we do not want to disable plugin just because object is missing. instead we will let people to do it explicitly
// {
// skipLinking: true,
// disabled: true,
// };
//TODO: consider supportedPlatforms for plugins
const isPluginPlatDisabled = pluginPlat.disabled === true;
const isPluginDisabled = plugin.disabled === true;
const isPluginPlatSupported = plugin.supportedPlatforms
? plugin.supportedPlatforms.includes(platformToCheck)
: true;

if (ignorePlatformObjectCheck || includeDisabledOrExcludedPlugins) {
if (isPluginDisabled) {
logDefault(`Plugin ${key} is marked disabled. skipping.`);
} else if (isPluginPlatDisabled) {
logDefault(
`Plugin ${key} is marked disabled for platform ${platformToCheck}. skipping.`
);
} else if (!isPluginPlatSupported) {
logDefault(
`Plugin ${key}'s supportedPlatforms does not include ${platformToCheck}. skipping.`
);
}

handleActivePlugin(plugin, pluginPlat, key);
} else if (!isPluginPlatDisabled && !isPluginDisabled && isPluginPlatSupported) {
handleActivePlugin(plugin, pluginPlat, key);
}
handleActivePlugin(plugin, pluginPlat, key);
} else if (!isPluginPlatDisabled && !isPluginDisabled && isPluginPlatSupported) {
handleActivePlugin(plugin, pluginPlat, key);
}
});
} else if (includeDisabledOrExcludedPlugins) {
const pluginPlat = plugin[platform] || {};
if (excludedPlugins.includes(key)) {
plugin.disabled = true;
handleActivePlugin(plugin, pluginPlat, key);
}
platformsToCheck.forEach((platformToCheck) => {
const pluginPlat = plugin[platformToCheck] || {};
if (excludedPlugins.includes(key)) {
plugin.disabled = true;
handleActivePlugin(plugin, pluginPlat, key);
}
});
}
});
// Not valid warning as web based plugins might not need web definition object to work
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/schema/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export const zodTemplateConfigFragment = z
name: z.string(),
version: z.string(),
browserslist: z.any(),
scripts: z
.record(z.string(), z.string())
.describe('Defines scripts you want to include in template'),
})
.partial()
),
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ const _copyIncludedPaths = (c: RnvContext, name: string) => {
if (!fsExistsSync(destPath) && fsExistsSync(sourcePath)) {
try {
if (fsLstatSync(sourcePath).isDirectory()) {
logInfo(`Missing directory ${chalk().bold.white(`${destPath}.js`)}. COPYING from TEMPLATE...DONE`);
logInfo(`Missing directory ${chalk().bold.white(`${destPath}`)}. COPYING from TEMPLATE...DONE`);
copyFolderContentsRecursiveSync(sourcePath, destPath);
} else {
logInfo(`Missing file ${chalk().bold.white(`${destPath}.js`)}. COPYING from TEMPLATE...DONE`);
logInfo(`Missing file ${chalk().bold.white(`${destPath}`)}. COPYING from TEMPLATE...DONE`);
copyFileSync(sourcePath, destPath);
}
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion packages/template-starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"templateOverrides",
"tsconfig.json",
"typings",
"webpack.config.js"
"webpack.config.js",
"scripts"
],
"repository": {
"type": "git",
Expand Down
47 changes: 12 additions & 35 deletions packages/template-starter/renative.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,20 @@
".gitignore",
"appConfigs/app",
"appConfigs/base/assets",
"appConfigs/base/fonts"
"appConfigs/base/fonts",
"scripts"
]
},
{
"paths": [
"Gemfile",
"metro.config.js",
".bundle",
"react-native.config.js"
],
"platforms": [
"ios",
"android",
"androidwear",
"tvos",
"firetv",
"androidtv"
]
"paths": ["Gemfile", "metro.config.js", ".bundle", "react-native.config.js"],
"platforms": ["ios", "android", "androidwear", "tvos", "firetv", "androidtv"]
},
{
"paths": [
"next.config.js",
"next-env.d.ts",
"src/pages"
],
"platforms": [
"web"
]
"paths": ["next.config.js", "next-env.d.ts", "src/pages"],
"platforms": ["web"]
},
{
"paths": [
"webpack.config.js"
],
"paths": ["webpack.config.js"],
"platforms": [
"windows",
"macos",
Expand Down Expand Up @@ -83,21 +64,17 @@
"minipass": "7.1.2",
"readable-stream": "4.5.2"
},
"browserslist": [
">0.2%",
"not op_mini all"
]
"browserslist": [">0.2%", "not op_mini all"],
"scripts": {
"postinstall": "node scripts/postinstall.js"
}
}
},
"bootstrapConfig": {
"rnvNewPatchDependencies": {
"pkg-dir": "7.0.0",
"xmlbuilder": "^15.1.1"
},
"defaultSelectedPlatforms": [
"web",
"ios",
"android"
]
"defaultSelectedPlatforms": ["web", "ios", "android"]
}
}
75 changes: 75 additions & 0 deletions packages/template-starter/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const path = require('path');
const {
createRnvContext,
loadWorkspacesConfigSync,
loadDefaultConfigTemplates,
updateRenativeConfigs,
overrideTemplatePlugins,
createRnvApi,
getConfigProp,
doResolve,
logError,
RnvFileName,
fsExistsSync,
fsReadFileSync,
removeDirSync,
revertOverrideToOriginal,
} = require('@rnv/core');

const Logger = require('@rnv/cli/lib/logger');
const RNV_HOME_DIR = path.join(__dirname, '..');

(async () => {
try {
createRnvApi({
logger: Logger,
getConfigProp,
doResolve,
});
createRnvContext({ RNV_HOME_DIR });

loadWorkspacesConfigSync();
await loadDefaultConfigTemplates();
await updateRenativeConfigs();
await resetOverrides();

await overrideTemplatePlugins();
} catch (error) {
logError(error);
}
})();

export const resetOverrides = async () => {
const overrideDir = path.join(process.cwd(), '.rnv', 'overrides');

const appliedOverrideFilePath = path.join(overrideDir, RnvFileName.appliedOverride);

if (fsExistsSync(appliedOverrideFilePath)) {
const appliedOverrides = JSON.parse(fsReadFileSync(appliedOverrideFilePath).toString());

Object.keys(appliedOverrides).forEach((moduleName) => {
const appliedVersion = appliedOverrides[moduleName].version;
const packageJsonPath = path.join(process.cwd(), 'node_modules', moduleName, RnvFileName.package);

if (fsExistsSync(packageJsonPath)) {
const packageContent = JSON.parse(fsReadFileSync(packageJsonPath).toString());
const currentVersion = packageContent.version;

if (currentVersion === appliedVersion) {
const packageOverrides = appliedOverrides[moduleName];
Object.keys(packageOverrides).forEach((filePath) => {
if (filePath !== 'version') {
const backupPath = path.join(overrideDir, moduleName, filePath);
const destinationPath = path.join(process.cwd(), 'node_modules', moduleName, filePath);

revertOverrideToOriginal(destinationPath, backupPath);
}
});
}
}
});
removeDirSync(overrideDir);
return true;
}
return false;
};
Loading