@spark-ui/cli-utils 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # 1.1.0 (2023-02-14)
7
+
8
+ ### Features
9
+
10
+ - **cli-utils:** add cli command to generate new components ([4e79fa1](https://github.com/adevinta/spark/commit/4e79fa19c2e84f98ac8fd7f9b75ecfaab2ea8227))
11
+ - **cli-utils:** fix typo ([afcaf10](https://github.com/adevinta/spark/commit/afcaf1088284a7c87e7fdc8d36331ea4ab7571c8))
12
+ - **cli-utils:** trigger script from bin alias ([c81f054](https://github.com/adevinta/spark/commit/c81f054ef2fb004203da3e5b77c02e4a83e2c191))
13
+ - **cli-utils:** update code and files structure ([be32555](https://github.com/adevinta/spark/commit/be325553849c51667ca1dad24042b8305775212f))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { fileURLToPath } from 'node:url'
4
+ import { program } from 'commander'
5
+ import glob from 'glob'
6
+ import { pascalCase } from 'pascal-case'
7
+ import { log, showError, writeFile } from '../utils.js'
8
+
9
+ const BASE_DIR = process.cwd()
10
+ const WORDS_ONLY_REGEX = /^[A-Za-z-]*$/
11
+
12
+ program
13
+ .option('-D, --description <description>', 'Description of this component')
14
+ .on('--help', () => {
15
+ console.log('Examples:')
16
+ console.log(' $ spark generate button')
17
+ console.log(' $ spark generate button -D "My button description"')
18
+ console.log(' $ spark generate button --description "My button description"')
19
+ console.log(' $ spark generate --help')
20
+ })
21
+ .parse(process.argv)
22
+
23
+ const [component] = program.args
24
+ const { description = '' } = program.opts()
25
+
26
+ if (!component) {
27
+ showError('Argument "component" must be defined!')
28
+ }
29
+ if (!WORDS_ONLY_REGEX.test(component)) {
30
+ showError('"component" name must contain letters and dash symbols only')
31
+ }
32
+
33
+ const componentDir = `/packages/components/${component}/`
34
+ const componentPath = `${BASE_DIR}${componentDir}`
35
+ const templatesPattern = fileURLToPath(new URL('../templates/**/*.js', import.meta.url))
36
+
37
+ glob(templatesPattern, async (err, res) => {
38
+ if (err) showError(err)
39
+ if (res) {
40
+ const templateContents = res.map(templatePath =>
41
+ import(templatePath).then(module => ({
42
+ path: templatePath
43
+ .replace(/(.*)\/templates\//, componentPath)
44
+ .replace('Component', pascalCase(component))
45
+ .replaceAll(/\[|\]|\.js$/g, ''),
46
+ content: module.default({ component, description }),
47
+ }))
48
+ )
49
+
50
+ const filesToWrite = await Promise.all(templateContents)
51
+
52
+ Promise.all(filesToWrite.map(writeFile)).then(() => {
53
+ log('All component files have been properly written!')
54
+ })
55
+ }
56
+ })
package/bin/spark.mjs ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { program } from 'commander'
4
+ import { createRequire } from 'module'
5
+
6
+ const require = createRequire(import.meta.url)
7
+ const { version } = require('../package.json')
8
+
9
+ program.version(version, '--version')
10
+
11
+ program.command('generate <component>', 'Generate a component scaffolding').alias('g')
12
+
13
+ program.parse(process.argv)
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@spark-ui/cli-utils",
3
+ "version": "1.1.0",
4
+ "description": "Spark CLI utils",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "bin": {
9
+ "spark": "./bin/spark.mjs",
10
+ "spark-generate": "./bin/spark-generate.mjs"
11
+ },
12
+ "type": "module",
13
+ "devDependencies": {
14
+ "chalk": "5.2.0",
15
+ "commander": "10.0.0",
16
+ "fs-extra": "11.1.0",
17
+ "glob": "8.1.0",
18
+ "pascal-case": "3.1.2"
19
+ },
20
+ "gitHead": "18ed2818aad6616ab8abea5a94c7b165995e8b01"
21
+ }
@@ -0,0 +1,3 @@
1
+ export default () => `src
2
+ **/*.stories.*
3
+ `
@@ -0,0 +1,15 @@
1
+ export default ({ component, description }) => `{
2
+ "name": "@spark-ui/${component}",
3
+ "version": "1.0.0",
4
+ "description": "${description}",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts",
11
+ "scripts": {
12
+ "build": "vite build"
13
+ }
14
+ }
15
+ `
@@ -0,0 +1,8 @@
1
+ export default () => `{
2
+ "compilerOptions": {
3
+ "baseUrl": ".",
4
+ "declarationDir": "./dist",
5
+ "rootDir": "./src"
6
+ }
7
+ }
8
+ `
@@ -0,0 +1,7 @@
1
+ export default () => `import path from 'path'
2
+ import { getComponentConfiguration } from '../../../config/index'
3
+
4
+ const { name } = require(path.resolve(__dirname, 'package.json'))
5
+
6
+ export default getComponentConfiguration(name)
7
+ `
@@ -0,0 +1,40 @@
1
+ import { pascalCase } from 'pascal-case'
2
+
3
+ export default ({ component, description }) => {
4
+ const componentName = pascalCase(component)
5
+
6
+ return `import { ArgsTable, Meta, Story } from '@storybook/addon-docs'
7
+ import { ReactLiveBlock } from '@docs/helpers/ReactLiveBlock'
8
+ import { StoryHeading } from '@docs/helpers/StoryHeading'
9
+
10
+ import { ${componentName} } from '.'
11
+
12
+ import * as stories from './${componentName}.stories'
13
+
14
+ <Meta title="Components/Misc/${componentName}" component={${componentName}} />
15
+
16
+ # ${componentName}
17
+
18
+ ${description}
19
+
20
+ <StoryHeading label="Install" />
21
+
22
+ \`\`\`
23
+ npm install @spark-ui/${component}
24
+ \`\`\`
25
+
26
+ <StoryHeading label="Import" />
27
+
28
+ \`\`\`
29
+ import { ${componentName} } from "@spark-ui/${component}"
30
+ \`\`\`
31
+
32
+ <StoryHeading label="Props" />
33
+
34
+ <ArgsTable of={${componentName}} />
35
+
36
+ <StoryHeading label="Variants" />
37
+
38
+ <Story name="default" story={stories.Default} />
39
+ `
40
+ }
@@ -0,0 +1,16 @@
1
+ import { pascalCase } from 'pascal-case'
2
+
3
+ export default ({ component, description }) => {
4
+ const componentName = pascalCase(component)
5
+
6
+ return `import { ReactLiveBlock } from '@docs/helpers/ReactLiveBlock'
7
+
8
+ import { ${componentName} } from '.'
9
+
10
+ export const Default = () => (
11
+ <ReactLiveBlock scope={{ ${componentName} }}>
12
+ <${componentName}>Hello World!</${componentName}>
13
+ </ReactLiveBlock>
14
+ )
15
+ `
16
+ }
@@ -0,0 +1,34 @@
1
+ import { pascalCase } from 'pascal-case'
2
+
3
+ export default ({ component }) => {
4
+ const componentName = pascalCase(component)
5
+
6
+ return `import { render, screen } from '@testing-library/react'
7
+ import userEvent from '@testing-library/user-event'
8
+ import { describe, expect, it, vi } from 'vitest'
9
+
10
+ import { ${componentName} } from './${componentName}'
11
+
12
+ describe('${componentName}', () => {
13
+ it('should render', () => {
14
+ render(<${componentName}>Hello World!</${componentName}>)
15
+
16
+ expect(screen.getByText('Hello World!')).toBeInTheDocument()
17
+ })
18
+
19
+ it('should trigger click event', async () => {
20
+ const user = userEvent.setup()
21
+ const clickEvent = vi.fn()
22
+
23
+ // Given
24
+ render(<div onClick={clickEvent}>Hello World!</div>)
25
+
26
+ // When
27
+ await user.click(screen.getByText('Hello World!'))
28
+
29
+ // Then
30
+ expect(clickEvent).toHaveBeenCalledTimes(1)
31
+ })
32
+ })
33
+ `
34
+ }
@@ -0,0 +1,14 @@
1
+ import { pascalCase } from 'pascal-case'
2
+
3
+ export default ({ component }) => {
4
+ const componentName = pascalCase(component)
5
+
6
+ return `import { ComponentPropsWithoutRef, PropsWithChildren } from 'react'
7
+
8
+ export type ${componentName}Props = ComponentPropsWithoutRef<'div'>
9
+
10
+ export function ${componentName}(props: PropsWithChildren<${componentName}Props>) {
11
+ return <div {...props} />
12
+ }
13
+ `
14
+ }
@@ -0,0 +1,8 @@
1
+ import { pascalCase } from 'pascal-case'
2
+
3
+ export default ({ component }) => {
4
+ const componentName = pascalCase(component)
5
+
6
+ return `
7
+ `
8
+ }
@@ -0,0 +1,8 @@
1
+ import { pascalCase } from 'pascal-case'
2
+
3
+ export default ({ component }) => {
4
+ const componentName = pascalCase(component)
5
+
6
+ return `export { ${componentName} } from './${componentName}'
7
+ `
8
+ }
package/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ import fs from 'node:fs'
2
+
3
+ import chalk from 'chalk'
4
+ import fse from 'fs-extra'
5
+
6
+ export const log = msg => console.log(chalk.gray(msg)) // eslint-disable-line no-console
7
+
8
+ export const showError = (msg, foreignProgram) => {
9
+ console.error(chalk.red(`✖ Error: ${msg}\n`))
10
+ process.exit(1)
11
+ }
12
+
13
+ export const writeFile = ({ path, content }) =>
14
+ fse
15
+ .outputFile(path, content)
16
+ .then(() => log(`Created ${path}`))
17
+ .catch(() => showError(`Failed creating ${path}`))