Giter Club home page Giter Club logo

prettier-schematic's Introduction

Angular Prettier schematic

A Schematic that adds prettier and a pre-commit hook for formatting staged files.

CircleCI npm Commitizen friendly

Usage 🚀

Run in an Angular CLI project

ng add @schuchard/prettier

Then you're done. Continue your normal workflow of git add, commit, push or a similar workflow with your IDE/editor.

You can optionally install globally:

npm install -g @schuchard/prettier

then in an Angular CLI project:

ng g @schuchard/prettier:add

How does Prettier work with Angular

Automatically against staged files

By default lint-staged is configured along with a pre-commit hook. This will run Prettier against all new files as they are committed according to the settings defined in prettier.config.json. Generally speaking, your workflow should remain unchanged - git add, commit, push

lint-staged-example

Disabling lint-staged install

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

ng g @schuchard/prettier:add --lintStaged=false

Manually

While lint-staged only runs prettier against staged files, you can manually run Prettier against ALL targeted files with the script added to the package.json

npm run prettier

Angular formatting

Beginning with 1.15, Prettier supports formatting HTML and Angular files.

Format all Angular Files - {js,json,css,md,ts,html,component.html}

format-angular-files

Format only Typescript files

Previous versions of this schematic only formatted Typescript files. That functionality is still available and is configured in the CLI prompts or via the --formatAllAngularFiles=false if desired. The default is true.

format-typescript-files

Default Prettier options

Angular 7

This schematic takes advantage of CLI prompts for configuring Prettier options. If you're unsure of a setting, press enter to select the default. You can skip a prompt by passing any of the options when call the schematic.

ng g @schuchard/prettier:add --printWidth=100

schematic-cli

< Angular 7

Without any CLI arguments the default 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 flag to the schematic with the desired value for any of the options. For example:
    • ng g @schuchard/prettier:add --printWidth=100 --tabWidth=4

Example default prettier.config.js

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;

Contributing

Getting started

Install dependencies:

yarn && cd sandbox && yarn

Test changes to the schematic

yarn dev

Test changes to the schematic AND run E2E tests in the sandbox

yarn test

Reset the sandbox state after running the schematic locally

yarn clean

Updating the Sandbox

  1. remove the /sandbox directory
  2. npm i -g @angular/cli
  3. ng new sandbox
  4. update the package.json
"build": "ng build --prod --progress=false",
"test": "ng test --watch=false",

Documentation

Unsure how to do something with schematics? Check the Angular schematics for inspiration.

Inspiration came from this excellent article by Aaron Frost

Publishing

  • First, ensure you're authenticated with npm login.
npm run release

Issues & Requests 📬

Submit an issue

prettier-schematic's People

Contributors

den-dp avatar dependabot[bot] avatar schuchard avatar tallyb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

prettier-schematic's Issues

update to a recent version of the CLI's DevKit

Describe the bug
angular/components#21045

To Reproduce
Steps to reproduce the behavior:

  1. run ng update @angular/core @angular/cli @angular/material in a project on Angular v10 to update to v11

Expected behavior
No exceptions.

Screenshots
N/A

Desktop (please complete the following information):

  • OS: macOS
  • Node Version: 14.15.0
  • Angular & Angular/CLI Version: 11.0.0 and CLI 11.0.1

Additional context
The deprecated Workspace APIs were removed in v11. It looks like these schematics need some updates to the newest CLI DevKit.

Prettier NPM script fails and takes me to index.js inside node_module\@schuchard

Describe the bug
When I run the prettier NPM script, the command never runs. Instead, it takes me to index.js file inside node_modules@schuchard\prettier\src\prettier. VS Code editor stops on line 19 of that file.

To Reproduce
Steps to reproduce the behavior:

  1. Add schuchard\prettier schematic using ng add through NRWL's Angular Console
  2. Configure prettier
  3. Remove tslint.json conflicts
  4. Run prettier NPM script from terminal inside project folder
  5. ISSUE: Script command stops and VS Code editor takes me to index.js.

Expected behavior
Expected all of the files inside my project to get pretty. :)

Screenshots
Here's a screenshot of where my editor takes me soon after running the command.
image

Desktop (please complete the following information):
Angular CLI: 7.3.5
Node: 10.13.0
OS: win32 x64
Angular: 7.2.9

