Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
#37 sepearte out computeCopyrightYears
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonLi8 committed Jan 20, 2020
1 parent 8cbd6b6 commit 1d36c8d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
40 changes: 28 additions & 12 deletions src/Copyright.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,33 @@ module.exports = ( () => {
class Copyright {

/**
* Utility method to generate a copyright statement for a file. The start year is computed from when the file was
* Utility method to compute the copyright years for a file. The start year is computed from when the file was
* checked into git or the current year if it hasn't been checked-in yet. The end year is assumed to be the current
* year. Result includes the correct comment delimiters and follows the format described at the top of this file.
* year. If the given file path doesn't exist, it will return the current year as a string.
* @public
*
* @param {String} filePath - path of the file, relative to the root directory that invoked the command.
* @returns {String} - the copyright years string (e.g. '2019-2020')
*/
static computeCopyrightYears( filePath ) {
Util.assert( typeof filePath === 'string', `invalid filePath: ${ filePath }` );

// Compute the year the file was checked into git as the start year. If it hasn't been checked into git yet, the
// start year is the current year. Solution from:
// https://stackoverflow.com/questions/2390199/finding-the-date-time-a-file-was-first-added-to-a-git-repository
const startYear = shell.exec( `git log --follow --format=%aI -- ${ filePath } | tail -1`, { silent: true } )
.trim().split( '-' )[ 0 ] || Util.CURRENT_YEAR;

const endYear = Util.CURRENT_YEAR; // The end year is assumed to be the current year.

// Create and return year string or the year range string to use in the copyright statement.
return ( parseInt( startYear, 10 ) === endYear ) ? startYear : `${ startYear }-${ endYear }`;
}

/**
* Utility method to generate a copyright statement for a file. See computeCopyrightYears() for documentation on how
* the copyright years string is computed. Result includes the correct comment delimiters and follows the format
* described at the top of this file.
* @public
*
* @param {String} filePath - path of the file, relative to the root directory that invoked the command.
Expand All @@ -67,16 +91,8 @@ module.exports = ( () => {
// Reference to the validated and parsed generator author (see ./Generator.js for more documentation).
const author = Generator.getReplacementValue( 'AUTHOR' );

// Compute the year the file was checked into git as the start year. If it hasn't been checked into git yet, the
// start year is the current year. Solution from:
// https://stackoverflow.com/questions/2390199/finding-the-date-time-a-file-was-first-added-to-a-git-repository
const startYear = shell.exec( `git log --follow --format=%aI -- ${ filePath } | tail -1`, { silent: true } )
.trim().split( '-' )[ 0 ] || Util.CURRENT_YEAR;

const endYear = Util.CURRENT_YEAR; // The end year is assumed to be the current year.

// Create the year string or the year range string to use in the copyright statement.
const yearsString = ( parseInt( startYear, 10 ) === endYear ) ? startYear : `${ startYear }-${ endYear }`;
// Reference the years string from computeCopyrightYears().
const yearsString = this.computeCopyrightYears( filePath );

// Create the copyright line without the comment delimiters first. Then return the parsed value.
const copyrightContent = `Copyright © ${ yearsString } ${ author }. All rights reserved.`;
Expand Down
6 changes: 2 additions & 4 deletions src/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ module.exports = ( () => {

// constants
// Object literal that describes the replacement strings in template files to replace. Each key is the replacement
// string (without the brackets for now) and correlates with one of the three values stated below:
// string (without the brackets for now) and correlates with one of the two values stated below:
// 1. String[] - nested keys path to the package value. For example, PACKAGE_JSON.foo.bar would have nested keys
// [ 'foo', 'bar' ]. PACKAGE_JSON is checked to have the nested keys (see
// UserConfig.parseNestedJSONValue()).
// 2. Object Literal - an object literal with:
// - a path key that correlates to an array of the nested package keys as described in 1.
// - a parse key that correlates to a function that is called to 'parse' a value that is
// retrieved from the package object. The returned value is the replacement value.
// 3. * - the actual replacement value to replace the replacement string in the template file.
const REPLACEMENT_STRINGS_SCHEMA = {
AUTHOR: [ 'author', 'name' ],
AUTHOR_EMAIL: [ 'author', 'email' ],
Expand All @@ -55,8 +54,7 @@ module.exports = ( () => {
VERSION: [ 'version' ],
LICENSE: [ 'license' ],
GITHUB_URL: { path: [ 'repository', 'url' ], parse: value => value.replace( /.git|git+/i, '' ) },
REPO_TITLE: { path: [ 'name' ], parse: value => Util.toTitleCase( value ) },
COPYRIGHT_YEARS: Util.CURRENT_YEAR // Use the current year for now, then use ./Copyright to update after generating
REPO_TITLE: { path: [ 'name' ], parse: value => Util.toTitleCase( value ) }
};

// Object literal that keeps track of the replacement values. This helps performance and ensures that replacement
Expand Down

0 comments on commit 1d36c8d

Please sign in to comment.