Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support js in .hbs files #34

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
module.exports = {
parserOptions: {
sourceType: 'module',
},
parser: '@typescript-eslint/parser',
extends: [
'plugin:prettier/recommended',
'plugin:import/recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:@typescript-eslint/recommended',
],
plugins: ['prettier', 'jsx-a11y', 'simple-import-sort', '@typescript-eslint'],
rules: {
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',

'import/no-unresolved': 'off',
'no-use-before-define': 'off',
'@typescript-eslint/no-use-before-define': ['error'],
'no-shadow': 'off',
'@typescript-eslint/no-shadow': ['error'],
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
semi: ['error', 'never'],
'linebreak-style': 'off',
'max-len': ['error', 140],
'jsx-a11y/anchor-has-content': 'off',
'jsx-a11y/no-static-element-interactions': 'off',
'no-console': 'off',
'spaced-comment': 'off',
'import/no-cycle': 'off',
'import/no-extraneous-dependencies': 'off',
'import/no-useless-path-segments': 'off',
'import/extensions': 'off',
'import/no-commonjs': 'error',
'no-param-reassign': 'off',
'dot-notation': 'off',
'no-restricted-syntax': [
'error',
{
selector: 'VariableDeclarator > ObjectPattern ObjectPattern',
message: 'Вложенная деструктуризация сложно воспринимается!',
},
],
'no-restricted-imports': ['error', 'lodash'],
'operator-linebreak': 'off',
'implicit-arrow-linebreak': 'off',
'prefer-destructuring': 'off',
'object-curly-newline': [
'off',
{
ObjectExpression: { minProperties: 0, multiline: true },
ObjectPattern: { minProperties: 0, multiline: true },
},
],
'function-paren-newline': 'off',
'no-else-return': ['error', { allowElseIf: true }],
'padded-blocks': ['error', 'never'],
camelcase: ['error', { properties: 'never' }],
'no-multiple-empty-lines': ['error', { max: 2, maxEOF: 1 }],
'arrow-parens': 'off',
'no-mixed-operators': 'off',
'jsx-a11y/click-events-have-key-events': 'off',
'jsx-a11y/anchor-is-valid': 'off',
'jsx-a11y/iframe-has-title': 'off',
'jsx-a11y/alt-text': 'off',
'jsx-a11y/label-has-for': 'off',
'jsx-a11y/label-has-associated-control': 'off',
'jsx-a11y/no-noninteractive-element-interactions': 'off',
'jsx-a11y/media-has-caption': 'off',
'jsx-a11y/control-has-associated-label': 'off',
'no-restricted-globals': 'off',
'no-multi-spaces': 'error',
'import/no-named-default': 'off',
'import/prefer-default-export': 'off',
'no-useless-escape': 'error',
'brace-style': ['error', '1tbs', { allowSingleLine: true }],
'no-return-assign': ['error', 'except-parens'],
'lines-between-class-members': 'off',
'comma-dangle': [
'error',
{
arrays: 'always-multiline',
exports: 'always-multiline',
functions: 'ignore',
imports: 'always-multiline',
objects: 'always-multiline',
},
],
'max-classes-per-file': 'off',
'prettier/prettier': ['error', { endOfLine: 'auto' }],
'padding-line-between-statements': [
'error',
{ blankLine: 'always', prev: 'block-like', next: 'const' },
{ blankLine: 'always', prev: 'const', next: 'block-like' },
{ blankLine: 'always', prev: '*', next: 'return' },
{ blankLine: 'always', prev: 'if', next: 'block-like' },
{ blankLine: 'always', prev: 'block-like', next: 'if' },
],
'simple-import-sort/imports': [
'error',
{
groups: [
[
'^react',
'^@?\\w',
'^(@|@company|@ui|components|utils|config|vendored-lib)(/.*|$)',
'^\\u0000',
'^\\.\\.(?!/?$)',
'^\\.\\./?$',
'^\\./(?=.*/)(?!/?$)',
'^\\.(?!/?$)',
'^\\./?$',
'^.+\\.s?css$',
],
],
},
],
'@typescript-eslint/no-empty-interface': [
'error',
{
allowSingleExtends: true,
},
],
},
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.DS_Store
.idea

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
Expand Down Expand Up @@ -105,3 +107,6 @@ dist

# Antora
build

# lint-staged
tsconfig.*.lint.json
9 changes: 9 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
yarn lint-staged --allow-empty
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавил линтеры ямл, ts-а и js-а + запустил их на прекоммите


status=$?

if [ $status -ne 0 ]; then
echo -e "\033[0;31mLinting failed\033[0m"
exit 1
fi

23 changes: 23 additions & 0 deletions .lintstagedrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('node:fs')

const generateTSConfig = stagedFilenames => {
return type => {
const tsconfig = JSON.parse(fs.readFileSync('tsconfig.json', 'utf8'))
if (stagedFilenames.length === 0) return ''

tsconfig.include = [...stagedFilenames, 'src/**/*.d.ts']
fs.writeFileSync(`tsconfig.${type}.lint.json`, JSON.stringify(tsconfig))

return `${type} --skipLibCheck --noEmit --pretty --project tsconfig.${type}.lint.json`
}
}

const lintTypes = (fileName) => generateTSConfig(fileName)('tsc')

module.exports = {
'src/**/*.(ts)': [lintTypes],
'src/**/*.(ts|js)': ['eslint --fix'],
'config/**/*.(ts)': [lintTypes],
'config/**/*.(ts|js)': ['eslint --fix'],
'**/*.(yml|yaml)': ['npx yaml-lint'],
}
11 changes: 11 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "avoid"
}
4 changes: 2 additions & 2 deletions antora-playbook-en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
site:
title: Taymyr Antora UI
url: /
url: /en
start_page: ROOT::index.adoc
content:
sources:
Expand All @@ -15,7 +15,7 @@ ui:

