-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrepo.ts
More file actions
268 lines (240 loc) · 7.5 KB
/
repo.ts
File metadata and controls
268 lines (240 loc) · 7.5 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import path from 'node:path';
import { readFile, writeFile, mkdir } from 'node:fs/promises';
import { defaultLogger, Logger } from '@openfn/logger';
import exec from '../util/exec';
const defaultPkg = {
name: 'openfn-repo',
description: 'A repository for modules used by the openfn runtime',
private: true,
author: 'Open Function Group <admin@openfn.org>',
version: '1.0.0',
dependencies: {},
};
const npmInstallFlags = [
'--no-audit',
'--no-fund',
'--no-package-lock',
`--min-release-age=1`,
];
export const defaultRepoPath = '/tmp/openfn/repo';
type InstallList = Array<{ name: string; version: string }>;
/*
* Install a module from a specifier (ie, name@version) to the provided repo path.
* If a matching version is already installed, this does nothing.
*/
export const install = async (
specifiers: string[],
repoPath: string = defaultRepoPath,
log: Logger = defaultLogger,
// for unit testing
execFn = exec,
versionLookup = getLatestVersion
): Promise<string[]> => {
// map over the input
const mapped: string[] = [];
const forInstalling: InstallList = [];
const cached: Record<string, string> = {};
await ensureRepo(repoPath);
for (const s of specifiers) {
if (cached[s]) {
continue;
}
let { name, version } = getNameAndVersion(s);
if (!version || version.match(/^(next|latest)$/)) {
version = await versionLookup(s);
log.info(`Looked up latest version of ${s}: found ${version}`);
}
const mappedSpecifier = `${name}@${version}`;
mapped.push(mappedSpecifier);
cached[s] = mappedSpecifier;
const exists = await getModulePath(mappedSpecifier, repoPath, log);
if (exists) {
log.info(`Skipping ${mappedSpecifier} as already installed`);
} else {
forInstalling.push({ name, version });
log.info(`Will install ${name} version ${version}`);
}
}
if (forInstalling.length) {
const aliases = forInstalling.map(({ name, version }) => {
const alias = `npm:${name}@${version}`;
const aliasedName = `${name}_${version}`;
return `${aliasedName}@${alias}`;
});
log.info(`npm install ${npmInstallFlags.join(' ')} ${aliases.join(' ')}`);
// TODO it would be nice to report something about what's going on under the hood here
await execFn(
`npm install ${npmInstallFlags.join(' ')} ${aliases.join(' ')}`,
{
cwd: repoPath,
}
);
log.success(
`Installed ${forInstalling
.map(({ name, version }) => `${name}@${version}`)
.join(', ')}`
);
}
return mapped;
};
/*
* Ensures a repo exists at that target path
* If a package.json cannot be found, one will be created with default values
* Returns the package json it finds
*/
export const ensureRepo = async (path: string, log: Logger = defaultLogger) => {
await mkdir(path, { recursive: true });
const pkgPath = `${path}/package.json`;
try {
const raw = await readFile(pkgPath, 'utf8');
const pkg = JSON.parse(raw);
return pkg;
} catch (e) {
log.debug(`Creating new repo at ${pkgPath}`);
await writeFile(pkgPath, JSON.stringify(defaultPkg, null, 2));
return { ...defaultPkg };
}
};
export const getNameAndVersion = (specifier: string) => {
let name;
let version;
const atIndex = specifier.lastIndexOf('@');
if (atIndex > 0) {
name = specifier.substring(0, atIndex);
version = specifier.substring(atIndex + 1);
} else {
name = specifier;
}
return { name, version } as { name: string; version?: string };
};
// If there's no version in the specifer, we'll use @latest
// This ensures that a matching module can be found
// Someone needs to be responsible for ensureing that @latest is actually correct
// Which is an auto install issue
export const getAliasedName = (specifier: string, version?: string) => {
let name;
if (version === undefined) {
const x = getNameAndVersion(specifier);
name = x.name;
version = x.version;
} else {
name = specifier;
}
if (version) {
return `${name}_${version}`;
}
return name;
};
export const getLatestVersion = async (specifier: string) => {
const { stdout } = await exec(`npm view ${specifier} version`);
return stdout.trim(); // TODO this works for now but isn't very robust
};
export const loadRepoPkg = async (repoPath: string = defaultRepoPath) => {
try {
const pkgRaw = await readFile(`${repoPath}/package.json`, 'utf8');
return JSON.parse(pkgRaw);
} catch (e) {
// TODO should we report this error anywhere? It's probably fine
//console.error('ERROR PARSING REPO JSON');
return null;
}
};
// Note that the specifier shouldn't have an @
export const getLatestInstalledVersion = async (
specifier: string,
repoPath: string = defaultRepoPath,
pkg?: object,
log = defaultLogger
) => {
if (!pkg) {
pkg = await loadRepoPkg(repoPath);
}
if (pkg) {
// @ts-ignore
const { dependencies } = pkg;
let latest: string | null = null;
Object.keys(dependencies).forEach((d: string) => {
if (d.startsWith(`${specifier}_`)) {
const [_name, version] = d.split('_'); // todo what if there's genuinely an underscore in the name?
if (!latest || version > latest) {
latest = version;
}
}
});
if (latest) {
log.debug(`Using latest installed version of ${specifier}: ${latest}`);
return `${specifier}_${latest}`;
}
}
return null;
};
const getRepoAlias = async (
specifier: string,
repoPath: string = defaultRepoPath,
log = defaultLogger
) => {
const { version } = getNameAndVersion(specifier);
if (version) {
// TODO: fuzzy semver match
const a = getAliasedName(specifier);
const pkg = await loadRepoPkg(repoPath);
if (pkg && pkg.dependencies && pkg.dependencies[a]) {
return a;
}
} else {
return getLatestInstalledVersion(specifier, repoPath, undefined, log);
}
};
export const getModulePath = async (
specifier: string,
repoPath: string = defaultRepoPath,
log = defaultLogger
) => {
const alias = await getRepoAlias(specifier, repoPath, log);
if (alias) {
const p = path.resolve(`${repoPath}`, `node_modules/${alias}`);
return p;
}
return null;
};
// ESM doesn't support importing directories, and from node 19 this is enforced
// For a given specifier, this will return a path to the main index.js file
// I don't think this will work for nested imports though
export const getModuleEntryPoint = async (
specifier: string,
modulePath?: string,
repoPath: string = defaultRepoPath,
log = defaultLogger
): Promise<{ path: string; version: string } | null> => {
const moduleRoot =
modulePath || (await getModulePath(specifier, repoPath, log));
if (moduleRoot) {
const pkgRaw = await readFile(
path.join(moduleRoot, 'package.json'),
'utf8'
);
const pkg = JSON.parse(pkgRaw);
let main = 'index.js';
// TODO Turns out that importing the ESM format actually blows up
// (at least when we try to import lodash)
// if (pkg.exports) {
// if (typeof pkg.exports === 'string') {
// main = pkg.exports;
// } else {
// const defaultExport = pkg.exports['.']; // TODO what if this doesn't exist...
// if (typeof defaultExport == 'string') {
// main = defaultExport;
// } else {
// main = defaultExport.import;
// }
// }
// } else
// Safer for now to just use the CJS import
if (pkg.main) {
main = pkg.main;
}
const p = path.resolve(moduleRoot, main);
return { path: p, version: pkg.version };
}
return null;
};