Additional context
I removed @schuchard/prettier package from my dev dependencies in package.json file, while leaving the other packages that came from schematic intact, reinstalled prettier, and the NPM command worked. Strange...

Question: prettier.config.js

Is there a reason to use prettier.config.js over json format?

I wonder if I can change it to json format.

Thanks

Prettier --write found some errors

When I run the command npm run prettier or try to commit, this error happens. What could be wrong?

prettier --write found some errors. Please fix them and try committing again.

/../node_modules/.bin/prettier: linha 1: use strict: command not found
/../node_modules/.bin/prettier: linha 2: syntax error next to unexpected token `exports,'
/../node_modules/.bin/prettier: linha 2: `Object.defineProperty(exports, "__esModule", { value: true });'
husky > pre-commit hook failed (add --no-verify to bypass)

Log File:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'run', 'prettier' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prettier' ]
5 info lifecycle projectX~prettier: projectX
6 verbose lifecycle projectX~prettier: unsafe-perm in lifecycle true
7 verbose lifecycle projectX~prettier: PATH: /usr/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/opt/desenvolvimento/Trabalho/Pontta/projectX/node_modules/.bin:/home/arch/.yarn/bin:/home/arch/.config/yarn/global/node_modules/.bin:/home/arch/.npm-global/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/var/lib/flatpak/exports/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
8 verbose lifecycle projectX~prettier: CWD: /opt/desenvolvimento/Trabalho/Pontta/projectX
9 silly lifecycle projectX~prettier: Args: [ '-c',
9 silly lifecycle   'prettier --write "**/*.{js,json,css,md,ts,html,component.html}"' ]
10 silly lifecycle projectX~prettier: Returned: code: 2  signal: null
11 info lifecycle projectX~prettier: Failed to exec prettier script
12 verbose stack Error: projectX prettier: `prettier --write "**/*.{js,json,css,md,ts,html,component.html}"`
12 verbose stack Exit status 2
12 verbose stack     at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
12 verbose stack     at EventEmitter.emit (events.js:182:13)
12 verbose stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
12 verbose stack     at ChildProcess.emit (events.js:182:13)
12 verbose stack     at maybeClose (internal/child_process.js:978:16)
12 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:265:5)
13 verbose pkgid projectX
14 verbose cwd /opt/desenvolvimento/Trabalho/Pontta/projectX
15 verbose Linux 4.19.2-arch1-1-ARCH
16 verbose argv "/usr/bin/node" "/usr/bin/npm" "run" "prettier"
17 verbose node v11.2.0
18 verbose npm  v6.4.1
19 error code ELIFECYCLE
20 error errno 2
21 error projectX prettier: `prettier --write "**/*.{js,json,css,md,ts,html,component.html}"`
21 error Exit status 2
22 error Failed at the projectX prettier script.
22 error This is probably not a problem with npm. There is likely additional logging output above.
23 verbose exit [ 2, true ]

My prettier.config file

module.exports = {
	printWidth: 80,
	tabWidth: 2,
	useTabs: true,
	semi: false,
	singleQuote: true,
	trailingComma: 'none',
	bracketSpacing: true,
	jsxBracketSameLine: false,
	arrowParens: 'avoid',
	rangeStart: 0,
	rangeEnd: Infinity,
	parser: 'angular',
	requirePragma: false,
	insertPragma: false,
	proseWrap: 'always'
};

Error running command in powershell

Describe the bug

image

Changing single quotes for doubles in the script works

image

image

To Reproduce
Steps to reproduce the behavior:

  1. npm run prettier

Desktop (please complete the following information):

  • OS: Windows 10
  • Node Version: 8.12.0
  • Angular & Angular/CLI Version: 7.0.3 & 7.0.4

PD: Thanks for amazing tool ❤

Problem with tslint.json rules

Describe the bug
Hi, from a time to here @angular/cli adds to tslint.json "tslint: recommended", because of this all the rules are not deleted.

To Reproduce
Create a new angular app with @angular/cli

Expected behavior
Maybe add tslint-config-prettier?

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. macOS] Windows 10 64 bits
  • Node Version [e.g. 22] 8.12.0
  • Angular & Angular/CLI Version 7.3.4

Additional context
Add any other context about the problem here.

Error running command in root directory

Describe the bug
Received the error bellow after attempting to add through the command ng g @schuchard/prettier:add

