-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,028 additions
and
4,505 deletions.
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,7 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true, | ||
"plugins": [ | ||
"./extras/prettier-imports" | ||
] | ||
} |
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
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,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 |
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
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,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 |
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,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' | ||
} | ||
} |
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,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) |
Oops, something went wrong.