-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeartifact-connect.js
More file actions
139 lines (135 loc) · 4.51 KB
/
codeartifact-connect.js
File metadata and controls
139 lines (135 loc) · 4.51 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
module.exports = {
name: `codeartifact-connect`,
factory: (require) => {
const { exec } = require('child_process');
/**
* @param {Record<string, string>} env
* @returns {Promise<string>} token
*/
const getToken = async (env) => {
const domain = env.CODE_ARTIFACT_DOMAIN;
return await new Promise((resolve, reject) => {
try {
exec(
`aws codeartifact get-authorization-token --domain ${domain} --domain-owner $AWS_ACCOUNT_ID --query authorizationToken --region $AWS_REGION --output text`,
{ env },
(error, stdout, stderr) => {
if (stdout?.trim()) {
resolve(stdout.trim());
}
if (error !== null) {
reject(error);
}
resolve();
}
);
} catch (ex) {
reject(ex);
}
});
};
const getCodeArtifactUrl = (env) => {
const domain = env.CODE_ARTIFACT_DOMAIN;
return `https://${domain}-${env.AWS_ACCOUNT_ID}.d.codeartifact.${env.AWS_REGION}.amazonaws.com/npm/${domain}`;
};
const needsToken = (project) => {
const env = project.configuration.env;
const npmScopes = project.configuration.values.get('npmScopes');
const npmRegistries = project.configuration.values.get('npmRegistries');
if (!npmScopes && !npmRegistries) {
return false;
}
const domain = env.CODE_ARTIFACT_DOMAIN;
if (
npmScopes?.has(domain) &&
!npmScopes?.get(domain).get('npmAuthToken')
) {
return true;
}
const url = getCodeArtifactUrl(env);
if (
npmRegistries?.has(url) &&
!npmRegistries?.get(url).get('npmAuthToken')
) {
return true;
}
return false;
};
const setToken = async (project) => {
const env = project.configuration.env;
const domain = env.CODE_ARTIFACT_DOMAIN;
const token = await getToken(env);
const url = getCodeArtifactUrl(env);
const values = project.configuration.values;
values?.get('npmScopes')?.get(domain)?.set('npmAuthToken', token);
values?.get('npmRegistries')?.get(url)?.set('npmAuthToken', token);
console.log(`Updated ${domain} codeartifact token`);
return token;
};
return {
hooks: {
/**
* @param {Project} project
* @param {{
* reportWarning: (name: string, text: string) => void;
* reportError: (name: string, text: string) => void;
* }} report
*/
async validateProject(project, report) {
try {
if (!needsToken(project)) {
return;
}
await setToken(project);
} catch (error) {
report.reportError(`codeartifact-connect error: ${error}`);
}
},
// /**
// *
// * @param {Project} project
// * @param {NodeJS.ProcessEnv} env
// * @param {(name: string, argv0: string, args: Array<string>) => Promise<void>} makePathWrapper
// */
// async setupScriptEnvironment(project) {
// try {
// if (!needsToken(project)) {
// return;
// }
// await setToken(project);
// } catch (ex) {
// console.warn(ex);
// }
// },
/**
* @param {() => Promise<Response>} executor
* @param {WrapNetworkRequestInfo} info
* @returns {Promise<() => Promise<Response>>}
*/
async wrapNetworkRequest(executor, info) {
const url = getCodeArtifactUrl(info.configuration.env);
if (!info.target.match(url)) {
return executor;
}
if (!needsToken(info)) {
return executor;
}
const token = await setToken(info);
info.headers.authorization = `Bearer ${token}`;
return executor;
},
/**
* @param {Workspace} workspace
* @param {object} rawManifest
* @returns {Promise<void> | void}
*/
async beforeWorkspacePacking({ project }) {
if (!needsToken(project)) {
return;
}
await setToken(project);
}
}
};
}
};