Skip to content

Commit

Permalink
feat: added declarative config support
Browse files Browse the repository at this point in the history
  • Loading branch information
GeeWizWow authored and antongolub committed Jun 13, 2022
1 parent 4b20da7 commit c98ff10
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 48 deletions.
41 changes: 0 additions & 41 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,6 @@ const cli = meow(
const argvStart = process.argv.includes("--") ? process.argv.indexOf("--") + 1 : 2;
return process.argv.slice(argvStart);
},
flags: {
sequentialInit: {
type: "boolean",
},
sequentialPrepare: {
type: "boolean",
default: true,
},
firstParent: {
type: "boolean",
},
debug: {
type: "boolean",
},
"deps.bump": {
type: "string",
default: "override",
},
"deps.release": {
type: "string",
default: "patch",
},
"deps.prefix": {
type: "string",
default: "",
},
ignorePrivate: {
type: "boolean",
default: true,
},
ignorePackages: {
type: "string",
},
tagFormat: {
type: "string",
default: "${name}@${version}",
},
dryRun: {
type: "boolean",
},
},
}
);

Expand Down
15 changes: 9 additions & 6 deletions bin/runner.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { createRequire } from "module";

export default async (flags) => {
export default async (cliFlags) => {
const require = createRequire(import.meta.url);

if (flags.debug) {
require("debug").enable("msr:*");
}

// Imports.
const getPackagePaths = (await import("../lib/getPackagePaths.js")).default;
const getConfigMultiSemrel = (await import("../lib/getConfigMultiSemrel.js")).default;
const multiSemanticRelease = (await import("../lib/multiSemanticRelease.js")).default;
const multisemrelPkgJson = require("../package.json");
const semrelPkgJson = require("semantic-release/package.json");
Expand All @@ -18,6 +15,12 @@ export default async (flags) => {

// Catch errors.
try {
const flags = await getConfigMultiSemrel(cwd, cliFlags);

if (flags.debug) {
require("debug").enable("msr:*");
}

console.log(`multi-semantic-release version: ${multisemrelPkgJson.version}`);
console.log(`semantic-release version: ${semrelPkgJson.version}`);
console.log(`flags: ${JSON.stringify(flags, null, 2)}`);
Expand All @@ -34,7 +37,7 @@ export default async (flags) => {
},
(error) => {
// Log out errors.
console.error(`[multi-semantic-release111]:`, error);
console.error(`[multi-semantic-release]:`, error);
process.exit(1);
}
);
Expand Down
67 changes: 67 additions & 0 deletions lib/getConfigMultiSemrel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { cosmiconfig } from "cosmiconfig";
import { default as resolveFrom } from "resolve-from";
import { pickBy, isNil, castArray } from "lodash-es";

const CONFIG_NAME = "multi-release";
const CONFIG_FILES = [
"package.json",
`.${CONFIG_NAME}rc`,
`.${CONFIG_NAME}rc.json`,
`.${CONFIG_NAME}rc.yaml`,
`.${CONFIG_NAME}rc.yml`,
`.${CONFIG_NAME}rc.js`,
`.${CONFIG_NAME}rc.cjs`,
`${CONFIG_NAME}.config.js`,
`${CONFIG_NAME}.config.cjs`,
];

/**
* Get the multi semantic release configuration options for a given directory.
*
* @param {string} cwd The directory to search.
* @param {Object} cliOptions cli supplied options.
* @returns {Object} The found configuration option
*
* @internal
*/
export default async function getConfig(cwd, cliOptions) {
const { config } = (await cosmiconfig(CONFIG_NAME, { searchPlaces: CONFIG_FILES }).search(cwd)) || {};
const { extends: extendPaths, ...rest } = { ...config, ...cliOptions };

let options = rest;

if (extendPaths) {
// If `extends` is defined, load and merge each shareable config with `options`
options = {
...castArray(extendPaths).reduce((result, extendPath) => {
const extendsOptions = require(resolveFrom.silent(__dirname, extendPath) ||
resolveFrom(cwd, extendPath));
return { ...result, ...extendsOptions };
}, {}),
...options,
};
}

// Set default options values if not defined yet
options = {
sequentialInit: false,
sequentialPrepare: true,
firstParent: false,
debug: false,
ignorePrivate: true,
ignorePackages: "",
tagFormat: "${name}@${version}",
dryRun: false,
// Remove `null` and `undefined` options so they can be replaced with default ones
...pickBy(options, (option) => !isNil(option)),
// Treat nested objects differently as otherwise we'll loose undefined keys
deps: {
bump: "override",
release: "patch",
prefix: "",
...pickBy(options.deps, (option) => !isNil(option)),
},
};

return options;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@
"lodash-es": "^4.17.21",
"meow": "^10.1.2",
"promise-events": "^0.2.4",
"resolve-from": "^5.0.0",
"semantic-release": "^19.0.2",
"semver": "^7.3.7",
"signale": "^1.4.0",
"stream-buffers": "^3.0.2"
},
"devDependencies": {
"@jest/globals": "^28.1.0",
"@commitlint/config-conventional": "^17.0.0",
"@jest/globals": "^28.1.0",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^8.0.4",
Expand Down

0 comments on commit c98ff10

Please sign in to comment.