@mixer/parallel-prettier
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -17,11 +17,14 @@ #!/usr/bin/env node | ||
.option('--concurrency [value]', 'Maximum concurrency', String(os_1.cpus().length)) | ||
.option('--quiet, -q', 'If set, pprettier will not output progress') | ||
.option('-q, --quiet', 'If set, pprettier will not output progress') | ||
.option('--ignore-path [value]', 'Path to an ignore file', '.prettierignore') | ||
.version(`@mixer/parallel-prettier version ${version} / prettier version ${prettier.version}`) | ||
.parse(process.argv); | ||
const opts = program.opts(); | ||
require('./master').spawnWorkers({ | ||
check: program.listDifferent, | ||
concurrency: program.concurrency, | ||
check: opts.listDifferent, | ||
concurrency: opts.concurrency, | ||
files: program.args, | ||
quiet: program.quiet, | ||
write: program.write, | ||
quiet: opts.quiet, | ||
write: opts.write, | ||
ignorePath: opts.ignorePath, | ||
}); | ||
@@ -28,0 +31,0 @@ } |
@@ -5,14 +5,31 @@ "use strict"; | ||
*--------------------------------------------------------*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.spawnWorkers = void 0; | ||
const globStream = require("glob-stream"); | ||
const ignore_1 = require("ignore"); | ||
const rxjs_1 = require("rxjs"); | ||
const operators_1 = require("rxjs/operators"); | ||
const fs_1 = require("fs"); | ||
const progress_reporter_1 = require("./progress-reporter"); | ||
const worker_pool_1 = require("./worker-pool"); | ||
const path_1 = require("path"); | ||
const bufferSize = 50; | ||
function runGlobs(files) { | ||
return new rxjs_1.Observable(subscriber => { | ||
function runGlobs(files, ignore) { | ||
return new rxjs_1.Observable((subscriber) => { | ||
const stream = globStream(files); | ||
stream.addListener('data', data => subscriber.next(data)); | ||
stream.addListener('error', err => subscriber.error(err)); | ||
stream.addListener('data', (data) => { | ||
if (!ignore.ignores(path_1.relative(data.cwd, data.path))) { | ||
subscriber.next(data); | ||
} | ||
}); | ||
stream.addListener('error', (err) => subscriber.error(err)); | ||
stream.addListener('finish', () => subscriber.complete()); | ||
@@ -22,20 +39,32 @@ stream.resume(); | ||
} | ||
function spawnWorkers(options) { | ||
const pool = new worker_pool_1.WorkerPool(options); | ||
const progress = new progress_reporter_1.ProgressReporter(options.quiet, options.check); | ||
runGlobs(options.files) | ||
.pipe(operators_1.bufferCount(bufferSize), operators_1.mergeMap(files => pool.format(files))) | ||
.subscribe(result => progress.update(result), err => { | ||
throw err; | ||
}, () => { | ||
progress.complete(); | ||
if (progress.reformatted && options.check) { | ||
process.exit(1); | ||
function getIgnore(ignorePath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
return ignore_1.default().add(yield fs_1.promises.readFile(ignorePath, 'utf-8')); | ||
} | ||
else { | ||
process.exit(0); | ||
catch (e) { | ||
return ignore_1.default(); | ||
} | ||
}); | ||
} | ||
function spawnWorkers(options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const pool = new worker_pool_1.WorkerPool(options); | ||
const progress = new progress_reporter_1.ProgressReporter(options.quiet, options.check); | ||
runGlobs(options.files, yield getIgnore(options.ignorePath)) | ||
.pipe(operators_1.bufferCount(bufferSize), operators_1.mergeMap((files) => pool.format(files), pool.maxSize * 2)) | ||
.subscribe((result) => progress.update(result), (err) => { | ||
throw err; | ||
}, () => { | ||
progress.complete(); | ||
if ((progress.reformatted && options.check) || progress.failed) { | ||
process.exit(1); | ||
} | ||
else { | ||
process.exit(0); | ||
} | ||
}); | ||
}); | ||
} | ||
exports.spawnWorkers = spawnWorkers; | ||
//# sourceMappingURL=master.js.map |
@@ -6,2 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ProgressReporter = void 0; | ||
const ora = require("ora"); | ||
@@ -17,2 +18,3 @@ const path_1 = require("path"); | ||
this.reformatted = 0; | ||
this.failed = 0; | ||
if (!quiet) { | ||
@@ -28,2 +30,3 @@ this.spinner = ora('Starting...').start(); | ||
this.reformatted += results.formatted.length; | ||
this.failed += results.failed.length; | ||
if (results.formatted.length) { | ||
@@ -30,0 +33,0 @@ if (this.spinner) { |
@@ -6,5 +6,12 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.WorkerPool = exports.WorkerExitedError = void 0; | ||
const cluster = require("cluster"); | ||
const rxjs_1 = require("rxjs"); | ||
const operators_1 = require("rxjs/operators"); | ||
class WorkerExitedError extends Error { | ||
constructor(codeOrSignal) { | ||
super(`Worker exited with unexpected ${codeOrSignal} code`); | ||
} | ||
} | ||
exports.WorkerExitedError = WorkerExitedError; | ||
/** | ||
@@ -20,2 +27,8 @@ * Pool of workers. | ||
/** | ||
* Maximum size of the worker pool. | ||
*/ | ||
get maxSize() { | ||
return this.options.concurrency; | ||
} | ||
/** | ||
* Schedules the given files to be formatted. | ||
@@ -30,5 +43,7 @@ */ | ||
target.active++; | ||
target.worker.send({ type: 1 /* WorkerFiles */, files, id }); | ||
this.sortWorkers(); | ||
return rxjs_1.fromEvent(target.worker, 'message').pipe(operators_1.map(([m]) => m), operators_1.filter(m => m.id === id), operators_1.take(1), operators_1.tap(() => { | ||
return target.worker.pipe(operators_1.switchMap((worker) => { | ||
worker.send({ type: 1 /* WorkerFiles */, files, id }); | ||
return rxjs_1.fromEvent(worker, 'message'); | ||
}), operators_1.map(([m]) => m), operators_1.filter((m) => m.id === id), operators_1.take(1), operators_1.tap(() => { | ||
target.active--; | ||
@@ -42,5 +57,8 @@ this.sortWorkers(); | ||
spawnWorker() { | ||
const worker = { worker: cluster.fork(), active: 0 }; | ||
this.workers.unshift(worker); | ||
worker.worker.send({ | ||
const worker = cluster.fork(); | ||
const subject = new rxjs_1.BehaviorSubject(worker); | ||
this.workers.unshift({ worker: subject, active: 0 }); | ||
worker.on('exit', (code, signal) => subject.error(new WorkerExitedError(code !== null && code !== void 0 ? code : signal))); | ||
worker.on('error', (err) => subject.error(err)); | ||
worker.send({ | ||
mode: this.options.check | ||
@@ -47,0 +65,0 @@ ? 2 /* Assert */ |
@@ -15,2 +15,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.startWorker = void 0; | ||
const fs_1 = require("fs"); | ||
@@ -21,4 +22,2 @@ const prettier = require("prettier"); | ||
const util_1 = require("util"); | ||
const readFileAsync = util_1.promisify(fs_1.readFile); | ||
const writeFileAsync = util_1.promisify(fs_1.writeFile); | ||
/** | ||
@@ -33,2 +32,3 @@ * Reads the files from the observable stream and, with the specified | ||
formatted: [], | ||
failed: [], | ||
id: files.id, | ||
@@ -38,4 +38,12 @@ type: 2 /* Formatted */, | ||
return rxjs_1.of(...files.files).pipe(operators_1.mergeMap((file) => __awaiter(this, void 0, void 0, function* () { | ||
const contents = yield readFileAsync(file.path, 'utf-8'); | ||
const formatted = prettier.format(contents, Object.assign(Object.assign({}, (yield prettier.resolveConfig(file.path))), { filepath: file.path })); | ||
const contents = yield fs_1.promises.readFile(file.path, 'utf-8'); | ||
let formatted; | ||
try { | ||
formatted = prettier.format(contents, Object.assign(Object.assign({}, (yield prettier.resolveConfig(file.path))), { filepath: file.path })); | ||
} | ||
catch (e) { | ||
process.stderr.write('\r\n' + util_1.inspect(e) + '\r\n'); | ||
output.failed.push(file); | ||
return output; | ||
} | ||
if (formatted === contents) { | ||
@@ -45,3 +53,3 @@ return output; | ||
if (settings.mode === 0 /* Write */) { | ||
yield writeFileAsync(file.path, formatted); | ||
yield fs_1.promises.writeFile(file.path, formatted); | ||
} | ||
@@ -68,7 +76,7 @@ else if (settings.mode === 1 /* Print */) { | ||
}); | ||
rxjs_1.combineLatest(settings, files) | ||
rxjs_1.combineLatest([settings, files]) | ||
.pipe(operators_1.mergeMap(([s, f]) => runFormatting(s, f))) | ||
.subscribe( | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
message => process.send(message), err => { | ||
(message) => process.send(message), (err) => { | ||
throw err; | ||
@@ -75,0 +83,0 @@ }); |
{ | ||
"name": "@mixer/parallel-prettier", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Concurrent prettier runner", | ||
@@ -12,2 +12,3 @@ "main": "./dist/index.js", | ||
"build": "tsc", | ||
"hello": "node --inspect=9229 hello.js", | ||
"fmt": "tsc && node dist/index --write \"src/**/*.ts\" && npm run test:lint -- --fix", | ||
@@ -35,8 +36,9 @@ "test": "npm run test:lint && npm run test:fmt", | ||
"dependencies": { | ||
"chalk": "^4.0.0", | ||
"commander": "^5.0.0", | ||
"chalk": "^4.1.0", | ||
"commander": "^7.0.0", | ||
"glob-stream": "^6.1.0", | ||
"ora": "^4.0.3", | ||
"ignore": "^5.1.8", | ||
"ora": "^5.3.0", | ||
"prettier": "^2.0.4", | ||
"rxjs": "^6.5.5" | ||
"rxjs": "^6.6.3" | ||
}, | ||
@@ -48,9 +50,8 @@ "peerDependencies": { | ||
"@types/glob-stream": "^6.1.0", | ||
"@types/node": "^12.0.0", | ||
"@types/prettier": "^2.0.0", | ||
"@typescript-eslint/eslint-plugin": "^2.27.0", | ||
"@typescript-eslint/parser": "^2.27.0", | ||
"eslint": "^6.8.0", | ||
"eslint-plugin-header": "^3.0.0", | ||
"typescript": "^3.8.3" | ||
"@types/node": "^14.14.22", | ||
"@types/prettier": "^2.1.6", | ||
"@typescript-eslint/eslint-plugin": "^4.14.0", | ||
"@typescript-eslint/parser": "^4.14.0", | ||
"eslint": "^7.18.0", | ||
"typescript": "^4.1.3" | ||
}, | ||
@@ -57,0 +58,0 @@ "prettier": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7
24643
8
15
321
1
+ Addedignore@^5.1.8
+ Addedbase64-js@1.5.1(transitive)
+ Addedbl@4.1.0(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedcommander@7.2.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedis-unicode-supported@0.1.0(transitive)
+ Addedlog-symbols@4.1.0(transitive)
+ Addedora@5.4.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.23.0.0(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcommander@5.1.0(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedlog-symbols@3.0.0(transitive)
- Removedmute-stream@0.0.8(transitive)
- Removedora@4.1.1(transitive)
- Removedsupports-color@5.5.0(transitive)
Updatedchalk@^4.1.0
Updatedcommander@^7.0.0
Updatedora@^5.3.0
Updatedrxjs@^6.6.3