No known key found for this signature in database
GPG Key ID: 51D5A407BFCE64A9
8 changed files with 1015 additions and 210 deletions
-
1.gitignore
-
21README.md
-
44package.json
-
150src/commands/add/prettier.ts
-
6src/commands/wall/index.ts
-
46src/utls/base-command.ts
-
146src/utls/print.ts
-
807yarn.lock
@ -0,0 +1,150 @@ |
|||
// import {flags} from '@oclif/command'
|
|||
import {BaseCommand} from '../../utls/base-command' |
|||
import * as latestVersion from 'latest-version' |
|||
import {system, filesystem} from 'gluegun' |
|||
import * as prompts from 'prompts' |
|||
import * as fs from 'fs' |
|||
import {plugins, statusMatrix, add, commit, config as gitConfig} from 'isomorphic-git' |
|||
|
|||
plugins.set('fs', fs) |
|||
|
|||
export default class Prettier extends BaseCommand { |
|||
static description = 'describe the command here' |
|||
|
|||
static examples = [ |
|||
`$ nbx wall
|
|||
hello world from ./src/hello.ts! |
|||
`,
|
|||
] |
|||
|
|||
static flags = { |
|||
...BaseCommand.flags, |
|||
} |
|||
|
|||
async run() { |
|||
const packagePath = filesystem.path('.', 'package.json') |
|||
|
|||
if (filesystem.exists(packagePath) !== 'file') { |
|||
this.error('There is no package.json not found in the current folder') |
|||
} |
|||
|
|||
const packageJson = filesystem.read(packagePath, 'json') |
|||
if (packageJson.devDependencies.prettier) { |
|||
this.error('Prettier is already installed in this project.') |
|||
} |
|||
|
|||
const {mask, shouldCommit} = await prompts([ |
|||
{ |
|||
type: 'text', |
|||
message: 'On what files it should run prettier', |
|||
name: 'mask', |
|||
initial: '**/*.{js,vue,json,ts,tsx,md,yml,html}', |
|||
}, |
|||
{ |
|||
type: 'confirm', |
|||
message: 'Do you want gitmoji commits with the prettier setup ?', |
|||
name: 'shouldCommit', |
|||
initial: true, |
|||
}, |
|||
], {onCancel: () => this.error('User canceled prompt.')}) |
|||
|
|||
if (shouldCommit) { |
|||
const {config} = await this.getConfig() |
|||
if (!config?.git?.user) { |
|||
this.error('Missing config key git.user for git commits') |
|||
} |
|||
if (!config?.git?.email) { |
|||
this.error('Missing config key git.email for git commits') |
|||
} |
|||
await gitConfig({ |
|||
dir: '.', |
|||
path: 'user.name', |
|||
value: config?.git?.user, |
|||
}) |
|||
await gitConfig({ |
|||
dir: '.', |
|||
path: 'user.email', |
|||
value: config?.git?.email, |
|||
}) |
|||
const changes = (await statusMatrix({dir: '.', pattern: '**'})) |
|||
.filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) |
|||
if (changes.length > 0) { |
|||
this.error('There is unsaved changed in the git repository, aborting') |
|||
} |
|||
} |
|||
|
|||
const addDevDependency = async (name: string): Promise<void> => { |
|||
await this.runWithSpinner(`Adding ${name} dependency`, async () => { |
|||
const versionToInstall = await latestVersion(name) |
|||
await system.exec(`yarn add -D ${name}@${versionToInstall}`) |
|||
if (shouldCommit) { |
|||
await add({filepath: 'package.json', dir: '.'}) |
|||
await add({filepath: 'yarn.lock', dir: '.'}) |
|||
await commit({dir: '.', message: `:heavy_plus_sign: add ${name}@${versionToInstall}`}) |
|||
} |
|||
}) |
|||
} |
|||
|
|||
await addDevDependency('prettier') |
|||
await addDevDependency('husky') |
|||
await addDevDependency('pretty-quick') |
|||
|
|||
await this.runWithSpinner('Adding package.json scripts', async () => { |
|||
const packageJsonWithDeps = filesystem.read(packagePath, 'json') |
|||
const finalPackageJson = { |
|||
...packageJsonWithDeps, |
|||
scripts: { |
|||
...packageJsonWithDeps.scripts, |
|||
// eslint-disable-next-line no-useless-escape
|
|||
'format:write': `prettier --write "${mask}"`, |
|||
// eslint-disable-next-line no-useless-escape
|
|||
'format:check': `prettier --list-different "${mask}"`, |
|||
}, |
|||
husky: { |
|||
hooks: { |
|||
'pre-commit': 'pretty-quick --staged', |
|||
}, |
|||
}, |
|||
} |
|||
|
|||
await filesystem.write(packagePath, finalPackageJson, {jsonIndent: 2}) |
|||
await system.exec('yarn') |
|||
|
|||
if (shouldCommit) { |
|||
await add({filepath: 'package.json', dir: '.'}) |
|||
await commit({dir: '.', message: ':wrench: add script and husky to package.json'}) |
|||
} |
|||
}) |
|||
|
|||
await this.runWithSpinner('Adding .prettierrc config file', async () => { |
|||
const prettierRcFile = { |
|||
semi: true, |
|||
singleQuote: true, |
|||
printWidth: 120, |
|||
quoteProps: 'consistent', |
|||
trailingComma: 'all', |
|||
} |
|||
|
|||
await filesystem.write(filesystem.path('.', '.prettierrc'), prettierRcFile, {jsonIndent: 2}) |
|||
|
|||
if (shouldCommit) { |
|||
await add({filepath: '.prettierrc', dir: '.'}) |
|||
await commit({dir: '.', message: ':wrench: add prettierrc config file'}) |
|||
} |
|||
}) |
|||
|
|||
await this.runWithSpinner('Updating code to match prettier style', async () => { |
|||
await system.exec('yarn format:write') |
|||
|
|||
if (shouldCommit) { |
|||
const commitsPromice = (await statusMatrix({dir: '.', pattern: '**'})) |
|||
.filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) |
|||
.map(arr => arr[0]) |
|||
.map(filepath => add({filepath, dir: '.'})) |
|||
|
|||
await Promise.all(commitsPromice) |
|||
await commit({dir: '.', message: ':art: apply prettier style to project'}) |
|||
} |
|||
}) |
|||
} |
|||
} |
|||
@ -1,146 +0,0 @@ |
|||
import * as importedColors from 'colors/safe' |
|||
|
|||
type NbxPrintColors = typeof importedColors & { |
|||
highlight: (t: string) => string; |
|||
info: (t: string) => string; |
|||
warning: (t: string) => string; |
|||
success: (t: string) => string; |
|||
error: (t: string) => string; |
|||
line: (t: string) => string; |
|||
muted: (t: string) => string; |
|||
} |
|||
// We're extending `colors` with a few more attributes
|
|||
const colors = importedColors as NbxPrintColors |
|||
colors.setTheme({ |
|||
highlight: 'cyan', |
|||
info: 'reset', |
|||
warning: 'yellow', |
|||
success: 'green', |
|||
error: 'red', |
|||
line: 'grey', |
|||
muted: 'grey', |
|||
}) |
|||
|
|||
/** |
|||
* Print a blank line. |
|||
*/ |
|||
function newline() { |
|||
// eslint-disable-next-line no-console
|
|||
console.log('') |
|||
} |
|||
|
|||
/** |
|||
* Prints a divider line |
|||
*/ |
|||
function divider() { |
|||
// eslint-disable-next-line no-console
|
|||
console.log(colors.line('---------------------------------------------------------------')) |
|||
} |
|||
|
|||
/** |
|||
* Prints text without theming. |
|||
* |
|||
* Use this when you're writing stuff outside the toolbox of our |
|||
* printing scheme. hint: rarely. |
|||
* |
|||
* @param message The message to write. |
|||
*/ |
|||
function fancy(message: any): void { |
|||
// eslint-disable-next-line no-console
|
|||
console.log(message) |
|||
} |
|||
|
|||
/** |
|||
* Writes a normal information message. |
|||
* |
|||
* This is the default type you should use. |
|||
* |
|||
* @param message The message to show. |
|||
*/ |
|||
function info(message: string): void { |
|||
// eslint-disable-next-line no-console
|
|||
console.log(colors.info(message)) |
|||
} |
|||
|
|||
/** |
|||
* Writes an error message. |
|||
* |
|||
* This is when something horribly goes wrong. |
|||
* |
|||
* @param message The message to show. |
|||
*/ |
|||
function error(message: string): void { |
|||
// eslint-disable-next-line no-console
|
|||
console.log(colors.error(message)) |
|||
} |
|||
|
|||
/** |
|||
* Writes a warning message. |
|||
* |
|||
* This is when the user might not be getting what they're expecting. |
|||
* |
|||
* @param message The message to show. |
|||
*/ |
|||
function warning(message: string): void { |
|||
// eslint-disable-next-line no-console
|
|||
console.log(colors.warning(message)) |
|||
} |
|||
|
|||
/** |
|||
* Writes a debug message. |
|||
* |
|||
* This is for devs only. |
|||
* |
|||
* @param message The message to show. |
|||
*/ |
|||
function debug(message: string, title = 'DEBUG'): void { |
|||
const topLine = `vvv -----[ ${title} ]----- vvv` |
|||
const botLine = `^^^ -----[ ${title} ]----- ^^^` |
|||
|
|||
// eslint-disable-next-line no-console
|
|||
console.log(colors.rainbow(topLine)) |
|||
// eslint-disable-next-line no-console
|
|||
console.log(message) |
|||
// eslint-disable-next-line no-console
|
|||
console.log(colors.rainbow(botLine)) |
|||
} |
|||
|
|||
/** |
|||
* Writes a success message. |
|||
* |
|||
* When something is successful. Use sparingly. |
|||
* |
|||
* @param message The message to show. |
|||
*/ |
|||
function success(message: string): void { |
|||
// eslint-disable-next-line no-console
|
|||
console.log(colors.success(message)) |
|||
} |
|||
|
|||
/** |
|||
* Creates a spinner and starts it up. |
|||
* |
|||
* @param config The text for the spinner or an ora configuration object. |
|||
* @returns The spinner. |
|||
*/ |
|||
function spin(config?: string | object): any { |
|||
return require('ora')(config || '').start() |
|||
} |
|||
|
|||
const checkmark = colors.success('✔︎') |
|||
const xmark = colors.error('ⅹ') |
|||
|
|||
export const print = { |
|||
colors, |
|||
newline, |
|||
divider, |
|||
fancy, |
|||
info, |
|||
error, |
|||
warning, |
|||
debug, |
|||
success, |
|||
spin, |
|||
checkmark, |
|||
xmark, |
|||
} |
|||
807
yarn.lock
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
Write
Preview
Loading…
Cancel
Save
Reference in new issue