Skip to content

Commit

Permalink
fix(copy-values): write results to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Aug 23, 2017
1 parent 5efe240 commit a641de4
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/copy-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ import path from 'path';
import chalk from 'chalk';
import { get, set } from 'lodash';
import getFiles from './lib/get-files';
import writeJson from './lib/write-json';

const getPackage = location => ({ location, json: require(location) });
const formatValue = value => JSON.stringify(value);
const toJson = value => JSON.stringify(value);

const reportChange = (key, previous, next) => {
if (toJson(previous) === toJson(next)) {
console.log(`${key}: ${chalk.green('✓ unchanged')}`);
} else {
console.log(`${key}: ${chalk.red(toJson(previous))}${chalk.green(toJson(next))}`);
}
};

export default async ({ keys, packagesPattern, sourcePattern }) => {
const [source] = (await getFiles(sourcePattern)).map(getPackage);
const packages = (await getFiles(packagesPattern)).map(getPackage);

packages.forEach(pkg => {
console.log(chalk.grey.underline(path.relative(process.cwd(), pkg.location)));
keys.forEach(key => {
const value = get(source.json, key);
const previousValue = formatValue(get(pkg.json, key));
const previousValue = get(pkg.json, key);
set(pkg.json, key, value);
const nextValue = formatValue(value);
if (previousValue === nextValue) {
console.log(`${key}: ${chalk.green('✓ unchanged')}`);
} else {
console.log(`${key}: ${chalk.red(previousValue)}${chalk.green(nextValue)}`);
}
writeJson(pkg.location, pkg.json);
reportChange(key, previousValue, value);
});
});
};

0 comments on commit a641de4

Please sign in to comment.