forked from bimeister/pupakit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do-prepublish.ts
67 lines (55 loc) · 2.35 KB
/
do-prepublish.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { isEmpty, isNil } from '@bimeister/utilities';
import { existsSync } from 'fs';
import { copyFile, readFile, writeFile } from 'fs/promises';
import { join } from 'path';
import { env } from 'process';
const IS_DEV_PUBLISH: boolean = Boolean(env.IS_DEV_PUBLISH);
const GIT_COMMIT_HASH: string | undefined = env.GIT_COMMIT_HASH;
const CURRENT_LOCATION: string = join(__dirname, 'projects');
const VERSION: string | undefined = env.VERSION;
const PUBLISH_PROJECT_FOLDERS: string[] = [
'common',
'forms',
'icons',
'kit',
'overlays',
'table',
'tree',
'widgets',
'dnd',
];
async function preparePackages(): Promise<void> {
for (const projectPath of PUBLISH_PROJECT_FOLDERS) {
const projectDistFolderPath: string = join(CURRENT_LOCATION, projectPath, 'dist');
const packageJsonPath: string = join(projectDistFolderPath, 'package.json');
await preparePackageJson(packageJsonPath);
await copyFile('./.npmignore', join(projectDistFolderPath, '.npmignore'));
await copyFile('./LICENSE.md', join(projectDistFolderPath, 'LICENSE.md'));
}
}
async function preparePackageJson(targetPackageJsonPath: string): Promise<void> {
if (!existsSync(targetPackageJsonPath)) {
throw new Error(`Looks like you forgot run build first. Path ${targetPackageJsonPath} does not exist.`);
}
const currentContent: object = await readFile(targetPackageJsonPath, 'utf-8').then((content: string) =>
Boolean(isEmpty(content)) ? {} : JSON.parse(content)
);
const currentContentEntries: [string, unknown][] = Object.entries(currentContent);
const contentValueByKey: Map<string, unknown> = new Map<string, unknown>(
currentContentEntries.map(([key, value]: [string, unknown]) => [key, value])
);
if (isNil(VERSION)) {
throw new Error('Package.json version is invalid');
}
const metadataSuffix: string = IS_DEV_PUBLISH ? 'dev' : 'next';
const updatedProperVersion: string = isNil(GIT_COMMIT_HASH)
? VERSION
: `${VERSION}-${metadataSuffix}.sha.${GIT_COMMIT_HASH.slice(0, 8)}`;
contentValueByKey.set('version', updatedProperVersion);
contentValueByKey.set('private', false);
const updatedContent: object = Object.fromEntries(contentValueByKey.entries());
return writeFile(targetPackageJsonPath, JSON.stringify(updatedContent));
}
Promise.resolve()
.then(() => preparePackages())
.catch((error: unknown) => console.warn(error));