-
Notifications
You must be signed in to change notification settings - Fork 68
LG-3534 Create sub component tool #1945
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1a98ff4
Refactor create to use sub-routines
TheSonOfThomp 530c95d
Sub-component creator
TheSonOfThomp c340770
Create ninety-hats-fix.md
TheSonOfThomp 9c01b42
Update component.template.ts
TheSonOfThomp 450e78f
Merge branch 'main' into adam/create-sub-component
TheSonOfThomp 1e946e3
adds subcomponent index file
TheSonOfThomp 3615bcf
Merge branch 'main' into adam/create-sub-component
TheSonOfThomp d55a7d1
Merge branch 'main' into adam/create-sub-component
TheSonOfThomp 1cc535f
Merge branch 'main' into adam/create-sub-component
TheSonOfThomp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@lg-tools/create': minor | ||
'@lg-tools/cli': minor | ||
--- | ||
|
||
Adds `--parent` flag to `lg create` command. Passing in this flag will create a subcomponent of the given parent. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* eslint-disable no-console */ | ||
import chalk from 'chalk'; | ||
import fse from 'fs-extra'; | ||
import path from 'path'; | ||
|
||
import { getNameVariants } from './utils/getNameVariants'; | ||
import { writeFiles } from './utils/writeFiles'; | ||
import { CreatePackageOptions } from './create.types'; | ||
import { | ||
component, | ||
componentIndex, | ||
index, | ||
pkgJson, | ||
readMe, | ||
spec, | ||
story, | ||
styles, | ||
tsConfig, | ||
types, | ||
} from './templates'; | ||
|
||
interface CreateComponentArgs | ||
extends Pick<Required<CreatePackageOptions>, 'scope' | 'directory'> { | ||
name: string; | ||
} | ||
|
||
export function createComponent({ | ||
name, | ||
scope, | ||
directory, | ||
}: CreateComponentArgs) { | ||
const rootDir = process.cwd(); | ||
|
||
const { packageNameKebab, packageNameTitle, packageNamePascal } = | ||
getNameVariants(name); | ||
|
||
console.log( | ||
chalk.green( | ||
`Creating package ${chalk.bold(scope + '/' + packageNameKebab)}`, | ||
), | ||
); | ||
|
||
// Create the appropriate directory | ||
const packageDir = path.resolve(rootDir, directory, packageNameKebab); | ||
|
||
// Create the package directories | ||
fse.mkdir(packageDir, { recursive: true }, err => { | ||
if (err) { | ||
console.log(`Package ${packageNameKebab} already exists`); | ||
return; | ||
} | ||
|
||
// Write the appropriate files for each template | ||
writeFiles( | ||
[ | ||
{ | ||
name: 'package.json', | ||
contents: pkgJson({ | ||
scope, | ||
packageNameKebab, | ||
packageNameTitle, | ||
}), | ||
}, | ||
{ | ||
name: 'tsconfig.json', | ||
contents: tsConfig, | ||
}, | ||
{ | ||
name: 'README.md', | ||
contents: readMe({ packageNameKebab, packageNameTitle }), | ||
}, | ||
{ | ||
name: 'src/index.ts', | ||
contents: index({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}.stories.tsx`, | ||
contents: story({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/index.ts`, | ||
contents: componentIndex({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/${packageNamePascal}.tsx`, | ||
contents: component({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/${packageNamePascal}.spec.tsx`, | ||
contents: spec({ packageNamePascal, packageNameKebab }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/${packageNamePascal}.types.ts`, | ||
contents: types({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/${packageNamePascal}.styles.ts`, | ||
contents: styles, | ||
}, | ||
], | ||
{ | ||
dir: packageDir, | ||
packageNamePascal, | ||
}, | ||
); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* eslint-disable no-console */ | ||
import chalk from 'chalk'; | ||
import fse from 'fs-extra'; | ||
import path from 'path'; | ||
|
||
import { getComponentPath } from './utils/getComponentPath'; | ||
import { getNameVariants } from './utils/getNameVariants'; | ||
import { writeFiles } from './utils/writeFiles'; | ||
import { CreatePackageOptions } from './create.types'; | ||
import { component, componentIndex, spec, styles, types } from './templates'; | ||
|
||
interface CreateComponentArgs { | ||
name: string; | ||
parent: Required<CreatePackageOptions>['parent']; | ||
} | ||
|
||
export function createSubComponent({ name, parent }: CreateComponentArgs) { | ||
const { packageNamePascal: parentNamePascal } = getNameVariants(parent); | ||
const { packageNameKebab, packageNamePascal } = getNameVariants(name); | ||
|
||
console.log( | ||
chalk.green( | ||
`Creating sub-component ${chalk.bold( | ||
parentNamePascal + '/' + packageNamePascal, | ||
)}`, | ||
), | ||
); | ||
|
||
const parentDir = getComponentPath(parent); | ||
|
||
if (!parentDir) { | ||
console.error(chalk.red(`Could not find parent package ${parent}`)); | ||
process.exit(127); | ||
} | ||
|
||
const subComponentDir = path.resolve(parentDir, 'src', packageNamePascal); | ||
|
||
fse.mkdir(subComponentDir, { recursive: true }, err => { | ||
if (err) { | ||
console.log(`Package ${packageNameKebab} already exists`); | ||
return; | ||
} | ||
|
||
writeFiles( | ||
[ | ||
{ | ||
name: `index.ts`, | ||
contents: componentIndex({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `${packageNamePascal}.tsx`, | ||
contents: component({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `${packageNamePascal}.spec.tsx`, | ||
contents: spec({ packageNamePascal, packageNameKebab }), | ||
}, | ||
{ | ||
name: `${packageNamePascal}.types.ts`, | ||
contents: types({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `${packageNamePascal}.styles.ts`, | ||
contents: styles, | ||
}, | ||
], | ||
{ | ||
dir: subComponentDir, | ||
packageNamePascal, | ||
}, | ||
); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,26 @@ | ||
/* eslint-disable no-console */ | ||
import { getLGConfig } from '@lg-tools/meta'; | ||
import chalk from 'chalk'; | ||
import fse from 'fs-extra'; | ||
import { camelCase, kebabCase, startCase } from 'lodash'; | ||
import path from 'path'; | ||
|
||
import { CreatePackageOptions } from './create.types'; | ||
import { | ||
component, | ||
componentIndex, | ||
index, | ||
pkgJson, | ||
readMe, | ||
spec, | ||
story, | ||
styles, | ||
tsConfig, | ||
types, | ||
} from './templates'; | ||
import { createComponent } from './createComponent'; | ||
import { createSubComponent } from './createSubComponent'; | ||
|
||
export function createPackage(name: string, options: CreatePackageOptions) { | ||
const rootDir = process.cwd(); | ||
|
||
const { scopes } = getLGConfig(); | ||
const scope = options.scope ?? Object.keys(scopes)[0]; | ||
const directory = options.directory ?? scopes[scope]; | ||
|
||
// Construct all required parameters | ||
const packageNameKebab = kebabCase(name); | ||
const packageNameTitle = startCase(name); | ||
const packageNamePascal = camelCase(name).replace(/^\w/, c => | ||
c.toUpperCase(), | ||
); | ||
|
||
console.log( | ||
chalk.green( | ||
`Creating package ${chalk.bold(scope + '/' + packageNameKebab)}`, | ||
), | ||
); | ||
|
||
// Create the appropriate directory | ||
const packageDir = path.resolve(rootDir, directory, packageNameKebab); | ||
|
||
// Create the package directories | ||
fse.mkdir(packageDir, { recursive: true }, err => { | ||
if (err) { | ||
console.log(`Package ${packageNameKebab} already exists`); | ||
return; | ||
} | ||
const parent = options.parent; | ||
|
||
// Write the appropriate files for each template | ||
writeFiles( | ||
[ | ||
{ | ||
name: 'package.json', | ||
contents: pkgJson({ | ||
scope, | ||
packageNameKebab, | ||
packageNameTitle, | ||
}), | ||
}, | ||
{ | ||
name: 'tsconfig.json', | ||
contents: tsConfig, | ||
}, | ||
{ | ||
name: 'README.md', | ||
contents: readMe({ packageNameKebab, packageNameTitle }), | ||
}, | ||
{ | ||
name: 'src/index.ts', | ||
contents: index({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}.stories.tsx`, | ||
contents: story({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/index.ts`, | ||
contents: componentIndex({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/${packageNamePascal}.tsx`, | ||
contents: component({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/${packageNamePascal}.spec.tsx`, | ||
contents: spec({ packageNamePascal, packageNameKebab }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/${packageNamePascal}.types.ts`, | ||
contents: types({ packageNamePascal }), | ||
}, | ||
{ | ||
name: `src/${packageNamePascal}/${packageNamePascal}.styles.ts`, | ||
contents: styles, | ||
}, | ||
], | ||
{ | ||
dir: packageDir, | ||
packageNamePascal, | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
function handleErr(err: NodeJS.ErrnoException | null) { | ||
if (err) throw err; | ||
} | ||
|
||
function writeFiles( | ||
files: Array<{ | ||
name: string; | ||
contents: string; | ||
}>, | ||
config: { | ||
dir: string; | ||
packageNamePascal: string; | ||
}, | ||
) { | ||
// Make the directory src and src/Component | ||
fse.mkdirSync(path.resolve(config.dir, 'src', config.packageNamePascal), { | ||
recursive: true, | ||
}); | ||
// TODO: get scope & directory from parent | ||
const scope = options.scope ?? Object.keys(scopes)[0]; | ||
const directory = options.directory ?? scopes[scope]; | ||
|
||
// Write all component files | ||
for (const { name, contents } of files) { | ||
fse.writeFile(path.resolve(config.dir, name), contents, handleErr); | ||
if (parent) { | ||
createSubComponent({ name, parent }); | ||
} else { | ||
createComponent({ | ||
name, | ||
scope, | ||
directory, | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { getLGConfig } from '@lg-tools/meta'; | ||
import fse from 'fs-extra'; | ||
import path from 'path'; | ||
|
||
/** Given a component name, return its absolute path */ | ||
export function getComponentPath(name: string): string | undefined { | ||
const { scopes } = getLGConfig(); | ||
const rootDir = process.cwd(); | ||
|
||
if (name.startsWith('@')) { | ||
// We have scope defined for us, so we can compute the absolute path | ||
const [scope, packageName] = name.split('/'); // ['@leafygreen-ui', 'button'] | ||
const parentDir: string | undefined = scopes[scope]; // 'packages' | ||
|
||
if (fse.existsSync(path.resolve(rootDir, parentDir, packageName))) { | ||
return path.resolve(rootDir, parentDir, packageName); | ||
} | ||
} else { | ||
// We need to check all package directories for the component | ||
for (const dir of Object.values(scopes)) { | ||
// ['packages', 'tools'] / 'button' | ||
if (fse.existsSync(path.resolve(rootDir, dir, name))) { | ||
return path.resolve(rootDir, dir, name); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.