Skip to content

Commit a75e61a

Browse files
author
Kelly Selden
committed
add stats command
1 parent eaa9504 commit a75e61a

5 files changed

Lines changed: 227 additions & 0 deletions

File tree

bin/commands/stats.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const args = require('../../src/args');
4+
const stats = require('../../src/stats');
5+
6+
module.exports.command = 'stats';
7+
8+
module.exports.describe = 'list blueprint version updates';
9+
10+
module.exports.builder = {
11+
blueprint: args['blueprint']
12+
};
13+
14+
module.exports.handler = async function handler(argv) {
15+
try {
16+
let result = await stats(argv);
17+
18+
console.log(result);
19+
} catch (err) {
20+
console.error(err);
21+
}
22+
};

src/choose-blueprint-updates.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,4 @@ All blueprints are up-to-date!`);
124124
}
125125

126126
module.exports = chooseBlueprintUpdates;
127+
module.exports.formatBlueprintLine = formatBlueprintLine;

src/stats.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const getBlueprintFilePath = require('./get-blueprint-file-path');
4+
const loadSafeBlueprintFile = require('./load-safe-blueprint-file');
5+
const checkForBlueprintUpdates = require('./check-for-blueprint-updates');
6+
const { formatBlueprintLine } = require('./choose-blueprint-updates');
7+
const findBlueprint = require('./find-blueprint');
8+
9+
module.exports = async function stats({
10+
blueprint
11+
} = {}) {
12+
let cwd = process.cwd();
13+
14+
let emberCliUpdateJsonPath = await getBlueprintFilePath(cwd);
15+
16+
let emberCliUpdateJson = await loadSafeBlueprintFile(emberCliUpdateJsonPath);
17+
18+
let { blueprints } = emberCliUpdateJson;
19+
20+
if (blueprint) {
21+
let existingBlueprint = findBlueprint(emberCliUpdateJson, blueprint, blueprint);
22+
23+
if (!existingBlueprint) {
24+
throw `blueprint "${blueprint}" was not found`;
25+
}
26+
27+
blueprints = [existingBlueprint];
28+
}
29+
30+
let blueprintUpdates = await checkForBlueprintUpdates({
31+
cwd,
32+
blueprints
33+
});
34+
35+
let stats = `${blueprintUpdates.map(formatBlueprintLine).join(`
36+
`)}`;
37+
38+
return stats;
39+
};

test/acceptance/ember-cli-update-test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe(function() {
4242
install,
4343
addon,
4444
bootstrap,
45+
stats,
4546
save,
4647
blueprintOptions = [],
4748
beforeMerge = () => Promise.resolve()
@@ -79,6 +80,11 @@ describe(function() {
7980
'bootstrap'
8081
];
8182
}
83+
if (stats) {
84+
args = [
85+
'stats'
86+
];
87+
}
8288
if (save) {
8389
args = [
8490
'save'
@@ -688,4 +694,49 @@ describe(function() {
688694

689695
assertNoUnstaged(status);
690696
});
697+
698+
it('can show single blueprint stats', async function() {
699+
let {
700+
packageName,
701+
location,
702+
version: from
703+
} = (await loadSafeBlueprintFile('test/fixtures/blueprint/app/local-app/local/my-app/config/ember-cli-update.json')).blueprints[1];
704+
705+
let {
706+
version: to
707+
} = (await loadSafeBlueprintFile('test/fixtures/blueprint/app/local-app/merge/my-app/config/ember-cli-update.json')).blueprints[1];
708+
709+
let {
710+
ps,
711+
promise
712+
} = await merge({
713+
fixturesPath: 'test/fixtures/blueprint/app/local-app/local',
714+
commitMessage: 'my-app',
715+
stats: true,
716+
blueprint: packageName,
717+
async beforeMerge() {
718+
await initBlueprint({
719+
fixturesPath: 'test/fixtures/blueprint/app/local',
720+
resolvedFrom: tmpPath,
721+
relativeDir: location
722+
});
723+
}
724+
});
725+
726+
let result = '';
727+
728+
ps.stdout.on('data', data => {
729+
let str = data.toString();
730+
result += str;
731+
});
732+
733+
let {
734+
status
735+
} = await promise;
736+
737+
assertNoStaged(status);
738+
739+
expect(result).to.equal(`${packageName}, current: ${from}, latest: ${to}
740+
`);
741+
});
691742
});

test/integration/stats-test.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
'use strict';
2+
3+
const { describe, it } = require('../helpers/mocha');
4+
const { expect } = require('../helpers/chai');
5+
const {
6+
buildTmp,
7+
processExit
8+
} = require('git-fixtures');
9+
const stats = require('../../src/stats');
10+
const {
11+
assertNoStaged
12+
} = require('../helpers/assertions');
13+
const { initBlueprint } = require('../helpers/blueprint');
14+
const loadSafeBlueprintFile = require('../../src/load-safe-blueprint-file');
15+
16+
describe(stats, function() {
17+
this.timeout(5 * 60 * 1000);
18+
19+
let cwd;
20+
let tmpPath;
21+
22+
before(function() {
23+
cwd = process.cwd();
24+
});
25+
26+
afterEach(function() {
27+
process.chdir(cwd);
28+
});
29+
30+
async function merge({
31+
blueprint,
32+
fixturesPath,
33+
commitMessage,
34+
beforeMerge = () => Promise.resolve()
35+
}) {
36+
tmpPath = await buildTmp({
37+
fixturesPath,
38+
commitMessage
39+
});
40+
41+
await beforeMerge();
42+
43+
process.chdir(tmpPath);
44+
45+
let promise = stats({
46+
blueprint
47+
});
48+
49+
return await processExit({
50+
promise,
51+
cwd: tmpPath,
52+
commitMessage,
53+
expect
54+
});
55+
}
56+
57+
it('works', async function() {
58+
let [
59+
{
60+
packageName: packageName1,
61+
version: from1
62+
},
63+
{
64+
packageName: packageName2,
65+
location,
66+
version: from2
67+
}
68+
] = (await loadSafeBlueprintFile('test/fixtures/blueprint/app/local-app/local/my-app/config/ember-cli-update.json')).blueprints;
69+
70+
let [
71+
{
72+
version: to1
73+
},
74+
{
75+
version: to2
76+
}
77+
] = (await loadSafeBlueprintFile('test/fixtures/blueprint/app/local-app/merge/my-app/config/ember-cli-update.json')).blueprints;
78+
79+
let {
80+
result,
81+
status
82+
} = await merge({
83+
fixturesPath: 'test/fixtures/blueprint/app/local-app/local',
84+
commitMessage: 'my-app',
85+
async beforeMerge() {
86+
await initBlueprint({
87+
fixturesPath: 'test/fixtures/blueprint/app/local',
88+
resolvedFrom: tmpPath,
89+
relativeDir: location
90+
});
91+
}
92+
});
93+
94+
assertNoStaged(status);
95+
96+
expect(result).to.equal(`${packageName1}, current: ${from1}, latest: ${to1}
97+
${packageName2}, current: ${from2}, latest: ${to2}`);
98+
});
99+
100+
it('handles missing blueprint', async function() {
101+
let {
102+
stderr,
103+
status
104+
} = await merge({
105+
fixturesPath: 'test/fixtures/blueprint/app/local-app/local',
106+
commitMessage: 'my-app',
107+
blueprint: 'missing'
108+
});
109+
110+
assertNoStaged(status);
111+
112+
expect(stderr).to.equal('blueprint "missing" was not found');
113+
});
114+
});

0 commit comments

Comments
 (0)