-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.ts
More file actions
123 lines (103 loc) · 4.08 KB
/
package.ts
File metadata and controls
123 lines (103 loc) · 4.08 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
import fs from "fs/promises";
import ora from "ora";
import path from "path";
import { exec } from "child_process";
import fsUtils from "./fsUtils";
import { DEFAULT_PM, CONFIG_NAMES, PMS, PACKAGE_JSON } from "./constants/packageManagement";
import { Colors } from "./utils/colors";
import { askQuestion, packageNameFromPlugin } from "./utils";
import type { PackageManager } from "./constants/packageManagement";
const getPackageManager = async (dirPath: string, extraQuestions: boolean): Promise<PackageManager> => {
let packageManager: PackageManager | undefined;
const packageManagers = Object.keys(PMS) as PackageManager[];
await Promise.all(
packageManagers.map(async (pm: PackageManager) => {
const isLockfileExist = await fsUtils.exists(path.resolve(dirPath, PMS[pm].lock));
if (isLockfileExist) {
packageManager = pm;
}
}),
);
if (packageManager) {
return packageManager;
}
if (!extraQuestions) {
return DEFAULT_PM;
}
return askQuestion<PackageManager>({
type: "list",
message: "Choose preferred package manager:",
choices: Object.keys(PMS),
default: DEFAULT_PM,
});
};
const findExistingConfig = async (dirPath: string): Promise<typeof CONFIG_NAMES[keyof typeof CONFIG_NAMES] | null> => {
const configExistsPromises = Object.values(CONFIG_NAMES).map(async configName => {
const configPath = path.resolve(dirPath, configName);
const exists = await fsUtils.exists(configPath);
return exists ? configName : null;
});
const existingConfigs = await Promise.all(configExistsPromises);
return existingConfigs.find(Boolean) || null;
};
const initNodeProject = (dirPath: string, packageManager: PackageManager): Promise<string> =>
new Promise<string>((resolve, reject) => {
exec(
`${packageManager} ${PMS[packageManager].init}`,
{
cwd: dirPath,
env: process.env,
},
(error, stdout, stderr) => (error ? reject(stderr) : resolve(stdout)),
);
});
export const initApp = async (dirPath: string, extraQuestions: boolean): Promise<PackageManager> => {
await fsUtils.ensureDirectory(dirPath);
const existingConfigName = await findExistingConfig(dirPath);
if (existingConfigName) {
console.error(`Looks like ${dirPath} already contains "${existingConfigName}".`);
console.error("Please remove old config or choose another directory.");
process.exit(1);
}
const packageManager = await getPackageManager(dirPath, extraQuestions);
const isPackageJsonExist = await fsUtils.exists(path.resolve(dirPath, PACKAGE_JSON));
if (!isPackageJsonExist) {
await initNodeProject(dirPath, packageManager);
}
return packageManager;
};
export const installPackages = async (
dirPath: string,
packageManager: PackageManager,
pluginsToInstall: string[],
registry: string,
): Promise<string> => {
const spinner = ora("Installing packages (this may take a while)").start();
const pluginsPackages = pluginsToInstall.map(packageNameFromPlugin).join(" ");
if (packageManager === "yarn") {
await fs.writeFile(path.join(dirPath, "yarn.lock"), "");
}
return new Promise<string>((resolve, reject) => {
exec(
PMS[packageManager].withRegistry(
`${packageManager} ${PMS[packageManager].install} testplane ${pluginsPackages}`,
registry,
),
{
cwd: dirPath,
env: process.env,
},
(error, stdout, stderr) => {
if (error) {
spinner.fail("An error occured during installation");
reject(`${stdout}\n${stderr}`);
} else {
spinner.succeed(
`Testplane and plugins have been installed successfully at ${Colors.fillGreen(dirPath)}`,
);
resolve(stdout);
}
},
);
});
};