Skip to content
Permalink
Browse files

fix(schema): add proper types

- fix the schema according to Prettier defaults
- update npmignore
- change getLatestNodeVersion to read the `dist-tags` for the `latest` package version
  • Loading branch information...
schuchard committed Jun 20, 2018
1 parent 65627ca commit a9f51ba36169c115c62bb10b07e6d25ffc5c1fde
Showing with 86 additions and 32 deletions.
  1. +3 −2 .npmignore
  2. +61 −4 README.md
  3. +2 −2 src/prettier/index.ts
  4. +11 −16 src/prettier/schema.json
  5. +2 −2 src/utility/prettier-util.ts
  6. +7 −6 src/utility/util.ts
@@ -6,9 +6,10 @@
!src/**/files/**/*.js
!src/**/files/**/*.ts

.vscode/*
*.vscode

sandbox
docs

*.circleci
*.circleci
*.github
@@ -7,6 +7,55 @@
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

## Usage 🚀

Install globally
```shell
npm install -g @schuchard/prettier
```

Then in an Angular CLI root directory:

```shell
ng g @schuchard/prettier:prettier
```

## Defaults

Without any CLI arguments the [default](https://prettier.io/docs/en/options.html) Prettier options will be applied. The defaults can be changed in one of two ways:

- modifying the `./prettier.config.js` after the schematic runs
- passing a CLI flag with the desired value for any of the options
- `ng g @schuchard/prettier:prettier --printWidth=100 --tabWidth=4`

```json
printWidth = 80;
tabWidth = 2;
useTabs = false;
semi = true;
singleQuote = false;
trailingComma = "none";
bracketSpacing = true;
jsxBracketSameLine = false;
arrowParens = "avoid";
rangeStart = 0;
rangeEnd = Infinity;
requirePragma = false;
insertPragma = false;
proseWrap = "preserve";
lintStaged = true;
```

### Lint-staged

By default [lint-staged](https://github.com/okonet/lint-staged) is [configured](https://prettier.io/docs/en/precommit.html#option-1-lint-staged-https-githubcom-okonet-lint-staged) automatically along with a pre-commit hook. This will run Prettier against all new files as they are committed.

lint-stage and the precommit hook can be disabled with the following

```shell
ng g @schuchard/prettier:prettier --lintStaged=false
```

## Contributing

### Getting started
@@ -31,10 +80,18 @@ This repo uses semantic-release and relies on [formatted](https://github.com/sem
yarn cm
```

### Publishing 🎉
## Documentation

Publishes are managed in CircleCI on merges into master. See the commit message [docs](https://github.com/semantic-release/semantic-release#commit-message-format) on how to trigger a new NPM publish and version.
Unsure how to do something with schematics? Check the Angular [schematics](https://github.com/angular/angular-cli/tree/master/packages/schematics/angular) for inspiration.

## Documentation
## Publishing

- First ensure you're authenticated with `npm login`.

```shell
npm run release
```

## Issues & Requests 📬

Unsure how to do something with schematics? Check the Angular [schematics](https://github.com/angular/angular-cli/tree/master/packages/schematics/angular) for inspiration.
Submit an [issue](https://github.com/schuchard/prettier-schematic/issues/new/choose)
@@ -46,7 +46,7 @@ function addDependencies(options: PrettierOptions): Rule {

return of('prettier', 'lint-staged', 'husky').pipe(
filter((pkg) => {
if (options.lintStaged === 'false') {
if (options.lintStaged === false) {
// remove lint-staged deps
return !lintStagedDep.some((p) => p === pkg);
}
@@ -136,7 +136,7 @@ function updateEditorConfig(options: PrettierOptions): Rule {

function addLintStagedConfig(options: PrettierOptions) {
return (tree: Tree, context: SchematicContext) => {
if (options.lintStaged !== 'false') {
if (options.lintStaged === true) {
addPropertyToPackageJson(tree, context, 'scripts', {
precommit: 'lint-staged',
});
@@ -7,27 +7,27 @@
"printWidth": {
"type": "number",
"description": "Specify the line length that the printer will wrap on.",
"default": "80"
"default": 80
},
"tabWidth": {
"type": "number",
"description": "Specify the number of spaces per indentation-level.",
"default": "false"
"default": 2
},
"useTabs": {
"type": "boolean",
"description": "Indent lines with tabs instead of spaces.",
"default": "false"
"default": false
},
"semi": {
"type": "boolean",
"description": "Print semicolons at the ends of statements.",
"default": "true"
"default": true
},
"singleQuote": {
"type": "boolean",
"description": "Use single quotes instead of double quotes.",
"default": "false"
"default": false
},
"trailingComma": {
"type": "string",
@@ -38,13 +38,13 @@
"bracketSpacing": {
"type": "boolean",
"description": "Print spaces between brackets in object literals.",
"default": "true"
"default": true
},
"jsxBracketSameLine": {
"type": "boolean",
"description":
"Put the > of a multi-line JSX element at the end of the last line instead of being alone on the next line (does not apply to self closing elements).",
"default": "false"
"default": false
},
"arrowParens": {
"type": "string",
@@ -54,24 +54,19 @@
"rangeStart": {
"type": "number",
"description": "Format only a segment of a file.",
"default": "0"
},
"rangeEnd": {
"type": "number",
"description": "Format only a segment of a file.",
"default": "Infinity"
"default": 0
},
"requirePragma": {
"type": "boolean",
"description":
"Prettier can restrict itself to only format files that contain a special comment, called a pragma, at the top of the file. This is very useful when gradually transitioning large, unformatted codebases to prettier.",
"default": "false"
"default": false
},
"insertPragma": {
"type": "boolean",
"description":
"Prettier can insert a special @format marker at the top of files specifying that the file has been formatted with prettier. This works well when used in tandem with the --require-pragma option.",
"default": "false"
"default": false
},
"proseWrap": {
"type": "string",
@@ -82,7 +77,7 @@
"lintStaged": {
"type": "boolean",
"description": "Run Prettier against staged git files.",
"default": "true"
"default": true
}
},
"required": [],
@@ -17,7 +17,7 @@ export interface PrettierOptions {
requirePragma?: boolean;
insertPragma?: boolean;
proseWrap?: 'preserve' | 'always' | 'never';
lintStaged?: 'true' | 'false';
lintStaged?: boolean;
[index: string]: any;
}

@@ -38,7 +38,7 @@ export class PrettierSettings implements PrettierOptions {
requirePragma = false;
insertPragma = false;
proseWrap = 'preserve' as 'preserve';
lintStaged = 'true' as 'true';
lintStaged = true;
}

export function getDefaultOptions(
@@ -18,23 +18,24 @@ export enum Config {
PackageJsonPath = 'package.json',
JsonIndentLevel = 4,
}

export function getLatestNodeVersion(packageName: string): Promise<NpmRegistryPackage> {
const DEFAULT_VERSION = 'latest';

return new Promise((resolve) => {
return get(`http://registry.npmjs.org/${packageName}/latest`, (res) => {
return get(`http://registry.npmjs.org/${packageName}`, (res) => {
let rawData = '';
res.on('data', (chunk) => (rawData += chunk));
res.on('end', () => {
try {
const { name, version } = JSON.parse(rawData);
resolve(buildPackage(name, version));
const response = JSON.parse(rawData);
const version = response && response['dist-tags'] || {};

resolve(buildPackage(packageName, version.latest));
} catch (e) {
resolve(buildPackage(name));
resolve(buildPackage(packageName));
}
});
}).on('error', () => resolve(buildPackage(name)));
}).on('error', () => resolve(buildPackage(packageName)));
});

function buildPackage(name: string, version: string = DEFAULT_VERSION): NpmRegistryPackage {

0 comments on commit a9f51ba

Please sign in to comment.
You can’t perform that action at this time.