Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Quick change (#6)
Browse files Browse the repository at this point in the history
* WIP --VERIFIED

* clean up --VERIFIED

* WIP --VERIFIED

* WIP --VERIFIED

* WIP --VERIFIED

* corrected bundles --VERIFIED

* corrected bundles --VERIFIED
  • Loading branch information
knighttower authored Oct 13, 2023
1 parent bb827d9 commit 50edac3
Show file tree
Hide file tree
Showing 36 changed files with 3,185 additions and 8,677 deletions.
90 changes: 90 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"root": true,
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"env": {
"browser": true,
"node": true,
"es6": true
},
"extends": [
"eslint:recommended",
"prettier"
// "plugin:vue/recommended"
],
"plugins": [
"prettier"
],
"rules": {
"array-bracket-newline": [
"error",
{
"multiline": true
}
],
"array-bracket-spacing": [
"error",
"never"
],
"array-element-newline": [
"error",
"consistent"
],
"arrow-parens": [
"error",
"always"
],
"comma-dangle": [
"error",
{
"arrays": "always-multiline",
"exports": "always-multiline",
"functions": "always-multiline",
"imports": "always-multiline",
"objects": "always-multiline"
}
],
"curly": "error",
"disallowMultipleVarDecl": 0,
"indent": [
"error",
4,
{
"ignoredNodes": [
"SwitchCase"
]
}
],
"linebreak-style": [
"error",
"unix"
],
"maximumLineLength": 0,
"no-console": 0,
"no-return-assign": [
"error",
"always"
],
"prettier/prettier": [
"error"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
// ,
// "vue/html-indent": [
// "warn",
// 4
// ]
}
}
1 change: 0 additions & 1 deletion .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jobs:
node-version: 16
- run: npm ci
- run: npm run test
- run: npm run build
tagged-release:
needs: build
runs-on: ubuntu-latest
Expand Down
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ Homestead.json
.phpunit.result.cache
.gitcommands
.gitconfig
.gitcommands
.gitconfig
29 changes: 29 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.*
.*/*
*.lock
node_modules
public
**/.htaccess
.htaccess*
**/node_modules
**/package-lock.json
**/public
**/yarn.lock
.DS_Store
.git
.gitignore
.idea
.vscode
.yarn-integrity
.yarnrc
.yarnrc.yml
yarn-error.log
yarn.lock
.env
.env.local
.env.development
**/build/
coverage
**/.nuxt
**/.jarvis
**/dist/
21 changes: 21 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

{
"printWidth": 120,
"useTabs": false,
"tabWidth":4,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "always",
"singleAttributePerLine": true,
"overrides": [
{
"files": "*.scss",
"options": {
"singleQuote": false
}
}
]
}
14 changes: 14 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
php:
preset: laravel
disabled:
- no_unused_imports
finder:
not-name:
- index.php
- server.php
js:
finder:
not-name:
- webpack.mix.js
- vite.config
css: true
139 changes: 139 additions & 0 deletions BuildExports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import fs from 'fs';
import path from 'path';
import glob from 'glob';
import { PowerHelper as helper, Utility as utils } from '@knighttower/js-utility-functions/index.mjs';
import { getFlagValue } from '@knighttower/js-utility-functions/node.mjs';

/**
* Reads a file and returns the names of the exported modules.
* @param {string} filePath
* @returns {Object} Object containing arrays of named exports and the default export
*/
function getExports(filePath) {
// get the whole file content
const content = fs.readFileSync(filePath, 'utf-8');
// Example matches: export const myVar, export function myFunc, export class MyClass
// Example matches: export myVar, export myFunc
const matchSingleExps = content.match(/export\s+(const|let|var|function|class)\s+(\w+)|export\s+(\w+)/g) || [];
// Example matches: export default class MyClass, export default function myFunc,
const matchDefClasses = content.match(/export\s+default\s+(class|function)\s*(\b(?!(\{|\())\w+\b)/) || [];
// Example matches: export default Name, Name as default
const matchDefSingles =
content.match(
/export\s+default\s+(\b(?!(\{|\(|(class|function)))\w+\b)|(?<=\{[^}]*)\w+\s+as\s+default(?=[^}]*\})/,
) || [];
// Example matches: export { myVar, myFunc }
const matchAliasesExps = content.match(/export\s*{([^}]+)}/g) || [];

// Storages
const singleExports = [];
const defaultExport = utils.emptyOrValue(
helper.cleanStr(
matchDefClasses[0] || matchDefSingles[0] || '',
'export',
'default',
'function',
'class',
/\bas\b/,
),
);
const aliasExports = [];
let namedExports = [];

// =========================================
// --> Process the arrays to clean and pick the export
// --------------------------
// Handle single exports
matchSingleExps.forEach((exp) => {
const parts = helper.cleanStr(exp, 'export').match(/\b\w+\b/g);
if (parts.length === 1) {
singleExports.push(parts[0]);
} else if (parts.length === 2) {
singleExports.push(parts[1]);
}
});

// Handle aliases
matchAliasesExps.forEach((aliasLine) => {
helper
// cleanup and create an array of aliases
.getChunks(helper.cleanStr(aliasLine, 'export', '{', '}'))
// exclude default export
.filter((chunk) => !chunk.includes('default'))
// iterate to pick the correct alias;
.forEach((chunk) => {
if (chunk.includes(' as ')) {
const alias = helper.getChunks(chunk, ' as ');
if (alias[1]) {
aliasExports.push(alias[1]);
}
} else {
aliasExports.push(chunk);
}
});
});

// Merge all named exports and filter
namedExports = [...aliasExports, ...singleExports].filter((name) => name !== 'default' && name !== defaultExport);

return {
named: Array.from(new Set(namedExports)), // Remove duplicates
default: defaultExport,
};
}

/**
* Generates the content for the index.js file.
* @param {Object} allExports - An object containing information about all exports
* @returns {string} - The content for the index.js file
*/
function generateIndexContent(allExports) {
let imports = '';
let exports = '';

for (const [filePath, { named, default: defaultExport }] of Object.entries(allExports)) {
const moduleName = path.basename(filePath).replace(/\.js|\.mjs/, '');
const relativePath = path.relative(process.cwd(), filePath).replace(/\\/g, '/');
const commentSingle = `// Single Modules and Aliases from: ${moduleName}\n`;
const commentDefault = `// Default Module from: ${moduleName}\n`;
if (named.length > 0) {
const namedModules = named.join(', ');

imports += commentSingle;
imports += `import { ${namedModules} } from './${relativePath}';\n`;
exports += commentSingle;
exports += `export { ${namedModules} };\n`;
}

if (defaultExport) {
imports += commentDefault;
imports += `import ${defaultExport} from './${relativePath}';\n`;
exports += commentDefault;
exports += `export { ${defaultExport} };\n`;
}
}

return `${imports}\n${exports}`;
}

/**
* Main function to generate the index.js file.
*/
(function generateIndex() {
const directory = getFlagValue('dir') ?? './src';
const destination = getFlagValue('out') ?? './index.mjs';
// Synchronously fetch all file paths within a directory and its subdirectories
// that have a .js or .mjs extension
const filePaths = glob.sync(`${directory}/**/*.{js,mjs}`);
const allExports = {};

filePaths.forEach((filePath) => {
if (!path.basename(filePath).includes('index')) {
allExports[filePath] = getExports(filePath);
}
});

const indexContent = generateIndexContent(allExports);
fs.writeFileSync(path.join(process.cwd(), destination), indexContent);
console.log('index generated');
})();
61 changes: 61 additions & 0 deletions BumpVersion.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const fs = require('fs');
const path = require('path');

/**
* Read package.json and return its content.
*
* @returns {Object} - Parsed JSON content of package.json.
*/
const readPackageJson = () => {
const filePath = path.join(process.cwd(), 'package.json');
const rawContent = fs.readFileSync(filePath, 'utf8');
return JSON.parse(rawContent);
};

/**
* Write content to package.json.
*
* @param {Object} content - The updated package.json content.
*/
const writePackageJson = (content) => {
const filePath = path.join(process.cwd(), 'package.json');
const updatedContent = JSON.stringify(content, null, 2);
fs.writeFileSync(filePath, updatedContent);
};

/**
* Bump the version based on the flag passed.
*
* @param {string} currentVersion - Current version from package.json.
* @param {string} flag - The flag that determines which part of the version to bump (major, minor, patch).
* @returns {string} - New bumped version.
*/
const bumpVersion = (currentVersion, flag) => {
const [major, minor, patch] = currentVersion.split('.').map(Number);

switch (flag) {
case '--major':
return `${major + 1}.0.0`;
case '--minor':
return `${major}.${minor + 1}.0`;
default:
return `${major}.${minor}.${patch + 1}`;
}
};

// Parse command-line arguments
const flag = process.argv[2];

// Read package.json and get current version
const packageJson = readPackageJson();
const { version: currentVersion } = packageJson;

// Bump the version
const newVersion = bumpVersion(currentVersion, flag);

// Update package.json with the new version
packageJson.version = newVersion;
writePackageJson(packageJson);

// Log output
console.log(`Version bumped from ${currentVersion} to ${newVersion}`);
Loading

0 comments on commit 50edac3

Please sign in to comment.