Skip to content

Commit

Permalink
feat: only generate lockfiles for jit plugins (#325)
Browse files Browse the repository at this point in the history
* feat: only generate lockfiles for jit plugins

* feat: shrinkwrap everything but core plugins
  • Loading branch information
mdonnalley authored May 10, 2024
1 parent c33e941 commit 462e6fa
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 23 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/update-plugin-list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: update-plugin-list

on:
workflow_dispatch:
schedule:
# Run every Wednesday at 14:00 UTC
- cron: '0 14 * * 3'

jobs:
update-plugin-list:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.SVC_CLI_BOT_GITHUB_TOKEN }}
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: |
npm view @salesforce/cli oclif.plugins --json | jq '[.[] | select(contains("@salesforce"))]' > core-plugins.json
npm view @salesforce/cli oclif.jitPlugins --json | jq keys > jit-plugins.json
- uses: salesforcecli/github-workflows/.github/actions/getGithubUserInfo@main
id: github-user-info
with:
SVC_CLI_BOT_GITHUB_TOKEN: ${{ secrets.SVC_CLI_BOT_GITHUB_TOKEN }}
- uses: salesforcecli/github-workflows/.github/actions/gitConfig@main
with:
username: ${{ steps.github-user-info.outputs.username }}
email: ${{ steps.github-user-info.outputs.email }}
# Push changes if 'git status' is not empty
- run: |
if [[ -n $(git status --short) ]]; then
git add -A
git commit -m "fix: update plugin lists"
git push
else
echo "Already up to date"
fi
35 changes: 20 additions & 15 deletions bin/sf-prepack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,33 @@

const chalk = require('chalk');
const shell = require('../utils/shelljs');
const { isPlugin } = require('../utils/project-type');
const { determineProjectType } = require('../utils/project-type');
const packageRoot = require('../utils/package-path');
const { semverIsLessThan } = require('../utils/semver');

shell.exec('yarn build');

