@linked-helper/framework.build-tools.aws-artifacts
Version:
LH framework build tools: upload artifacts
257 lines (247 loc) • 9.43 kB
JavaScript
import path$1 from 'node:path';
import path from 'path';
import readPackageUp from 'read-pkg-up';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import _glob from 'glob';
import fs from 'node:fs';
var IFiles;
(function (IFiles) {
IFiles.empty = {
'aix': [],
'android': [],
'darwin': [],
'freebsd': [],
'linux': [],
'openbsd': [],
'sunos': [],
'win32': [],
'cygwin': [],
'netbsd': [],
'haiku': [],
'all': [],
};
})(IFiles || (IFiles = {}));
var _IFiles = IFiles;
function assertString(a, msg = 'not a string') {
if (typeof a !== 'string') {
throw new Error(msg);
}
return a;
}
function assertStringNotEmpty(a, msg = 'not a string or empty') {
if (typeof a !== 'string' || !a) {
throw new Error(msg);
}
return a;
}
function toString(str, byDefault) {
return typeof str === 'string' && str
? str
: byDefault;
}
function toStringArray(v) {
if (v) {
if (Array.isArray(v)) {
return v.map(s => toString(s, '')).filter(s => !!s);
}
if (typeof v === 'string') {
return [v];
}
}
return [];
}
async function parsePackageJSON({ cwd = process.cwd(), } = {}) {
const packageInfo = await readPackageUp({ cwd });
if (!packageInfo) {
throw new Error('no `package.json` found');
}
// tslint:disable: no-unnecessary-initializer
const { path: packageJsonPath, packageJson: { name, version, 'aws-artifacts': { credentials: { accessKeyId = undefined, secretAccessKey = undefined, sessionToken = undefined, } = {}, region = undefined, bucket = undefined, prefix = undefined, root = './', files = undefined, } = {}, }, } = packageInfo;
// tslint:enable: no-unnecessary-initializer
return {
credentials: {
accessKeyId: toString(accessKeyId),
secretAccessKey: toString(secretAccessKey),
sessionToken: toString(sessionToken),
},
region: toString(region),
bucket: toString(bucket),
prefix: toString(prefix, `${assertStringNotEmpty(name, 'empty package name')}/${assertStringNotEmpty(version, 'empty package version')}`),
root: path.resolve(path.dirname(packageJsonPath), root ? String(root) : './'),
files: files
? (typeof files === 'object' && (Array.isArray(files) && {
..._IFiles.empty,
'all': toStringArray(files),
}
||
{
'aix': toStringArray(files.aix),
'android': toStringArray(files.android),
'darwin': toStringArray(files.darwin),
'freebsd': toStringArray(files.freebsd),
'linux': toStringArray(files.linux),
'openbsd': toStringArray(files.openbsd),
'sunos': toStringArray(files.sunos),
'win32': toStringArray(files.win32),
'cygwin': toStringArray(files.cygwin),
'netbsd': toStringArray(files.netbsd),
'haiku': toStringArray(files.haiku),
'all': toStringArray(files.all),
})
||
typeof files === 'string' && {
'aix': [],
'android': [],
'darwin': [],
'freebsd': [],
'linux': [],
'openbsd': [],
'sunos': [],
'win32': [],
'cygwin': [],
'netbsd': [],
'haiku': [],
'all': toStringArray(files),
}
||
{
..._IFiles.empty,
})
: {
..._IFiles.empty,
},
};
}
async function parse({ cwd = process.cwd(), credentials, region, bucket, prefix, root, files, platform = process.platform, } = {}) {
const configFromPackageJson = await parsePackageJSON({ cwd });
return {
credentials: credentials
? {
accessKeyId: assertStringNotEmpty(credentials.accessKeyId, 'incorrect `credentials.accessKeyId`'),
secretAccessKey: assertStringNotEmpty(credentials.secretAccessKey, 'incorrect `credentials.secretAccessKey`'),
sessionToken: credentials.sessionToken || undefined,
}
: {
accessKeyId: assertStringNotEmpty(configFromPackageJson.credentials.accessKeyId, 'incorrect `credentials.accessKeyId`'),
secretAccessKey: assertStringNotEmpty(configFromPackageJson.credentials.secretAccessKey, 'incorrect `credentials.secretAccessKey`'),
sessionToken: configFromPackageJson.credentials.sessionToken || undefined,
},
region: assertStringNotEmpty(region ?? configFromPackageJson.region, 'incorrect `region`'),
bucket: assertStringNotEmpty(bucket ?? configFromPackageJson.bucket, 'incorrect `bucket`'),
prefix: prefix === undefined
? configFromPackageJson.prefix
: assertString(prefix),
root: typeof root === 'string'
? path$1.resolve(cwd, root)
: configFromPackageJson.root,
files: {
'aix': toStringArray(files?.aix ?? configFromPackageJson.files?.aix),
'android': toStringArray(files?.android ?? configFromPackageJson.files?.android),
'darwin': toStringArray(files?.darwin ?? configFromPackageJson.files?.darwin),
'freebsd': toStringArray(files?.freebsd ?? configFromPackageJson.files?.freebsd),
'linux': toStringArray(files?.linux ?? configFromPackageJson.files?.linux),
'openbsd': toStringArray(files?.openbsd ?? configFromPackageJson.files?.openbsd),
'sunos': toStringArray(files?.sunos ?? configFromPackageJson.files?.sunos),
'win32': toStringArray(files?.win32 ?? configFromPackageJson.files?.win32),
'cygwin': toStringArray(files?.cygwin ?? configFromPackageJson.files?.cygwin),
'netbsd': toStringArray(files?.netbsd ?? configFromPackageJson.files?.netbsd),
'haiku': toStringArray(files?.netbsd ?? configFromPackageJson.files?.haiku),
'all': toStringArray(files?.all ?? configFromPackageJson.files?.all),
},
platform,
};
}
var IConfig;
(function (IConfig) {
IConfig.IFiles = _IFiles;
IConfig.parse = parse;
IConfig.parsePackageJSON = parsePackageJSON;
})(IConfig || (IConfig = {}));
var IConfig$1 = IConfig;
function getS3Client(options) {
return options.s3 ?? new S3Client(options);
}
var IPlatform;
(function (IPlatform) {
IPlatform.all = [
'aix',
'android',
'darwin',
'freebsd',
'linux',
'openbsd',
'sunos',
'win32',
'cygwin',
'netbsd',
'haiku',
];
})(IPlatform || (IPlatform = {}));
var IPlatform$1 = IPlatform;
async function uploadFiles({ prefix, root, files, platform, ...dest }, { onListFiles, onFileUploaded, onFileUploadFailed, onUploaded, } = {}) {
const glob = (() => {
return (pattern) => _glob(pattern, {
cwd: root,
nodir: true,
absolute: true,
});
})();
const fileInfos = [];
const platforms = platform === 'all'
? IPlatform$1.all
: [platform];
for (const p of [...platforms, 'all']) {
for (const pattern of files[p]) {
const paths = await glob(pattern);
fileInfos.push(...paths.map(filePath => ({
path: filePath,
relativePath: path$1.relative(root, filePath).split(path$1.sep).join(path$1.posix.sep),
key: path$1.join(prefix, path$1.relative(root, filePath)).split(path$1.sep).join(path$1.posix.sep),
})));
}
}
try {
onListFiles?.(fileInfos);
}
catch (dontCare) { /* do nothing */ }
if (!fileInfos.length) {
throw new Error('no files found');
}
const uploaded = [];
await Promise.all(fileInfos.map(async (file) => {
try {
await upload({
...dest,
key: file.key,
data: fs.createReadStream(file.path),
});
uploaded.push(file);
try {
onFileUploaded?.(file, [...uploaded], fileInfos);
}
catch (dontCare) { /* do nothing */ }
}
catch (err) {
try {
onFileUploadFailed?.(file, err, [...uploaded], fileInfos);
}
catch (dontCare) { /* do nothing */ }
throw err;
}
}));
try {
onUploaded?.(fileInfos);
}
catch (dontCare) { /* do nothing */ }
return uploaded;
}
async function upload({ bucket, key, data, ...client }) {
const s3 = getS3Client(client);
await s3.send(new PutObjectCommand({
Bucket: bucket,
Key: key,
Body: data,
}));
return key;
}
export { IConfig as I, IPlatform as a, IConfig$1 as b, IPlatform$1 as c, getS3Client as g, uploadFiles as u };