Skip to content

Commit 284f50d

Browse files
author
Kelly Selden
committed
share code between reset and stats
1 parent a8f91db commit 284f50d

File tree

6 files changed

+182
-98
lines changed

6 files changed

+182
-98
lines changed

src/get-blueprint-from-args.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const parseBlueprintPackage = require('./parse-blueprint-package');
4+
const resolvePackage = require('./resolve-package');
5+
const findBlueprint = require('./find-blueprint');
6+
const { defaultTo } = require('./constants');
7+
8+
async function getBlueprintFromArgs({
9+
cwd,
10+
emberCliUpdateJson,
11+
blueprint,
12+
to = defaultTo
13+
}) {
14+
let parsedPackage = await parseBlueprintPackage({
15+
cwd,
16+
blueprint
17+
});
18+
let url = parsedPackage.url;
19+
20+
let packageInfo = await resolvePackage({
21+
name: parsedPackage.name,
22+
url,
23+
range: to
24+
});
25+
26+
let packageName = packageInfo.name;
27+
let name = packageInfo.name;
28+
29+
let existingBlueprint = findBlueprint(emberCliUpdateJson, packageName, name);
30+
if (!existingBlueprint) {
31+
throw `blueprint "${blueprint}" was not found`;
32+
}
33+
34+
return {
35+
packageInfo,
36+
existingBlueprint
37+
};
38+
}
39+
40+
module.exports = getBlueprintFromArgs;

src/reset.js

Lines changed: 86 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -6,121 +6,116 @@ const parseBlueprintPackage = require('./parse-blueprint-package');
66
const saveBlueprint = require('./save-blueprint');
77
const loadSafeBlueprintFile = require('./load-safe-blueprint-file');
88
const { getBlueprintRelativeFilePath } = require('./get-blueprint-file-path');
9-
const findBlueprint = require('./find-blueprint');
109
const getBaseBlueprint = require('./get-base-blueprint');
1110
const getBlueprintFilePath = require('./get-blueprint-file-path');
1211
const resolvePackage = require('./resolve-package');
1312
const { defaultTo } = require('./constants');
1413
const chooseBlueprintUpdates = require('./choose-blueprint-updates');
14+
const getBlueprintFromArgs = require('./get-blueprint-from-args');
1515

