UNPKG

@linked-helper/framework.build-tools.aws-artifacts

Version:

LH framework build tools: upload artifacts

270 lines (257 loc) 10.1 kB
'use strict'; var path$1 = require('node:path'); var path = require('path'); var readPackageUp = require('read-pkg-up'); var clientS3 = require('@aws-sdk/client-s3'); var _glob = require('glob'); var fs = require('node:fs'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var path__default$1 = /*#__PURE__*/_interopDefault(path$1); var path__default = /*#__PURE__*/_interopDefault(path); var readPackageUp__default = /*#__PURE__*/_interopDefault(readPackageUp); var _glob__default = /*#__PURE__*/_interopDefault(_glob); var fs__default = /*#__PURE__*/_interopDefault(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__default.default({ 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__default.default.resolve(path__default.default.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__default$1.default.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, }; } exports.IConfig = void 0; (function (IConfig) { IConfig.IFiles = _IFiles; IConfig.parse = parse; IConfig.parsePackageJSON = parsePackageJSON; })(exports.IConfig || (exports.IConfig = {})); var IConfig = exports.IConfig; function getS3Client(options) { return options.s3 ?? new clientS3.S3Client(options); } exports.IPlatform = void 0; (function (IPlatform) { IPlatform.all = [ 'aix', 'android', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32', 'cygwin', 'netbsd', 'haiku', ]; })(exports.IPlatform || (exports.IPlatform = {})); var IPlatform = exports.IPlatform; async function uploadFiles({ prefix, root, files, platform, ...dest }, { onListFiles, onFileUploaded, onFileUploadFailed, onUploaded, } = {}) { const glob = (() => { return (pattern) => _glob__default.default(pattern, { cwd: root, nodir: true, absolute: true, }); })(); const fileInfos = []; const platforms = platform === 'all' ? IPlatform.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__default$1.default.relative(root, filePath).split(path__default$1.default.sep).join(path__default$1.default.posix.sep), key: path__default$1.default.join(prefix, path__default$1.default.relative(root, filePath)).split(path__default$1.default.sep).join(path__default$1.default.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__default.default.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 clientS3.PutObjectCommand({ Bucket: bucket, Key: key, Body: data, })); return key; } exports.IConfig$1 = IConfig; exports.IPlatform$1 = IPlatform; exports.getS3Client = getS3Client; exports.uploadFiles = uploadFiles;