Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihaase committed Sep 14, 2019
1 parent 6afcdf4 commit 013f77a
Show file tree
Hide file tree
Showing 16 changed files with 1,028 additions and 4,505 deletions.
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": false,
"singleQuote": true,
"plugins": [
"./extras/prettier-imports"
]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017
Copyright (c) 2019

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# CLI Starter

A CLI Starter for your next CLI project.

[![License](https://img.shields.io/github/license/lenneTech/cli-starter)](/LICENSE) [![CircleCI](https://circleci.com/gh/lenneTech/cli-starter/tree/master.svg?style=shield)](https://circleci.com/gh/lenneTech/cli-starter/tree/master)
[![Dependency Status](https://david-dm.org/lenneTech/cli-starter.svg)](https://david-dm.org/lenneTech/cli-starter) [![devDependency Status](https://david-dm.org/lenneTech/cli-starter/dev-status.svg)](https://david-dm.org/lenneTech/cli-starter?type=dev)

<!--
[![GitHub forks](https://img.shields.io/github/forks/lenneTech/cli-starter)](https://github.com/lenneTech/cli-starter/fork) [![GitHub stars](https://img.shields.io/github/stars/lenneTech/cli-starter)](https://github.com/lenneTech/cli-starter)
-->

# Renaming CLI to your name

run:

```shell
$ npm rename <YOUR CLI NAME>
```

If all works, you can remove the .bak files.

## Customizing your CLI

Check out the documentation at https://github.com/infinitered/gluegun/tree/master/docs.

## Publishing to NPM

To package your CLI up for NPM, do this:

```shell
$ npm login
$ npm whoami
$ npm lint
$ npm test
(if typescript, run `npm run build` here)
$ npm publish
(if you are publish a plublic package for the first time: npm publish --access public)
```

# License

MIT - see LICENSE
5 changes: 3 additions & 2 deletions __tests__/cli-integration.test.ts → __tests__/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as config from '../package.json'
const { system, filesystem } = require('gluegun')

const src = filesystem.path(__dirname, '..')
Expand All @@ -7,10 +8,10 @@ const cli = async cmd =>

test('outputs version', async () => {
const output = await cli('--version')
expect(output).toContain('0.0.1')
expect(output).toContain(config.version)
})

test('outputs help', async () => {
const output = await cli('--help')
expect(output).toContain('0.0.1')
expect(output).toContain(config.version)
})
63 changes: 63 additions & 0 deletions extras/prettier-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { parsers: typescriptParsers } = require('prettier/parser-typescript')
const ts = require('typescript')

// =============================================================================
// Prettier plugin to optimize and sort imports
// see https://github.com/prettier/prettier/issues/6260
// =============================================================================

class SingleLanguageServiceHost {
constructor(name, content) {
this.name = name
this.content = content
this.getCompilationSettings = ts.getDefaultCompilerOptions
this.getDefaultLibFileName = ts.getDefaultLibFilePath
}
getScriptFileNames() {
return [this.name]
}
getScriptVersion() {
return ts.version
}
getScriptSnapshot() {
return ts.ScriptSnapshot.fromString(this.content)
}
getCurrentDirectory() {
return ''
}
}

function applyChanges(text, changes) {
return changes.reduceRight((text, change) => {
const head = text.slice(0, change.span.start)
const tail = text.slice(change.span.start + change.span.length)
return `${head}${change.newText}${tail}`
}, text)
}

function organizeImports(text) {
const fileName = 'file.ts'
const host = new SingleLanguageServiceHost(fileName, text)
const languageService = ts.createLanguageService(host)
const formatOptions = ts.getDefaultFormatCodeSettings()
const fileChanges = languageService.organizeImports(
{ type: 'file', fileName },
formatOptions,
{}
)
const textChanges = [...fileChanges.map(change => change.textChanges)]
return applyChanges(text, textChanges)
}

const parsers = {
typescript: {
...typescriptParsers.typescript,
preprocess(text) {
text = organizeImports(text)
return text
}
}
}

// Uses module.export because of 'Unexpected token export' error
module.exports = parsers
169 changes: 169 additions & 0 deletions extras/rename/commands/rename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { IHelperExtendedGluegunToolbox } from '@lenne.tech/cli-plugin-helper/src/interfaces/extended-gluegun-toolbox.interface'
import { rename as renameOrg } from 'fs'
import { dirname } from 'path'
import { promisify } from 'util'

const rename = promisify(renameOrg)

/**
* Rename files and data of cli starter
*/
module.exports = {
name: 'rename',
description: 'Rename files and data of cli starter',
hidden: true,
run: async (toolbox: IHelperExtendedGluegunToolbox) => {
// Toolbox
const {
helper,
npm,
parameters: params,
patching: { patch, update },
print: { info, spin, success },
prompt: { ask },
strings: { kebabCase },
system: { run, startTimer }
} = toolbox

// Get project name
const name = await helper.getInput(params.first, {
name: 'Project name',
showError: true
})
if (!name) {
return
}

// Get author
const author = await helper.getInput(params.options.author, {
name: 'Author',
showError: true
})

// Link
let link = params.options.link && !params.options.nolink
if (!params.options.link && !params.options.nolink) {
link = !!(await ask({
type: 'confirm',
name: 'link',
message: 'Link when finished?'
})).link
}

// Start timer and spinner
const timer = startTimer()
const spinner = spin('Rename files and data')

// Set up different spellings
const nameKebab = kebabCase(name) // kebab-case

// Get package.json
const { path: packagePath, data: packageData } = await npm.getPackageJson()
const rootPath = dirname(packagePath)

// Get original package name
const packageName = packageData.name.replace(/^.*\//, '')

// Set data for package.json
const newPackageData = {
...packageData,
name: nameKebab,
version: '0.0.1',
description: name,
keywords: ['cli'],
author: author,
contributors: [author],
homepage: '',
repository: {
type: 'git',
url: ''
},
bugs: {
url: ''
},
bin: {}
}
newPackageData.bin[nameKebab] = 'bin/' + nameKebab
await npm.setPackageJson(newPackageData)

// Update package-lock.json
const packageLockPath = rootPath + '/package-lock.json'
await update(packageLockPath, data => {
data.name = nameKebab
data.version = '0.0.1'
return data
})

// Patch tests
await patch(rootPath + '/__tests__/cli.test.ts', {
insert: nameKebab,
replace: new RegExp(packageName, 'g'),
force: true
})

// Rename bin
await rename(
rootPath + '/bin/' + packageName,
rootPath + '/bin/' + nameKebab
)

// Patch docs
await patch(rootPath + '/docs/commands.md', {
insert: name,
replace: new RegExp(packageName, 'g'),
force: true
})
await patch(rootPath + '/docs/plugins.md', {
insert: name,
replace: new RegExp(packageName, 'g'),
force: true
})

// Patch CLI
await patch(rootPath + '/src/cli.ts', {
insert: nameKebab,
replace: new RegExp(packageName, 'g'),
force: true
})

// Patch and rename commands
let showCommandsInfo = false
try {
await patch(rootPath + '/src/commands/' + packageName + '.ts', {
insert: nameKebab,
replace: new RegExp(packageName, 'g'),
force: true
})
await patch(rootPath + '/src/commands/' + packageName + '.ts', {
insert: name,
replace: new RegExp('CLI-Starter project', 'g'),
force: true
})
await rename(
rootPath + '/src/commands/' + packageName + '.ts',
rootPath + '/src/commands/' + nameKebab + '.ts'
)
} catch (e) {
showCommandsInfo = true
}

// Link
if (link) {
await run(`cd ${rootPath} && npm run build && npm link`)
}

// Success info
spinner.succeed()
success(
`Renamed${link ? ' and linked' : ''} in ${helper.msToMinutesAndSeconds(
timer()
)}m.`
)
if (showCommandsInfo) {
info('Commands could not be adjusted!')
}
info('Please remember to customize the README.md.')

return 'rename'
}
}
21 changes: 21 additions & 0 deletions extras/rename/rename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { build } = require('gluegun')

// Async run function
async function run(argv) {
// Create a CLI runtime
const cli = build()
.brand('rename')
.src(__dirname)
// .plugins('./node_modules', { matching: 'cli-starter-*', hidden: true })
.plugin('./node_modules/@lenne.tech/cli-plugin-helper/dist', {
extensionFilePattern: '*.js',
commandFilePattern: '*.js'
})
.create()

// Run cli
await cli.run(argv)
}

// Run
run(process.argv)
Loading

0 comments on commit 013f77a

Please sign in to comment.