masnagam/npm-diet

View on GitHub
bin/npm-diet-delta

Summary

Maintainability
Test Coverage
#!/usr/bin/env node

// Copyright 2018 Masayuki Nagamachi <masayuki.nagamachi@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const program = require('commander');
const { promisify } = require('util');
const fs = require('fs');
const streamToString = require('stream-to-string');
const lib = require('../lib');

const readFile = promisify(fs.readFile);

const HELP = `
  Description:

    This command computes delta values between two analyses, and outputs a
    JSON-formatted analysis result to STDOUT.

    Analyses can be loaded from STDIN and/or files.  The following commands make
    the same result.

      $ (npm-diet measure chalk ; npm-diet measure colors) | \\
          npm-diet delta --stdin
      $ npm-diet measure chalk | npm-diet delta --stdin colors.measure.json
      $ npm-diet delta chalk.measure.json colors.measure.json

    In the all cases above, this command uses the analysis result of 'chalk' as
    the baseline values.
`;

program
  .description(
    'Compute a difference from a baselin package set')
  .option(
    '--stdin',
    'read package specifiers from STDIN before reading from arguments')
  .arguments(
    '[baseline] [subject]')
  .allowUnknownOption()  // for '--no-color'
  .on('--help', () => console.log(HELP))
  .action(async (baselineFile, subjectFile, options) => {
    let baselineJson, subjectJson;
    if (options.stdin) {
      [baselineJson, subjectJson] =
        (await streamToString(process.stdin)).split('\n');
      if (baselineJson === undefined) {
        throw new Error('no baseline data read from STDIN');
      }
      if (baselineFile) {
        if (baselineFile) {
          subjectJson = await readFile(baselineFile, { encoding: 'utf8' });
        }
      }
      if (subjectJson === undefined) {
        throw new Error('no subject data')
      }
    } else {
      if (baselineFile === undefined) {
        throw new Error('no baseline file specified in arguments');
      }
      if (subjectFile === undefined) {
        throw new Error('no subject file specified in arguments');
      }
      baselineJson = await readFile(baselineFile,{ encoding: 'utf8' });
      subjectJson = await readFile(subjectFile,{ encoding: 'utf8' });
    }
    const baseline = JSON.parse(baselineJson);
    const subject = JSON.parse(subjectJson);
    const deltaAnalysis = lib.analyze.delta(baseline, subject);
    console.log(JSON.stringify(deltaAnalysis));
  })
  .parse(process.argv);