1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 |
|
4 | /* eslint-disable no-console */
|
5 |
|
6 | const fs = require('fs');
|
7 | const getChannelURL = require('../src');
|
8 | const channel = process.argv[2];
|
9 | const shouldUpdatePackage = process.argv.includes('-w') || process.argv.includes('--write');
|
10 | const DETECT_TRAILING_WHITESPACE = /\s+$/;
|
11 |
|
12 | function printUsage() {
|
13 | console.log(`
|
14 | ember-source-channel-url is a utility module to easily obtain the URL
|
15 | to a tarball representing the latest \`ember-source\` build for a given
|
16 | channel.
|
17 |
|
18 | USAGE:
|
19 | ember-source-channel-url [CHANNEL] [FLAGS]
|
20 |
|
21 | FLAGS:
|
22 |
|
23 | -w, --write Update the local package.json to use the retrieved ember-source URL
|
24 | -h, --help Prints help information
|
25 |
|
26 | EXAMPLE:
|
27 |
|
28 | * Print the most recent URL for the specified channel:
|
29 |
|
30 | $ ember-source-channel-url canary
|
31 | $ ember-source-channel-url beta
|
32 | $ ember-source-channel-url release
|
33 |
|
34 | * Update the local project's \`package.json\` to use the most recent URL for the canary channel:
|
35 |
|
36 | $ ember-source-channel-url canary --write
|
37 | `);
|
38 | }
|
39 |
|
40 | if (['release', 'beta', 'canary'].indexOf(channel) === -1) {
|
41 | printUsage();
|
42 | process.exitCode = 1;
|
43 | } else {
|
44 | getChannelURL(channel).then(url => {
|
45 | if (process.stdout.isTTY) {
|
46 | console.log(
|
47 | `The URL for the latest tarball from ember-source's ${channel} channel is:\n\n\t${url}\n`
|
48 | );
|
49 | } else {
|
50 | process.stdout.write(url);
|
51 | }
|
52 |
|
53 | if (shouldUpdatePackage) {
|
54 | if (!fs.existsSync('package.json')) {
|
55 | console.log(
|
56 | `You passed --write to ember-source-channel-url but no package.json is available to update.`
|
57 | );
|
58 |
|
59 | process.exitCode = 2;
|
60 | }
|
61 |
|
62 | let contents = fs.readFileSync('package.json', { encoding: 'utf8' });
|
63 | let trailingWhitespace = DETECT_TRAILING_WHITESPACE.exec(contents);
|
64 | let pkg = JSON.parse(contents);
|
65 |
|
66 | let dependencyType = ['dependencies', 'devDependencies'].find(
|
67 | type => pkg[type] && pkg[type]['ember-source']
|
68 | );
|
69 |
|
70 | if (dependencyType) {
|
71 | pkg[dependencyType]['ember-source'] = url;
|
72 |
|
73 | let updatedContents = JSON.stringify(pkg, null, 2);
|
74 |
|
75 | if (trailingWhitespace) {
|
76 | updatedContents += trailingWhitespace[0];
|
77 | }
|
78 |
|
79 | fs.writeFileSync('package.json', updatedContents, { encoding: 'utf8' });
|
80 | } else {
|
81 | console.log(
|
82 | `You passed --write to ember-source-channel-url but ember-source is not included in dependencies or devDependencies in the package.json.`
|
83 | );
|
84 |
|
85 | process.exitCode = 3;
|
86 | }
|
87 | }
|
88 | });
|
89 | }
|