-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
workspace.js
121 lines (97 loc) · 3.7 KB
/
workspace.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
const CoreObject = require('core-object');
const fs = require('fs-extra');
const RSVP = require('rsvp');
const path = require('path');
const extend = require('extend');
const debug = require('debug')('ember-try:dependency-manager-adapter:workspaces');
const walkSync = require('walk-sync');
const NpmAdapter = require('./npm');
module.exports = CoreObject.extend({
init() {
this._super.apply(this, arguments);
this.run = this.run || require('../utils/run');
if (!this.useYarnCommand) {
throw new Error('workspaces are currently only supported by Yarn, you must set `useYarn` to true');
}
},
packageJSON: 'package.json',
setup(options) {
if (!options) {
options = {};
}
let packageJSON = JSON.parse(fs.readFileSync(this.packageJSON));
let workspaceGlobs;
if (Array.isArray(packageJSON.workspaces)) {
workspaceGlobs = packageJSON.workspaces
}
if (packageJSON.workspaces && Array.isArray(packageJSON.workspaces.packages)) {
workspaceGlobs = packageJSON.workspaces.packages
}
if (!workspaceGlobs || !workspaceGlobs.length) {
throw new Error('you must define the `workspaces` property in package.json with at least one workspace to use workspaces with ember-try');
}
// workspaces is a list of globs, loop over the list and find
// all paths that contain a `package.json` file
let workspacePaths = walkSync('.', { globs: workspaceGlobs }).filter(workspacePath => {
let packageJSONPath = path.join(this.cwd, workspacePath, 'package.json');
return fs.existsSync(packageJSONPath);
});
this._packageAdapters = workspacePaths.map(workspacePath => {
return new NpmAdapter({
cwd: workspacePath,
run: this.run,
managerOptions: this.managerOptions,
useYarnCommand: true,
buildManagerOptions: this.buildManagerOptions,
});
});
return RSVP.all(this._packageAdapters.map(adapter => adapter.setup(options)));
},
changeToDependencySet(depSet) {
// TODO: What should this do for tables? Nesting? Needs different output
this._packageAdapters.forEach(adapter => {
adapter.applyDependencySet(depSet);
});
return this._install(depSet).then(() => {
let deps = extend({}, depSet.dependencies || {}, depSet.devDependencies || {});
let currentDeps = Object.keys(deps).map((dep) => {
return {
name: dep,
versionExpected: deps[dep],
versionSeen: this._findCurrentVersionOf(dep),
packageManager: 'yarn',
};
});
debug('Switched to dependencies: \n', currentDeps);
return RSVP.Promise.resolve(currentDeps);
});
},
cleanup() {
return RSVP.all(this._packageAdapters.map(adapter => adapter.cleanup()));
},
_install(depSet) {
let mgrOptions = this.managerOptions || [];
// buildManagerOptions overrides all default
if (typeof this.buildManagerOptions === 'function') {
mgrOptions = this.buildManagerOptions(depSet);
if (!Array.isArray(mgrOptions)) {
throw new Error('buildManagerOptions must return an array of options');
}
} else {
if (mgrOptions.indexOf('--no-lockfile') === -1) {
mgrOptions = mgrOptions.concat(['--no-lockfile']);
}
// npm warns on incompatible engines
// yarn errors, not a good experience
if (mgrOptions.indexOf('--ignore-engines') === -1) {
mgrOptions = mgrOptions.concat(['--ignore-engines']);
}
}
debug('Run yarn install with options %s', mgrOptions);
return this.run('yarn', [].concat(['install'], mgrOptions), { cwd: this.cwd });
},
_findCurrentVersionOf(dep) {
return this._packageAdapters[0]._findCurrentVersionOf(dep);
},
});