Skip to content

Commit

Permalink
refactor(packages/sui-widget-embedder): apply new linting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
kikoruiz committed Nov 21, 2023
1 parent 4a43745 commit bb047e5
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 98 deletions.
42 changes: 9 additions & 33 deletions packages/sui-widget-embedder/bin/sui-widget-embedder-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ const suiWidgetEmbedderConfig = config['sui-widget-embedder'] || {}

program
.option('-C, --clean', 'Remove public folder before create a new one')
.option(
'-R --remoteCdn <url>',
'Remote url where the downloader will be placed'
)
.option('-R --remoteCdn <url>', 'Remote url where the downloader will be placed')
.on('--help', () => {
console.log(' Description:')
console.log('')
Expand All @@ -34,12 +31,8 @@ program
console.log('')
console.log(' $ sui-widget-embedder build')
console.log('')
console.log(
' You can even choose where should the downloader going to get the files:'
)
console.log(
' $ sui-widget-embedder build -remoteCdn http://mysourcedomain.com'
)
console.log(' You can even choose where should the downloader going to get the files:')
console.log(' $ sui-widget-embedder build -remoteCdn http://mysourcedomain.com')
console.log('')
})
.parse(process.argv)
Expand Down Expand Up @@ -71,30 +64,19 @@ const build = ({page, remoteCdn}) => {
})
}

const pagesFor = ({path}) =>
readdirSync(path).filter(file => statSync(resolve(path, file)).isDirectory())
const pagesFor = ({path}) => readdirSync(path).filter(file => statSync(resolve(path, file)).isDirectory())

const manifests = () =>
pagesFor({path: PUBLIC_PATH}).reduce((acc, page) => {
acc[page] = require(resolve(
process.cwd(),
'public',
page,
'asset-manifest.json'
))
acc[page] = require(resolve(process.cwd(), 'public', page, 'asset-manifest.json'))
return acc
}, {})

const pageConfigs = () =>
pagesFor({path: PAGES_PATH}).reduce(
(acc, page) => ({
...acc,
[page]: require(resolve(
process.cwd(),
PAGES_FOLDER,
page,
'package.json'
))
[page]: require(resolve(process.cwd(), PAGES_FOLDER, page, 'package.json'))
}),
{}
)
Expand All @@ -113,10 +95,7 @@ const createDownloader = async () => {
output,
downloader
.replace("require('static-manifests')", JSON.stringify(staticManifests))
.replace(
"require('static-pageConfigs')",
JSON.stringify(staticPageConfigs)
)
.replace("require('static-pageConfigs')", JSON.stringify(staticPageConfigs))
)

console.log(`Created a new ${FILE_DOWNLOADER} file`)
Expand All @@ -126,11 +105,8 @@ const createDownloader = async () => {
}
}

const serialPromiseExecution = promises =>
promises.reduce((acc, func) => acc.then(() => func()), Promise.resolve([]))
const serialPromiseExecution = promises => promises.reduce((acc, func) => acc.then(() => func()), Promise.resolve([]))

serialPromiseExecution(
pagesFor({path: PAGES_FOLDER}).map(page => () => build({page, remoteCdn}))
)
serialPromiseExecution(pagesFor({path: PAGES_FOLDER}).map(page => () => build({page, remoteCdn})))
.then(createDownloader)
.catch(showError)
13 changes: 3 additions & 10 deletions packages/sui-widget-embedder/bin/sui-widget-embedder-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const program = require('commander')
const ncp = require('copy-paste')

const appFactory = require('../development/index.js')
const config =
require(`${process.cwd()}/package.json`).config['sui-widget-embedder'] || {}
const config = require(`${process.cwd()}/package.json`).config['sui-widget-embedder'] || {}

