-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathgenerateCiYamlCli.ts
More file actions
75 lines (61 loc) · 3.04 KB
/
generateCiYamlCli.ts
File metadata and controls
75 lines (61 loc) · 3.04 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
#!/usr/bin/env node
import commandLineArgs from "command-line-args";
import { createOrUpdateCiYaml } from "./common/ciYamlUtils.js";
import { getNpmPackageInfo, getNpmPackageName } from "./common/npmUtils.js";
import { VersionPolicyName } from "./common/types.js";
import { logger } from "./utils/logger.js";
import path from "path";
const generateCiYamlCli = async (
sdkRepoPath: string | undefined,
packageFolderPath: string | undefined
) => {
if (!sdkRepoPath || !packageFolderPath) {
logger.error(`SdkRepoPath and PackagePath are required.`);
logger.error(
`Usage: generate-ci-yaml --sdkRepoPath <SdkRepoPath> --packagePath <PackagePath>`
);
process.exit(1);
}
// Calculate relative path from sdkRepoPath to packagePath
const normalizedSdkRepoPath = path.resolve(sdkRepoPath);
const absolutePackagePath = path.resolve(packageFolderPath);
const relativePackagePath = path.relative(normalizedSdkRepoPath, absolutePackagePath);
// Validate that the package path is safely inside the SDK repo path.
// Reject obvious path traversal attempts such as absolute paths outside the repo
// or relative paths that start with "..".
const normalizedSdkRepoPathWithSep = normalizedSdkRepoPath.endsWith(path.sep)
? normalizedSdkRepoPath
: normalizedSdkRepoPath + path.sep;
const isRelativeTraversal =
!path.isAbsolute(packageFolderPath) &&
(packageFolderPath === ".." || packageFolderPath.startsWith(".." + path.sep));
const isOutsideRepoByPrefix = !absolutePackagePath.startsWith(normalizedSdkRepoPathWithSep);
const isOutsideRepoByRelative =
relativePackagePath === ".." || relativePackagePath.startsWith(".." + path.sep);
if (isRelativeTraversal || isOutsideRepoByPrefix || isOutsideRepoByRelative) {
logger.error(
`The provided packagePath ("${packageFolderPath}") resolves outside the sdkRepoPath ("${sdkRepoPath}"). ` +
`Please provide a package path that is within the SDK repository root.`
);
process.exit(1);
}
logger.info(`SDK Repo Path: ${normalizedSdkRepoPath}`);
logger.info(`Package Path (absolute): ${absolutePackagePath}`);
logger.info(`Package Path (relative): ${relativePackagePath}`);
const npmPackageInfo = await getNpmPackageInfo(absolutePackagePath);
const packageName = getNpmPackageName(npmPackageInfo);
const versionPolicyName: VersionPolicyName = packageName.includes("arm-") ? "management" : "client";
logger.info(`Detected versionPolicyName: ${versionPolicyName} for package: ${packageName}`);
const ciPath = await createOrUpdateCiYaml(
relativePackagePath.replace(/\\/g, "/"),
versionPolicyName,
npmPackageInfo
);
logger.info(`CI yaml file created/updated at: ${ciPath}`);
};
const optionDefinitions = [
{ name: "sdkRepoPath", type: String },
{ name: "packagePath", type: String },
];
const options = commandLineArgs(optionDefinitions);
generateCiYamlCli(options["sdkRepoPath"], options["packagePath"]);