Skip to content

Commit

Permalink
feat: add cra, next and gatsby
Browse files Browse the repository at this point in the history
  • Loading branch information
pdrbrnd committed Dec 5, 2019
1 parent 8c155f6 commit c1df870
Show file tree
Hide file tree
Showing 32 changed files with 17,891 additions and 3,835 deletions.
11 changes: 9 additions & 2 deletions .eslintrc
@@ -1,8 +1,15 @@
{
"extends": ["oclif", "oclif-typescript"],
"extends": ["oclif", "oclif-typescript", "plugin:react/recommended"],
"rules": {
"object-curly-spacing": "off",
"indent": "off",
"unicorn/filename-case": "off"
"unicorn/filename-case": "off",
"no-process-exit": "off",
"unicorn/no-process-exit": "off",
"no-console": "off",
"no-extraneous-require": "off",
"node/no-extraneous-require": "off",
"node/no-missing-require": "off",
"unicorn/no-abusive-eslint-disable": "off"
}
}
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,5 @@
/tmp
/yarn.lock
node_modules

.DS_Store
21,084 changes: 17,384 additions & 3,700 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -14,11 +14,11 @@
"@oclif/config": "^1.13.3",
"@oclif/plugin-help": "^2.2.1",
"chalk": "^3.0.0",
"cli-ux": "^5.3.3",
"copy-template-dir": "^1.4.0",
"execa": "^3.4.0",
"fs": "0.0.1-security",
"jsonfile": "^5.0.0",
"listr": "^0.14.3",
"ora": "^4.0.3",
"path": "^0.12.7",
"tslib": "^1.10.0"
},
Expand All @@ -30,11 +30,16 @@
"@types/listr": "^0.14.2",
"@types/node": "^10.17.6",
"@types/react": "^16.9.14",
"@types/react-dom": "^16.9.4",
"eslint": "^5.16.0",
"eslint-config-oclif": "^3.1.0",
"eslint-config-oclif-typescript": "^0.1.0",
"eslint-plugin-react": "^7.17.0",
"next": "^9.1.4",
"nyc": "^14.1.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "^3.3.0",
"ts-node": "^8.5.4",
"typescript": "^3.7.2"
},
Expand Down
118 changes: 69 additions & 49 deletions src/index.ts
@@ -1,13 +1,18 @@
import { Command, flags } from '@oclif/command'
import fs from 'fs'
import chalk from 'chalk'
import execa from 'execa'
import Listr from 'listr'

import { projectTypes, Stacks } from './types'
import log from './utils/log'

import createGatsby from './lib/gatsby'
import applyCommonConfig from './lib/common'
import applyOverrides from './lib/overrides'
import gatsby from './lib/gatsby'
import next from './lib/next'
import cra from './lib/cra'
import common from './lib/common'
import overrides from './lib/overrides'

const stacks = ['cra', 'gatsby', 'next'] as const

type Stacks = typeof stacks[number]

