Skip to content

Commit

Permalink
chore(format): use @fbi-js/configs
Browse files Browse the repository at this point in the history
  • Loading branch information
neikvon committed Jan 20, 2021
1 parent 2ac9845 commit 2521177
Show file tree
Hide file tree
Showing 31 changed files with 641 additions and 364 deletions.
8 changes: 0 additions & 8 deletions .eslintignore

This file was deleted.

20 changes: 0 additions & 20 deletions .eslintrc.js

This file was deleted.

6 changes: 0 additions & 6 deletions .prettierrc

This file was deleted.

10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"files.autoSave": "onFocusChange",
"editor.tabSize": 2,
"editor.wordSeparators": "`~!@#$%^&*()=+[{]}\\|;:'\",.<>/?",

// code style
"editor.defaultFormatter": "numso.prettier-standard-vscode",
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
"typescript.format.insertSpaceBeforeFunctionParenthesis": true,
}
27 changes: 20 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"scripts": {
"build": "rm -rf lib && tsc",
"watch": "npm run build && tsc --watch",
"lint": "eslint . --fix",
"clean": "rm -rf lib && rm -rf node_modules"
"lint": "eslint --ext ts src",
"clean": "rm -rf lib && rm -rf node_modules",
"format": "prettier --write \"src/**/*.{ts,js,json}\" && eslint --ext ts src --fix"
},
"keywords": [
"fbi",
Expand All @@ -41,19 +42,31 @@
"commander": "6.2.1",
"enquirer": "^2.3.6",
"execa": "^5.0.0",
"fs-extra": "^9.0.1",
"ora": "^5.1.0",
"fs-extra": "^9.1.0",
"ora": "^5.2.0",
"pkg-dir": "^5.0.0",
"semver": "^7.3.4",
"tiny-glob": "^0.2.8"
},
"devDependencies": {
"@fbi-js/eslint-config-typescript": "^1.2.0",
"@fbi-js/tsconfig": "^1.2.0",
"@types/node": "*",
"@types/semver": "^7",
"@typescript-eslint/eslint-plugin": "^4",
"@typescript-eslint/parser": "^4",
"eslint": "^7",
"eslint-config-prettier": "^6",
"prettier": "^2.2.1",
"typescript": "^4"
},
"eslintConfig": {
"extends": ["@fbi-js/typescript"]
},
"prettier": {
"semi": false,
"tabWidth": 2,
"endOfLine": "lf",
"printWidth": 80,
"singleQuote": true,
"trailingComma": "none",
"arrowParens": "always"
}
}
40 changes: 27 additions & 13 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export class Cli extends BaseClass {
fbi: Fbi
program: commander.Command

constructor() {
constructor () {
super()
this.fbi = new Fbi()
this.program = this.createProgram(this.fbi.id)
}

public async run() {
public async run () {
const isBuiltInCmd = this.isBuiltInCommand()
const config = this.loadConfig()

Expand All @@ -32,7 +32,9 @@ export class Cli extends BaseClass {
const factory = this.fbi.resolveFactory(factoryId, factoryVersion)
if (!factory) {
this.error(
`factory "${factoryId}${factoryVersion ? `@${factoryVersion}` : ''}" not found`
`factory "${factoryId}${
factoryVersion ? `@${factoryVersion}` : ''
}" not found`
)
} else {
factories.unshift(factory)
Expand All @@ -51,25 +53,35 @@ export class Cli extends BaseClass {
this.context.set('debug', true)
})

await this.program.parseAsync(process.argv).catch((err) => (err ? this.error(err).exit() : ''))
await this.program
.parseAsync(process.argv)
.catch((err) => (err ? this.error(err).exit() : ''))
}

private createProgram(id: string): commander.Command {
private createProgram (id: string): commander.Command {
const program = createCommand()
const _this = this
program
.storeOptionsAsProperties(false)
.passCommandToAction(false)
.name(id)
.version(`${id} ${pkg.version}`, '-v, --version', 'output the current version')
.version(
`${id} ${pkg.version}`,
'-v, --version',
'output the current version'
)
.usage('[command] ...')
.description(pkg.description)
.option('-d, --debug', 'output extra debugging')
.on('--help', () => {
console.log('')
console.log(`Run ${this.style.cyan(id + ' list')} for available commands`)
console.log(
`Run ${this.style.cyan(id + ' <command> -h')} for detailed usage of given command`
`Run ${this.style.cyan(id + ' list')} for available commands`
)
console.log(
`Run ${this.style.cyan(
id + ' <command> -h'
)} for detailed usage of given command`
)
})
.command('help', { hidden: true }) // hide 'help' command
Expand All @@ -90,9 +102,11 @@ export class Cli extends BaseClass {
return program
}

private registerCommands(commands: Command[]): void {
private registerCommands (commands: Command[]): void {
for (const command of commands) {
const nameAndArgs = `${command.id}${command.args ? ` ${command.args}` : ''}`
const nameAndArgs = `${command.id}${
command.args ? ` ${command.args}` : ''
}`
const cmd = this.program.command(nameAndArgs)
if (command.alias) {
cmd.alias(command.alias)
Expand All @@ -107,8 +121,8 @@ export class Cli extends BaseClass {
typeof disabled === 'string' && disabled.trim()
? `${prefix} ${disabled.trim()}`
: disabled
? prefix
: ''
? prefix
: ''
if (message) {
this.warn(message).exit()
}
Expand Down Expand Up @@ -146,7 +160,7 @@ export class Cli extends BaseClass {
}
}

private isBuiltInCommand(argv = process.argv): boolean {
private isBuiltInCommand (argv = process.argv): boolean {
const id = argv.slice(2)[0]
if (!id || id.startsWith('-')) {
return true
Expand Down
48 changes: 29 additions & 19 deletions src/commands/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ export default class CommandClean extends Command {
id = 'clean'
alias = ''
args = ''
description = `clean stale factories and projects`
description = 'clean stale factories and projects'
flags = []
examples = ['fbi clean']

constructor(public factory: Fbi) {
constructor (public factory: Fbi) {
super()
}

public async run(flags: any) {
this.debug(`Running command "${this.id}" from factory "${this.factory.id}" with options:`, {
flags
})
public async run (flags: any) {
this.debug(
`Running command "${this.id}" from factory "${this.factory.id}" with options:`,
{
flags
}
)
await this.removeUnavailableFactories()
await this.removeUnavailableProjects()
}

private async removeUnavailableFactories() {
const spinner = this.createSpinner(`collecting non-available factories...`).start()
private async removeUnavailableFactories () {
const spinner = this.createSpinner(
'collecting non-available factories...'
).start()
const factories = this.store.get()
const nonexist = (
await Promise.all(
Expand All @@ -39,7 +44,7 @@ export default class CommandClean extends Command {
).filter(Boolean)

if (nonexist.length < 1) {
spinner.succeed(`all factories are available`)
spinner.succeed('all factories are available')
return
}

Expand All @@ -55,8 +60,10 @@ export default class CommandClean extends Command {
await this.deleteAction(selected, 'factories')
}

private async removeUnavailableProjects() {
const spinner = this.createSpinner(`collecting non-available projects...`).start()
private async removeUnavailableProjects () {
const spinner = this.createSpinner(
'collecting non-available projects...'
).start()
const items = this.projectStore.get()
const nonexist = (
await Promise.all(
Expand All @@ -74,7 +81,7 @@ export default class CommandClean extends Command {
).filter(Boolean)

if (nonexist.length < 1) {
spinner.succeed(`all projects are available`)
spinner.succeed('all projects are available')
return
}

Expand All @@ -90,35 +97,38 @@ export default class CommandClean extends Command {
await this.deleteAction(selected, 'projects')
}

private async selectDeletion(arr: Record<string, any>[]) {
private async selectDeletion (arr: Record<string, any>[]) {
const { selected } = (await this.prompt({
type: 'multiselect',
name: 'selected',
message: `Select items for deletion from store`,
message: 'Select items for deletion from store',
hint: '(Use <space> to select, <return> to submit)',
choices: arr.map((x: any) => ({
name: x.id,
value: x.path,
hint: x.path
})),
result(names: any) {
return this.map(names)
result (names: any) {
return (this as any).map(names)
}
})) as any

return isValidObject(selected) ? selected : null
}

private async deleteAction(selected: Record<string, string>, name: string) {
private async deleteAction (selected: Record<string, string>, name: string) {
const spinner = this.createSpinner(`Cleaning ${name}...`).start()
await Promise.all(
Object[name === 'projects' ? 'values' : 'keys'](selected).map(
async (key: any) => await this[name === 'projects' ? 'projectStore' : 'store'].del(key)
async (key: any) =>
await this[name === 'projects' ? 'projectStore' : 'store'].del(key)
)
)
.then((ret: any[]) => {
if (ret.length > 0) {
spinner.succeed(`Removed non-available ${name}: ${Object.keys(selected).join(', ')}`)
spinner.succeed(
`Removed non-available ${name}: ${Object.keys(selected).join(', ')}`
)
} else {
spinner.stop()
}
Expand Down
Loading

0 comments on commit 2521177

Please sign in to comment.