-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.ts
135 lines (124 loc) · 3.83 KB
/
helpers.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
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
124
125
126
127
128
129
130
131
132
133
134
135
import { sdk } from "@cto.ai/sdk";
import fs from "fs";
import { track } from "./analytics";
import { bannerLink, workflowFilename } from "./config";
import { SocialMedia } from "../types";
import { pExec } from "../utils/pExec";
const insertBanner = async (projectName: string) => {
await track({
event: `Adding CTO.ai banner to README`
});
try {
const folderStr: any = await pExec(`cd ${projectName} && ls`);
if (folderStr.stdout.includes("README.md")) {
await pExec(
`cd ${projectName} && echo "$(echo -n "${bannerLink}\n" | cat - README.md)" > README.md`
);
} else {
await pExec(
`cd ${projectName} && touch README.md && echo "${bannerLink}" >> README.md`
);
}
} catch (error) {
await track({
event: `Error inserting banner in README`,
error: JSON.stringify(error)
});
throw `Error inserting banner in README`;
}
};
const customizeConfigFile = async (
projectName: string,
userFullName: string,
userEmail: string,
githubUserName: string,
isUserSite: boolean,
socialMedia: SocialMedia
) => {
const pathPrefix = isUserSite ? "" : projectName;
const configFilePath = `/ops/${projectName}/config.js`;
const [firstName, lastName] = userFullName.split(" ");
const { twitter, linkedin } = socialMedia;
const configFileContents = fs.readFileSync(configFilePath, "utf8");
const updatedConfigFileContents = configFileContents
.replace(/REPOSITORY = '.*'/g, `REPOSITORY = '${pathPrefix}'`)
.replace(/FIRST_NAME = '.*'/g, `FIRST_NAME = '${firstName}'`)
.replace(/LAST_NAME = '.*'/g, `LAST_NAME = '${lastName}'`)
.replace(/EMAIL = '.*'/g, `EMAIL = '${userEmail}'`)
.replace(/GITHUB_USERNAME = '.*'/g, `GITHUB_USERNAME = '${githubUserName}'`)
.replace(/TWITTER_USERNAME = '.*'/g, `TWITTER_USERNAME = '${twitter}'`)
.replace(/LINKEDIN_USERNAME = '.*'/g, `LINKEDIN_USERNAME = '${linkedin}'`);
fs.writeFileSync(configFilePath, updatedConfigFileContents);
};
const customizeWorkflowFile = async (
projectName: string,
isUserSite: boolean
) => {
// Deploying a user site is only done from master
if (isUserSite) {
const workflowPath = `/ops/${projectName}/.github/workflows/${workflowFilename}`;
const workflowFileContents = fs.readFileSync(workflowPath, "utf8");
const updatedWorkflowFileContents = workflowFileContents
.replace(/master\n/g, `develop\n`)
.replace(/BRANCH: gh-pages/g, `BRANCH: master`);
fs.writeFileSync(workflowPath, updatedWorkflowFileContents);
}
};
export const customizeApp = async (
projectName: string,
userFullName: string,
userEmail: string,
githubUserName: string,
isUserSite: boolean,
socialMedia: SocialMedia
) => {
try {
await insertBanner(projectName);
await customizeConfigFile(
projectName,
userFullName,
userEmail,
githubUserName,
isUserSite,
socialMedia
);
await customizeWorkflowFile(projectName, isUserSite);
await track({
event: `Successfully customized app`
});
} catch (error) {
await track({
event: `Error customizing app`,
error: JSON.stringify(error)
});
throw `Error customizing app`;
}
};
export const isSlack = () => {
return sdk.getInterfaceType() === "slack";
};
export const poll = async (
fn,
fnArgs,
validate,
interval = 5000,
maxAttempts = 100
) => {
let attempts = 0;
const executePoll = async (resolve, reject) => {
try {
const result = await fn(...fnArgs);
attempts++;
if (validate(result)) {
return resolve(result);
} else if (maxAttempts && attempts === maxAttempts) {
return reject(new Error(`Exceeded max attempts ${maxAttempts}`));
} else {
setTimeout(executePoll, interval, resolve, reject);
}
} catch (error) {
return reject(error);
}
};
return new Promise(executePoll);
};