UNPKG

@interaktiv/dia-scripts

Version:

CLI toolbox with common scripts for most sort of projects at DIA

37 lines (27 loc) 1.7 kB
"use strict"; const path = require('path'); const exit = require('exit'); const spawn = require('cross-spawn'); const yargsParser = require('yargs-parser'); const { hasPkgProp, resolveBin, hasFile } = require('../utils'); const args = process.argv.slice(2); const here = p => path.join(__dirname, p); const hereRelative = p => here(p).replace(process.cwd(), '.'); const parsedArgs = yargsParser(args); const useBuiltinConfig = args.includes('--config') === false && hasFile('.prettierrc') === false && hasFile('.prettierrc.js') === false && hasFile('prettier.config.js') === false && hasPkgProp('prettierrc') === false; const config = useBuiltinConfig ? ['--config', hereRelative('../config/prettierrc.js')] : []; const useBuiltinIgnore = args.includes('--ignore-path') === false && hasFile('.prettierignore') === false; const ignore = useBuiltinIgnore ? ['--ignore-path', require.resolve('@interaktiv/prettier-config-dia/.prettierignore')] : []; const write = ['--no-write', '--check', '--list-different'].some(flag => args.includes(flag)) ? [] : ['--write']; // This ensures that when running format as a pre-commit hook and we get the // full file path, we make that non-absolute so it is treated as a glob, // This way the prettierignore will be applied const relativeArgs = args.map(a => a.replace(`${process.cwd()}/`, '')); const filesToApply = parsedArgs._.length > 0 ? [] : ['**/*.+(js|jsx|json|vue|less|scss|sass|css|ts|tsx|md|mdown|markdown|mdx|cls|class|trg|trigger|apx|apex|graphql|yml|yaml|cmp|component)']; const result = spawn.sync(resolveBin('prettier'), [...config, ...ignore, ...write, ...filesToApply].concat(relativeArgs), { stdio: 'inherit' }); exit(result.status);