Skip to content

Commit

Permalink
Merge pull request #415 from salesforcecli/md/wr-esm
Browse files Browse the repository at this point in the history
Md/wr esm
  • Loading branch information
WillieRuemmele authored Oct 26, 2023
2 parents ebc1887 + b0a3325 commit 11d60a8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 54 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Salesforce",
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
"@oclif/core": "^3.8.0",
"@oclif/core": "^3.9.0",
"@salesforce/core": "^5.3.10",
"@salesforce/sf-plugins-core": "^4.0.0",
"fast-levenshtein": "^3.0.0"
Expand All @@ -15,7 +15,7 @@
"@oclif/test": "^3.0.3",
"@salesforce/cli-plugins-testkit": "^4.4.7",
"@salesforce/dev-config": "^4.1.0",
"@salesforce/dev-scripts": "^5.12.0",
"@salesforce/dev-scripts": "^5.12.2",
"@salesforce/kit": "^3.0.11",
"@salesforce/plugin-command-reference": "^3.0.40",
"@salesforce/plugin-deploy-retrieve": "^1.18.7",
Expand Down Expand Up @@ -198,7 +198,7 @@
"output": []
},
"test:command-reference": {
"command": "\"./bin/dev.js\" commandreference:generate --erroronwarnings",
"command": "ts-node \"./bin/dev.js\" commandreference:generate --erroronwarnings",
"files": [
"src/**/*.ts",
"messages/**",
Expand All @@ -209,7 +209,7 @@
]
},
"test:deprecation-policy": {
"command": "\"./bin/dev.js\" snapshot:compare",
"command": "ts-node \"./bin/dev.js\" snapshot:compare",
"files": [
"src/**/*.ts"
],
Expand All @@ -219,7 +219,7 @@
]
},
"test:json-schema": {
"command": "\"./bin/dev.js\" schema:compare",
"command": "ts-node \"./bin/dev.js\" schema:compare",
"files": [
"src/**/*.ts",
"schemas"
Expand Down
19 changes: 7 additions & 12 deletions src/hooks/init/load_config_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
*/

import type { Hook, Interfaces } from '@oclif/core';
import { Config, ConfigPropertyMeta, Logger } from '@salesforce/core';
import { Config, ConfigPropertyMeta } from '@salesforce/core';
import { isObject, get } from '@salesforce/ts-types';
import { load } from '@oclif/core/lib/module-loader.js';
import { ModuleLoader } from '@oclif/core';

const log = Logger.childFromRoot('plugin-settings:load_config_meta');
const OCLIF_META_PJSON_KEY = 'configMeta';

async function loadConfigMeta(plugin: Interfaces.Plugin): Promise<ConfigPropertyMeta | undefined> {
Expand All @@ -21,25 +20,21 @@ async function loadConfigMeta(plugin: Interfaces.Plugin): Promise<ConfigProperty
return;
}

const x = (await load(plugin, configMetaPath)) as { default: ConfigPropertyMeta };

log.info(x);

return x.default ?? x;
} catch {
const module = await ModuleLoader.load<{ default?: ConfigPropertyMeta }>(plugin, configMetaPath);
return module.default;
} catch (err) {
return;
}
}

