-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprepare.ts
69 lines (56 loc) · 1.92 KB
/
prepare.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
68
69
import * as os from 'node:os';
import * as path from 'node:path';
import fs from 'fs-extra';
import 'reflect-metadata';
import SemanticReleaseError from '@semantic-release/error';
import { transformAndValidate } from 'class-transformer-validator';
import { PluginConfig } from './classes/plugin-config.class.js';
import { PrepareContext } from 'semantic-release';
import { replaceVersions } from './utils/replace-versions.js';
import { copyFiles } from './utils/copy-files.js';
import { copyAssets } from './utils/copy-assets.js';
function getReadmePath(workDir: string): string | undefined {
return ['.wordpress-org/readme.txt', 'readme.txt'].reduce(
(acc, p) => (fs.existsSync(path.join(workDir, p)) ? p : acc),
undefined,
);
}
export async function prepare(
config: PluginConfig,
context: PrepareContext,
): Promise<void> {
config = await transformAndValidate(PluginConfig, config);
const errors: SemanticReleaseError[] = [];
const files =
config.type === 'plugin'
? [`${config.slug}.php`, ...config.versionFiles]
: ['style.css', ...config.versionFiles];
const workDir = config.copyFiles
? path.join(
path.join(os.tmpdir(), `workDir-${config.workDir}`, config.slug),
)
: path.resolve(config.path);
if (config.copyFiles) {
await fs.mkdir(workDir, { recursive: true });
await fs.copy(path.resolve(config.path), workDir);
}
const readmePath = config.withReadme ? getReadmePath(workDir) : undefined;
readmePath && files.push(readmePath);
errors.push(
...(await replaceVersions(workDir, files, context.nextRelease.version)),
);
if (errors.length) {
throw new AggregateError(errors);
}
errors.push(...(await copyFiles(config, workDir, readmePath)));
if (errors.length) {
throw new AggregateError(errors);
}
if (!config.withAssets) {
return;
}
await copyAssets(config, workDir);
if (errors.length) {
throw new AggregateError(errors);
}
}