#!/usr/bin/env node 'use strict'; /* eslint-disable no-console */ const fs = require('fs'); const getChannelURL = require('../src'); const channel = process.argv[2]; const shouldUpdatePackage = process.argv.includes('-w') || process.argv.includes('--write'); const DETECT_TRAILING_WHITESPACE = /\s+$/; function printUsage() { console.log(` ember-source-channel-url is a utility module to easily obtain the URL to a tarball representing the latest \`ember-source\` build for a given channel. USAGE: ember-source-channel-url [CHANNEL] [FLAGS] FLAGS: -w, --write Update the local package.json to use the retrieved ember-source URL -h, --help Prints help information EXAMPLE: * Print the most recent URL for the specified channel: $ ember-source-channel-url canary $ ember-source-channel-url beta $ ember-source-channel-url release * Update the local project's \`package.json\` to use the most recent URL for the canary channel: $ ember-source-channel-url canary --write `); } if (['release', 'beta', 'canary'].indexOf(channel) === -1) { printUsage(); process.exitCode = 1; } else { getChannelURL(channel).then(url => { if (process.stdout.isTTY) { console.log( `The URL for the latest tarball from ember-source's ${channel} channel is:\n\n\t${url}\n` ); } else { process.stdout.write(url); } if (shouldUpdatePackage) { if (!fs.existsSync('package.json')) { console.log( `You passed --write to ember-source-channel-url but no package.json is available to update.` ); process.exitCode = 2; } let contents = fs.readFileSync('package.json', { encoding: 'utf8' }); let trailingWhitespace = DETECT_TRAILING_WHITESPACE.exec(contents); let pkg = JSON.parse(contents); let dependencyType = ['dependencies', 'devDependencies'].find( type => pkg[type] && pkg[type]['ember-source'] ); if (dependencyType) { pkg[dependencyType]['ember-source'] = url; let updatedContents = JSON.stringify(pkg, null, 2); if (trailingWhitespace) { updatedContents += trailingWhitespace[0]; } fs.writeFileSync('package.json', updatedContents, { encoding: 'utf8' }); } else { console.log( `You passed --write to ember-source-channel-url but ember-source is not included in dependencies or devDependencies in the package.json.` ); process.exitCode = 3; } } }); }