class SignificaStart extends Command {
static description = 'Significa project starter'
Expand All @@ -18,69 +23,84 @@ class SignificaStart extends Command {
name: flags.string({ char: 'n', description: "Project's name" }),
type: flags.string({
char: 't',
description: `Project's type (${projectTypes.join(', ')})`,
description: `Project's type (${stacks.join(', ')})`,
}),
}

static args = [
{
name: 'type',
required: true,
options: (projectTypes as unknown) as string[],
options: (stacks as unknown) as string[],
},
{ name: 'name', required: true },
]

startProject(name: string, type: Stacks) {
async run() {
const {
args: { name, type },
}: { args: { name: string; type: Stacks } } = this.parse(SignificaStart)

if (fs.existsSync(name)) {
log.error('Folder already exists')
process.exit(1)
}

// Start project
log.info(`Starting new ${chalk.yellow(type)} project: ${chalk.blue(name)}`)
switch (type) {
case 'cra':
return execa('npx', ['create-react-app', name, '--typescript'])
await cra(name)
break
case 'gatsby': {
return createGatsby(name)
await gatsby(name)
break
}
case 'next':
return execa('npx', [
'create-next-app',
'--example',
'with-typescript',
name,
])
default:
await next(name)
break
default:
log.error(`Expected ${type} to be one of: ${stacks.join(' ,')}`)
process.exit(1)
}
}

async run() {
const {
args: { name, type },
}: { args: { name: string; type: Stacks } } = this.parse(SignificaStart)
// Apply src folder
// <here>

// Apply Significa UI
// <here>

// Add static type checking
log.info('Adding static type checking and base configuration')
await common(name)

// Apply overrides
log.info('Applying project overrides')
await overrides(name, type)

// Tests
if (type !== 'cra') {
const shouldAddTests = await log.confirm('Add tests?')

if (shouldAddTests) {
// Add tests
// <here>
}
}

// Storybook
const shouldAddStorybook = await log.confirm('Add storybook?')

if (shouldAddStorybook) {
// Add storybook
// <here>
}

const task = new Listr([
{
title: `Starting new ${chalk.yellow(type)} project: ${chalk.blue(
name
)}`,
task: async () => this.startProject(name, type),
},
{
title: 'Apply common src folder',
task: async () => {},
},
{
title: 'Static type checking',
task: async () => applyCommonConfig(name),
},
{
title: 'Apply overrides',
task: async () => applyOverrides(name, type),
},
{
title: 'Final touches', // Git, etc.
task: async () => {},
},
])

task.run().catch(() => {}) // noop
log.success(
`Project created! \n\n Type in ${chalk.blue(
`cd ${name}`
)} and happy coding!\n`
)
}
}

Expand Down
56 changes: 24 additions & 32 deletions src/lib/common.ts
@@ -1,9 +1,9 @@
import Listr from 'listr'
import execa from 'execa'
import path from 'path'
const copy = require('copy-template-dir')

import addScript from '../utils/add-script'
import log from '../utils/log'

const scripts: { [key: string]: string } = {
format: 'npm run prettier -- --write',
Expand All @@ -19,45 +19,37 @@ const devDependencies: string[] = [
'@significa/prettier-config',
'@significa/tsconfig-config',
//
'eslint',
'prettier',
'husky',
'lint-staged',
'@commitlint/cli',
'@commitlint/config-conventional',
'cz-conventional-changelog',
'husky',
'lint-staged',
]

async function applyCommonConfig(name: string) {
const cwd = path.join(process.cwd(), name)

return new Listr([
{
title: 'Adding scripts to package.json',
task: async () => {
Object.keys(scripts).forEach(async key => {
await addScript(`${cwd}/package.json`, key, scripts[key])
})
},
},
{
title: 'Add files',
task: async () => {
copy(
path.join(__dirname, '../templates/common'),
cwd,
{ name },
(err: any) => {
if (err) throw err
}
)
},
},
{
title: 'Installing dependencies',
task: async () => {
await execa('npm', ['i', '--save-dev', ...devDependencies], { cwd })
},
},
])
const spinner = log.step('Adding scripts to package.json')
Object.keys(scripts).forEach(async key => {
await addScript(`${cwd}/package.json`, key, scripts[key])
})

log.step('Adding configuration files')
await copy(
path.join(__dirname, '../templates/common'),
cwd,
{ name },
(err: any) => {
if (err) throw err
}
)

log.step('Installing dependencies')
await execa('npm', ['i', '--save-dev', ...devDependencies], { cwd })

spinner.succeed()
}

export default applyCommonConfig
40 changes: 40 additions & 0 deletions src/lib/cra.ts
@@ -0,0 +1,40 @@
import execa from 'execa'
import path from 'path'
const copy = require('copy-template-dir')

import log from '../utils/log'

const dependencies: string[] = ['styled-components']

async function cra(name: string) {
const cwd = path.join(process.cwd(), name)

const spinner = log.step('Installing create react app')
await execa('npx', [
'create-react-app',
name,
'--template',
'typescript',
'--use-npm',
])

log.step('Removing src folder')
await execa('rm', ['-rf', 'src'], { cwd })

log.step('Adding project files')
await copy(
path.join(__dirname, '../templates/cra'),
cwd,
{ name },
(err: any) => {
if (err) throw err
}
)

log.step('Installing dependencies')
await execa('npm', ['i', '--save', ...dependencies], { cwd })

spinner.succeed()
}

export default cra

0 comments on commit c1df870

Please sign in to comment.