forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeFile.ts
More file actions
142 lines (121 loc) · 4.24 KB
/
ChangeFile.ts
File metadata and controls
142 lines (121 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'path';
import gitInfo = require('git-repo-info');
import { JsonFile } from '@microsoft/node-core-library';
import { RushConfiguration } from './RushConfiguration';
import {
IChangeFile,
IChangeInfo
} from './ChangeManagement';
import { Git } from '../logic/Git';
/**
* This class represents a single change file.
*/
export class ChangeFile {
private _changeFileData: IChangeFile;
private _rushConfiguration: RushConfiguration;
/**
* @internal
*/
public constructor(
changeFileData: IChangeFile,
rushConfiguration: RushConfiguration
) {
if (!changeFileData) {
throw new Error(`changeFileData does not have a value`);
}
if (!rushConfiguration) {
throw new Error(`rushConfiguration does not have a value`);
}
this._changeFileData = changeFileData;
this._rushConfiguration = rushConfiguration;
}
/**
* Adds a change entry into the change file
* @param data - change information
*/
public addChange(data: IChangeInfo): void {
this._changeFileData.changes.push(data);
}
/**
* Gets all the change entries about the specified package from the change file.
* @param packageName - package name
*/
public getChanges(packageName: string): IChangeInfo[] {
const changes: IChangeInfo[] = [];
for (const info of this._changeFileData.changes) {
if (info.packageName === packageName) {
changes.push(info);
}
}
return changes;
}
/**
* Writes the change file to disk in sync mode.
* Returns the file path.
* @returns the path to the file that was written (based on generatePath())
*/
public writeSync(): string {
const filePath: string = this.generatePath();
JsonFile.save(this._changeFileData, filePath, {
ensureFolderExists: true
});
return filePath;
}
/**
* Generates a file path for storing the change file to disk.
* Note that this value may change if called twice in a row,
* as it is partially based on the current date/time.
*/
public generatePath(): string {
let branch: string | undefined = undefined;
const repoInfo: gitInfo.GitRepoInfo | undefined = Git.getGitInfo();
branch = repoInfo && repoInfo.branch;
if (!branch) {
console.log('Could not automatically detect git branch name, using timestamp instead.');
}
// example filename: yourbranchname_2017-05-01-20-20.json
const filename: string = (branch ?
this._escapeFilename(`${branch}_${this._getTimestamp()}.json`) :
`${this._getTimestamp()}.json`);
const filePath: string = path.join(this._rushConfiguration.changesFolder,
...this._changeFileData.packageName.split('/'),
filename);
return filePath;
}
/**
* Gets the current time, formatted as YYYY-MM-DD-HH-MM
* Optionally will include seconds
*/
private _getTimestamp(useSeconds: boolean = false): string | undefined {
// Create a date string with the current time
// dateString === "2016-10-19T22:47:49.606Z"
const dateString: string = new Date().toJSON();
// Parse out 2 capture groups, the date and the time
const dateParseRegex: RegExp = /([0-9]{4}-[0-9]{2}-[0-9]{2}).*([0-9]{2}:[0-9]{2}:[0-9]{2})/;
// matches[1] === "2016-10-19"
// matches[2] === "22:47:49"
const matches: RegExpMatchArray | null = dateString.match(dateParseRegex);
if (matches) {
// formattedDate === "2016-10-19"
const formattedDate: string = matches[1];
let formattedTime: string;
if (useSeconds) {
// formattedTime === "22-47-49"
formattedTime = matches[2].replace(':', '-');
} else {
// formattedTime === "22-47"
const timeParts: string[] = matches[2].split(':');
formattedTime = `${timeParts[0]}-${timeParts[1]}`;
}
return `${formattedDate}-${formattedTime}`;
}
return undefined;
}
private _escapeFilename(filename: string, replacer: string = '-'): string {
// Removes / ? < > \ : * | ", really anything that isn't a letter, number, '.' '_' or '-'
const badCharacters: RegExp = /[^a-zA-Z0-9._-]/g;
return filename.replace(badCharacters, replacer);
}
}