Error:
Expected end of file, got ", { "extends": [ ..." at 108:1.

To Reproduce
Steps to reproduce the behavior:

  1. Install globally npm install -g @schuchard/prettier
  2. Run in an Angular CLI root directory ng g @schuchard/prettier:add
  3. Select options as shown in the screenshot below
  4. See error

Expected behavior
Process to complete without errors and the creation of prettier and tslint config files.

Screenshots
screen shot 2018-10-30 at 10 16 44 pm

Desktop
Angular CLI: 7.0.2
Node: 9.11.1
OS: darwin x64
Angular:
...

Package Version

@angular-devkit/architect 0.10.2
@angular-devkit/core 7.0.2
@angular-devkit/schematics 7.0.2
@schematics/angular 7.0.2
@schematics/update 0.10.2
rxjs 6.3.3
typescript 3.1.3

index.js Windows Script Host Syntax Error 800A03EA

Describe the bug
After upgrading my Ionic Angular project from 9 to 11 and replacing tslint with eslint I get a Windows Script Host error.

To Reproduce
Upgrade an existing Angular project from version 9 to 11 and replace tslint with eslint

Expected behavior
I don't expect that windows outputs an error.

Screenshots
windows_script_host

Desktop (please complete the following information):

  • OS: Windows 10
  • Node Version 12.16.2
  • Angular & Angular/CLI Version 11.0.5

Additional context
It seems like the real error is that the index.js file doesn't get executed by node?

Prettier fails in CI

Describe the bug

In have installed the schematic and added script: npm run prettier script to my Azure Pipeline.
It runs prettier -c \"**/*.{js,json,css,scss,less,md,ts,html,component.html}\" as configuring in package.json scripts.

This will call node_modules/.bin/prettier which is a symlink to index.js of this package.

The error in the pipeline I get is:

/home/vsts/work/1/s/src/node_modules/.bin/prettier: 1: use strict: not found
/home/vsts/work/1/s/src/node_modules/.bin/prettier: 2: Syntax error: word unexpected (expecting ")")

It seems that it is trying to execute the Node script as bash.

I can reproduce it by running npm ci locally and then npm run prettier.
When I add this to index.js:1 manually, everything works:

#!/usr/bin/env node

Can you reproduce this? If this is really a bug, the above solution might work for everyone.

Simplified tslint.json causes “Cannot convert undefined or null to object” error

Describe the bug
When airbnb rules are used, and tslint.json is simplified to:
{ "extends": "tslint-config-airbnb", "rulesDirectory": ["node_modules/codelyzer"] }

then ng g @schuchard/prettier:add commands shows “Cannot convert undefined or null to object” error after answering all questions.

To Reproduce
Steps to reproduce the behavior:

  1. Replace tslint.json with the content mentioned above.
  2. Run npm install @schuchard/prettier@latest --save-dev
  3. Run npx ng g @schuchard/prettier:add and answer all questions.
  4. See “Cannot convert undefined or null to object” error.

Expected behavior
No error, tslint.json updated if necessary.

Desktop (please complete the following information):

  • OS: Windows
  • Node Version: v10.13.0
  • Angular & Angular/CLI Version: 7.1.3

Add CLI builder

Is your feature request related to a problem? Please describe.

Run prettier check automatically during ng lint

Describe the solution you'd like

I think that a CLI builder can be setup to run prettier check on ng lint

https://angular.io/guide/cli-builder

angularfire does something similar with ng deploy

https://github.com/angular/angularfire

Describe alternatives you've considered

An alternative would be to modify package.json so that lint is ng lint && npm run prettier.

Remove jasmine as a dependency

I cannot build our angular project due to a clash of types between jasmine and jest. Jasmine is added as a dependency and therefore installed when the package is installed. Jasmine should be installed as a devdep rather than a dep.

Broken by husky v5/6

Describe the bug

In the current configuration, the pre-commit hook doesn't work if version 5 or above of husky is installed

To Reproduce
Steps to reproduce the behavior:

  1. ng new an app
  2. run the schematic
  3. create a scss file that has text on the last line
  4. commit

Expected behavior
Prettier should add an empty line to the end of the scss file

Actual
Prettier does not run.

Screenshots
If applicable, add screenshots to help explain your problem.

Was able to fix by installing husky@4 (installed v4.3.8)

Only CSS file suffix included by default, even though project uses preprocessor

