ngx-devkit-builders 0.0.4 → 0.0.6

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/README.md CHANGED
@@ -41,7 +41,8 @@ _More builders can be added in the future._
41
41
  "outputFile": "src/environments/version.ts", // or src/assets/version.json
42
42
  "fields": ["version", "date", "author", "git"], // or custom selection
43
43
  "lint": "eslint", // or "tslint"
44
- "variable": "VERSION" // or whatever you want
44
+ "variable": "VERSION", // or whatever you want
45
+ "verbose": false // or true
45
46
  }
46
47
  }
47
48
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ngx-devkit-builders",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "author": {
5
5
  "name": "Dominik Hladík",
6
6
  "email": "dominik.hladik@seznam.cz",
package/version/index.js CHANGED
@@ -3,44 +3,80 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const architect_1 = require("@angular-devkit/architect");
4
4
  const core_1 = require("@angular-devkit/core");
5
5
  const fs_1 = require("fs");
6
- exports.default = (0, architect_1.createBuilder)(({ outputFile, fields, lint, variable }, ctx) => {
6
+ exports.default = (0, architect_1.createBuilder)(({ outputFile, fields, lint, variable, verbose }, ctx) => {
7
7
  ctx.logger.info('🚧 Creating version information file…');
8
8
  let targetFile = '';
9
+ const generalError = '❌ Creating version information file failed';
9
10
  try {
11
+ const result = {};
10
12
  const rootPath = (0, core_1.getSystemPath)((0, core_1.normalize)(ctx.workspaceRoot));
11
13
  const encoding = 'utf-8';
14
+ if (verbose === true)
15
+ ctx.logger.info(`Encoding for read/write is ${encoding}`);
12
16
  const fileToPatch = `${rootPath}/${outputFile}`;
17
+ if (verbose === true)
18
+ ctx.logger.info(`Output file is ${fileToPatch}`);
13
19
  targetFile = fileToPatch;
14
- const packageJsonContent = (0, fs_1.readFileSync)(`${rootPath}/package.json`, encoding);
15
- let head = (0, fs_1.readFileSync)(`${rootPath}/.git/HEAD`, encoding).toString().trim();
16
- if (head.split(':').length > 1) {
17
- head = head.split(':')[1].trim();
20
+ const packageJson = JSON.parse((0, fs_1.readFileSync)(`${rootPath}/package.json`, encoding));
21
+ if (fields.includes('version')) {
22
+ result.version = packageJson.version;
18
23
  }
19
- const commit = head ? (0, fs_1.readFileSync)(`${rootPath}/.git/${head}`, encoding).toString().trim() : undefined;
20
- const branch = head?.split('/')?.pop();
21
- const packageJson = JSON.parse(packageJsonContent);
22
- const git = branch || commit ? { branch, commit } : undefined;
23
- const json = JSON.stringify({
24
- version: fields.includes('version') ? packageJson.version : undefined,
25
- date: fields.includes('date') ? new Date().toISOString() : undefined,
26
- author: fields.includes('author') ? packageJson.author : undefined,
27
- git: fields.includes('git') ? git : undefined
28
- }, null, 2);
24
+ if (fields.includes('date')) {
25
+ result.date = new Date().toISOString();
26
+ }
27
+ if (fields.includes('author')) {
28
+ result.author = packageJson.author;
29
+ }
30
+ if (fields.includes('git')) {
31
+ const headFile = `${rootPath}/.git/HEAD`;
32
+ if (verbose === true)
33
+ ctx.logger.info(`Head is ${headFile}`);
34
+ if (!(0, fs_1.existsSync)(headFile)) {
35
+ ctx.logger.info(generalError);
36
+ ctx.logger.error(`${headFile} was not found`);
37
+ return {
38
+ success: false
39
+ };
40
+ }
41
+ let commit = (0, fs_1.readFileSync)(headFile).toString().trim();
42
+ let branch = '';
43
+ if (commit.indexOf(':') !== -1) {
44
+ branch = commit.substring(5);
45
+ const refFile = `${rootPath}/.git/${commit.substring(5)}`;
46
+ if (verbose === true)
47
+ ctx.logger.info(`Last commit is located here ${refFile}`);
48
+ if (!(0, fs_1.existsSync)(refFile)) {
49
+ ctx.logger.info(generalError);
50
+ ctx.logger.error(`${refFile} was not found`);
51
+ return {
52
+ success: false
53
+ };
54
+ }
55
+ commit = (0, fs_1.readFileSync)(refFile).toString().trim();
56
+ }
57
+ else {
58
+ branch = commit;
59
+ }
60
+ result.git = branch || commit ? { branch: branch.replace(/^refs\/heads\//, ''), commit } : undefined;
61
+ }
62
+ const json = JSON.stringify(result, null, 2);
29
63
  const rawFormat = outputFile?.split('.')?.pop();
30
64
  const format = ['json', 'ts'].includes(rawFormat) ? rawFormat : 'json';
65
+ if (verbose === true)
66
+ ctx.logger.info(`Output format is: ${format}`);
31
67
  if (format === 'ts') {
32
68
  (0, fs_1.writeFileSync)(fileToPatch, `// IMPORTANT: THIS FILE IS AUTO GENERATED!
33
69
  /* ${lint === 'tslint' ? 'tslint:disable' : 'eslint-disable'} */
34
70
  export const ${variable} = ${json};
35
71
  /* ${lint === 'tslint' ? 'tslint:enable' : 'eslint-enable'} */
36
- `, { encoding: 'utf-8' });
72
+ `, { encoding });
37
73
  }
38
74
  else {
39
75
  (0, fs_1.writeFileSync)(fileToPatch, json, { encoding });
40
76
  }
41
77
  }
42
78
  catch (error) {
43
- ctx.logger.info('❌ Creating version information file failed');
79
+ ctx.logger.info(generalError);
44
80
  ctx.logger.error(JSON.stringify(error, null, 2));
45
81
  return {
46
82
  success: false
@@ -26,6 +26,11 @@
26
26
  "description": "Variable name which will be used inside generated file",
27
27
  "type": "string",
28
28
  "default": "VERSION"
29
+ },
30
+ "verbose": {
31
+ "description": "Extended logging output",
32
+ "type": "boolean",
33
+ "default": "false"
29
34
  }
30
35
  }
31
36
  }