const hook: Hook<'init'> = async ({ config }): Promise<void> => {
const hook: Hook<'init'> = async ({ config, context }): Promise<void> => {
const flattenedConfigMetas = (
await Promise.all(
(config.getPluginsList() || []).flatMap(async (plugin) => {
const configMeta = await loadConfigMeta(plugin);
if (!configMeta) {
log.info(`No config meta found for ${plugin.name}`);
context.debug(`No config meta found for ${plugin.name}`);
}

return configMeta;
})
)
Expand Down
72 changes: 45 additions & 27 deletions test/hooks/load_config_meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,68 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import { test, expect } from '@oclif/test';
import { Plugin } from '@oclif/core';
import { expect } from 'chai';
import { Config as OclifConfig, Plugin } from '@oclif/core';
import { Config } from '@salesforce/core';
import { stubMethod } from '@salesforce/ts-sinon';
import sinon from 'sinon';
import { SinonSandbox, SinonStub } from 'sinon';
import tsSrcConfigMetaMock from '../config-meta-mocks/typescript-src/src/config-meta.js';
// @ts-expect-error because it's a js file with no types
import jsLibConfigMetaMock from '../config-meta-mocks/javascript-lib/lib/config-meta.js';

process.env.NODE_ENV = 'development';

describe('hooks', () => {
let sandbox: SinonSandbox;
beforeEach(() => {
let config: OclifConfig;
let addAllowedPropertiesStub: SinonStub;

beforeEach(async () => {
sandbox = sinon.createSandbox();
stubMethod(sandbox, Config, 'addAllowedProperties');
addAllowedPropertiesStub = stubMethod(sandbox, Config, 'addAllowedProperties');
config = await OclifConfig.load(process.cwd());
});

afterEach(() => {
sandbox.restore();
});
test
.stdout()
.loadConfig()
.do((ctx) => {
const mockPluginRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../config-meta-mocks/typescript-src'
);
ctx.config.plugins.set('sfdx-cli-ts-plugin', {

it('should load config metas from a ts src directory', async () => {
const mockPluginRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../config-meta-mocks/typescript-src'
);

sandbox.stub(config, 'plugins').value(
new Map(config.plugins).set('sfdx-cli-ts-plugin', {
root: mockPluginRoot,
hooks: {},
name: 'sf-cli-ts-plugin',
pjson: JSON.parse(readFileSync(path.resolve(mockPluginRoot, 'package.json'), 'utf-8')),
} as Plugin)
);

await config.runHook('init', { argv: [], id: 'test' });
expect(addAllowedPropertiesStub.firstCall.args[0][1]).to.equal(tsSrcConfigMetaMock.default);
});

it('should load config metas from a js lib directory', async () => {
const mockPluginRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../config-meta-mocks/javascript-lib'
);

sandbox.stub(config, 'plugins').value(
new Map(config.plugins).set('sfdx-cli-js-plugin', {
root: mockPluginRoot,
hooks: {},
name: 'sf-cli-js-plugin',
pjson: JSON.parse(readFileSync(path.resolve(mockPluginRoot, 'package.json'), 'utf-8')),
} as unknown as Plugin);
})
.hook('init')
.do(() => {
expect(tsSrcConfigMetaMock.default).to.deep.equal([
{
key: 'customKey',
},
]);
// modified since devPlugins now includes plugin-deploy-retrieve to exercise a config-meta that it includes.
// see https://github.com/salesforcecli/plugin-deploy-retrieve/blob/main/src/configMeta.ts
expect((Config.addAllowedProperties as SinonStub).firstCall.args[0][1]).to.equal(tsSrcConfigMetaMock.default);
})
.it('loads config metas from a ts src directory');
} as Plugin)
);

await config.runHook('init', { argv: [], id: 'test' });
expect(addAllowedPropertiesStub.firstCall.args[0][1]).to.equal(jsLibConfigMetaMock.default);
});
});
51 changes: 41 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
dependencies:
regenerator-runtime "^0.14.0"

"@babel/template@^7.20.7", "@babel/template@^7.22.15":
"@babel/template@^7.22.15":
version "7.22.15"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.22.15.tgz#09576efc3830f0430f4548ef971dde1350ef2f38"
integrity sha512-QPErUVm4uyJa60rkI73qneDacvdvzxshT3kksGqlGWYdOTIUOwJ7RDUL8sGqslY1uXWSL6xMFKEXDS3ox2uF0w==
Expand All @@ -184,7 +184,7 @@
"@babel/parser" "^7.22.15"
"@babel/types" "^7.22.15"

"@babel/traverse@^7.21.0", "@babel/traverse@^7.23.2":
"@babel/traverse@^7.23.2":
version "7.23.2"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.23.2.tgz#329c7a06735e144a506bdb2cad0268b7f46f4ad8"
integrity sha512-azpe59SQ48qG6nu2CzcMLbxUudtN+dOM9kDbUqGq3HXUJRlo7i8fvPoxQUzYgLZ4cMVmuZgm8vvBpNeRhd6XSw==
Expand All @@ -200,7 +200,7 @@
debug "^4.1.0"
globals "^11.1.0"

"@babel/types@^7.20.2", "@babel/types@^7.21.0", "@babel/types@^7.22.15", "@babel/types@^7.22.5", "@babel/types@^7.23.0":
"@babel/types@^7.22.15", "@babel/types@^7.22.5", "@babel/types@^7.23.0":
version "7.23.0"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.23.0.tgz#8c1f020c9df0e737e4e247c0619f58c68458aaeb"
integrity sha512-0oIyUfKoI3mSqMvsxBdclDwxXKXAUA8v/apZbc+iSyARYou1o8ZGDxbUYyLFoW2arqS2jDGqJuZvv1d/io1axg==
Expand Down Expand Up @@ -777,7 +777,7 @@
wordwrap "^1.0.0"
wrap-ansi "^7.0.0"

"@oclif/core@^3.0.0", "@oclif/core@^3.0.4", "@oclif/core@^3.3.1", "@oclif/core@^3.5.0", "@oclif/core@^3.6.0", "@oclif/core@^3.8.0":
"@oclif/core@^3.0.0", "@oclif/core@^3.0.4", "@oclif/core@^3.3.1", "@oclif/core@^3.5.0", "@oclif/core@^3.6.0":
version "3.8.0"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-3.8.0.tgz#45f5f630b3b593c3486e7835953ad6fb2af01bcb"
integrity sha512-fKqg9QzjIflDcYljZkZEeY6zoRyk4AZ5e2V4LUIsSOR7+B78qpqNqDPJFTI8TvrEU3+Q+ssELntOL2VA3SMsqQ==
Expand Down Expand Up @@ -808,6 +808,37 @@
wordwrap "^1.0.0"
wrap-ansi "^7.0.0"

"@oclif/core@^3.9.0":
version "3.9.0"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-3.9.0.tgz#51c53ea4eafd3d643a55a37477f3e7c9d2cc2942"
integrity sha512-9UT0ySJgaUvERUQwDFh0u9Q5cfoBttfyaJ1sorSms6H5AELIjQ2Yvu2QfzPmnAit2rod+hdcDZ+O1Hia5Zcz+Q==
dependencies:
ansi-escapes "^4.3.2"
ansi-styles "^4.3.0"
cardinal "^2.1.1"
chalk "^4.1.2"
clean-stack "^3.0.1"
cli-progress "^3.12.0"
debug "^4.3.4"
ejs "^3.1.9"
get-package-type "^0.1.0"
globby "^11.1.0"
hyperlinker "^1.0.0"
indent-string "^4.0.0"
is-wsl "^2.2.0"
js-yaml "^3.14.1"
natural-orderby "^2.0.3"
object-treeify "^1.1.33"
password-prompt "^1.1.2"
slice-ansi "^4.0.0"
string-width "^4.2.3"
strip-ansi "^6.0.1"
supports-color "^8.1.1"
supports-hyperlinks "^2.2.0"
widest-line "^3.1.0"
wordwrap "^1.0.0"
wrap-ansi "^7.0.0"

"@oclif/plugin-command-snapshot@^5.0.1":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@oclif/plugin-command-snapshot/-/plugin-command-snapshot-5.0.1.tgz#6c21b2ef8007cda21f0caacd3462d5eedd622f32"
Expand Down Expand Up @@ -1022,10 +1053,10 @@
resolved "https://registry.yarnpkg.com/@salesforce/dev-config/-/dev-config-4.1.0.tgz#e529576466d074e7a5f1441236510fef123da01e"
integrity sha512-2iDDepiIwjXHS5IVY7pwv8jMo4xWosJ7p/UTj+lllpB/gnJiYLhjJPE4Z3FCGFKyvfg5jGaimCd8Ca6bLGsCQA==

"@salesforce/dev-scripts@^5.12.0":
version "5.12.0"
resolved "https://registry.yarnpkg.com/@salesforce/dev-scripts/-/dev-scripts-5.12.0.tgz#4aa4f37083221772dcc0bf91e78f504039c61f57"
integrity sha512-I84Xy91cyM1SCAKXTCCvGoLHTIrohLJt8uY/AFLGv+vePL8bzCBnp42N1giVAXxTwx8/WtMewGkpr+gFnRsORA==
"@salesforce/dev-scripts@^5.12.2":
version "5.12.2"
resolved "https://registry.yarnpkg.com/@salesforce/dev-scripts/-/dev-scripts-5.12.2.tgz#2008349169912c2569538f75484f45948de461fe"
integrity sha512-VZkk5bsTzsjHZjxlPrCiBMne/ep4IG3gBuID3LxlXzFTRS8Ulxi28jRrOzQ+ig8l8trgFfnqw64zuE6NOxe3lA==
dependencies:
"@commitlint/cli" "^17.1.2"
"@commitlint/config-conventional" "^17.1.0"
Expand Down Expand Up @@ -5066,7 +5097,7 @@ json5@^1.0.2:
dependencies:
minimist "^1.2.0"

json5@^2.2.2, json5@^2.2.3:
json5@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==
Expand Down Expand Up @@ -7050,7 +7081,7 @@ redeyed@~2.1.0:
dependencies:
esprima "~4.0.0"

regenerator-runtime@^0.13.11, regenerator-runtime@^0.13.3:
regenerator-runtime@^0.13.3:
version "0.13.11"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==
Expand Down

0 comments on commit 11d60a8

Please sign in to comment.