Skip to content

Commit

Permalink
refactor!: Change configurations naming convention
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Exports changed.
  • Loading branch information
MorevM committed Mar 19, 2022
1 parent ec0a994 commit 931e407
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .stylelintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
extends: [
'./build/configurations/scss-warn-autofixable.js',
'./build/configurations/scss.js',
],
};
2 changes: 1 addition & 1 deletion configurations/css/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { processExports } = require('../../utils/helpers.js');
const { parts } = require('./_settings.js');

module.exports = (autofixable) => processExports({ autofixable, parts });
module.exports = (mode) => processExports({ mode, parts });
2 changes: 1 addition & 1 deletion configurations/scss/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { processExports } = require('../../utils/helpers.js');
const { parts, base } = require('./_settings.js');

module.exports = (autofixable) => processExports({ autofixable, parts, base });
module.exports = (mode) => processExports({ mode, parts, base });
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
"exports": {
".": "./build/configurations/scss.js",
"./css": "./build/configurations/css.js",
"./css/warn-autofixable": "./build/configurations/css-warn-autofixable.js",
"./css/off-autofixable": "./build/configurations/css-off-autofixable.js",
"./css/strict": "./build/configurations/css-strict.js",
"./css/quiet": "./build/configurations/css-quiet.js",
"./scss": "./build/configurations/scss.js",
"./scss/warn-autofixable": "./build/configurations/scss-warn-autofixable.js",
"./scss/off-autofixable": "./build/configurations/scss-off-autofixable.js"
"./scss/strict": "./build/configurations/scss-strict.js",
"./scss/quiet": "./build/configurations/scss-quiet.js"
},
"main": "./build/configurations/scss.js",
"scripts": {
Expand Down
19 changes: 7 additions & 12 deletions utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,23 @@ const toExports = (source) =>
`module.exports = ${util.inspect(source, { maxArrayLength: 9999, depth: 999 })}`
.replace(/["'](require\(.*\))["']/g, '$1'); // unwrap require call

const getNames = ({ name }, autofixable) => {
const filename = (autofixable === 'bypass')
? `${name}.js`
: `${name}-${autofixable}-autofixable.js`;

const exportsName = (autofixable === 'bypass')
? `./${name}`
: `./${name}/${autofixable}-autofixable`;
const getNames = ({ name }, mode) => {
const filename = (mode === 'default') ? `${name}.js` : `${name}-${mode}.js`;
const exportsName = (mode === 'default') ? `./${name}` : `./${name}/${mode}`;

return { filename, exportsName };
};

(async () => {
await configurations.forEach(async (config) => {
await ['bypass', 'warn', 'off'].forEach(async (autofixable) => {
await ['default', 'strict', 'quiet'].forEach(async (mode) => {
const extend = config.extend
? `./${getNames({ name: config.extend }, autofixable).filename}`
? `./${getNames({ name: config.extend }, mode).filename}`
: null;
let configSource = makeConfig([{ ...config, extend, autofixable }]);
let configSource = makeConfig([{ ...config, extend, mode }]);
configSource = toExports(configSource);

const { filename, exportsName } = getNames(config, autofixable);
const { filename, exportsName } = getNames(config, mode);
exportsField[exportsName] = `${CONFIGS_DIR_REL}${filename}`;

fs.writeFileSync(`${CONFIGS_DIR}/${filename}`, configSource);
Expand Down
12 changes: 6 additions & 6 deletions utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ const autofixableRulesToOff = (rules, autofixableList) => Object.fromEntries(
.map(([rule, value]) => [rule, autofixableList.includes(rule) ? null : value]),
);

const getProcessedRules = ({ autofixable, base, rules }) => {
if (autofixable === 'bypass') return rules;
const getProcessedRules = ({ mode, base, rules }) => {
if (mode === 'strict') return rules;

const autofixableRules = Object.entries(base.rules)
.filter(([key]) => key.startsWith('+'))
.map(([key]) => key.slice(1));

return autofixable === 'warn'
return mode === 'default'
? autofixableRulesToWarn(rules, autofixableRules)
: autofixableRulesToOff(rules, autofixableRules);
: autofixableRulesToOff(rules, autofixableRules); // assume `quiet`
};

const processExports = ({ autofixable, base, parts }) => {
const processExports = ({ mode, base, parts }) => {
const mergedParts = _mergeWith(
{},
{ ...base },
Expand All @@ -54,7 +54,7 @@ const processExports = ({ autofixable, base, parts }) => {
}, []),
);

const processedRules = getProcessedRules({ autofixable, base: mergedParts, rules });
const processedRules = getProcessedRules({ mode, base: mergedParts, rules });

return _mergeWith(
{ ...mergedParts, rules: processedRules },
Expand Down
4 changes: 2 additions & 2 deletions utils/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const makeConfig = (_configs) => {

let result = {};

configs.forEach(({ name, autofixable, overrides, extend }) => {
configs.forEach(({ name, mode, overrides, extend }) => {
const configFactory = require(resolve(CONFIGS_PATH, name));
let processed = configFactory(autofixable);
let processed = configFactory(mode);

if (extend) {
processed = {
Expand Down

0 comments on commit 931e407

Please sign in to comment.