license-downloader 1.2.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,24 +2,33 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4
4
 
5
- ## [1.2.0](https://github.com/BePo65/license-downloader/compare/v1.1.0...v1.2.0) (2024-09-12)
5
+ ## [1.3.2](https://github.com/BePo65/license-downloader/compare/v1.3.1...v1.3.2) (2025-05-02)
6
+
7
+ ## [1.3.1](https://github.com/BePo65/license-downloader/compare/v1.3.0...v1.3.1) (2024-12-10)
8
+
9
+ ### Bug Fixes
6
10
 
11
+ - update packages to fix github dependabot security warnings ([91ff3d9](https://github.com/BePo65/license-downloader/commit/91ff3d99861db72264b7230f64cf3c7fce9461e3))
12
+ - update packages to fix github dependabot security warnings ([f7e3ec2](https://github.com/BePo65/license-downloader/commit/f7e3ec202e0790ff79ca2088ec92d9490873a7ef))
13
+
14
+ ## [1.3.0](https://github.com/BePo65/license-downloader/compare/v1.2.0...v1.3.0) (2024-09-17)
15
+
16
+ ## [1.2.0](https://github.com/BePo65/license-downloader/compare/v1.1.0...v1.2.0) (2024-09-12)
7
17
 
8
18
  ### Features
9
19
 
10
- * support GITHUB_TOKEN env variable by default ([258f8e0](https://github.com/BePo65/license-downloader/commit/258f8e02c39e61cd4c3b5e85df68f068b85bea84))
20
+ - support GITHUB_TOKEN env variable by default ([258f8e0](https://github.com/BePo65/license-downloader/commit/258f8e02c39e61cd4c3b5e85df68f068b85bea84))
11
21
 
12
22
  ## [1.1.0](https://github.com/BePo65/license-downloader/compare/v1.0.12...v1.1.0) (2024-06-30)
13
23
 
14
-
15
24
  ### Features
16
25
 
17
- * add --help flag for cli to show short documentation ([cb02040](https://github.com/BePo65/license-downloader/commit/cb020408288e857f8dab5802fbbe7d192e0e0477))
26
+ - add --help flag for cli to show short documentation ([cb02040](https://github.com/BePo65/license-downloader/commit/cb020408288e857f8dab5802fbbe7d192e0e0477))
18
27
 
19
28
  ### Bug Fixes
20
29
 
21
- * fix handling of authorization
22
- * fix handling of packages with long package path or not ending with '.git'
30
+ - fix handling of authorization
31
+ - fix handling of packages with long package path or not ending with '.git'
23
32
 
24
33
  ## [1.0.12](https://github.com/BePo65/license-downloader/compare/v1.0.11...v1.0.12) (2024-06-22)
25
34
 
@@ -43,42 +52,37 @@ All notable changes to this project will be documented in this file. See [commit
43
52
 
44
53
  ### [1.0.2](https://github.com/BePo65/license-downloader/compare/v1.0.1...v1.0.2) (2021-05-12)
45
54
 
46
-
47
55
  ### Bug Fixes
48
56
 
49
- * index.js needs shebang for use with npx ([415b986](https://github.com/BePo65/license-downloader/commit/415b98621c2ddb50f5d07cc3586dedf816e2c810))
57
+ - index.js needs shebang for use with npx ([415b986](https://github.com/BePo65/license-downloader/commit/415b98621c2ddb50f5d07cc3586dedf816e2c810))
50
58
 
51
59
  ### [1.0.1](https://github.com/BePo65/license-downloader/compare/v1.0.0...v1.0.1) (2021-05-12)
52
60
 
53
-
54
61
  ### Bug Fixes
55
62
 
56
- * add bin property to run program with npx ([43443f7](https://github.com/BePo65/license-downloader/commit/43443f7c83e2b76d9f2f8c41c43f475ff35ddb79))
63
+ - add bin property to run program with npx ([43443f7](https://github.com/BePo65/license-downloader/commit/43443f7c83e2b76d9f2f8c41c43f475ff35ddb79))
57
64
 
58
65
  ## [1.0.0](https://github.com/BePo65/license-downloader/compare/v0.3.1...v1.0.0) (2021-05-12)
59
66
 
60
67
  ### [0.3.1](https://github.com/BePo65/license-downloader/compare/v0.3.0...v0.3.1) (2021-05-12)
61
68
 
62
-
63
69
  ### Bug Fixes
64
70
 
65
- * convert readme-updater to commonjs file ([4a5d1dd](https://github.com/BePo65/license-downloader/commit/4a5d1dd8abd89ea8b8d52372f529cdb60fa1ac61))
71
+ - convert readme-updater to commonjs file ([4a5d1dd](https://github.com/BePo65/license-downloader/commit/4a5d1dd8abd89ea8b8d52372f529cdb60fa1ac61))
66
72
 
67
73
  ## 0.3.0 (2021-05-12)
68
74
 
69
-
70
75
  ### Features
71
76
 
72
- * add authorization for github accesses ([e022c52](https://github.com/BePo65/license-downloader/commit/e022c52a9de32287cd4c974384a3d5ca141de972))
73
- * add default export to modules ([e05aa5a](https://github.com/BePo65/license-downloader/commit/e05aa5a9eabca7ec74c75e57de2a0b08bdb8f4c4))
74
- * add license file ([9da79f5](https://github.com/BePo65/license-downloader/commit/9da79f5c6b9b3253ca0f0b48f35a06ef64034ec8))
75
- * get license files and export extended list fo file ([0a818bc](https://github.com/BePo65/license-downloader/commit/0a818bc2797e14219f9f1050fdec9013b2c052f1))
76
- * rename license files to '<packageName>.LICENSE.txt ([2029f4a](https://github.com/BePo65/license-downloader/commit/2029f4a5694f39a96bd145167f6b9ed18da91590))
77
-
77
+ - add authorization for github accesses ([e022c52](https://github.com/BePo65/license-downloader/commit/e022c52a9de32287cd4c974384a3d5ca141de972))
78
+ - add default export to modules ([e05aa5a](https://github.com/BePo65/license-downloader/commit/e05aa5a9eabca7ec74c75e57de2a0b08bdb8f4c4))
79
+ - add license file ([9da79f5](https://github.com/BePo65/license-downloader/commit/9da79f5c6b9b3253ca0f0b48f35a06ef64034ec8))
80
+ - get license files and export extended list fo file ([0a818bc](https://github.com/BePo65/license-downloader/commit/0a818bc2797e14219f9f1050fdec9013b2c052f1))
81
+ - rename license files to '<packageName>.LICENSE.txt ([2029f4a](https://github.com/BePo65/license-downloader/commit/2029f4a5694f39a96bd145167f6b9ed18da91590))
78
82
 
79
83
  ### Bug Fixes
80
84
 
81
- * log to stderr improved when no license file found ([c1a4534](https://github.com/BePo65/license-downloader/commit/c1a45346e2aeb3fdb7812bb104d2bb83f2b19f74))
82
- * no error when directory already exists on mkdir ([5736c2f](https://github.com/BePo65/license-downloader/commit/5736c2f302e030e43ede3c436edd2b861617d023))
83
- * overwrite output json file when already exists ([ee89151](https://github.com/BePo65/license-downloader/commit/ee8915128cc261743e94189a81a5e439a8602e91))
84
- * update readme file ([6fbe174](https://github.com/BePo65/license-downloader/commit/6fbe174635c8a72c14e38f8d0fd410be797ff929))
85
+ - log to stderr improved when no license file found ([c1a4534](https://github.com/BePo65/license-downloader/commit/c1a45346e2aeb3fdb7812bb104d2bb83f2b19f74))
86
+ - no error when directory already exists on mkdir ([5736c2f](https://github.com/BePo65/license-downloader/commit/5736c2f302e030e43ede3c436edd2b861617d023))
87
+ - overwrite output json file when already exists ([ee89151](https://github.com/BePo65/license-downloader/commit/ee8915128cc261743e94189a81a5e439a8602e91))
88
+ - update readme file ([6fbe174](https://github.com/BePo65/license-downloader/commit/6fbe174635c8a72c14e38f8d0fd410be797ff929))
package/README.md CHANGED
@@ -1,120 +1,133 @@
1
- # license-downloader - an add-on to license-report tool
2
- ![Version](https://img.shields.io/badge/version-1.2.0-blue.svg?cacheSeconds=2592000)
3
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](#)
4
-
5
- > Download the license files of the dependencies of a project based on the json report from the package 'license-report'.
6
-
7
- ## Install
8
-
9
- ```sh
10
- npm install license-downloader
11
- ```
12
-
13
- ## Usage
14
-
15
- 'license-downloader' uses the github REST API to find the location of the license files of the project (dev-) dependencies. The license file location does not depend on the package version. Details about how the license file is detected can be found in [licensee/licensee > What we look at](https://github.com/licensee/licensee/blob/master/docs/what-we-look-at.md) and [licensee/licensee > README](https://github.com/licensee/licensee/tree/master/docs).
16
-
17
- Optionally it downloads these files to a given directory.
18
-
19
- ### Usage with downloading the license files:
20
- ```sh
21
- cd your/project/
22
- npx license-report > ./license-report.json
23
- npx license-downloader --source ./license-report.json --licDir ./license-files --download
24
- ```
25
-
26
- A copy of the 'license-report.json' file with properties 'licenseFileLink' added is saved to the './license-files' directory (defined in option `--licDir`). The downloaded licenses are save in a subdirectory named 'licenses' in the directory defined in `--licDir`.
27
-
28
- If option `--licDir` is missing, the modified 'license-report.json' file and the 'licenses' subdirectory are written to the path of the source file.
29
-
30
- ### Usage without downloading the license files:
31
- ```sh
32
- cd your/project/
33
- npx license-report > ./license-report.json
34
- npx license-downloader --source ./license-report.json
35
- ```
36
-
37
- The copy of the 'license-report.json' file with properties 'licenseFileLink' added is saved to the directory of the source file.
38
-
39
- ## Authorization in GitHub
40
-
41
- License-downloader uses the github api to collect information about the license file of a project. If the rate limit for anonymous access to this api is exceeded (60 accesses per hour) you will get warnings like this:
42
- ```
43
- kessler/node-tableify: License query failed. Rate limit of 60 requests per hour exceeded. please wait 1534 seconds before trying again.
44
- caolan/async: License query failed. Rate limit of 60 requests per hour exceeded. please wait 1534 seconds before trying again.
45
- ```
46
-
47
- To avoid this problem you can use a github personal access token to use the higher limits of your personal github plan. As an alternative you can use fine-grained access tokens; details can be found in the github documentation for "Get the license for a repository".
48
-
49
- There are 2 possibilities:
50
-
51
- **use a file (recommended)**
52
-
53
- write the github token, generated in your github developer settings, to a file.
54
-
55
- The full path to this file is written to an environment variable and the name of this environment variable is set in the config parameter 'githubToken.tokenFileEnvVar':
56
- ```sh
57
- export GITHUB_TOKEN_FILE=/run/secrets/github_pat.txt
58
- cd your/project/
59
- npx license-report > ./license-report.json
60
- npx license-downloader --source ./license-report.json --licDir ./license-files --githubToken.tokenFileEnvVar GITHUB_TOKEN_FILE --download
61
- ```
62
- This is the most secure variant, as access rights can be and should be set for this file to prevent unauthorized access.
63
-
64
- **use an environment variable**
65
-
66
- Generate a github token in your github developer settings and write it to an environment variable.
67
-
68
- If the environment variable name is `GITHUB_TOKEN`, then it will be used automatically.
69
- If you want to use another environment variable, you can set it with the parameter `--githubToken.tokenEnvVar`.
70
-
71
- Examples:
72
- ```sh
73
- export GITHUB_TOKEN=yourgithubtoken
74
- cd your/project/
75
- npx license-report > ./license-report.json
76
- npx license-downloader --source ./license-report.json --licDir ./license-files --download
77
- ```
78
-
79
- Or with a different environment variable name:
80
- ```sh
81
- export MY_GITHUB_TOKEN=yourgithubtoken
82
- cd your/project/
83
- npx license-report > ./license-report.json
84
- npx license-downloader --source ./license-report.json --licDir ./license-files --githubToken.tokenEnvVar MY_GITHUB_TOKEN --download
85
- ```
86
-
87
- ## Show debug log
88
-
89
- Use in linux shell
90
- ```sh
91
- export DEBUG=license-downloader
92
- ```
93
- or in windows command line
94
- ```sh
95
- SET DEBUG=license-downloader
96
- ```
97
-
98
- ## Development
99
-
100
- This repo uses standard-changelog to create the CHANGELOG. To ensure that the commit messages follow the standard-changelog rules, husky is used for git hooks.
101
-
102
- To initialize the git hooks after checking out the repo, run `npx husky install`.
103
-
104
- Allowed types for commit messages are:
105
- + build
106
- + ci
107
- + docs
108
- + feat
109
- + fix
110
- + perf
111
- + refactor
112
- + release
113
- + revert
114
- + style
115
- + test
116
-
117
- Allowed scopes are:
118
- + app
119
- + hacks
120
- + tools
1
+ # license-downloader - an add-on to license-report tool
2
+
3
+ ![Version](https://img.shields.io/badge/version-1.3.2-blue.svg?cacheSeconds=2592000)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](#)
5
+
6
+ > Download the license files of the dependencies of a project based on the json report from the package 'license-report'.
7
+
8
+ ## Install
9
+
10
+ ```sh
11
+ npm install license-downloader
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ 'license-downloader' uses the github REST API to find the location of the license files of the project (dev-) dependencies. The license file location does not depend on the package version. Details about how the license file is detected can be found in [licensee/licensee > What we look at](https://github.com/licensee/licensee/blob/master/docs/what-we-look-at.md) and [licensee/licensee > README](https://github.com/licensee/licensee/tree/master/docs).
17
+
18
+ Optionally it downloads these files to a given directory.
19
+
20
+ ### Usage with downloading the license files:
21
+
22
+ ```sh
23
+ cd your/project/
24
+ npx license-report > ./license-report.json
25
+ npx license-downloader --source ./license-report.json --licDir ./license-files --download
26
+ ```
27
+
28
+ A copy of the 'license-report.json' file with properties 'licenseFileLink' added is saved to the './license-files' directory (defined in option `--licDir`). The downloaded licenses are save in a subdirectory named 'licenses' in the directory defined in `--licDir`.
29
+
30
+ If option `--licDir` is missing, the modified 'license-report.json' file and the 'licenses' subdirectory are written to the path of the source file.
31
+
32
+ ### Usage without downloading the license files:
33
+
34
+ ```sh
35
+ cd your/project/
36
+ npx license-report > ./license-report.json
37
+ npx license-downloader --source ./license-report.json
38
+ ```
39
+
40
+ The copy of the 'license-report.json' file with properties 'licenseFileLink' added is saved to the directory of the source file.
41
+
42
+ ## Authorization in GitHub
43
+
44
+ License-downloader uses the github api to collect information about the license file of a project. If the rate limit for anonymous access to this api is exceeded (60 accesses per hour) you will get warnings like this:
45
+
46
+ ```
47
+ kessler/node-tableify: License query failed. Rate limit of 60 requests per hour exceeded. please wait 1534 seconds before trying again.
48
+ caolan/async: License query failed. Rate limit of 60 requests per hour exceeded. please wait 1534 seconds before trying again.
49
+ ```
50
+
51
+ To avoid this problem you can use a github personal access token to use the higher limits of your personal github plan. As an alternative you can use fine-grained access tokens; details can be found in the github documentation for "Get the license for a repository".
52
+
53
+ There are 2 possibilities:
54
+
55
+ **use a file (recommended)**
56
+
57
+ write the github token, generated in your github developer settings, to a file.
58
+
59
+ The full path to this file is written to an environment variable and the name of this environment variable is set in the config parameter 'githubToken.tokenFileEnvVar':
60
+
61
+ ```sh
62
+ export GITHUB_TOKEN_FILE=/run/secrets/github_pat.txt
63
+ cd your/project/
64
+ npx license-report > ./license-report.json
65
+ npx license-downloader --source ./license-report.json --licDir ./license-files --githubToken.tokenFileEnvVar GITHUB_TOKEN_FILE --download
66
+ ```
67
+
68
+ This is the most secure variant, as access rights can be and should be set for this file to prevent unauthorized access.
69
+
70
+ **use an environment variable**
71
+
72
+ Generate a github token in your github developer settings and write it to an environment variable.
73
+
74
+ If the environment variable name is `GITHUB_TOKEN`, then it will be used automatically.
75
+ If you want to use another environment variable, you can set it with the parameter `--githubToken.tokenEnvVar`.
76
+
77
+ Examples:
78
+
79
+ ```sh
80
+ export GITHUB_TOKEN=yourgithubtoken
81
+ cd your/project/
82
+ npx license-report > ./license-report.json
83
+ npx license-downloader --source ./license-report.json --licDir ./license-files --download
84
+ ```
85
+
86
+ Or with a different environment variable name:
87
+
88
+ ```sh
89
+ export MY_GITHUB_TOKEN=yourgithubtoken
90
+ cd your/project/
91
+ npx license-report > ./license-report.json
92
+ npx license-downloader --source ./license-report.json --licDir ./license-files --githubToken.tokenEnvVar MY_GITHUB_TOKEN --download
93
+ ```
94
+
95
+ ## Show debug log
96
+
97
+ Use in linux shell
98
+
99
+ ```sh
100
+ export DEBUG=license-downloader
101
+ ```
102
+
103
+ or in windows command line
104
+
105
+ ```sh
106
+ SET DEBUG=license-downloader
107
+ ```
108
+
109
+ ## Development
110
+
111
+ This repo uses standard-changelog to create the CHANGELOG. To ensure that the commit messages follow the standard-changelog rules, husky is used for git hooks.
112
+
113
+ To initialize the git hooks after checking out the repo, run `npx husky install`.
114
+
115
+ Allowed types for commit messages are:
116
+
117
+ - build
118
+ - ci
119
+ - docs
120
+ - feat
121
+ - fix
122
+ - perf
123
+ - refactor
124
+ - release
125
+ - revert
126
+ - style
127
+ - test
128
+
129
+ Allowed scopes are:
130
+
131
+ - app
132
+ - hacks
133
+ - tools
package/index.js CHANGED
@@ -5,7 +5,11 @@ import path from 'path';
5
5
  import config from './lib/config.js';
6
6
  import FsService from './lib/filesystem-service.js';
7
7
  import WebService from './lib/web-service.js';
8
- import { helpText, licenseFilesTargetFolder, sourceToTargetFilename } from './lib/util.js';
8
+ import {
9
+ helpText,
10
+ licenseFilesTargetFolder,
11
+ sourceToTargetFilename,
12
+ } from './lib/util.js';
9
13
 
10
14
  (async () => {
11
15
  if (config.help) {
@@ -16,17 +20,32 @@ import { helpText, licenseFilesTargetFolder, sourceToTargetFilename } from './li
16
20
  try {
17
21
  const sourceFilename = config.source;
18
22
  const json = await FsService.readJson(sourceFilename);
19
- await WebService.addLicenseFilePath(json, config.httpRetryOptions, config.githubToken);
20
- const targetFilename = sourceToTargetFilename(sourceFilename, config.licDir);
23
+ await WebService.addLicenseFilePath(
24
+ json,
25
+ config.httpRetryOptions,
26
+ config.githubToken,
27
+ );
28
+ const targetFilename = sourceToTargetFilename(
29
+ sourceFilename,
30
+ config.licDir,
31
+ );
21
32
  FsService.createDirFromFilename(targetFilename);
22
33
  await FsService.writeJson(json, targetFilename);
23
34
  if (config.download) {
24
35
  const sourceDirectory = path.dirname(sourceFilename);
25
- const targetDirectory = licenseFilesTargetFolder(config.licDir, sourceDirectory);
26
- await WebService.downloadLicenseFiles(json, targetDirectory, config.httpRetryOptions, config.githubToken);
36
+ const targetDirectory = licenseFilesTargetFolder(
37
+ config.licDir,
38
+ sourceDirectory,
39
+ );
40
+ await WebService.downloadLicenseFiles(
41
+ json,
42
+ targetDirectory,
43
+ config.httpRetryOptions,
44
+ config.githubToken,
45
+ );
27
46
  }
28
47
  } catch (e) {
29
48
  console.error(e.message);
30
- process.exit(1); // eslint-disable-line n/no-process-exit
49
+ process.exit(1); // eslint-disable-line n/no-process-exit
31
50
  }
32
51
  })();
package/lib/config.js CHANGED
@@ -1,47 +1,46 @@
1
- import rc from 'rc';
2
-
3
- export default rc('license-downloader', {
4
-
5
- /*
6
- full path to json file generated by 'license-report'
7
- describing all packages to handle
8
- */
9
- source: '',
10
-
11
- /*
12
- Download license files if true,
13
- if false, only the paths of the license files are added to a copy
14
- of the source file
15
- */
16
- download: false,
17
-
18
- /*
19
- target directory for downloaded license files;
20
- on null or undefined a directory named 'licenses' in the directory of the
21
- source is used
22
- */
23
- licDir: null,
24
-
25
- /*
26
- Name of the environment variable holding the access token ('tokenEnvVar')
27
- or the name of the file containing the token ('tokenFileEnvVar') for
28
- accessing the github api for downloading the license files;
29
- tokenFileEnvVar has precedence over tokenEnvVar
30
- Example 1: export GITHUB_TOKEN=abcdefghi
31
- and then: tokenEnvVar: 'GITHUB_TOKEN'
32
- Example 2: export GITHUB_TOKEN=/run/secrets/github_pat.txt
33
- content of file /run/secrets/github_pat.txt: abcdefghi
34
- and then: tokenFileEnvVar: 'GITHUB_TOKEN_FILE'
35
- */
36
- githubToken: {
37
- tokenEnvVar: 'GITHUB_TOKEN',
38
- tokenFileEnvVar: ''
39
- },
40
-
41
- /*
42
- retry options for 'got'
43
- */
44
- httpRetryOptions: {
45
- maxAttempts: 3
46
- }
47
- });
1
+ import rc from 'rc';
2
+
3
+ export default rc('license-downloader', {
4
+ /*
5
+ full path to json file generated by 'license-report'
6
+ describing all packages to handle
7
+ */
8
+ source: '',
9
+
10
+ /*
11
+ Download license files if true,
12
+ if false, only the paths of the license files are added to a copy
13
+ of the source file
14
+ */
15
+ download: false,
16
+
17
+ /*
18
+ target directory for downloaded license files;
19
+ on null or undefined a directory named 'licenses' in the directory of the
20
+ source is used
21
+ */
22
+ licDir: null,
23
+
24
+ /*
25
+ Name of the environment variable holding the access token ('tokenEnvVar')
26
+ or the name of the file containing the token ('tokenFileEnvVar') for
27
+ accessing the github api for downloading the license files;
28
+ tokenFileEnvVar has precedence over tokenEnvVar
29
+ Example 1: export GITHUB_TOKEN=abcdefghi
30
+ and then: tokenEnvVar: 'GITHUB_TOKEN'
31
+ Example 2: export GITHUB_TOKEN=/run/secrets/github_pat.txt
32
+ content of file /run/secrets/github_pat.txt: abcdefghi
33
+ and then: tokenFileEnvVar: 'GITHUB_TOKEN_FILE'
34
+ */
35
+ githubToken: {
36
+ tokenEnvVar: 'GITHUB_TOKEN',
37
+ tokenFileEnvVar: '',
38
+ },
39
+
40
+ /*
41
+ retry options for 'got'
42
+ */
43
+ httpRetryOptions: {
44
+ maxAttempts: 3,
45
+ },
46
+ });
@@ -1,60 +1,62 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import jsonfile from 'jsonfile';
4
-
5
- /**
6
- * Reads json file generated by license-report (with option "--output=json")
7
- * and generates javascript object array from it
8
- * @param {string} filePath - full path to the license-report generated json file
9
- * @returns {*} content of the license-report generated json file as object array
10
- */
11
- export const readJson = async (filePath) => {
12
- if (!filePath) {
13
- throw new Error('license-downloader must be called with a "--source <license-report-json-file>" parameter');
14
- }
15
-
16
- if (!fs.existsSync(filePath)) {
17
- throw new Error(`Source file '${filePath}' does not exist`);
18
- }
19
-
20
- const json = await jsonfile.readFile(filePath);
21
- return json;
22
- }
23
-
24
- /**
25
- * Writes javascript object array as json string to file overwriting existing file
26
- * @param {object} json - object array to be written as json file
27
- * @param {string} filePath - full path to the output file to be generated
28
- */
29
- export const writeJson = async (json, filePath) => {
30
- if (!filePath) {
31
- throw new Error('No target filename defined');
32
- }
33
-
34
- await jsonfile.writeFile(filePath, json);
35
- }
36
-
37
- /**
38
- * Create directory for given path if not exist
39
- * @param {string} dirPath - full path of the directory
40
- */
41
- export const createDirIfNotExists = (dirPath) => {
42
- if (!fs.existsSync(dirPath)) {
43
- fs.mkdirSync(dirPath, { recursive: true });
44
- }
45
- }
46
-
47
- /**
48
- * Create directory for given file if not exist
49
- * @param {string} fullFilename - full path to a file
50
- */
51
- export const createDirFromFilename = (fullFilename) => {
52
- createDirIfNotExists(path.dirname(fullFilename));
53
- }
54
-
55
- export default {
56
- readJson,
57
- writeJson,
58
- createDirIfNotExists,
59
- createDirFromFilename
60
- };
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import jsonfile from 'jsonfile';
4
+
5
+ /**
6
+ * Reads json file generated by license-report (with option "--output=json")
7
+ * and generates javascript object array from it
8
+ * @param {string} filePath - full path to the license-report generated json file
9
+ * @returns {*} content of the license-report generated json file as object array
10
+ */
11
+ export const readJson = async (filePath) => {
12
+ if (!filePath) {
13
+ throw new Error(
14
+ 'license-downloader must be called with a "--source <license-report-json-file>" parameter',
15
+ );
16
+ }
17
+
18
+ if (!fs.existsSync(filePath)) {
19
+ throw new Error(`Source file '${filePath}' does not exist`);
20
+ }
21
+
22
+ const json = await jsonfile.readFile(filePath);
23
+ return json;
24
+ };
25
+
26
+ /**
27
+ * Writes javascript object array as json string to file overwriting existing file
28
+ * @param {object} json - object array to be written as json file
29
+ * @param {string} filePath - full path to the output file to be generated
30
+ */
31
+ export const writeJson = async (json, filePath) => {
32
+ if (!filePath) {
33
+ throw new Error('No target filename defined');
34
+ }
35
+
36
+ await jsonfile.writeFile(filePath, json);
37
+ };
38
+
39
+ /**
40
+ * Create directory for given path if not exist
41
+ * @param {string} dirPath - full path of the directory
42
+ */
43
+ export const createDirIfNotExists = (dirPath) => {
44
+ if (!fs.existsSync(dirPath)) {
45
+ fs.mkdirSync(dirPath, { recursive: true });
46
+ }
47
+ };
48
+
49
+ /**
50
+ * Create directory for given file if not exist
51
+ * @param {string} fullFilename - full path to a file
52
+ */
53
+ export const createDirFromFilename = (fullFilename) => {
54
+ createDirIfNotExists(path.dirname(fullFilename));
55
+ };
56
+
57
+ export default {
58
+ readJson,
59
+ writeJson,
60
+ createDirIfNotExists,
61
+ createDirFromFilename,
62
+ };