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

feat(packages/sui-studio): allow packages store its component on any file and gets its documentation #1546

Draft
wants to merge 61 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
06dd5dd
feat(packages/sui-studio): allow packages store its component on any …
andresin87 Nov 21, 2022
2226e46
feat(packages/sui-studio): add package needed
andresin87 Nov 23, 2022
ecf181b
feat(packages/sui-js-compiler): Migrate to ESM
midudev Mar 1, 2022
0d7e0bd
fix(packages/sui-bundler): required fallback resolvers to avoid brows…
Oct 18, 2022
b568898
feat(packages/sui-mock): msw wrapper sui implementation
Oct 18, 2022
fd5e780
test(packages/sui-mock): add some test coverage
Oct 18, 2022
9b24bd1
chore(Root): publish betas
Oct 18, 2022
ba9ace7
fix(packages/sui-mock): improve export method name
Oct 18, 2022
d0b015b
docs(packages/sui-mock): update readme
Oct 18, 2022
b3b648c
chore(packages/sui-mock): publish beta
Oct 18, 2022
eddd0fe
fix(packages/sui-bundler): fix config prod fallback resolvers
Oct 18, 2022
83ade10
fix(packages/sui-test-contract): use same version of msw
Oct 18, 2022
84b6e1e
docs(packages/sui-mock): update deps
Oct 18, 2022
15dbde0
fix(Root): remove beta version from package jsons and avoid auto rele…
Oct 18, 2022
10ad350
docs(packages/sui-mock): fix readme docs after last fixes
Oct 19, 2022
3b6ee58
Update packages/sui-mock/README.md
rmoralp Oct 19, 2022
feba214
docs(packages/sui-mockmock): recommend sui-mock for mocking in mockmock
Oct 19, 2022
af518c8
docs(packages/sui-mock): improve docs
Oct 19, 2022
e780d97
release(packages/sui-bundler): v9.37.0 [skip ci]
sui-bot Oct 19, 2022
57075c6
release(packages/sui-mock): v1.1.0 [skip ci]
sui-bot Oct 19, 2022
386c271
release(packages/sui-test-contract): v2.8.0 [skip ci]
sui-bot Oct 19, 2022
4613fb7
fix(packages/sui-js-compiler): fix __dirname and migrate to esm
Oct 20, 2022
7fed371
fix(packages/sui-js-compiler): use correct bin file extension
Oct 20, 2022
243f808
release(packages/sui-js-compiler): v1.20.0 [skip ci]
sui-bot Oct 20, 2022
c652997
release(packages/sui-studio): v11.29.0 [skip ci]
sui-bot Oct 21, 2022
d37c1be
feat(packages/sui-i18n): import node-polyglot 0.43 to our package
jordevo Oct 22, 2022
90f3590
feat(packages/sui-test-contract): add custom api url for each endpoint
Oct 24, 2022
9c02edd
Update packages/sui-test-contract/README.md
rmoralp Oct 24, 2022
0395de8
release(packages/sui-test-contract): v2.9.0 [skip ci]
sui-bot Oct 24, 2022
55fce04
feat(packages/sui-i18n): remove node-polyglot dependency
jordevo Oct 28, 2022
d64cd63
docs(packages/sui-i18n): remove comments from polyglot js adapter
jordevo Oct 28, 2022
d731fea
refactor(packages/sui-i18n): use arrow functions for helpers
jordevo Oct 28, 2022
bb5f7e3
docs(packages/sui-i18n): update license notice
jordevo Oct 28, 2022
e175ad3
refactor(packages/sui-i18n): remove no longer relevant mention to ver…
jordevo Oct 31, 2022
16187f9
release(packages/sui-i18n): v1.31.0 [skip ci]
sui-bot Nov 3, 2022
78b9df1
feat(packages/sui-js-compiler): update swc deps
Nov 9, 2022
61295e0
test(packages/sui-js-compiler): fix swc test
Nov 9, 2022
ded7ba4
release(packages/sui-js-compiler): v1.21.0 [skip ci]
sui-bot Nov 10, 2022
1ed8224
release(packages/sui-studio): v11.30.0 [skip ci]
sui-bot Nov 11, 2022
b0a6b87
fix(packages/sui-studio): Remove h1 from SUI Studios
Nov 11, 2022
57a2418
fix(packages/sui-studio): Recover correct h1 h2 styles for API Tab
Nov 11, 2022
658f80a
feat(packages/sui-studio): add studio name title
andresin87 Nov 14, 2022
1ebc3a5
feat(packages/sui-studio): add studio name if necessary
andresin87 Nov 14, 2022
ad9dd76
release(packages/sui-studio): v11.31.0 [skip ci]
sui-bot Nov 14, 2022
36ea1ba
release(packages/sui-studio): v11.32.0 [skip ci]
sui-bot Nov 21, 2022
e9b8b04
feat(packages/sui-theme): add new molecule select and autosuggest tok…
Nov 21, 2022
9af81fa
release(packages/sui-theme): v8.125.0 [skip ci]
sui-bot Nov 22, 2022
8d9df07
feat(packages/sui-studio): find default export module placement recus…
andresin87 Nov 24, 2022
fbaba99
Merge branch 'master' into feat/sui-studio
andresin87 Nov 24, 2022
738d019
Update packages/sui-mock/README.md
andresin87 Nov 24, 2022
e6740b6
Update packages/sui-studio/bin/helpers/copy.js
andresin87 Nov 24, 2022
3dc758a
feat(packages/sui-studio): allow packages store its component on any …
andresin87 Nov 21, 2022
ed0923a
feat(packages/sui-studio): add package needed
andresin87 Nov 23, 2022
43e4f5f
fix(packages/sui-js-compiler): fix __dirname and migrate to esm
Oct 20, 2022
1f452cc
fix(packages/sui-js-compiler): use correct bin file extension
Oct 20, 2022
108b2ba
feat(packages/sui-studio): find default export module placement recus…
andresin87 Nov 24, 2022
74a5325
Update packages/sui-studio/bin/helpers/copy.js
andresin87 Nov 24, 2022
b84db3b
feat(packages/sui-js-compiler): expose the swc settings in the packag…
andresin87 Nov 24, 2022
fc5e9d6
Merge remote-tracking branch 'origin/feat/sui-studio' into feat/sui-s…
andresin87 Nov 24, 2022
148cfb2
feat(packages/sui-js-compiler): right route
andresin87 Nov 24, 2022
d75d737
fix(packages/sui-js-compiler): exporting path
andresin87 Nov 24, 2022
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
5 changes: 4 additions & 1 deletion packages/sui-js-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"version": "1.21.0",
"description": "JavaScript Compiler",
"type": "module",
"exports": "./src/index.js",
"exports": {
".": "./index.js",
"./swc-config.js": "./swc-config.js"
},
"bin": {
"sui-js-compiler": "./index.js"
},
Expand Down
15 changes: 15 additions & 0 deletions packages/sui-studio/bin/helpers/checkDepth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const path = require('path')

