Skip to content

Commit c5b53a2

Browse files
author
Kelly Selden
committed
reset command
1 parent 2d3bd71 commit c5b53a2

File tree

10 files changed

+303
-54
lines changed

10 files changed

+303
-54
lines changed

bin/commands/reset.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 args = require('../../src/args');
4+
const reset = require('../../src/reset');
5+
6+
module.exports.command = 'reset';
7+
8+
module.exports.describe = 'reset a blueprint';
9+
10+
module.exports.builder = {
11+
blueprint: args['blueprint'],
12+
to: args['to']
13+
};
14+
15+
module.exports.handler = async function handler(argv) {
16+
try {
17+
let result = await reset(argv);
18+
19+
let ps = result.resolveConflictsProcess;
20+
if (ps) {
21+
process.stdin.pipe(ps.stdin);
22+
ps.stdout.pipe(process.stdout);
23+
ps.stderr.pipe(process.stderr);
24+
}
25+
26+
let message = await result.promise;
27+
if (message) {
28+
console.log(message);
29+
}
30+
31+
// since we are piping, not inheriting, the child process
32+
// doesn't have the power to close its parent
33+
if (ps) {
34+
// eslint-disable-next-line no-process-exit
35+
process.exit();
36+
}
37+
} catch (err) {
38+
console.error(err);
39+
}
40+
};

src/reset.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
'use strict';
2+
3+
const boilerplateUpdate = require('boilerplate-update');
4+
const getStartAndEndCommands = require('./get-start-and-end-commands');
5+
const parseBlueprintPackage = require('./parse-blueprint-package');
6+
const saveBlueprint = require('./save-blueprint');
7+
const loadDefaultBlueprintFromDisk = require('./load-default-blueprint-from-disk');
8+
const loadSafeBlueprint = require('./load-safe-blueprint');
9+
const loadSafeBlueprintFile = require('./load-safe-blueprint-file');
10+
const { getBlueprintRelativeFilePath } = require('./get-blueprint-file-path');
11+
const findBlueprint = require('./find-blueprint');
12+
const getBaseBlueprint = require('./get-base-blueprint');
13+
const getBlueprintFilePath = require('./get-blueprint-file-path');
14+
const resolvePackage = require('./resolve-package');
15+
const { defaultTo } = require('./constants');
16+
17+
module.exports = async function reset({
18+
blueprint: _blueprint,
19+
to = defaultTo
20+
}) {
21+
let cwd = process.cwd();
22+
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);
27+
28+
let packageName;
29+
let name;
30+
let location;
31+
let url;
32+
if (_blueprint) {
33+
let parsedPackage = await parseBlueprintPackage({
34+
cwd,
35+
blueprint: _blueprint
36+
});
37+
packageName = parsedPackage.name;
38+
name = parsedPackage.name;
39+
location = parsedPackage.location;
40+
url = parsedPackage.url;
41+
} else {
42+
let defaultBlueprint = await loadDefaultBlueprintFromDisk(cwd);
43+
packageName = defaultBlueprint.packageName;
44+
name = defaultBlueprint.name;
45+
}
46+
47+
let packageInfo = await resolvePackage({
48+
name: packageName,
49+
url,
50+
range: to
51+
});
52+
53+
packageName = packageInfo.name;
54+
if (!name) {
55+
name = packageInfo.name;
56+
}
57+
let version = packageInfo.version;
58+
let path = packageInfo.path;
59+
60+
let emberCliUpdateJson = await loadSafeBlueprintFile(emberCliUpdateJsonPath);
61+
62+
let blueprint;
63+
64+
let existingBlueprint = findBlueprint(emberCliUpdateJson, packageName, name);
65+
if (existingBlueprint) {
66+
blueprint = existingBlueprint;
67+
} else {
68+
blueprint = {
69+
packageName,
70+
name,
71+
location
72+
};
73+
}
74+
75+
blueprint = loadSafeBlueprint(blueprint);
76+
77+
blueprint.version = version;
78+
blueprint.path = path;
79+
80+
let baseBlueprint = await getBaseBlueprint({
81+
cwd,
82+
blueprints: emberCliUpdateJson.blueprints,
83+
blueprint
84+
});
85+
86+
if (!baseBlueprint) {
87+
// for non-existing default blueprints
88+
blueprint.isBaseBlueprint = true;
89+
}
90+
91+
let {
92+
promise,
93+
resolveConflictsProcess
94+
} = await boilerplateUpdate({
95+
endVersion: blueprint.version,
96+
reset: true,
97+
createCustomDiff: true,
98+
customDiffOptions: ({
99+
packageJson
100+
}) => getStartAndEndCommands({
101+
packageJson,
102+
baseBlueprint,
103+
endBlueprint: blueprint
104+
}),
105+
ignoredFiles: [await getBlueprintRelativeFilePath(cwd)]
106+
});
107+
108+
return {
109+
promise: (async() => {
110+
let result = await promise;
111+
112+
await saveBlueprint({
113+
emberCliUpdateJsonPath,
114+
blueprint
115+
});
116+
117+
return result;
118+
})(),
119+
resolveConflictsProcess
120+
};
121+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ describe(function() {
389389
} = await promise;
390390

391391
fixtureCompare({
392-
mergeFixtures: 'test/fixtures/blueprint/app/local-app/reset/my-app'
392+
mergeFixtures: 'test/fixtures/blueprint/app/local-app/reset/merge/my-app'
393393
});
394394

395395
assertNoStaged(status);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"schemaVersion": 0,
3+
"packages": [
4+
{
5+
"name": "ember-cli-update-npm-blueprint-test",
6+
"version": "0.0.24",
7+
"blueprints": [
8+
{
9+
"name": "ember-cli-update-npm-blueprint-test",
10+
"isBaseBlueprint": true
11+
}
12+
]
13+
},
14+
{
15+
"name": "ember-cli-update-git-blueprint-test",
16+
"location": "../local-blueprint",
17+
"version": "0.0.0",
18+
"blueprints": [
19+
{
20+
"name": "ember-cli-update-git-blueprint-test",
21+
"codemodsSource": "test-codemods",
22+
"options": [
23+
"--supplied-option=foo"
24+
]
25+
}
26+
]
27+
}
28+
]
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "@my-scope/my-app",
3+
"version": "0.0.0",
4+
"description": "",
5+
"main": "index.js"
6+
}