1616
module.exports = async function reset({
1717
blueprint: _blueprint,
1818
to = defaultTo
1919
}) {
20-
let cwd = process.cwd();
20+
try {
21+
let cwd = process.cwd();
2122

22-
// A custom config location in package.json may be reset/init away,
23-
// so we can no longer look it up on the fly after the run.
24-
// We must rely on a lookup before the run.
25-
let emberCliUpdateJsonPath = await getBlueprintFilePath(cwd);
23+
// A custom config location in package.json may be reset/init away,
24+
// so we can no longer look it up on the fly after the run.
25+
// We must rely on a lookup before the run.
26+
let emberCliUpdateJsonPath = await getBlueprintFilePath(cwd);
2627

27-
let emberCliUpdateJson = await loadSafeBlueprintFile(emberCliUpdateJsonPath);
28+
let emberCliUpdateJson = await loadSafeBlueprintFile(emberCliUpdateJsonPath);
2829

29-
let { blueprints } = emberCliUpdateJson;
30+
let { blueprints } = emberCliUpdateJson;
3031

31-
if (!blueprints.length) {
32-
throw new Error('No blueprints found.');
33-
}
32+
if (!blueprints.length) {
33+
throw 'no blueprints found';
34+
}
3435

35-
let blueprint;
36-
let packageInfo;
36+
let blueprint;
37+
let packageInfo;
38+
39+
if (_blueprint) {
40+
let {
41+
packageInfo: _packageInfo,
42+
existingBlueprint
43+
} = await getBlueprintFromArgs({
44+
cwd,
45+
emberCliUpdateJson,
46+
blueprint: _blueprint,
47+
to
48+
});
3749

38-
if (_blueprint) {
39-
let parsedPackage = await parseBlueprintPackage({
40-
cwd,
41-
blueprint: _blueprint
42-
});
43-
let url = parsedPackage.url;
50+
packageInfo = _packageInfo;
51+
blueprint = existingBlueprint;
52+
} else {
53+
let {
54+
blueprint: _blueprint
55+
} = await chooseBlueprintUpdates({
56+
cwd,
57+
emberCliUpdateJson,
58+
reset: true
59+
});
4460

45-
packageInfo = await resolvePackage({
46-
name: parsedPackage.name,
47-
url,
48-
range: to
49-
});
61+
blueprint = _blueprint;
5062

51-
let packageName = packageInfo.name;
52-
let name = packageInfo.name;
63+
let parsedPackage = await parseBlueprintPackage({
64+
cwd,
65+
blueprint: blueprint.location || blueprint.packageName
66+
});
67+
let url = parsedPackage.url;
5368

54-
let existingBlueprint = findBlueprint(emberCliUpdateJson, packageName, name);
55-
if (!existingBlueprint) {
56-
throw new Error('Blueprint not found.');
69+
packageInfo = await resolvePackage({
70+
name: blueprint.packageName,
71+
url,
72+
range: to
73+
});
5774
}
5875

59-
blueprint = existingBlueprint;
60-
} else {
61-
let {
62-
blueprint: _blueprint
63-
} = await chooseBlueprintUpdates({
64-
cwd,
65-
emberCliUpdateJson,
66-
reset: true
67-
});
68-
69-
blueprint = _blueprint;
76+
blueprint.version = packageInfo.version;
77+
blueprint.path = packageInfo.path;
7078

71-
let parsedPackage = await parseBlueprintPackage({
72-
cwd,
73-
blueprint: blueprint.location || blueprint.packageName
74-
});
75-
let url = parsedPackage.url;
79+
let baseBlueprint;
80+
if (!blueprint.isBaseBlueprint) {
81+
baseBlueprint = await getBaseBlueprint({
82+
cwd,
83+
blueprints,
84+
blueprint
85+
});
86+
}
7687

77-
packageInfo = await resolvePackage({
78-
name: blueprint.packageName,
79-
url,
80-
range: to
88+
let {
89+
promise,
90+
resolveConflictsProcess
91+
} = await boilerplateUpdate({
92+
endVersion: blueprint.version,
93+
reset: true,
94+
createCustomDiff: true,
95+
customDiffOptions: ({
96+
packageJson
97+
}) => getStartAndEndCommands({
98+
packageJson,
99+
baseBlueprint,
100+
endBlueprint: blueprint
101+
}),
102+
ignoredFiles: [await getBlueprintRelativeFilePath(cwd)]
81103
});
82-
}
83-
84-
blueprint.version = packageInfo.version;
85-
blueprint.path = packageInfo.path;
86104

87-
let baseBlueprint;
88-
if (!blueprint.isBaseBlueprint) {
89-
baseBlueprint = await getBaseBlueprint({
90-
cwd,
91-
blueprints,
92-
blueprint
93-
});
105+
return {
106+
promise: (async() => {
107+
let result = await promise;
108+
109+
await saveBlueprint({
110+
emberCliUpdateJsonPath,
111+
blueprint
112+
});
113+
114+
return result;
115+
})(),
116+
resolveConflictsProcess
117+
};
118+
} catch (err) {
119+
return { promise: Promise.reject(err) };
94120
}
95-
96-
let {
97-
promise,
98-
resolveConflictsProcess
99-
} = await boilerplateUpdate({
100-
endVersion: blueprint.version,
101-
reset: true,
102-
createCustomDiff: true,
103-
customDiffOptions: ({
104-
packageJson
105-
}) => getStartAndEndCommands({
106-
packageJson,
107-
baseBlueprint,
108-
endBlueprint: blueprint
109-
}),
110-
ignoredFiles: [await getBlueprintRelativeFilePath(cwd)]
111-
});
112-
113-
return {
114-
promise: (async() => {
115-
let result = await promise;
116-
117-
await saveBlueprint({
118-
emberCliUpdateJsonPath,
119-
blueprint
120-
});
121-
122-
return result;
123-
})(),
124-
resolveConflictsProcess
125-
};
126121
};

src/stats.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const getBlueprintFilePath = require('./get-blueprint-file-path');
44
const loadSafeBlueprintFile = require('./load-safe-blueprint-file');
55
const checkForBlueprintUpdates = require('./check-for-blueprint-updates');
66
const { formatBlueprintLine } = require('./choose-blueprint-updates');
7-
const findBlueprint = require('./find-blueprint');
7+
const getBlueprintFromArgs = require('./get-blueprint-from-args');
88

99
module.exports = async function stats({
1010
blueprint
@@ -17,12 +17,18 @@ module.exports = async function stats({
1717

1818
let { blueprints } = emberCliUpdateJson;
1919

20-
if (blueprint) {
21-
let existingBlueprint = findBlueprint(emberCliUpdateJson, blueprint, blueprint);
20+
if (!blueprints.length) {
21+
throw 'no blueprints found';
22+
}
2223

23-
if (!existingBlueprint) {
24-
throw `blueprint "${blueprint}" was not found`;
25-
}
24+
if (blueprint) {
25+
let {
26+
existingBlueprint
27+
} = await getBlueprintFromArgs({
28+
cwd,
29+
emberCliUpdateJson,
30+
blueprint
31+
});
2632

2733
blueprints = [existingBlueprint];
2834
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ describe(function() {
726726
fixturesPath: 'test/fixtures/blueprint/app/local-app/local',
727727
commitMessage: 'my-app',
728728
stats: true,
729-
blueprint: packageName,
729+
blueprint: location,
730730
async beforeMerge() {
731731
await initBlueprint({
732732
fixturesPath: 'test/fixtures/blueprint/app/local',

test/integration/reset-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,33 @@ describe(reset, function() {
102102

103103
assertNoStaged(status);
104104
});
105+
106+
it('handles missing blueprint', async function() {
107+
let {
108+
stderr,
109+
status
110+
} = await merge({
111+
fixturesPath: 'test/fixtures/blueprint/app/local-app/local',
112+
commitMessage: 'my-app',
113+
blueprint: 'missing'
114+
});
115+
116+
assertNoStaged(status);
117+
118+
expect(stderr).to.equal('blueprint "missing" was not found');
119+
});
120+
121+
it('handles no blueprints', async function() {
122+
let {
123+
stderr,
124+
status
125+
} = await merge({
126+
fixturesPath: 'test/fixtures/app/local',
127+
commitMessage: 'my-app'
128+
});
129+
130+
assertNoStaged(status);
131+
132+
expect(stderr).to.equal('no blueprints found');
133+
});
105134
});

test/integration/stats-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,18 @@ ${packageName2}, current: ${from2}, latest: ${to2}`);
111111

112112
expect(stderr).to.equal('blueprint "missing" was not found');
113113
});
114+
115+
it('handles no blueprints', async function() {
116+
let {
117+
stderr,
118+
status
119+
} = await merge({
120+
fixturesPath: 'test/fixtures/app/local',
121+
commitMessage: 'my-app'
122+
});
123+
124+
assertNoStaged(status);
125+
126+
expect(stderr).to.equal('no blueprints found');
127+
});
114128
});

0 commit comments

Comments
 (0)