/**
* Check depth
* @param {string} filePath - path of the file
* @param {number} up - number of directories to go up
* @returns {boolean}
*/
module.exports = (filePath, up) => {
// components/atom/button
const depth = path.normalize(filePath).split(path.sep).length - 1
return depth >= up
}
45 changes: 2 additions & 43 deletions packages/sui-studio/bin/helpers/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,8 @@ const path = require('path')
const fs = require('fs-extra')
const glob = require('fast-glob')

const {DEBUG} = process.env

/**
* Show logging information if debug mode is enabled
* @param {...any} args - arguments to log
* @returns {void}
*/
const debug = (...args) => {
DEBUG && console.log('[copyfiles] ', ...args)
}

/**
* Check depth
* @param {string} filePath - path of the file
* @param {number} up - number of directories to go up
* @returns {boolean}
*/
const checkDepth = (filePath, up) => {
// components/atom/button
const depth = path.normalize(filePath).split(path.sep).length - 1
return depth >= up
}

/**
* Resolve file path
* @param {string} filePath - path of the file
* @param {object} config - configuration object
* @param {object} config.flatten - flatten the path
* @param {number} config.up - number of directories to go up
* @returns {string}
*/
const resolveFilePath = (filePath, {flatten, up}) => {
if (flatten === true) return path.basename(filePath)
if (up === 0) return filePath

if (!checkDepth(filePath, up)) {
throw new Error(
"The number of folders you're trying to go up are not correct. Check the path or the up config"
)
}

return path.join(...path.normalize(filePath).split(path.sep).slice(up))
}
const debug = require('./debug.js')
const resolveFilePath = require('./resolveFilePath.js')

