fixpack
Advanced tools
Comparing version 3.0.6 to 4.0.0
@@ -42,3 +42,7 @@ { | ||
], | ||
"wipe": false | ||
"dryRun": false, | ||
"wipe": false, | ||
"indent": null, | ||
"newLine": null, | ||
"finalNewLine": null | ||
} |
@@ -5,3 +5,2 @@ #!/usr/bin/env node | ||
const os = require('os') | ||
const ALCE = require('alce') | ||
@@ -11,6 +10,11 @@ const fs = require('fs') | ||
const chalk = require('chalk') | ||
const detectIndent = require('detect-indent') | ||
const detectNewline = require('detect-newline') | ||
const defaultConfig = require('./config') | ||
function checkMissing(pack, config) { | ||
const CRLF = '\r\n' | ||
const LF = '\n' | ||
function checkMissing (pack, config) { | ||
let warnItems | ||
@@ -25,7 +29,6 @@ let required | ||
} | ||
required.forEach(function(key) { | ||
if (pack[key] == null) | ||
throw new Error(config.fileName + ' files must have a ' + key) | ||
required.forEach(function (key) { | ||
if (pack[key] == null) { throw new Error(config.fileName + ' files must have a ' + key) } | ||
}) | ||
warnItems.forEach(function(key) { | ||
warnItems.forEach(function (key) { | ||
if (pack[key] == null && !config.quiet) console.log(chalk.yellow('missing ' + key)) | ||
@@ -35,3 +38,3 @@ }) | ||
function sortAlphabetically(object) { | ||
function sortAlphabetically (object) { | ||
if (Array.isArray(object)) { | ||
@@ -44,3 +47,3 @@ object.sort() | ||
.sort() | ||
.forEach(function(key) { | ||
.forEach(function (key) { | ||
sorted[key] = object[key] | ||
@@ -52,10 +55,22 @@ }) | ||
module.exports = function(file, config) { | ||
module.exports = function (file, config) { | ||
config = Object.assign(defaultConfig, config || {}) | ||
if (!fs.existsSync(file)) { | ||
if (!config.quiet) console.log(chalk.red('No such file: ' + file)) | ||
process.exit(1) | ||
return 1 | ||
} | ||
config.fileName = path.basename(file) | ||
const original = fs.readFileSync(file, { encoding: 'utf8' }) | ||
const indent = config.indent != null | ||
? config.indent | ||
: detectIndent(original).indent | ||
const newLine = config.newLine != null | ||
? ( | ||
config.newLine === 'CRLF' | ||
? CRLF | ||
: LF | ||
) : detectNewline(original) | ||
const finalNewLine = config.finalNewLine != null | ||
? !!config.finalNewLine | ||
: /\n$/.test(original) | ||
const out = {} | ||
@@ -70,3 +85,3 @@ let pack = ALCE.parse(original) | ||
// handle the specific ones we want, then remove | ||
config.sortToTop.forEach(function(key) { | ||
config.sortToTop.forEach(function (key) { | ||
if (pack[key] != null) out[key] = pack[key] | ||
@@ -89,3 +104,3 @@ delete pack[key] | ||
// sort some sub items alphabetically | ||
config.sortedSubItems.forEach(function(key) { | ||
config.sortedSubItems.forEach(function (key) { | ||
if (out[key] != null) out[key] = sortAlphabetically(out[key]) | ||
@@ -102,3 +117,3 @@ }) | ||
] | ||
versionedKeys.forEach(function(key) { | ||
versionedKeys.forEach(function (key) { | ||
const depGroup = out[key] | ||
@@ -114,16 +129,22 @@ if (depGroup) { | ||
// write it out | ||
outputString = JSON.stringify(out, null, 2) + os.EOL | ||
outputString = JSON.stringify(out, null, indent) | ||
if (outputString !== original) { | ||
fs.writeFileSync(file, outputString, { encoding: 'utf8' }) | ||
if (!config.quiet) { | ||
console.log(chalk.bold(config.fileName) + chalk.green(' fixed') + '!') | ||
} | ||
if (newLine === CRLF) { | ||
outputString = outputString.replace(/\n/g, CRLF) + (finalNewLine ? CRLF : '') | ||
} else { | ||
if (!config.quiet) { | ||
console.log( | ||
chalk.bold(config.fileName) + chalk.green(' already clean') + '!' | ||
) | ||
} | ||
outputString = outputString + (finalNewLine ? LF : '') | ||
} | ||
if (outputString === original) { | ||
config.quiet || console.log(chalk.bold(config.fileName) + chalk.green(' already clean') + '!') | ||
return 0 | ||
} | ||
if (config.dryRun) { | ||
config.quiet || console.log(chalk.bold(config.fileName) + chalk.red(' not fixed') + '!') | ||
} else { | ||
fs.writeFileSync(file, outputString, { encoding: 'utf8' }) | ||
config.quiet || console.log(chalk.bold(config.fileName) + chalk.green(' fixed') + '!') | ||
} | ||
return 1 | ||
} |
{ | ||
"name": "fixpack", | ||
"description": "cli tool that cleans up package.json files.", | ||
"version": "3.0.6", | ||
"version": "4.0.0", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -12,4 +12,6 @@ "bin": "./bin/fixpack", | ||
"dependencies": { | ||
"alce": "1.0.0", | ||
"chalk": "^2.4.1", | ||
"alce": "1.2.0", | ||
"chalk": "^3.0.0", | ||
"detect-indent": "^6.0.0", | ||
"detect-newline": "^3.1.0", | ||
"extend-object": "^1.0.0", | ||
@@ -16,0 +18,0 @@ "rc": "^1.2.8" |
@@ -24,5 +24,6 @@ # fixpack | ||
- homepage | ||
- license' | ||
- license | ||
Fix all indenting to 2 spaces. | ||
Maintain current indentation, End of Line, and Final New Line, or set them to your configured value | ||
(see [configuration](#configuration)). | ||
@@ -33,2 +34,4 @@ Oh, and it will tolerate improperly quoted and comma'd JSON thanks to [ALCE](https://npmjs.org/package/alce). | ||
Oh, and it will exit with `0` when already fixed or with `1` otherwise (so combined with `--dryRun` flag it can be used as CI check) | ||
## Usage | ||
@@ -103,2 +106,4 @@ | ||
files: ['package.json'], | ||
// Will not fix file, only inform if is fixed | ||
dryRun: false, | ||
// Will set all deps to '*' | ||
@@ -108,3 +113,13 @@ // this may be useful because then you can | ||
// to install latest stable releases of everything. | ||
wipe: false | ||
wipe: false, | ||
// Sets the expected indentation. If number, is number of spaces, | ||
// otherwise can be string to use as indentation (like a tab). | ||
// if undefined/null (default), indentation is detected from file and preserved. | ||
indent: null, | ||
// Sets line endings to be either "LF" or "CRLF" | ||
// if undefined/null (default), newLine is detected from file and preserved. | ||
newLine: null, | ||
// Boolean if there should be an empty line at the end of the file. | ||
// if undefined/null (default), finalNewLine is detected from file and preserved. | ||
finalNewLine: null | ||
} | ||
@@ -111,0 +126,0 @@ |
Sorry, the diff of this file is not supported yet
9885
169
148
6
+ Addeddetect-indent@^6.0.0
+ Addeddetect-newline@^3.1.0
+ Addedalce@1.2.0(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedchalk@3.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddetect-indent@6.1.0(transitive)
+ Addeddetect-newline@3.1.0(transitive)
+ Addedesprima@1.2.5(transitive)
+ Addedestraverse@1.9.3(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedsupports-color@7.2.0(transitive)
- Removedalce@1.0.0(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesprima@1.0.4(transitive)
- Removedestraverse@1.3.2(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedsupports-color@5.5.0(transitive)
Updatedalce@1.2.0
Updatedchalk@^3.0.0