/
ember-source-channel-url
executable file
·89 lines (68 loc) · 2.46 KB
/
ember-source-channel-url
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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;
}
}
});
}