module.exports = async function copyFiles(args, config = {}) {
const input = args.slice()
Expand Down
2 changes: 1 addition & 1 deletion packages/sui-studio/bin/helpers/copyStaticFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function copyStaticFiles() {
'components/README.md',
'components/*/*/README.md',
'components/*/*/CHANGELOG.md',
'components/*/*/src/index.js',
'components/*/*/src/**/*.js',
'components/*/*/demo/playground',
DESTINATION_FOLDER
],
Expand Down
11 changes: 11 additions & 0 deletions packages/sui-studio/bin/helpers/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {DEBUG} = process.env

/**
* Show logging information if debug mode is enabled
* @param {...any} args - arguments to log
* @returns {void}
*/

module.exports = function (...args) {
DEBUG && console.log('[copyfiles] ', ...args)
}
155 changes: 155 additions & 0 deletions packages/sui-studio/bin/helpers/findExportedExpressions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
const {parseSync, transformFileSync} = require('@swc/core')
const path = require('node:path')

function findExportedExpressions(
file,
{isDefault = true, variableName, swc: swcSettings} = {}
) {
const {dir, base = 'index.js'} = path.parse(`${file}`)

const {code: transformedCode} = transformFileSync(`${file}`, swcSettings)

const AST = parseSync(transformedCode)

if (typeof AST !== 'object') {
throw new Error('invalid file data')
}
const {body} = AST
const globalDeclarations = body.reduce(
(
acc,
{
type,
source,
id,
declaration = {},
declarations = [],
specifiers = [],
identifier = {}
}
) => {
if (type === 'ExportNamedDeclaration' && source !== null) {
const {value, type} = source
if (type === 'StringLiteral') {
specifiers?.forEach(({type, orig, exported}) => {
if (['ExportDefaultSpecifier', 'ExportSpecifier'].includes(type)) {
acc = {
...acc,
[exported?.value || orig?.value]: {
localName: orig?.value,
currentName: exported?.value || orig?.value,
source:
path.relative(process.cwd(), path.resolve(dir, value)) ||
`${dir}/${base}`
}
}
}
})
}
}
if (type === 'ExportDeclaration') {
const {declarations = []} = declaration
declarations.forEach(({id: {value}}) => {
acc = {
...acc,
[value]: {localName: value, source: `${dir}/${base}`}
}
})
}
if (type === 'VariableDeclaration') {
declarations.forEach(({id: {value}} = {id: {}}) => {
acc = {...acc, [value]: {localName: value, source: `${dir}/${base}`}}
})
}
if (['FunctionDeclaration', 'ClassDeclaration'].includes(type)) {
const {value} = identifier
acc = {...acc, [value]: {localName: value, source: `${dir}/${base}`}}
}
if (type === 'ImportDeclaration' && source !== null) {
const {value, type} = source
if (type === 'StringLiteral') {
specifiers?.forEach(({type, local, imported}, i) => {
if (['ImportDefaultSpecifier', 'ImportSpecifier'].includes(type)) {
acc = {
...acc,
[imported?.value || local?.value]: {
localName: local?.value,
importedName: imported?.value || local?.value,
source:
path.relative(process.cwd(), path.resolve(dir, value)) ||
`${dir}/${base}`
}
}
}
})
}
}
return acc
},
{}
)

let result = body.find(({type, specifiers}) => {
if (type === 'ExportDefaultDeclaration' && isDefault) {
return true
}
if (type === 'ExportNamedDeclaration') {
specifiers?.forEach(({type, orig, exported}) => {
if (type === 'ExportSpecifier') {
const currentName = exported?.value || orig?.value
if (variableName === currentName) {
return true
}
}
})
}
if (
type === 'ExportDefaultExpression' &&
(variableName === undefined || isDefault)
) {
return true
}
})
result = (({type, expression, specifiers}) => {
if (type === 'ExportDefaultDeclaration' && isDefault) {
return {route: path.resolve(dir, base), found: true, isDefault: true}
} else if (type === 'ExportNamedDeclaration') {
specifiers?.forEach(({type, orig, exported}) => {
if (type === 'ExportSpecifier') {
const currentName = exported?.value || orig?.value
if (variableName === currentName) {
return {
isDefault: false,
route: globalDeclarations[currentName].source,
found: globalDeclarations[currentName].source === file,
variableName: globalDeclarations[currentName].localName
}
}
}
})
} else if (
type === 'ExportDefaultExpression' &&
variableName === undefined
) {
const {value} = expression
const {source, localName} = {...(globalDeclarations[value] || {})}
return {
route: source,
found: source === file,
variableName: localName
}
}
})(result)
if (!result) {
result = {route: path.resolve(dir, base), found: false, isDefault: false}
}
if (!result.found && file !== result.route) {
result = findExportedExpressions(result.route, {
isDefault: result.isDefault,
variableName: result.variableName
})
}
return result
}

module.exports = findExportedExpressions
18 changes: 15 additions & 3 deletions packages/sui-studio/bin/helpers/generateApiDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ const path = require('node:path')
const fg = require('fast-glob')
const fs = require('fs-extra')
const reactDocs = require('react-docgen')
const findExportedExpressions = require('./findExportedExpressions.js')

module.exports = function generateApiDocs() {
module.exports = async function generateApiDocs() {
console.log('[sui-studio] Generating API documentation for components...')
console.time('[sui-studio] API generation took')

const {default: swcConfig} = await import('@s-ui/js-compiler/swc-config.js')

const components = fg.sync('components/*/*/src/index.js', {deep: 4})

components.forEach(file => {
const source = fs.readFileSync(file, 'utf-8')
let docFilePath = file
const {dir} = path.parse(file)
const gen = findExportedExpressions(file, {swc: swcConfig})
const {route, found} = gen
if (found) {
docFilePath = route.split().join('')
}

const source = fs.readFileSync(docFilePath, 'utf-8')

let docs = {}

try {
Expand All @@ -22,7 +34,7 @@ module.exports = function generateApiDocs() {
console.warn(`[sui-studio] Couldn't generate API docs for ${file}`)
}

const outputFile = file.replace('index.js', 'definitions.json')
const outputFile = dir.replace('src', '') + 'definitions.json'
fs.writeFileSync(
path.resolve(process.cwd(), `public/${outputFile}`),
JSON.stringify(docs, null, 2)
Expand Down
41 changes: 41 additions & 0 deletions packages/sui-studio/bin/helpers/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @ts-check

'use strict'

const path = require('path')
const fs = require('fs-extra')
const glob = require('fast-glob')

const debug = require('./debug.js')
const resolveFilePath = require('./resolveFilePath.js')

module.exports = async function removeFiles(args, config = {}) {
const input = args.slice()
const outDir = input.pop()
const globOpts = {}

const {flatten = false, up = 0} = config

if (config.exclude) globOpts.ignore = config.exclude
if (config.all) globOpts.dot = true
if (config.follow) globOpts.followSymbolicLinks = true

debug(`Config for glob: `, globOpts)

const files = await glob(input, globOpts)

if (files.length === 0) {
debug('No files found.')
return
}

debug(`Removing ${files.length} files from ${input} `)

return Promise.all(
files.map(file => {
const outName = path.join(outDir, resolveFilePath(file, {flatten, up}))
debug(`Removing ${outName}`)
return fs.unlinkSync(outName)
})
)
}
14 changes: 14 additions & 0 deletions packages/sui-studio/bin/helpers/removeStaticFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const remove = require('./remove.js')

const DESTINATION_FOLDER = 'public'

module.exports = function removeStaticFiles() {
console.log('[sui-studio] Removing unnecessary static files...')
console.time('[sui-studio] Removing static files took')

return remove(['components/*/*/src/**/*.js', DESTINATION_FOLDER], {
exclude: 'node_modules/**'
}).then(() => {
console.timeEnd('[sui-studio] Removing static files took')
})
}
25 changes: 25 additions & 0 deletions packages/sui-studio/bin/helpers/resolveFilePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const path = require('path')
const checkDepth = require('./checkDepth.js')

/**
* Resolve file path
* @param {string} filePath - path of the file
* @param {object} config - configuration object
* @param {object} config.flatten - flatten the path
* @param {number} config.up - number of directories to go up
* @returns {string}
*/
module.exports = (filePath, {flatten, up}) => {
if (flatten === true) return path.basename(filePath)
if (up === 0) return filePath

if (!checkDepth(filePath, up)) {
throw new Error(
"The number of folders you're trying to go up are not correct. Check the path or the up config"
)
}

return path.join(...path.normalize(filePath).split(path.sep).slice(up))
}
2 changes: 2 additions & 0 deletions packages/sui-studio/bin/sui-studio-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const program = require('commander')
const {NO_COMPONENTS_MESSAGE} = require('../config/index.js')
const generateApiDocs = require('./helpers/generateApiDocs.js')
const copyStaticFiles = require('./helpers/copyStaticFiles.js')
const removeStaticFiles = require('./helpers/removeStaticFiles.js')
const copyGlobals = require('./helpers/copyGlobals.js')

program
Expand Down Expand Up @@ -59,6 +60,7 @@ if (needsBuild) {
.then(copyStaticFiles)
.then(copyGlobals)
.then(generateApiDocs)
.then(removeStaticFiles)
.then(() => process.exit(0))
.catch(() => process.exit(1))
}
Loading