diff options
Diffstat (limited to 'scripts/frontend/prettier.js')
-rw-r--r-- | scripts/frontend/prettier.js | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/scripts/frontend/prettier.js b/scripts/frontend/prettier.js index 39de77bc333..6e4e36b9b2d 100644 --- a/scripts/frontend/prettier.js +++ b/scripts/frontend/prettier.js @@ -9,6 +9,8 @@ const getStagedFiles = require('./frontend_script_utils').getStagedFiles; const mode = process.argv[2] || 'check'; const shouldSave = mode === 'save' || mode === 'save-all'; const allFiles = mode === 'check-all' || mode === 'save-all'; +let dirPath = process.argv[3] || ''; +if (dirPath && dirPath.charAt(dirPath.length - 1) !== '/') dirPath += '/'; const config = { patterns: ['**/*.js', '**/*.vue', '**/*.scss'], @@ -39,9 +41,10 @@ prettierIgnore.add( const availableExtensions = Object.keys(config.parsers); -console.log(`Loading ${allFiles ? 'All' : 'Staged'} Files ...`); +console.log(`Loading ${allFiles ? 'All' : 'Selected'} Files ...`); -const stagedFiles = allFiles ? null : getStagedFiles(availableExtensions.map(ext => `*.${ext}`)); +const stagedFiles = + allFiles || dirPath ? null : getStagedFiles(availableExtensions.map(ext => `*.${ext}`)); if (stagedFiles) { if (!stagedFiles.length || (stagedFiles.length === 1 && !stagedFiles[0])) { @@ -60,6 +63,13 @@ if (allFiles) { const patterns = config.patterns; const globPattern = patterns.length > 1 ? `{${patterns.join(',')}}` : `${patterns.join(',')}`; files = glob.sync(globPattern, { ignore }).filter(f => allFiles || stagedFiles.includes(f)); +} else if (dirPath) { + const ignore = config.ignore; + const patterns = config.patterns.map(item => { + return dirPath + item; + }); + const globPattern = patterns.length > 1 ? `{${patterns.join(',')}}` : `${patterns.join(',')}`; + files = glob.sync(globPattern, { ignore }); } else { files = stagedFiles.filter(f => availableExtensions.includes(f.split('.').pop())); } @@ -73,12 +83,11 @@ if (!files.length) { console.log(`${shouldSave ? 'Updating' : 'Checking'} ${files.length} file(s)`); -prettier - .resolveConfig('.') - .then(options => { - console.log('Found options : ', options); - files.forEach(file => { - try { +files.forEach(file => { + try { + prettier + .resolveConfig(file) + .then(options => { const fileExtension = file.split('.').pop(); Object.assign(options, { parser: config.parsers[fileExtension], @@ -101,17 +110,17 @@ prettier } console.log(`Prettify Manually : ${file}`); } - } catch (error) { - didError = true; - console.log(`\n\nError with ${file}: ${error.message}`); - } - }); - - if (didWarn || didError) { - process.exit(1); - } - }) - .catch(e => { - console.log(`Error on loading the Config File: ${e.message}`); - process.exit(1); - }); + }) + .catch(e => { + console.log(`Error on loading the Config File: ${e.message}`); + process.exit(1); + }); + } catch (error) { + didError = true; + console.log(`\n\nError with ${file}: ${error.message}`); + } +}); + +if (didWarn || didError) { + process.exit(1); +} |