UNPKG

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

Version:

LH framework build tools: upload artifacts

155 lines (153 loc) 6.02 kB
#!/usr/bin/env node import { a as IPlatform, I as IConfig, u as uploadFiles } from './uploadFiles-OK2kqTja.mjs'; import { d as downloadFiles, i as isDependency } from './isDependency-BFZ-t443.mjs'; import fs from 'node:fs'; import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; import 'node:path'; import 'path'; import 'read-pkg-up'; import '@aws-sdk/client-s3'; import 'glob'; import 'node:stream'; import 'node:util'; // tslint:disable-next-line: no-string-literal const verbose = process.env['npm_config_loglevel'] === 'verbose' || process.env['npm_config_loglevel'] === 'silly'; let debug = () => { }; if (verbose) { // tslint:disable-next-line: no-console debug = console.debug.bind(console); const myLogFileStream = fs.createWriteStream(`aws-artifacts-log.${Date.now()}.txt`); const fn = process.stdout.write; function write() { myLogFileStream.write.apply(myLogFileStream, arguments); return fn.apply(process.stdout, arguments); } process.stdout.write = write; } yargs(hideBin(process.argv)) .string('accessKeyId') .string('secretAccessKey') .string('sessionToken') .string('region') .string('bucket') .normalize('prefix') .normalize('root') .normalize('cwd') .choices('platform', [...IPlatform.all, 'all']) // tslint:disable-next-line: no-string-literal .default('platform', Number(process.env['LH_FRAMEWORK_BUILD_TOOLS_AWS_ARTIFACTS_PLATFORM_ALL']) > 0 ? 'all' : process.platform) .describe({ accessKeyId: 'AWS user\'s accessKeyId', secretAccessKey: 'AWS user\'s secretAccessKey', sessionToken: 'AWS user\'s sessionToken (if needed)', region: 'AWS bucket region', bucket: 'AWS bucket name', prefix: 'prefix (folder/path) for artifacts in AWS bucket (`${name}/${version}` by default)', root: 'root path for artifacts:\n' + '* uploading: final key in AWS will be `${prefix}/${relative-path-to-root}`\n' + '* downloading: file will be downloaded to `${root}/${key-without-prefix}`', platform: 'the platform download artifacts for, by default `process.platform`', }) .command('upload [files...]', 'upload artifacts to AWS', () => { }, async (argv) => { debug('upload', argv); const credentials = argv.accessKeyId && argv.secretAccessKey ? { accessKeyId: argv.accessKeyId, secretAccessKey: argv.secretAccessKey, sessionToken: argv.sessionToken, } : undefined; const config = await IConfig.parse({ ...argv, credentials, }); debug('config:', config); await uploadFiles(config, { // tslint:disable: no-console onListFiles: files => { console.log(`uploading ${files.length} file${files.length === 1 ? '' : 's'}:`); console.log(files.map(f => `"${f.relativePath}"`).join('\n')); }, onFileUploaded: f => { console.log(`uploaded "${f.relativePath}" ("${f.key}")`); }, onUploaded: files => { console.log(`uploaded ${files.length} file${files.length === 1 ? '' : 's'}:`); console.log(files.map(f => `"${f.relativePath}" ("${f.key}")`).join('\n')); }, // tslint:enable: no-console }); }) .command('download', 'download artifacts from AWS', () => { }, async (argv) => { debug('download', argv); const credentials = argv.accessKeyId && argv.secretAccessKey ? { accessKeyId: argv.accessKeyId, secretAccessKey: argv.secretAccessKey, sessionToken: argv.sessionToken, } : undefined; const config = await IConfig.parse({ ...argv, credentials, }); debug('config:', config); await downloadFiles(config, { // tslint:disable: no-console onListFiles: files => { console.log(`downloading ${files.length} file${files.length === 1 ? '' : 's'}:`); console.log(files.map(f => `"${f.relativePath}"`).join('\n')); }, onFileDownloaded: f => { console.log(`downloaded "${f.relativePath}" ("${f.key}")`); }, onDownloaded: files => { console.log(`downloaded ${files.length} file${files.length === 1 ? '' : 's'}:`); console.log(files.map(f => `"${f.relativePath}" ("${f.key}")`).join('\n')); }, // tslint:enable: no-console }); }) .command('postinstall', 'download artifacts from AWS if needed', () => { }, async (argv) => { // tslint:disable-next-line: no-string-literal debug('postinstall', argv, { cwd: process.cwd(), npm_package_name: process.env['npm_package_name'] }); if (isDependency()) { const credentials = argv.accessKeyId && argv.secretAccessKey ? { accessKeyId: argv.accessKeyId, secretAccessKey: argv.secretAccessKey, sessionToken: argv.sessionToken, } : undefined; const config = await IConfig.parse({ ...argv, credentials, }); debug('config:', config); await downloadFiles(config, { // tslint:disable: no-console onListFiles: files => { console.log(`downloading ${files.length} file${files.length === 1 ? '' : 's'}:`); console.log(files.map(f => `"${f.relativePath}"`).join('\n')); }, onFileDownloaded: f => { console.log(`downloaded "${f.relativePath}" ("${f.key}")`); }, onDownloaded: files => { console.log(`downloaded ${files.length} file${files.length === 1 ? '' : 's'}:`); console.log(files.map(f => `"${f.relativePath}" ("${f.key}")`).join('\n')); }, // tslint:enable: no-console }); } else { // tslint:disable-next-line: no-console console.log('is not a dependency, skip'); } }) .demandCommand() .help() .argv;