output:
clean: false
dir: build/site
dir: build/site/en
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поднастроил тут, чтобы файлики по категории локали всё же были)


antora:
extensions:
Expand Down
7 changes: 7 additions & 0 deletions config/constants/Env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const mode = process.env.NODE_ENV

export const Env = {
isDev: mode === 'development',
isProd: mode === 'production',
cwd: process.cwd(),
}
12 changes: 12 additions & 0 deletions config/helpers/getDirectoryFileNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'node:fs'
import path from 'node:path'

export const getDirectoryFileNames = (dirPath: string): string[] => {
const files = fs.readdirSync(dirPath, { withFileTypes: true }).map(dirent => {
const fullPath = path.join(dirPath, dirent.name)

return dirent.isDirectory() ? getDirectoryFileNames(fullPath) : fullPath
})

return files.flat()
}
33 changes: 33 additions & 0 deletions config/helpers/htmlPlugin/createHtmlPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import HtmlWebpackPlugin from 'html-webpack-plugin'
import get from 'lodash/get'

interface IHtmlPluginCreatingOptions {
fileName: string
filterKey: string
filterValue: string
filterByTagNode?: (tagNode: Record<string, unknown>) => boolean
}

export const createHtmlPlugin = (config: IHtmlPluginCreatingOptions) => {
const { fileName, filterKey, filterValue, filterByTagNode = () => true } = config

const getTemplateContent: HtmlWebpackPlugin.Options['templateContent'] = htmlWebpackPlugin => {
return (
htmlWebpackPlugin.tags.headTags
.filter(
(tagNode: Record<string, unknown>) =>
get(tagNode, filterKey) === filterValue && filterByTagNode(tagNode),
)
.join('')
// It must be Handlebars template with root path as variable
.replaceAll('uiRootPath', '{{{uiRootPath}}}')
)
}

return new HtmlWebpackPlugin({
publicPath: 'uiRootPath',
templateContent: ({ htmlWebpackPlugin }) => `${getTemplateContent(htmlWebpackPlugin)}`,
filename: fileName,
inject: false,
})
}
19 changes: 19 additions & 0 deletions config/helpers/htmlPlugin/filterInjectingScriptByScriptNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IHtmlPluginCreatingOptions } from './types'

export const filterInjectingScriptByScriptNames = (scriptNames: string[]) => {
const filterByTagNode: IHtmlPluginCreatingOptions['filterByTagNode'] = tagNode => {
if ('attributes' in tagNode) {
const { attributes } = tagNode

if (typeof attributes === 'object' && attributes !== null && 'src' in attributes) {
const src = attributes.src

return typeof src === 'string' && scriptNames.some(scriptName => src.endsWith(scriptName))
}
}

return false
}

return filterByTagNode
}
2 changes: 2 additions & 0 deletions config/helpers/htmlPlugin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createHtmlPlugin } from './createHtmlPlugin'
export { filterInjectingScriptByScriptNames } from './filterInjectingScriptByScriptNames'
6 changes: 6 additions & 0 deletions config/helpers/htmlPlugin/types/IHtmlPluginCreatingOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface IHtmlPluginCreatingOptions {
fileName: string
filterKey: string
filterValue: string
filterByTagNode?: (tagNode: Record<string, unknown>) => boolean
}
1 change: 1 addition & 0 deletions config/helpers/htmlPlugin/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { IHtmlPluginCreatingOptions } from './IHtmlPluginCreatingOptions'
2 changes: 2 additions & 0 deletions config/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { spawnCommand } from './spawnCommand'
export { getDirectoryFileNames } from './getDirectoryFileNames'
29 changes: 29 additions & 0 deletions config/helpers/spawnCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { spawn, SpawnOptions } from 'node:child_process'

export const spawnCommand = (commandStr: string, options?: SpawnOptions) => {
const [command, ...args] = commandStr.split(' ')

if (!command) return Promise.reject()

return new Promise((resolve, reject) => {
const spawnedProcess = spawn(command, args, {
shell: true,
stdio: 'inherit',
env: { FORCE_COLOR: 'true', ...process.env },
...options,
})

spawnedProcess.stdout?.on('data', data => {
console.log(data.toString())
})

spawnedProcess?.on('close', code => {
if (code === 1) {
//eslint-disable-next-line
reject()
}

resolve(null)
})
})
}
21 changes: 21 additions & 0 deletions config/plugins/BuildAntoraPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Compiler } from 'webpack'
import { spawnCommand } from '../helpers/spawnCommand'

export class BuildAntoraPlugin {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавил плагин, который билдит антору после компиляции сборки и после делает форсированное обновления в браузере

apply(compiler: Compiler) {
let shouldUpdateAntora = true
compiler.hooks.done.tap('Build antora', async stats => {
if (shouldUpdateAntora) {
await spawnCommand('npm run build-antora')
shouldUpdateAntora = false
setTimeout(() => {
compiler.hooks.done.callAsync(stats, () => {
return
})
}, 500)
} else {
shouldUpdateAntora = true
}
})
}
}
Loading