-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(cli): add lb4 copyright command to generate/update file headers
#4994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| --- | ||
| lang: en | ||
| title: 'Generate copyright/license header for JavaScript/TypeScript files' | ||
| keywords: LoopBack 4.0, LoopBack 4 | ||
| sidebar: lb4_sidebar | ||
| permalink: /doc/en/lb4/Copyright-generator.html | ||
| --- | ||
|
|
||
| ### Synopsis | ||
|
|
||
| The `lb4 copyright` command runs inside a Node.js project with `package.json` to | ||
| add or update copyright/license header for JavaScript and TypeScript files based | ||
| on `package.json` and git history. | ||
|
|
||
| The command also supports [lerna](https://github.com/lerna/lerna) monorepos. It | ||
| traverses all packages within the monorepo and apply copyright/license headers. | ||
|
|
||
| ```sh | ||
| lb4 copyright [options] | ||
| ``` | ||
|
|
||
| The following is an example of such headers. | ||
|
|
||
| ```js | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/cli | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
| ``` | ||
|
|
||
| The year(s) is built from the git history of the file and `Node module` is read | ||
| from the `name` property in `package.json`. | ||
|
|
||
| Please note the command expects `git` is installed. | ||
|
|
||
| ### Options | ||
|
|
||
| `--owner` : _(Optional)_ The owner of the copyright, such as `IBM Corp.`. | ||
|
|
||
| `--license` : _(Optional)_ The name of the license, such as `MIT`. | ||
|
|
||
| `--gitOnly` : _(Optional)_ A flag to control if only git tracked files are | ||
| updated. Default to `true`. | ||
|
|
||
| ### Interactive Prompts | ||
|
|
||
| The command prompts you for: | ||
|
|
||
| 1. The copyright owner. The default value is read from `copyright.owner` or | ||
| `author` in the `package.json`. | ||
|
|
||
| 2. The license name. The default value is read from `license` in the | ||
| `package.json`. | ||
|
|
||
| The default owner is `IBM Corp.` and license is `MIT` with the following | ||
| `package.json`. | ||
|
|
||
| ```json | ||
| { | ||
| "name": "@loopback/boot", | ||
| "version": "2.0.2", | ||
| "author": "IBM Corp.", | ||
| "copyright.owner": "IBM Corp.", | ||
| "license": "MIT" | ||
| } | ||
| ``` | ||
|
|
||
| ### Output | ||
|
|
||
| The following output is captured when `lb4 copyright` is run against | ||
| [`loopback4-example-shopping`](https://github.com/strongloop/loopback4-example-shopping). | ||
|
|
||
| ``` | ||
| ? Copyright owner: IBM Corp. | ||
| ? License name: (Use arrow keys or type to search) | ||
| ❯ MIT (MIT License) | ||
| ISC (ISC License) | ||
| Artistic-2.0 (Artistic License 2.0) | ||
| Apache-2.0 (Apache License 2.0) | ||
| ... | ||
| ? Do you want to update package.json and LICENSE? No | ||
| Updating project loopback4-example-recommender (packages/recommender) | ||
| Updating project loopback4-example-shopping (packages/shopping) | ||
| Updating project loopback4-example-shopping-monorepo (.) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/cli | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const fse = require('fs-extra'); | ||
| const _ = require('lodash'); | ||
| const {promisify} = require('util'); | ||
| const glob = promisify(require('glob')); | ||
|
|
||
| const defaultFS = { | ||
| write: fse.writeFile, | ||
| read: fse.readFile, | ||
| writeJSON: fse.writeJson, | ||
| readJSON: fse.readJson, | ||
| exists: fse.exists, | ||
| }; | ||
|
|
||
| /** | ||
| * List all JS/TS files | ||
| * @param {string[]} paths - Paths to search | ||
| */ | ||
| async function jsOrTsFiles(cwd, paths = []) { | ||
| paths = [].concat(paths); | ||
| let globs; | ||
| if (paths.length === 0) { | ||
| globs = [glob('**/*.{js,ts}', {nodir: true, follow: false, cwd})]; | ||
| } else { | ||
| globs = paths.map(p => { | ||
| if (/\/$/.test(p)) { | ||
| p += '**/*.{js,ts}'; | ||
| } else if (!/[^*]\.(js|ts)$/.test(p)) { | ||
| p += '/**/*.{js,ts}'; | ||
| } | ||
| return glob(p, {nodir: true, follow: false, cwd}); | ||
| }); | ||
| } | ||
| paths = await Promise.all(globs); | ||
| paths = _.flatten(paths); | ||
| return _.filter(paths, /\.(js|ts)$/); | ||
| } | ||
|
|
||
| exports.FSE = defaultFS; | ||
| exports.jsOrTsFiles = jsOrTsFiles; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/cli | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const _ = require('lodash'); | ||
| const cp = require('child_process'); | ||
| const util = require('util'); | ||
| const debug = require('debug')('loopback:cli:copyright:git'); | ||
|
|
||
| const cache = new Map(); | ||
|
|
||
| /** | ||
| * Run a git command | ||
| * @param {string} cwd - Current directory to run the command | ||
| * @param {...any} args - Args for the git command | ||
| */ | ||
| async function git(cwd, ...args) { | ||
| const cmd = 'git ' + util.format(...args); | ||
| const key = `${cwd}:${cmd}`; | ||
| debug('Running %s in directory', cmd, cwd); | ||
| if (cache.has(key)) { | ||
| return cache.get(key); | ||
| } | ||
| return new Promise((resolve, reject) => { | ||
| cp.exec(cmd, {maxBuffer: 1024 * 1024, cwd}, (err, stdout) => { | ||
| stdout = _(stdout || '') | ||
| .split(/[\r\n]+/g) | ||
| .map(_.trim) | ||
| .filter() | ||
| .value(); | ||
| if (err) { | ||
| // reject(err); | ||
| resolve([]); | ||
| } else { | ||
| cache.set(key, stdout); | ||
| debug('Stdout', stdout); | ||
| resolve(stdout); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Inspect years for a given file based on git history | ||
| * @param {string} file - JS/TS file | ||
| */ | ||
| async function getYears(file) { | ||
| file = file || '.'; | ||
| let dates = await git( | ||
| process.cwd(), | ||
| '--no-pager log --pretty=%%ai --all -- %s', | ||
| file, | ||
| ); | ||
| debug('Dates for %s', file, dates); | ||
| if (_.isEmpty(dates)) { | ||
| // if the given path doesn't have any git history, assume it is new | ||
| dates = [new Date().toJSON()]; | ||
| } else { | ||
| dates = [_.head(dates), _.last(dates)]; | ||
| } | ||
| const years = _.map(dates, getYear); | ||
| return _.uniq(years).sort(); | ||
| } | ||
|
|
||
| // assumes ISO-8601 (or similar) format | ||
| function getYear(str) { | ||
| return str.slice(0, 4); | ||
| } | ||
|
|
||
| exports.git = git; | ||
| exports.getYears = getYears; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.