test/fixtures/blueprint/app/local-app/reset/my-app/config/ember-cli-update.json renamed to test/fixtures/blueprint/app/local-app/reset/merge/my-app/config/ember-cli-update.json

File renamed without changes.

test/fixtures/blueprint/app/local-app/reset/my-app/test-blueprint-file.txt renamed to test/fixtures/blueprint/app/local-app/reset/merge/my-app/test-blueprint-file.txt

File renamed without changes.

test/integration/index-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ applicable codemods: ember-modules-codemod, ember-qunit-codemod, ember-test-help
419419
});
420420

421421
fixtureCompare({
422-
mergeFixtures: 'test/fixtures/blueprint/app/local-app/reset/my-app'
422+
mergeFixtures: 'test/fixtures/blueprint/app/local-app/reset/merge/my-app'
423423
});
424424

425425
assertNoStaged(status);

test/integration/init-test.js

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const {
1111
} = require('git-fixtures');
1212
const init = require('../../src/init');
1313
const {
14-
assertNoStaged,
1514
assertNoUnstaged
1615
} = require('../helpers/assertions');
1716
const { initBlueprint } = require('../helpers/blueprint');
@@ -75,57 +74,6 @@ describe(init, function() {
7574
});
7675
}
7776

78-
it('can reset a custom blueprint', async function() {
79-
let [
80-
{
81-
packageName
82-
},
83-
{
84-
location,
85-
options,
86-
codemodsSource,
87-
version: to
88-
}
89-
] = (await loadSafeBlueprintFile('test/fixtures/blueprint/app/local-app/reset/my-app/config/ember-cli-update.json')).blueprints;
90-
91-
await merge({
92-
fixturesPath: 'test/fixtures/blueprint/app/local-app/init',
93-
commitMessage: 'my-app',
94-
blueprint: packageName
95-
});
96-
97-
let commitMessage = 'base init';
98-
99-
await commit({ m: commitMessage });
100-
101-
await initBlueprint({
102-
fixturesPath: path.resolve(__dirname, '../fixtures/blueprint/app/local'),
103-
resolvedFrom: tmpPath,
104-
relativeDir: location
105-
});
106-
107-
let {
108-
status
109-
} = await processExit({
110-
promise: init({
111-
reset: true,
112-
blueprint: location,
113-
to,
114-
codemodsSource,
115-
blueprintOptions: options
116-
}),
117-
cwd: tmpPath,
118-
commitMessage,
119-
expect
120-
});
121-
122-
fixtureCompare({
123-
mergeFixtures: 'test/fixtures/blueprint/app/local-app/reset/my-app'
124-
});
125-
126-
assertNoStaged(status);
127-
});
128-
12977
it('can initialize a custom blueprint', async function() {
13078
let [
13179
{

test/integration/reset-test.js

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

0 commit comments

Comments
 (0)