Describe the bug
css file suffix only is mentioned in the prettier npm script

To Reproduce
Steps to reproduce the behaviour:

  1. run this schematic on an Angular project using SASS/SCSS
  2. open package.json
  3. see the prettier script`

Expected behaviour
It should detect what is the project style preprocessor and use its file suffix along with CSS

Desktop (please complete the following information):

  • all Angular & Angular/CLI Versions

Additional question:
Why is there component.html alongside with html? It does not seem to have any additional effect.

Prettier --write failing in Local machine

Screenshot 2021-07-20 at 8 14 30 PM

My package Json file { "name": "salesforce-app", "private": true, "version": "1.0.0", "description": "Salesforce App", "scripts": { "lint": "npm run lint:aura && npm run lint:lwc", "lint:aura": "eslint --fix **/aura/**/*", "lint:lwc": "eslint --fix **/lwc/**/*", "test": "npm run test:unit", "test:unit": "sfdx-lwc-jest", "test:unit:watch": "sfdx-lwc-jest --watch", "test:unit:debug": "sfdx-lwc-jest --debug", "test:unit:coverage": "sfdx-lwc-jest --coverage", "prettier": "prettier --write \"force-app/main/default/{aura,classes,lwc}/**/*.{css,js,cmp,xml,cls,html}\"", "prettier:verify": "prettier --list-different \"force-app/main/default/{aura,classes,lwc}/**/*.{css,js,cmp,xml,cls,html}\"", "prepare": "husky install" }, "devDependencies": { "@lwc/eslint-plugin-lwc": "^1.0.1", "@prettier/plugin-xml": "^0.13.1", "@salesforce/eslint-config-lwc": "^2.0.0", "@salesforce/eslint-plugin-aura": "^2.0.0", "@salesforce/eslint-plugin-lightning": "^0.1.1", "@salesforce/sfdx-lwc-jest": "^0.12.6", "eslint": "^7.29.0", "eslint-config-airbnb-base": "^14.2.1", "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.23.4", "eslint-plugin-jest": "^24.3.6", "eslint-plugin-prettier": "^3.4.0", "husky": "^7.0.0", "lint-staged": "^11.0.0", "prettier": "^2.3.2", "prettier-plugin-apex": "^1.10.0" }, "lint-staged": { "force-app/main/default/{aura,classes,lwc}/**/*.{css,js,cmp,xml,cls,html}": [ "prettier --write" ], "force-app/main/default/{aura,lwc}/**/*": [ "eslint --fix" ] } }

When I stage my changes and run commit --amend, prettier --write is running, while running it getting failed

Node version 14 and npm 6 version i have

use strict: command not found when @schuchard/prettier installed after prettier.

Describe the bug
The bug manifests when @schuchard/prettier is installed after prettier. In my scenario, I was doing it through npm ci on a build machine, but you can run npm i prettier && npm i @schuchard/prettier to replicate it.

The output is:

$ npx prettier --write "**/*.{js,json,css,md,ts,html,component.html}"
/source/test/node_modules/.bin/prettier: line 1: use strict: command not found
/source/test/node_modules/.bin/prettier: line 2: syntax error near unexpected token `exports,'
/source/test/node_modules/.bin/prettier: line 2: `Object.defineProperty(exports, "__esModule", { value: true });'

To Reproduce
Steps to reproduce the behavior:

  1. npm i prettier && npm i @schuchard/prettier
  2. npx prettier --write "**/*.{js,json,css,md,ts,html,component.html}"
  3. See error

Expected behavior
Prettier runs as expected

Desktop (please complete the following information):

  • OS: MacOS
  • Node Version 12
  • Angular & Angular/CLI Version

Additional context
From my investigation, it appears that this library is overwriting the symlink for prettier in node_modules/.bin/prettier.

By doing an ls -ahl node_modules/.bin/prettier you can see where the symlinks point.

After prettier is installed: prettier -> ../prettier/bin-prettier.js after
After @schuchard/prettier is installed: prettier -> ../@schuchard/prettier/src/prettier/index.js

Now I'm not sure what the actual bug is. Whether it is that the prettier symlink is being overwritten, or that the script file that it is linking to doesn't have the proper #!/usr/bin/env node as the npm bin docs says to do.

My intuition is that the prettier library bin symlink shouldn't be overwritten.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.