forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreformat-files.js
64 lines (52 loc) · 1.43 KB
/
reformat-files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/** @format */
/**
* External dependencies
*/
const fs = require( 'fs' );
const glob = require( 'glob' );
const ignore = require( 'ignore' );
const path = require( 'path' );
const prettier = require( 'prettier' );
/**
* Internal dependencies
*/
const shouldFormat = require( './utils/should-format' );
/**
* Module constants
*/
const defaultPrettierConfig = undefined;
const sassPrettierConfig = { parser: 'scss' };
/**
* Find all JS and JSX files in the project that have the @format tag
* and reformat them with Prettier. Useful when upgrading Prettier to
* a newer version.
*/
// Load ignore file
const ignoreFile = fs.readFileSync( '.eslintignore', 'utf-8' );
const ig = ignore().add( ignoreFile );
// List .js and .jsx files in the current directory
const files = glob.sync( '**/*.{js,jsx,scss}', {
ignore: 'node_modules/**', // ignore node_modules by default
nodir: true,
} );
files.forEach( file => {
// skip ignored files
if ( ig.ignores( file ) ) {
return;
}
const text = fs.readFileSync( file, 'utf8' );
// skip if the first docblock doesn't have @format tag
if ( ! shouldFormat( text ) ) {
return;
}
const formattedText = prettier.format(
text,
file.endsWith( '.scss' ) ? sassPrettierConfig : defaultPrettierConfig
);
// did the re-formatting change anything?
if ( formattedText === text ) {
return;
}
fs.writeFileSync( file, formattedText );
console.log( `Prettier reformatted file: ${ file }` );
} );