-
Notifications
You must be signed in to change notification settings - Fork 171
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: add create-fractal package #1131
Open
mihkeleidast
wants to merge
3
commits into
main
Choose a base branch
from
issue/635
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,18 @@ | ||
# @frctl/create-fractal | ||
|
||
New Fractal pattern library initializer. | ||
|
||
[![NPM Version](https://img.shields.io/npm/v/@frctl/create-fractal)](https://www.npmjs.com/package/@frctl/create-fractal) | ||
|
||
## Usage | ||
|
||
Run `npm init @frctl/fractal [dir]` in your terminal. `[dir]` is the new folder you want to create your project in. | ||
|
||
## Testing locally | ||
|
||
1. Find out the full path to this directory. | ||
2. Run `npx [path/to/this/dir]`. | ||
|
||
## License | ||
|
||
[MIT](https://github.com/frctl/fractal/blob/main/LICENSE) |
This file contains 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,33 @@ | ||
{ | ||
"name": "@frctl/create-fractal", | ||
"version": "0.0.0", | ||
"description": "New Fractal pattern library initializer.", | ||
"bin": "./src/index.js", | ||
"main": "./src/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/frctl/fractal.git", | ||
"directory": "packages/create-fractal" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/frctl/fractal/issues" | ||
}, | ||
"homepage": "https://github.com/frctl/fractal", | ||
"dependencies": { | ||
"@frctl/core": "^0.3.3", | ||
"execa": "^5.0.0", | ||
"fs-extra": "^9.1.0", | ||
"handlebars": "^4.7.7", | ||
"inquirer": "^7.3.3" | ||
}, | ||
"devDependencies": { | ||
"@frctl/fractal": "^1.5.11" | ||
}, | ||
"engines": { | ||
"node": ">= 10.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains 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,61 @@ | ||
const path = require('path'); | ||
|
||
const { utils, shell } = require('@frctl/core'); | ||
const fs = require('fs-extra'); | ||
const Handlebars = require('handlebars'); | ||
|
||
const pkg = require('../package.json'); | ||
|
||
module.exports = async ({ targetPath, templatePath, answers }) => { | ||
const componentsDir = path.join(targetPath, answers.componentsDir); | ||
const docsDir = path.join(targetPath, answers.docsDir); | ||
const publicDir = path.join(targetPath, answers.publicDir); | ||
const packageJSONPath = path.join(targetPath, 'package.json'); | ||
const gitIgnorePath = path.join(targetPath, '.gitignore'); | ||
const fractalFilePath = path.join(targetPath, 'fractal.config.js'); | ||
const docsIndexPath = path.join(docsDir, '01-index.md'); | ||
const componentCopyTo = path.join(componentsDir, 'example'); | ||
|
||
const fractalFileTpl = path.join(templatePath, 'fractal.hbs'); | ||
const docsIndexTpl = path.join(templatePath, 'docs/index.md'); | ||
const exampleComponent = path.join(templatePath, 'components/example'); | ||
|
||
const packageJSON = { | ||
name: utils.slugify(answers.projectTitle), | ||
version: '0.1.0', | ||
dependencies: { | ||
'@frctl/fractal': `${pkg.devDependencies['@frctl/fractal']}`, | ||
}, | ||
scripts: { | ||
'fractal:start': 'fractal start --sync', | ||
'fractal:build': 'fractal build', | ||
}, | ||
}; | ||
|
||
const fractalContents = Handlebars.compile(fs.readFileSync(fractalFileTpl, 'utf8'))(answers); | ||
const indexContents = Handlebars.compile(fs.readFileSync(docsIndexTpl, 'utf8'))(answers); | ||
|
||
// ensure target folders exist | ||
await fs.ensureDir(targetPath); | ||
await fs.ensureDir(componentsDir); | ||
await fs.ensureDir(docsDir); | ||
await fs.ensureDir(publicDir); | ||
|
||
// write package.json | ||
await fs.writeJson(packageJSONPath, packageJSON, { spaces: 2 }); | ||
|
||
// copy example component | ||
await fs.copy(exampleComponent, componentCopyTo); | ||
|
||
// write git-specific files | ||
if (answers.useGit) { | ||
shell.touch(path.join(publicDir, '.gitkeep')); | ||
await fs.writeFile(gitIgnorePath, 'node_modules\n'); | ||
} | ||
|
||
// write config file | ||
await fs.writeFile(fractalFilePath, fractalContents); | ||
|
||
// write docs index | ||
await fs.writeFile(docsIndexPath, indexContents); | ||
}; |
This file contains 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,40 @@ | ||
const { utils } = require('@frctl/core'); | ||
const inquirer = require('inquirer'); | ||
|
||
// In the future, use template parameter to ask special questions depending on template name. | ||
module.exports = async ({ targetDir }) => { | ||
const questions = [ | ||
{ | ||
type: 'input', | ||
name: 'projectTitle', | ||
message: "What's the title of your project?", | ||
default: utils.titlize(targetDir), | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'componentsDir', | ||
message: 'Where would you like to keep your components?', | ||
default: 'components', | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'docsDir', | ||
message: 'Where would you like to keep your docs?', | ||
default: 'docs', | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'publicDir', | ||
message: 'What would you like to call your public directory?', | ||
default: 'public', | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'useGit', | ||
message: 'Will you use Git for version control on this project?', | ||
default: true, | ||
}, | ||
]; | ||
|
||
return await inquirer.prompt(questions); | ||
}; |
This file contains 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,69 @@ | ||
#!/usr/bin/env node | ||
|
||
const path = require('path'); | ||
|
||
const { shell } = require('@frctl/core'); | ||
const execa = require('execa'); | ||
const fs = require('fs-extra'); | ||
// const inquirer = require('inquirer'); | ||
|
||
const getAnswers = require('./get-answers'); | ||
const generateProject = require('./generate-project'); | ||
|
||
const templatesDir = path.join(__dirname, '../templates'); | ||
|
||
(async () => { | ||
const targetDir = process.argv.length >= 3 ? process.argv[2] : '.'; | ||
const targetPath = path.join(process.cwd(), targetDir); | ||
|
||
try { | ||
// ensure creating project in a new directory | ||
const pathExists = await fs.pathExists(targetPath); | ||
|
||
if (pathExists) { | ||
console.error(`Cannot create new project: The directory ${targetPath} already exists.`); | ||
return; | ||
} | ||
|
||
// enable when we have multiple starter templates | ||
/* | ||
const { template } = await inquirer.prompt([ | ||
{ | ||
type: 'list', | ||
name: 'template', | ||
message: 'What template do you want to start from?', | ||
choices: [ | ||
'handlebars', | ||
], | ||
} | ||
]); | ||
*/ | ||
const template = 'handlebars'; | ||
const templatePath = path.join(templatesDir, template); | ||
|
||
console.log('Creating new project.... just a few questions:'); | ||
// ask questions, get answers | ||
const answers = await getAnswers({ | ||
template, | ||
targetDir, | ||
}); | ||
|
||
console.log('Generating project structure...'); | ||
await generateProject({ | ||
targetPath, | ||
templatePath, | ||
answers, | ||
}); | ||
|
||
// do initial install | ||
console.log('Installing dependencies... This may take a while!'); | ||
shell.cd(targetPath); | ||
await execa('npm', ['install'], { stdio: 'inherit' }); | ||
|
||
// success! | ||
console.log('Your new Fractal project has been set up.'); | ||
} catch (e) { | ||
await fs.remove(targetPath); | ||
console.error(e); | ||
} | ||
})(); |
3 changes: 3 additions & 0 deletions
3
packages/create-fractal/templates/handlebars/components/example/example.config.yml
This file contains 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,3 @@ | ||
title: Example component | ||
context: | ||
text: This is an example component! |
1 change: 1 addition & 0 deletions
1
packages/create-fractal/templates/handlebars/components/example/example.hbs
This file contains 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 @@ | ||
<p>{{ text }}</p> |
This file contains 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,5 @@ | ||
--- | ||
title: {{ projectTitle }} | ||
--- | ||
|
||
This is your index page. You can edit its contents at `{{ docsDir }}/01-index.md` |
This file contains 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,39 @@ | ||
'use strict'; | ||
|
||
/* | ||
* Require the path module | ||
*/ | ||
const path = require('path'); | ||
|
||
/* | ||
* Require the Fractal module | ||
*/ | ||
const fractal = module.exports = require('@frctl/fractal').create(); | ||
|
||
{{#if projectTitle}} | ||
/* | ||
* Give your project a title. | ||
*/ | ||
fractal.set('project.title', '{{ projectTitle }}'); | ||
{{/if}} | ||
|
||
{{#if componentsDir}} | ||
/* | ||
* Tell Fractal where to look for components. | ||
*/ | ||
fractal.components.set('path', path.join(__dirname, '{{ componentsDir }}')); | ||
{{/if}} | ||
|
||
{{#if docsDir}} | ||
/* | ||
* Tell Fractal where to look for documentation pages. | ||
*/ | ||
fractal.docs.set('path', path.join(__dirname, '{{ docsDir }}')); | ||
{{/if}} | ||
|
||
{{#if publicDir}} | ||
/* | ||
* Tell the Fractal web preview plugin where to look for static assets. | ||
*/ | ||
fractal.web.set('static.path', path.join(__dirname, '{{ publicDir }}')); | ||
{{/if}} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also rename the package to just allow
npm init @frctl
or do something longer, e.g.npm init @frctl/library
, though I'm not sure it would be better. Did not check if we could do justnpm init fractal
, e.g. if the unscoped package name is free on npm.