Skip to content

Commit

Permalink
feature(putout) add support of codemods
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jan 13, 2019
1 parent b2b9557 commit 1e656c4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/putout/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ For example if you need to `remove-something` create `putout` plugin with name `
}
```

## Codemods

`putout` supports `codemodes` in the similar to plugins way, just create a directory `~/.putout` and put your plugins there. Here is example: [convert-tape-to-supertape](https://github.com/coderaiser/putout/tree/master/codemods/plugin-convert-tape-to-supertape).

## Why?

- because [eslint](https://eslint.org) avoids [fixes that could change the runtime behavior](https://eslint.org/docs/developer-guide/working-with-rules#applying-fixes).
Expand Down
38 changes: 35 additions & 3 deletions packages/putout/bin/putout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
const {
readFileSync,
writeFileSync,
readdirSync,
statSync,
} = require('fs');

Expand All @@ -21,6 +22,7 @@ const {
bold,
} = require('chalk');

const once = require('once');
const glob = require('glob');
const tryCatch = require('try-catch');
const deepmerge = require('deepmerge');
Expand All @@ -33,6 +35,7 @@ const {cwd} = process;

const putout = require('..');
const parseMatch = require('../lib/parse-match');
const readCodeMods = once(_readCodeMods);

const one = (f) => (a) => f(a);

Expand Down Expand Up @@ -107,7 +110,7 @@ function processFiles(name) {

const [e, result] = tryCatch(putout, input, {
fix,
...merge(options, parseMatch(match, name)),
...merge(readCodeMods(), options, parseMatch(match, name)),
});

if (e) {
Expand Down Expand Up @@ -204,9 +207,9 @@ function exit(e) {
process.exit(1);
}

function merge(base, custom) {
function merge(...args) {
const arrayMerge = (destinationArray, sourceArray) => sourceArray;
return deepmerge(base, custom, {
return deepmerge.all(args, {
arrayMerge,
});
}
Expand All @@ -232,3 +235,32 @@ function getOptions() {
return require(infoPath).putout;
}

function _readCodeMods() {
const {join} = require('path');
const {homedir} = require('os');

const dir = join(homedir(), '.putout');
const [e, names] = tryCatch(readdirSync, dir);

if (e)
return {};

const plugins = [];

console.log(names);

for (const name of names) {
const full = join(dir, name);
const plugin = require(full);
const shortName = name.replace('putout-plugin-');

plugins.push({
[shortName]: plugin
});
}

return {
plugins,
}
}

0 comments on commit 1e656c4

Please sign in to comment.