-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.ts
50 lines (40 loc) · 1.69 KB
/
package.ts
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
// publish-beta/package.ts
import path from 'node:path';
import { readFile, writeFile } from 'node:fs/promises';
import { debug } from 'debug';
import { getPRNumber } from '../github-api';
import { removeTestFilesFromSource } from './files';
const log = debug('publish-beta:package');
interface PackageJSON {
name: string;
version: string;
files: string[];
}
const NUMBER_OF_CHARS_TO_USE_FROM_COMMIT_SHA = 4;
export function generatePackageBetaTag(): string {
const commentSha = process.env['GITHUB_SHA'];
if (!commentSha) {
throw new Error('Unable to get GITHUB_SHA');
}
const id = commentSha.slice(-NUMBER_OF_CHARS_TO_USE_FROM_COMMIT_SHA, commentSha.length);
const prNumber = getPRNumber();
return `PR.${prNumber}-${id}`;
}
function checkFilesPropertyExists(packageJSON: string): void {
const packageJSONObject = JSON.parse(packageJSON) as { files?: string[] };
if (!packageJSONObject.files) {
throw new Error('package.json does not have a files: [] property');
}
}
export async function packageJSONUpdate(rootProjectDirectory: string): Promise<string> {
const packageJSONPath = path.join(rootProjectDirectory, 'package.json');
const readPackageJson = await readFile(packageJSONPath, 'utf8');
checkFilesPropertyExists(readPackageJson);
const packageJson = JSON.parse(readPackageJson) as PackageJSON;
await removeTestFilesFromSource(path.join(rootProjectDirectory, 'src'));
const newVersion = `${packageJson.version}-${generatePackageBetaTag()}`;
packageJson.version = newVersion;
await writeFile(packageJSONPath, JSON.stringify(packageJson));
log(`Updated package.json - new version is: ${packageJson.name}@${newVersion}`);
return `${packageJson.name}@${newVersion}`;
}