UNPKG

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

Version:

LH framework build tools: upload artifacts

162 lines (157 loc) 6.33 kB
#!/usr/bin/env node 'use strict'; var uploadFiles = require('./uploadFiles-DxoCy-HV.cjs'); var isDependency = require('./isDependency-ByhD9J-t.cjs'); var fs = require('node:fs'); var yargs = require('yargs'); var helpers = require('yargs/helpers'); require('node:path'); require('path'); require('read-pkg-up'); require('@aws-sdk/client-s3'); require('glob'); require('node:stream'); require('node:util'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var fs__default = /*#__PURE__*/_interopDefault(fs); var yargs__default = /*#__PURE__*/_interopDefault(yargs); // 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__default.default.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__default.default(helpers.hideBin(process.argv)) .string('accessKeyId') .string('secretAccessKey') .string('sessionToken') .string('region') .string('bucket') .normalize('prefix') .normalize('root') .normalize('cwd') .choices('platform', [...uploadFiles.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 uploadFiles.IConfig.parse({ ...argv, credentials, }); debug('config:', config); await uploadFiles.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 uploadFiles.IConfig.parse({ ...argv, credentials, }); debug('config:', config); await isDependency.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.isDependency()) { const credentials = argv.accessKeyId && argv.secretAccessKey ? { accessKeyId: argv.accessKeyId, secretAccessKey: argv.secretAccessKey, sessionToken: argv.sessionToken, } : undefined; const config = await uploadFiles.IConfig.parse({ ...argv, credentials, }); debug('config:', config); await isDependency.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;