const PORT = process.env.PORT || config.devPort || 3000
config.port = PORT
Expand All @@ -14,11 +13,7 @@ program
.usage('-p detail -a address -b')
.option('-b, --browser', 'Enable the dev mode inside a html file')
.option('-p, --page <name>', 'Name of the page')
.option(
'-a, --address <ip-address>',
'Local ip address to lookup statics, useful for virtual machines',
'localhost'
)
.option('-a, --address <ip-address>', 'Local ip address to lookup statics, useful for virtual machines', 'localhost')
.on('--help', () => {
console.log(' Description:')
console.log('')
Expand All @@ -27,9 +22,7 @@ program
console.log(' Examples:')
console.log('')
console.log(' $ sui-widget-embedder development --page detail')
console.log(
' $ sui-widget-embedder development --page detail --address 10.0.2.2'
)
console.log(' $ sui-widget-embedder development --page detail --address 10.0.2.2')
console.log('')
})
.parse(process.argv)
Expand Down
19 changes: 4 additions & 15 deletions packages/sui-widget-embedder/bin/sui-widget-embedder-generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ program
console.log('')
console.log(' $ sui-widget-embedder generate <page>')
console.log(' $ sui-widget-embedder generate detailPage')
console.log(
" $ sui-widget-embedder generate detailPage -E '/d\\\\w+\\\\.html'"
)
console.log(" $ sui-widget-embedder generate detailPage -E '/d\\\\w+\\\\.html'")
console.log(' $ custom-help --help')
console.log(' $ custom-help -h')
console.log('')
console.log(
colors.cyan(
' IMPORTANT - Regexp (-E) must be with commas ALWAYS to avoid terminal escape or interpretation'
)
colors.cyan(' IMPORTANT - Regexp (-E) must be with commas ALWAYS to avoid terminal escape or interpretation')
)
console.log('')
})
Expand Down Expand Up @@ -74,14 +70,7 @@ Promise.all([
writeFile(PAGE_PACKAGE_JSON_FILE, packageJSON(pageRegExpIdentifier)),
writeFile(PAGE_ENTRY_JS_POINT_FILE, indexJS()),
writeFile(PAGE_ENTRY_SCSS_POINT_FILE, indexSCSS(sitePrefix)),
writeFile(
PAGE_BASE_PROJECT_JSON_FILE,
JSON.stringify(packageInfo, null, ' ')
)
writeFile(PAGE_BASE_PROJECT_JSON_FILE, JSON.stringify(packageInfo, null, ' '))
]).then(() => {
console.log(
colors.green(
`➜ [${page}]: Your page files have been generated successfully.`
)
)
console.log(colors.green(`➜ [${page}]: Your page files have been generated successfully.`))
})
7 changes: 1 addition & 6 deletions packages/sui-widget-embedder/bin/sui-widget-embedder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ program.command('dev', 'Start a development server for a page').alias('d')

program.command('build', 'Build all statics by page').alias('b')

program
.command(
'generate',
'Generate a new page folder and main files with your first widget'
)
.alias('g')
program.command('generate', 'Generate a new page folder and main files with your first widget').alias('g')

program.parse(process.argv)
4 changes: 1 addition & 3 deletions packages/sui-widget-embedder/compiler/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ module.exports = ({page, remoteCdn, globalConfig = {}}) => {
output: {
...prodConfig.output,
path: path.resolve(process.cwd(), 'public', page),
publicPath: remoteCdn
? `${remoteCdn}/${page}/`
: prodConfig.output.publicPath,
publicPath: remoteCdn ? `${remoteCdn}/${page}/` : prodConfig.output.publicPath,
chunkLoadingGlobal: `webpackJsonp-${page}`
},
plugins: pipe(removePlugin('HtmlWebpackPlugin'))(prodConfig.plugins)
Expand Down
4 changes: 1 addition & 3 deletions packages/sui-widget-embedder/compiler/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const removePlugin = name => plugins => {
const pos = plugins
.map(p => p.constructor.toString())
.findIndex(string => string.match(name))
const pos = plugins.map(p => p.constructor.toString()).findIndex(string => string.match(name))
return [...plugins.slice(0, pos), ...plugins.slice(pos + 1)]
}

Expand Down
11 changes: 2 additions & 9 deletions packages/sui-widget-embedder/downloader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,7 @@
return css ? load.css(css) : Promise.resolve()
}

var loadScripts = [
manifest['runtime~app.js'],
manifest['main.js'],
manifest['vendor.js'],
manifest['app.js']
]
var loadScripts = [manifest['runtime~app.js'], manifest['main.js'], manifest['vendor.js'], manifest['app.js']]
.filter(Boolean)
.map(function (script) {
return function () {
Expand Down Expand Up @@ -133,9 +128,7 @@
}

function match(pathnameRegExp, hrefRegExp) {
return (
matchPathnameWithRegExp(pathnameRegExp) || matchhRefWithRegExp(hrefRegExp)
)
return matchPathnameWithRegExp(pathnameRegExp) || matchhRefWithRegExp(hrefRegExp)
}

var pages = []
Expand Down
24 changes: 5 additions & 19 deletions packages/sui-widget-embedder/src-react/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,24 @@ import Context from '@s-ui/react-context'
import {useMount} from '@s-ui/react-hooks'

function renderWidgetOnDOM({children, context, node}) {
ReactDOM.render(
<Context.Provider value={context}>{children}</Context.Provider>,
node
)
ReactDOM.render(<Context.Provider value={context}>{children}</Context.Provider>, node)
}

export default function Widget({
children,
context = {},
isVisible = true,
selector,
renderMultiple = false
}) {
export default function Widget({children, context = {}, isVisible = true, selector, renderMultiple = false}) {
useMount(function () {
if (!selector) {
return console.warn(
`[Widget] You must define a selector to use the Widget`
)
return console.warn(`[Widget] You must define a selector to use the Widget`)
}

const nodes = document.querySelectorAll(selector)
if (!nodes.length) {
return console.warn(
`[Widget] unable find nodes using selector ${selector}`
)
return console.warn(`[Widget] unable find nodes using selector ${selector}`)
}

// depending on renderMultiple, get the full array or only the first one
const nodesToRender = renderMultiple ? [].slice.call(nodes) : [nodes[0]]

isVisible &&
nodesToRender.map(node => renderWidgetOnDOM({children, context, node}))
isVisible && nodesToRender.map(node => renderWidgetOnDOM({children, context, node}))
})

return null
Expand Down

0 comments on commit bb047e5

Please sign in to comment.