diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1c6314a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..239ecff --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +yarn.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f98fed0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - '12' + - '10' + - '8' diff --git a/cli.js b/cli.js new file mode 100755 index 0000000..385ef96 --- /dev/null +++ b/cli.js @@ -0,0 +1,16 @@ +#!/usr/bin/env node +'use strict'; +const meow = require('meow'); +const createInkApp = require('.'); + +meow(` + Usage + $ mkdir my-cli + $ cd my-cli + $ create-ink-app +`); + +createInkApp().catch(error => { + console.error(error.stack); + process.exit(1); +}); diff --git a/index.js b/index.js new file mode 100644 index 0000000..837ebf3 --- /dev/null +++ b/index.js @@ -0,0 +1,88 @@ +'use strict'; +const {promisify} = require('util'); +const path = require('path'); +const fs = require('fs'); +const replaceString = require('replace-string'); +const slugify = require('slugify'); +const execa = require('execa'); +const Listr = require('listr'); +const cpy = require('cpy'); + +const readFile = promisify(fs.readFile); +const writeFile = promisify(fs.writeFile); + +const copyWithTemplate = async (from, to, variables) => { + const source = await readFile(from, 'utf8'); + let generatedSource = source; + + if (typeof variables === 'object') { + generatedSource = replaceString(source, '%NAME%', variables.name); + } + + await writeFile(to, generatedSource); +}; + +const fromPath = file => path.join(__dirname, 'template', file); +const toPath = file => path.join(process.cwd(), file); + +module.exports = () => { + const pkgName = slugify(path.basename(process.cwd())); + + const tasks = new Listr([ + { + title: 'Copy files', + task: async () => { + const variables = { + name: pkgName + }; + + return Promise.all([ + copyWithTemplate(fromPath('_package.json'), toPath('package.json'), variables), + copyWithTemplate(fromPath('readme.md'), toPath('readme.md'), variables), + copyWithTemplate(fromPath('cli.js'), toPath('cli.js'), variables), + cpy(fromPath('ui.js'), process.cwd()), + cpy(fromPath('test.js'), process.cwd()), + cpy([ + fromPath('.editorconfig'), + fromPath('.gitattributes'), + fromPath('.gitignore') + ], process.cwd()) + ]); + } + }, + { + title: 'Install dependencies', + task: async () => { + await execa('npm', [ + 'install', + 'meow', + 'ink', + 'react', + 'prop-types', + 'import-jsx' + ]); + + return execa('npm', [ + 'install', + '--save-dev', + 'xo', + 'ava', + 'ink-testing-library', + 'chalk', + '@babel/preset-react', + '@babel/register', + 'eslint-config-xo-react', + 'eslint-plugin-react', + 'eslint-plugin-react-hooks' + ]); + } + }, + { + title: 'Link executable', + task: () => execa('npm', ['link']) + } + ]); + + console.log(); + return tasks.run(); +}; diff --git a/license b/license new file mode 100644 index 0000000..d0aa8c4 --- /dev/null +++ b/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Vadim Demedes (vadimdemedes.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/media/demo.gif b/media/demo.gif new file mode 100644 index 0000000..20b4ae8 Binary files /dev/null and b/media/demo.gif differ diff --git a/package.json b/package.json new file mode 100644 index 0000000..76292e3 --- /dev/null +++ b/package.json @@ -0,0 +1,47 @@ +{ + "name": "create-ink-app", + "version": "0.0.0", + "description": "Generate a starter Ink app", + "license": "MIT", + "repository": "vadimdemedes/create-ink-app", + "author": { + "name": "Vadim Demedes", + "email": "vdemedes@gmail.com", + "url": "vadimdemedes.com" + }, + "bin": "cli.js", + "engines": { + "node": ">=8" + }, + "scripts": { + "test": "xo" + }, + "files": [ + "index.js", + "cli.js", + "template" + ], + "keywords": [ + "ink", + "ink-app", + "ink", + "ink-app", + "cli" + ], + "dependencies": { + "cpy": "^7.2.0", + "execa": "^1.0.0", + "listr": "^0.14.3", + "meow": "^5.0.0", + "replace-string": "^3.0.0", + "slugify": "^1.3.4" + }, + "devDependencies": { + "xo": "^0.21.0" + }, + "xo": { + "ignores": [ + "template" + ] + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..5ea7820 --- /dev/null +++ b/readme.md @@ -0,0 +1,21 @@ +# create-ink-app [![Build Status](https://travis-ci.org/vadimdemedes/create-ink-app.svg?branch=master)](https://travis-ci.org/vadimdemedes/create-ink-app) + +> Generate a starter [Ink](https://github.com/vadimdemedes/ink) app + + +## Usage + +This helper tool scaffolds out basic project structure for Ink apps and lets you avoid the boilerplate and get to building beautiful CLIs in no time. + +```bash +$ mkdir my-fancy-cli +$ cd my-fancy-cli +$ npx create-ink-app +``` + +![](media/demo.gif) + + +## License + +MIT © [Vadim Demedes](https://vadimdemedes.com) diff --git a/template/.editorconfig b/template/.editorconfig new file mode 100644 index 0000000..1c6314a --- /dev/null +++ b/template/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/template/.gitattributes b/template/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/template/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/template/.gitignore b/template/.gitignore new file mode 100644 index 0000000..239ecff --- /dev/null +++ b/template/.gitignore @@ -0,0 +1,2 @@ +node_modules +yarn.lock diff --git a/template/_package.json b/template/_package.json new file mode 100644 index 0000000..3bfbab1 --- /dev/null +++ b/template/_package.json @@ -0,0 +1,31 @@ +{ + "name": "%NAME%", + "version": "0.0.0", + "license": "MIT", + "bin": "cli.js", + "engines": { + "node": ">=8" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "cli.js", + "ui.js" + ], + "dependencies": {}, + "devDependencies": {}, + "ava": { + "require": [ + "@babel/register" + ] + }, + "babel": { + "presets": [ + "@babel/preset-react" + ] + }, + "xo": { + "extends": "xo-react" + } +} diff --git a/template/cli.js b/template/cli.js new file mode 100644 index 0000000..6305075 --- /dev/null +++ b/template/cli.js @@ -0,0 +1,22 @@ +#!/usr/bin/env node +'use strict'; +const React = require('react'); +const importJsx = require('import-jsx'); +const {render} = require('ink'); +const meow = require('meow'); + +const ui = importJsx('./ui'); + +const cli = meow(` + Usage + $ %NAME% + + Options + --name Your name + + Examples + $ %NAME% --name=Jane + Hello, Jane +`); + +render(React.createElement(ui, cli.flags)); diff --git a/template/readme.md b/template/readme.md new file mode 100644 index 0000000..e7a1084 --- /dev/null +++ b/template/readme.md @@ -0,0 +1,27 @@ +# %NAME% + +> This readme is automatically generated by [create-ink-app](https://github.com/vadimdemedes/create-ink-app) + + +## Install + +```bash +$ npm install --global %NAME% +``` + + +## CLI + +``` +$ %NAME% --help + + Usage + $ %NAME% + + Options + --name Your name + + Examples + $ %NAME% --name=Jane + Hello, Jane +``` diff --git a/template/test.js b/template/test.js new file mode 100644 index 0000000..9b3070d --- /dev/null +++ b/template/test.js @@ -0,0 +1,17 @@ +import React from 'react'; +import chalk from 'chalk'; +import test from 'ava'; +import {render} from 'ink-testing-library'; +import App from './ui'; + +test('greet unknown user', t => { + const {lastFrame} = render(); + + t.is(lastFrame(), chalk`Hello, {green Stranger}`); +}); + +test('greet user with a name', t => { + const {lastFrame} = render(); + + t.is(lastFrame(), chalk`Hello, {green Jane}`); +}); diff --git a/template/ui.js b/template/ui.js new file mode 100644 index 0000000..1016aef --- /dev/null +++ b/template/ui.js @@ -0,0 +1,20 @@ +'use strict'; +const React = require('react'); +const PropTypes = require('prop-types'); +const {Text, Color} = require('ink'); + +const App = ({name}) => ( + + Hello, {name} + +); + +App.propTypes = { + name: PropTypes.string +}; + +App.defaultProps = { + name: 'Stranger' +}; + +module.exports = App;