if (isPlugin(packageRoot)) {
const projectType = determineProjectType(packageRoot);

if (projectType !== 'other') {
if (shell.which('oclif')) {
shell.exec('oclif manifest .');
const version = shell.exec('oclif --version', { silent: true }).stdout.trim().replace('oclif/', '').split(' ')[0];
if (semverIsLessThan(version, '3.14.0')) {
// eslint-disable-next-line no-console
console.log(
chalk.yellow('Warning:'),
// eslint-disable-next-line max-len
`oclif version ${version} is less than 3.14.0. Please upgrade to 3.14.0 or higher to generate oclif.lock file.`
);
} else {
shell.exec('oclif lock');
}
shell.exec('oclif manifest');

shell.exec('npm shrinkwrap');
if (projectType !== 'core-plugin') {
const version = shell.exec('oclif --version', { silent: true }).stdout.trim().replace('oclif/', '').split(' ')[0];
if (semverIsLessThan(version, '3.14.0')) {
// eslint-disable-next-line no-console
console.log(
chalk.yellow('Warning:'),
// eslint-disable-next-line max-len
`oclif version ${version} is less than 3.14.0. Please upgrade to 3.14.0 or higher to generate oclif.lock file.`
);
} else {
shell.exec('oclif lock');
}

shell.exec('npm shrinkwrap');
}
} else if (shell.which('oclif-dev')) {
// eslint-disable-next-line no-console
console.log(chalk.yellow('Warning:'), 'oclif-dev is deprecated. Please use oclif instead.');
Expand Down
19 changes: 19 additions & 0 deletions core-plugins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
"@salesforce/plugin-apex",
"@salesforce/plugin-auth",
"@salesforce/plugin-data",
"@salesforce/plugin-deploy-retrieve",
"@salesforce/plugin-info",
"@salesforce/plugin-limits",
"@salesforce/plugin-marketplace",
"@salesforce/plugin-org",
"@salesforce/plugin-packaging",
"@salesforce/plugin-schema",
"@salesforce/plugin-settings",
"@salesforce/plugin-sobject",
"@salesforce/plugin-source",
"@salesforce/plugin-telemetry",
"@salesforce/plugin-templates",
"@salesforce/plugin-trust",
"@salesforce/plugin-user"
]
11 changes: 11 additions & 0 deletions jit-plugins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
"@salesforce/plugin-custom-metadata",
"@salesforce/plugin-community",
"@salesforce/plugin-dev",
"@salesforce/plugin-devops-center",
"@salesforce/plugin-env",
"@salesforce/plugin-functions",
"@salesforce/plugin-signups",
"@salesforce/sfdx-plugin-lwc-test",
"@salesforce/sfdx-scanner"
]
47 changes: 44 additions & 3 deletions utils/project-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,53 @@ const { join } = require('path');
const { readFileSync } = require('fs');

exports.isPlugin = function (packageRoot) {
return exports.determineProjectType(packageRoot) !== 'other';
};

const CACHE = new Map();
/**
* Determine project type.
*
* @param {string} packageRoot The root of the package.
* @returns {PackageType} The type of the package.
*
* PackageType = 'plugin' | 'core-plugin' | 'jit-plugin' | 'other'
*/
exports.determineProjectType = function (packageRoot) {
if (CACHE.has(packageRoot)) {
return CACHE.get(packageRoot);
}

let isPlugin = false;
let pjson;
try {
const contents = JSON.parse(readFileSync(join(packageRoot, 'package.json'), 'utf-8'));
isPlugin = contents && !!contents.oclif;
pjson = JSON.parse(readFileSync(join(packageRoot, 'package.json'), 'utf-8'));
isPlugin = pjson && !!pjson.oclif;
} catch (err) {
/* do nothing */
}
return isPlugin;

if (!isPlugin) {
CACHE.set(packageRoot, 'other');
return 'other';
}

const corePluginsList = readFileSync(join(__dirname, '..', 'core-plugins.json'), 'utf-8');
const corePlugins = JSON.parse(corePluginsList);
const isCorePlugin = pjson && !!pjson.oclif && corePlugins.includes(pjson.name);
if (isCorePlugin) {
CACHE.set(packageRoot, 'core-plugin');
return 'core-plugin';
}

const jitPluginsList = readFileSync(join(__dirname, '..', 'jit-plugins.json'), 'utf-8');
const jitPlugins = JSON.parse(jitPluginsList);
const isJitPlugin = pjson && !!pjson.oclif && jitPlugins.includes(pjson.name);
if (isJitPlugin) {
CACHE.set(packageRoot, 'jit-plugin');
return 'jit-plugin';
}

CACHE.set(packageRoot, 'plugin');
return 'plugin';
};
22 changes: 17 additions & 5 deletions utils/standardize-pjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const { resolveConfig } = require('./sf-config');
const { semverIsLessThan } = require('./semver');

const PackageJson = require('./package-json');
const { isPlugin } = require('./project-type');
const { determineProjectType } = require('./project-type');

const PLUGIN_FILES = ['/messages', '/oclif.manifest.json', '/oclif.lock', '/npm-shrinkwrap.json'];
const PLUGIN_FILES = ['/messages', '/oclif.manifest.json'];
const CORE_PLUGIN_FILES_BLOCK_LIST = ['/oclif.lock', '/npm-shrinkwrap.json'];
const JIT_PLUGIN_FILES = ['/messages', '/oclif.manifest.json', '/oclif.lock', '/npm-shrinkwrap.json'];

module.exports = (packageRoot = require('./package-path')) => {
const config = resolveConfig(packageRoot);
Expand All @@ -25,10 +27,20 @@ module.exports = (packageRoot = require('./package-path')) => {
pjson.actions.push(`updating license`);
}

if (isPlugin(packageRoot)) {
pjson.contents.oclif.topicSeparator = ' ';
pjson.contents.files = [...new Set([...pjson.contents.files, ...PLUGIN_FILES])].sort();
const type = determineProjectType(packageRoot);

if (type === 'jit-plugin') {
// ensure that jit plugins have the correct files
pjson.contents.files = [...new Set([...pjson.contents.files, ...JIT_PLUGIN_FILES])].sort();
}

if (type === 'core-plugin') {
// ensure that core plugins have the correct files and do not have any in the block list
pjson.contents.files = [
...new Set([...pjson.contents.files.filter((f) => !CORE_PLUGIN_FILES_BLOCK_LIST.includes(f)), ...PLUGIN_FILES]),
].sort();
}

// GENERATE SCRIPTS
const scriptList = Object.entries(config.scripts);
const wireitList = Object.entries(config.wireit);
Expand Down

0 comments on commit 462e6fa

Please sign in to comment.